No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_lerelation.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """
  2. Tests for _LeRelation object family
  3. """
  4. import unittest
  5. from unittest import TestCase
  6. from unittest.mock import patch
  7. from collections import OrderedDict
  8. import EditorialModel
  9. import EditorialModel.classtypes
  10. import DataSource.dummy
  11. import leapi
  12. import leapi.test.utils
  13. import leapi.lecrud as lecrud
  14. from leapi.lecrud import _LeCrud
  15. class LeRelationTestCase(TestCase):
  16. @classmethod
  17. def setUpClass(cls):
  18. """ Write the generated code in a temporary directory and import it """
  19. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  20. @classmethod
  21. def tearDownClass(cls):
  22. """ Remove the temporary directory created at class setup """
  23. leapi.test.utils.cleanup(cls.tmpdir)
  24. def test_prepare_filters(self):
  25. """ Testing the _prepare_filters() method """
  26. from dyncode import Numero, LeObject, LeRelation
  27. filters = [
  28. (
  29. 'rank = 1',
  30. ('rank', '=', '1')
  31. ),
  32. (
  33. 'nature = "parent"',
  34. ('nature', '=', '"parent"')
  35. ),
  36. (
  37. 'superior = 21',
  38. ('superior', '=', LeObject(21))
  39. ),
  40. (
  41. 'subordinate = 22',
  42. ('subordinate', '=', LeObject(22))
  43. ),
  44. (
  45. ('rank', '=', '1'),
  46. ('rank', '=', '1'),
  47. ),
  48. (
  49. ('superior', '=', LeObject(21)),
  50. ('superior', '=', LeObject(21)),
  51. ),
  52. (
  53. ('subordinate', '=', Numero(42)),
  54. ('subordinate', '=', Numero(42)),
  55. ),
  56. ]
  57. for filter_arg, filter_res in filters:
  58. res, rel_res = LeRelation._prepare_filters([filter_arg])
  59. self.assertEqual(len(res), 1)
  60. self.assertEqual(len(rel_res), 0)
  61. res = res[0]
  62. for i in range(3):
  63. self.assertEqual(filter_res[i], res[i], "%s != %s"%(filter_res, res))
  64. @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
  65. def test_delete(self, dsmock):
  66. """ Testing LeHierarch insert method """
  67. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
  68. LeHierarch(42).delete()
  69. dsmock.assert_called_once_with(LeHierarch, 42)
  70. dsmock.reset_mock()
  71. class LeHierarch(LeRelationTestCase):
  72. @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
  73. def test_get(self, dsmock):
  74. """ Tests the LeHierarch.get() method without limit group order etc."""
  75. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
  76. queries = [
  77. (
  78. ['superior = 42', 'subordinate = 24'], #filters
  79. ['superior', 'subordinate', 'nature', 'rank'], #field_l
  80. LeHierarch, #target
  81. ['superior', 'subordinate', 'nature', 'rank'], #field_ds
  82. [('superior','=',LeObject(42)), ('subordinate', '=', LeObject(24))], #filters_ds
  83. [], #rfilters_ds
  84. ),
  85. (
  86. [ LeRelation.sup_filter(Numero(42)) ],
  87. [],
  88. LeHierarch,
  89. [ 'nature', 'rank', 'subordinate', 'depth', 'superior', 'id_relation', EditorialModel.classtypes.relation_name],
  90. [('superior', '=', Numero(42))],
  91. [],
  92. ),
  93. ]
  94. for filters, field_l, target, field_ds, filters_ds, rfilters_ds in queries:
  95. eargs = (filters, field_l, target, field_ds, filters_ds, rfilters_ds)
  96. LeHierarch.get(filters, field_l)
  97. cargs = dsmock.call_args
  98. self.assertEqual(len(cargs), 2)
  99. cargs=cargs[1]
  100. self.assertEqual(cargs['target_cls'], target, "%s != %s"%(cargs, eargs))
  101. self.assertEqual(set(cargs['field_list']), set(field_ds), "%s != %s"%(cargs, eargs))
  102. self.assertEqual(cargs['filters'], filters_ds, "%s != %s"%(cargs, eargs))
  103. self.assertEqual(cargs['rel_filters'], rfilters_ds, "%s != %s"%(cargs, eargs))
  104. dsmock.reset_mock()
  105. @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
  106. def test_insert(self, dsmock):
  107. """ Testing LeHierarch insert method """
  108. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
  109. queries = [
  110. (
  111. {
  112. 'superior': Rubrique(7, class_id = Rubrique._class_id, type_id = Rubrique._type_id),
  113. 'subordinate': Numero(42, class_id = Numero._class_id, type_id = Numero._type_id),
  114. 'nature': 'parent',
  115. },
  116. {
  117. 'superior': Rubrique(7),
  118. 'subordinate': Numero(42),
  119. 'nature': 'parent',
  120. },
  121. ),
  122. ]
  123. """ # Those tests are not good
  124. (
  125. {
  126. 'superior': 7,
  127. 'subordinate': 42,
  128. 'nature': 'parent',
  129. },
  130. {
  131. 'superior': LeObject(7),
  132. 'subordinate': LeObject(42),
  133. 'nature': 'parent',
  134. }
  135. ),
  136. (
  137. {
  138. 'superior': LeObject(7),
  139. 'subordinate': LeObject(42),
  140. 'nature': 'parent',
  141. },
  142. {
  143. 'superior': LeObject(7),
  144. 'subordinate': LeObject(42),
  145. 'nature': 'parent',
  146. }
  147. ),
  148. (
  149. {
  150. 'superior': LeObject(7),
  151. 'subordinate': 42,
  152. 'nature': 'parent',
  153. },
  154. {
  155. 'superior': LeObject(7),
  156. 'subordinate': LeObject(42),
  157. 'nature': 'parent',
  158. }
  159. )
  160. ]
  161. """
  162. for query, equery in queries:
  163. equery['rank'] = 1
  164. equery['depth'] = None
  165. equery[EditorialModel.classtypes.relation_name] = None
  166. LeHierarch.insert(query)
  167. dsmock.assert_called_once_with(LeHierarch, **equery)
  168. dsmock.reset_mock()
  169. LeRelation.insert(query, 'LeHierarch')
  170. dsmock.assert_called_once_with(LeHierarch, **equery)
  171. dsmock.reset_mock()
  172. @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
  173. def test_delete(self, dsmock):
  174. """ Testing LeHierarch delete method """
  175. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
  176. rel = LeHierarch(10)
  177. rel.delete()
  178. dsmock.assert_called_once_with(LeHierarch, 10)
  179. @unittest.skip("Wait for LeRelation.update() to unskip")
  180. @patch('DataSource.dummy.leapidatasource.DummyDatasource.update')
  181. def test_update(self, dsmock):
  182. """ test LeHierach update method"""
  183. from dyncode import LeHierarch
  184. with self.assertRaises(NotImplementedError):
  185. LeHierarch.update({})
  186. class LeRel2TypeTestCase(LeRelationTestCase):
  187. @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
  188. def test_insert(self, dsmock):
  189. """ test LeHierach update method"""
  190. from dyncode import LeObject, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, RelTextesPersonneAuteur
  191. queries = [
  192. {
  193. 'superior': Article(42),
  194. 'subordinate': Personne(24),
  195. 'adresse': None,
  196. },
  197. {
  198. 'superior': Textes(42),
  199. 'subordinate': Personne(24),
  200. 'adresse': None,
  201. },
  202. {
  203. 'superior': Article(42),
  204. 'subordinate': Personne(24),
  205. 'adresse': "bar",
  206. },
  207. {
  208. 'superior': Textes(42),
  209. 'subordinate': Personne(24),
  210. 'adresse': "foo",
  211. },
  212. ]
  213. for query in queries:
  214. RelTextesPersonneAuteur.insert(query)
  215. eres = {
  216. 'nature': None,
  217. 'depth': None,
  218. 'rank': 1,
  219. EditorialModel.classtypes.relation_name: None,
  220. }
  221. eres.update(query)
  222. for fname in ('superior', 'subordinate'):
  223. if isinstance(eres[fname], int):
  224. eres[fname] = LeObject(eres[fname])
  225. dsmock.assert_called_once_with(RelTextesPersonneAuteur, **eres)
  226. dsmock.reset_mock()
  227. query[EditorialModel.classtypes.relation_name] = 'auteur'
  228. LeRel2Type.insert(query, "RelTextesPersonneAuteur")
  229. dsmock.assert_called_once_with(RelTextesPersonneAuteur, **eres)
  230. dsmock.reset_mock()
  231. @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
  232. def test_insert_fails(self, dsmock):
  233. """ test LeHierach update method"""
  234. from dyncode import LeObject, Rubrique, Numero, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, RelTextesPersonneAuteur
  235. queries = [
  236. {
  237. 'superior': Rubrique(42),
  238. 'subordinate': Personne(24),
  239. 'adresse': None,
  240. },
  241. {
  242. 'adresse': None,
  243. },
  244. {
  245. 'superior': Rubrique(42),
  246. 'subordinate': Rubrique(24),
  247. 'adresse': None,
  248. },
  249. {
  250. 'superior': Article(42),
  251. 'subordinate': Numero(24),
  252. 'adresse': 'foo',
  253. },
  254. {
  255. 'id_relation': 1337,
  256. 'superior': Article(42),
  257. 'subordinate': Numero(24),
  258. 'adresse': 'foo',
  259. },
  260. ]
  261. for query in queries:
  262. try:
  263. LeRel2Type.insert(query, 'Rel_textes2personne')
  264. self.fail("No exception raised")
  265. except Exception as e:
  266. if not isinstance(e, lecrud.LeApiErrors) and not isinstance(e, lecrud.LeApiDataCheckError):
  267. self.fail("Bad exception raised : "+str(e))
  268. try:
  269. RelTextesPersonneAuteur.insert(query)
  270. self.fail("No exception raised")
  271. except Exception as e:
  272. if not isinstance(e, lecrud.LeApiErrors) and not isinstance(e, lecrud.LeApiDataCheckError):
  273. self.fail("Bad exception raised : ", e)