Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test_lecrud.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. """
  2. Tests for _LeCrud and LeCrud
  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. from leapi.lecrud import _LeCrud
  12. ## @brief Test LeCrud methods
  13. # @note those tests need the full dynamically generated code
  14. class LeCrudTestCase(TestCase):
  15. @classmethod
  16. def setUpClass(cls):
  17. """ Write the generated code in a temporary directory and import it """
  18. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  19. @classmethod
  20. def tearDownClass(cls):
  21. """ Remove the temporary directory created at class setup """
  22. leapi.test.utils.cleanup(cls.tmpdir)
  23. def test_split_query_filter(self):
  24. """ Tests the _split_filter() classmethod """
  25. import dyncode
  26. query_results = {
  27. 'Hello = world' : ('Hello', '=', 'world'),
  28. 'hello <= "world"': ('hello', '<=', '"world"'),
  29. '_he42_ll-o >= \'world"': ('_he42_ll-o', '>=', '\'world"'),
  30. 'foo in ["foo", 42, \'bar\']': ('foo', ' in ', '["foo", 42, \'bar\']'),
  31. ' bar42 < 42': ('bar42', '<', '42'),
  32. ' _hidden > 1337': ('_hidden', '>', '1337'),
  33. '_42 not in foobar': ('_42', ' not in ', 'foobar'),
  34. 'hello in foo':('hello', ' in ', 'foo'),
  35. "\t\t\thello\t\t\nin\nfoo\t\t\n\t":('hello', ' in ', 'foo'),
  36. "hello \nnot\tin \nfoo":('hello', ' not in ', 'foo'),
  37. 'hello != bar':('hello', '!=', 'bar'),
  38. 'hello = "world>= <= != in not in"': ('hello', '=', '"world>= <= != in not in"'),
  39. 'superior.parent = 13': ('superior.parent', '=', '13'),
  40. 'hello like "%world%"': ('hello', ' like ', '"%world%"'),
  41. 'world not like "foo%bar"': ('world', ' not like ', '"foo%bar"'),
  42. }
  43. for query, result in query_results.items():
  44. res = dyncode.LeCrud._split_filter(query)
  45. self.assertEqual(res, result, "When parsing the query : '%s' the returned value is different from the expected '%s'"%(query, result))
  46. def test_invalid_split_query_filter(self):
  47. """ Testing the _split_filter() method with invalid queries """
  48. import dyncode
  49. invalid_queries = [
  50. '42 = 42',
  51. '4hello = foo',
  52. 'foo == bar',
  53. 'hello >> world',
  54. 'hello = ',
  55. ' = world',
  56. '=',
  57. '42',
  58. '"hello" = world',
  59. 'foo.bar = 15',
  60. ]
  61. for query in invalid_queries:
  62. with self.assertRaises(ValueError, msg='But the query was not valid : "%s"'%query):
  63. dyncode.LeCrud._split_filter(query)
  64. def test_field_is_relational(self):
  65. """ Testing the test to check if a field is relational """
  66. from dyncode import LeCrud
  67. test_fields = {
  68. 'superior.parent': True,
  69. 'subordinate.parent': True,
  70. 'foofoo.foo': False,
  71. }
  72. for field, eres in test_fields.items():
  73. self.assertEqual(LeCrud._field_is_relational(field), eres)
  74. def test_check_datas(self):
  75. """ testing the check_datas* methods """
  76. from dyncode import Publication, Numero, LeObject
  77. datas = { 'titre':'foobar' }
  78. Numero.check_datas_value(datas, False, False)
  79. Numero.check_datas_value(datas, True, False)
  80. with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
  81. Numero.check_datas_value({}, True)
  82. def test_prepare_filters(self):
  83. """ Testing the _prepare_filters() method """
  84. from dyncode import Publication, Numero, LeObject, Personnes
  85. #Simple filters
  86. filters = [
  87. 'lodel_id = 1',
  88. 'superior.parent > 2'
  89. ]
  90. filt, rfilt = Numero._prepare_filters(filters)
  91. self.assertEqual(filt, [('lodel_id', '=', '1')])
  92. self.assertEqual(rfilt, [((leapi.leobject.REL_SUP,'parent'), '>', '2')])
  93. #All fields, no relationnal and class given
  94. filters = []
  95. res_filt = []
  96. for field in Numero._fields:
  97. filters.append('%s=1'%field)
  98. res_filt.append((field, '=', '1'))
  99. filt, rfilt = Publication._prepare_filters(filters)
  100. self.assertEqual(rfilt, [])
  101. self.assertEqual(filt, res_filt)
  102. #Mixed type filters (tuple and string)
  103. filters = [
  104. ('lodel_id', '<=', '0'),
  105. 'subordinate.parent = 2',
  106. ]
  107. filt, rfilt = Numero._prepare_filters(filters)
  108. self.assertEqual(filt, [('lodel_id', '<=', '0')])
  109. self.assertEqual(rfilt, [((leapi.leobject.REL_SUB,'parent'), '=', '2')])
  110. def test_prepare_filters_invalid(self):
  111. """ Testing the _prepare_filters() method """
  112. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject
  113. #Numero fields filters but no letype nor leclass given
  114. filters = []
  115. res_filt = []
  116. for field in Numero._fields:
  117. filters.append('%s=1'%field)
  118. res_filt.append((field, '=', '1'))
  119. with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
  120. LeObject._prepare_filters(filters)
  121. #simply invalid filters
  122. filters = ['hello world !']
  123. with self.assertRaises(ValueError):
  124. Personnes._prepare_filters(filters)
  125. def test_prepare_order_fields(self):
  126. """ Testing the _prepare_order_fields """
  127. from dyncode import Article
  128. order_fields_list = [
  129. (
  130. ['titre'],
  131. [('titre', 'ASC')]
  132. ),
  133. (
  134. [('titre', 'asc')],
  135. [('titre', 'ASC')]
  136. ),
  137. (
  138. ['lodel_id', ('titre', 'asc')],
  139. [('lodel_id', 'ASC'), ('titre', 'ASC')]
  140. ),
  141. (
  142. [('titre', 'desc')],
  143. [('titre', 'DESC')]
  144. ),
  145. ]
  146. for fields_arg, expected_result in order_fields_list:
  147. result = Article._prepare_order_fields(fields_arg)
  148. self.assertEqual(result, expected_result)
  149. #
  150. # Tests mocking the datasource
  151. @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
  152. def test_insert(self, dsmock):
  153. from dyncode import Publication, Numero, LeObject, Personne, Article
  154. ndatas = [
  155. (Numero, {'titre' : 'FooBar'}),
  156. (Numero, {'titre':'hello'}),
  157. (Personne, {'nom':'world', 'prenom':'hello'}),
  158. (Article, {'titre': 'Ar Boof', 'soustitre': 'Wow!'}),
  159. ]
  160. for lecclass, ndats in ndatas:
  161. lecclass.insert(ndats)
  162. dsmock.assert_called_once_with(lecclass, **ndats)
  163. dsmock.reset_mock()
  164. lecclass.insert(ndats)
  165. dsmock.assert_called_once_with(lecclass, **ndats)
  166. dsmock.reset_mock()
  167. ## @todo try failing on inserting from LeClass child or LeObject
  168. @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
  169. def test_insert_fails(self, dsmock):
  170. from dyncode import Publication, Numero, LeObject, Personne, Article
  171. ndatas = [
  172. (Numero, dict()),
  173. (Numero, {'titre':'hello', 'lodel_id':42}),
  174. (Numero, {'tititre': 'hehello'}),
  175. (Personne, {'titre':'hello'}),
  176. (Article, {'titre': 'hello'}),
  177. ]
  178. for lecclass, ndats in ndatas:
  179. with self.assertRaises(leapi.lecrud.LeApiDataCheckError, msg="But trying to insert %s as %s"%(ndats, lecclass.__name__)):
  180. lecclass.insert(ndats)
  181. assert not dsmock.called
  182. pass
  183. @patch('DataSource.dummy.leapidatasource.DummyDatasource.update')
  184. def test_update(self, dsmock):
  185. from dyncode import Publication, Numero, LeObject
  186. args_l = [
  187. (
  188. Numero,
  189. {'lodel_id':'1'},
  190. {'titre': 'foobar'},
  191. [('lodel_id', '=', 1)],
  192. []
  193. ),
  194. ]
  195. for ccls, initarg, qdatas, efilters, erelfilters in args_l:
  196. obji = ccls(**initarg)
  197. obji.update(qdatas)
  198. dsmock.assert_called_once_with(ccls, efilters, erelfilters, **qdatas)
  199. ## @todo test invalid get
  200. @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
  201. def test_get(self, dsmock):
  202. """ Test the select method without group, limit, sort or offset """
  203. from dyncode import Publication, Numero, LeObject, Textes
  204. args = [
  205. (
  206. Numero,
  207. ['lodel_id', 'superior.parent'],
  208. ['titre != "foobar"'],
  209. ['lodel_id', (leapi.leobject.REL_SUP, 'parent')],
  210. [ ('titre','!=', '"foobar"'),
  211. ('type_id', '=', Numero._type_id),
  212. ('class_id', '=', Numero._class_id),
  213. ],
  214. []
  215. ),
  216. (
  217. Numero,
  218. ['lodel_id', 'titre', 'superior.parent', 'subordinate.translation'],
  219. ['superior.parent in [1,2,3,4,5]'],
  220. ['lodel_id', 'titre', (leapi.leobject.REL_SUP,'parent'), (leapi.leobject.REL_SUB, 'translation')],
  221. [
  222. ('type_id', '=', Numero._type_id),
  223. ('class_id', '=', Numero._class_id),
  224. ],
  225. [( (leapi.leobject.REL_SUP, 'parent'), ' in ', '[1,2,3,4,5]')]
  226. ),
  227. (
  228. Numero,
  229. [],
  230. [],
  231. Numero._fields,
  232. [
  233. ('type_id', '=', Numero._type_id),
  234. ('class_id', '=', Numero._class_id),
  235. ],
  236. []
  237. ),
  238. (
  239. Textes,
  240. ['lodel_id', 'titre', 'soustitre', 'superior.parent'],
  241. ['titre != "foobar"'],
  242. ['lodel_id', 'titre', 'soustitre', (leapi.leobject.REL_SUP, 'parent')],
  243. [ ('titre','!=', '"foobar"'),
  244. ('class_id', '=', Textes._class_id),
  245. ],
  246. [],
  247. ),
  248. (
  249. LeObject,
  250. ['lodel_id'],
  251. [],
  252. ['lodel_id'],
  253. [],
  254. [],
  255. ),
  256. ]
  257. for callcls, field_list, filters, fl_ds, filters_ds, rfilters_ds in args:
  258. callcls.get(filters, field_list)
  259. dsmock.assert_called_with(
  260. target_cls = callcls,
  261. field_list = fl_ds,
  262. filters = filters_ds,
  263. rel_filters = rfilters_ds,
  264. order=None,
  265. group=None,
  266. limit=None,
  267. offset=0
  268. )
  269. dsmock.reset_mock()
  270. @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
  271. def test_get_complete(self, dsmock):
  272. """ Test the select method with group limit sort and offset arguments """
  273. from dyncode import Numero
  274. args = [
  275. (
  276. Numero,
  277. {
  278. 'query_filters': [],
  279. 'field_list': ['lodel_id'],
  280. 'group': ['titre'],
  281. 'limit': 10,
  282. },
  283. {
  284. 'target_cls': Numero,
  285. 'field_list': ['lodel_id'],
  286. 'filters': [],
  287. 'rel_filters': [],
  288. 'group': [('titre', 'ASC')],
  289. 'order': None,
  290. 'limit': 10,
  291. 'offset': 0,
  292. },
  293. ),
  294. (
  295. Numero,
  296. {
  297. 'query_filters': ['superior.parent = 20'],
  298. 'field_list': ['lodel_id'],
  299. 'offset': 1024,
  300. 'order': ['titre', ('lodel_id', 'desc')]
  301. },
  302. {
  303. 'target_cls': Numero,
  304. 'field_list': ['lodel_id'],
  305. 'filters': [],
  306. 'rel_filters': [((leapi.lecrud.REL_SUP, 'parent'), '=', '20')],
  307. 'group': None,
  308. 'order': [('titre', 'ASC'), ('lodel_id', 'DESC')],
  309. 'limit': None,
  310. 'offset': 1024,
  311. },
  312. ),
  313. (
  314. Numero,
  315. {
  316. 'query_filters': ['superior.parent = 20'],
  317. 'field_list': ['lodel_id'],
  318. 'offset': 1024,
  319. 'limit': 2,
  320. 'order': ['titre', ('lodel_id', 'desc')],
  321. 'group': ['titre'],
  322. },
  323. {
  324. 'target_cls': Numero,
  325. 'field_list': ['lodel_id'],
  326. 'filters': [],
  327. 'rel_filters': [((leapi.lecrud.REL_SUP, 'parent'), '=', '20')],
  328. 'group': [('titre', 'ASC')],
  329. 'order': [('titre', 'ASC'), ('lodel_id', 'DESC')],
  330. 'limit': 2,
  331. 'offset': 1024,
  332. },
  333. ),
  334. ]
  335. for callcls, getargs, exptargs in args:
  336. if callcls.implements_letype:
  337. exptargs['filters'].append( ('type_id', '=', callcls._type_id) )
  338. if callcls.implements_leclass:
  339. exptargs['filters'].append( ('class_id', '=', callcls._class_id) )
  340. callcls.get(**getargs)
  341. dsmock.assert_called_once_with(**exptargs)
  342. dsmock.reset_mock()
  343. #
  344. # Utils methods check
  345. #
  346. def test_name2class(self):
  347. """ Testing name2class method """
  348. from dyncode import LeCrud, LeObject, LeRel2Type
  349. # Fetch all leapi dyn classes
  350. cls_lst = ['LeCrud', 'LeObject', 'LeRelation', 'LeHierarch', 'LeRel2Type', 'LeClass', 'LeType']
  351. leo_lst = LeObject._me_uid.values()
  352. r2t_lst = list()
  353. for leo in leo_lst:
  354. if leo.is_leclass() and hasattr(leo, '_linked_types'):
  355. for relleo in leo._linked_types:
  356. r2t_lst.append(LeRel2Type.relname(leo, relleo))
  357. leo_lst = [cls.__name__ for cls in leo_lst]
  358. # Begin test
  359. for clsname in cls_lst + leo_lst + r2t_lst:
  360. cls = LeCrud.name2class(clsname)
  361. self.assertEqual(cls.__name__, clsname)
  362. #Testing bad name or type
  363. badnames = ['_LeObject', 'foobar']
  364. for badname in badnames:
  365. self.assertFalse(LeCrud.name2class(badname))
  366. badnames = [int, LeObject]
  367. for badname in badnames:
  368. with self.assertRaises(ValueError):
  369. LeCrud.name2class(badname)
  370. def test_leobject(self):
  371. """ Test the LeObject shortcut getter """
  372. from dyncode import LeObject, LeCrud
  373. self.assertEqual(LeObject, LeCrud.leobject())
  374. def test_uidname(self):
  375. """ Tests the uid name getter """
  376. from dyncode import LeCrud, LeObject, LeRelation, Article, LeRel2Type
  377. with self.assertRaises(NotImplementedError):
  378. LeCrud.uidname()
  379. for lec in [LeObject, LeRelation, Article, LeRel2Type]:
  380. self.assertIn(lec.uidname(), lec._uid_fieldtype.keys())
  381. self.assertEqual(LeObject.uidname(), 'lodel_id')
  382. self.assertEqual(LeRelation.uidname(), 'id_relation')
  383. def test_typeasserts(self):
  384. """ Tests te implements_le* and is_le* methods """
  385. from dyncode import LeObject, LeCrud, LeRelation, LeHierarch, LeRel2Type, Article, Textes, Rel_Textes2Personne
  386. self.assertTrue(LeObject.is_leobject())