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 10KB

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