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_lecrud.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. Tests for _LeObject and LeObject
  3. """
  4. import unittest
  5. from unittest import TestCase
  6. from unittest.mock import patch
  7. import EditorialModel
  8. import leapi
  9. import leapi.test.utils
  10. from leapi.lecrud import _LeCrud
  11. class LeCrudTestCase(TestCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. """ Write the generated code in a temporary directory and import it """
  15. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  16. @classmethod
  17. def tearDownClass(cls):
  18. """ Remove the temporary directory created at class setup """
  19. leapi.test.utils.cleanup(cls.tmpdir)
  20. def test_split_query_filter(self):
  21. """ Tests the _split_filter() classmethod """
  22. import dyncode
  23. query_results = {
  24. 'Hello = world' : ('Hello', '=', 'world'),
  25. 'hello <= "world"': ('hello', '<=', '"world"'),
  26. '_he42_ll-o >= \'world"': ('_he42_ll-o', '>=', '\'world"'),
  27. 'foo in ["foo", 42, \'bar\']': ('foo', ' in ', '["foo", 42, \'bar\']'),
  28. ' bar42 < 42': ('bar42', '<', '42'),
  29. ' _hidden > 1337': ('_hidden', '>', '1337'),
  30. '_42 not in foobar': ('_42', ' not in ', 'foobar'),
  31. 'hello in foo':('hello', ' in ', 'foo'),
  32. "\t\t\thello\t\t\nin\nfoo\t\t\n\t":('hello', ' in ', 'foo'),
  33. "hello \nnot\tin \nfoo":('hello', ' not in ', 'foo'),
  34. 'hello != bar':('hello', '!=', 'bar'),
  35. 'hello = "world>= <= != in not in"': ('hello', '=', '"world>= <= != in not in"'),
  36. 'superior.parent = 13': ('superior.parent', '=', '13'),
  37. }
  38. for query, result in query_results.items():
  39. res = dyncode.LeCrud._split_filter(query)
  40. self.assertEqual(res, result, "When parsing the query : '%s' the returned value is different from the expected '%s'"%(query, result))
  41. def test_invalid_split_query_filter(self):
  42. """ Testing the _split_filter() method with invalid queries """
  43. import dyncode
  44. invalid_queries = [
  45. '42 = 42',
  46. '4hello = foo',
  47. 'foo == bar',
  48. 'hello >> world',
  49. 'hello = ',
  50. ' = world',
  51. '=',
  52. '42',
  53. '"hello" = world',
  54. 'foo.bar = 15',
  55. ]
  56. for query in invalid_queries:
  57. with self.assertRaises(ValueError, msg='But the query was not valid : "%s"'%query):
  58. dyncode.LeCrud._split_filter(query)
  59. def test_field_is_relational(self):
  60. """ Testing the test to check if a field is relational """
  61. from dyncode import LeCrud
  62. test_fields = {
  63. 'superior.parent': True,
  64. 'subordinate.parent': True,
  65. 'foofoo.foo': False,
  66. }
  67. for field, eres in test_fields.items():
  68. self.assertEqual(LeCrud._field_is_relational(field), eres)
  69. def test_check_datas(self):
  70. """ testing the check_datas* methods """
  71. from dyncode import Publication, Numero, LeObject
  72. datas = { 'titre':'foobar' }
  73. Numero.check_datas_value(datas, False, False)
  74. Numero.check_datas_value(datas, True, False)
  75. with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
  76. Numero.check_datas_value({}, True)
  77. def test_prepare_filters(self):
  78. """ Testing the _prepare_filters() method """
  79. from dyncode import Publication, Numero, LeObject, Personnes
  80. #Simple filters
  81. filters = [
  82. 'lodel_id = 1',
  83. 'superior.parent > 2'
  84. ]
  85. filt, rfilt = Numero._prepare_filters(filters)
  86. self.assertEqual(filt, [('lodel_id', '=', '1')])
  87. self.assertEqual(rfilt, [((leapi.leobject.REL_SUP,'parent'), '>', '2')])
  88. #All fields, no relationnal and class given
  89. filters = []
  90. res_filt = []
  91. for field in Numero._fields:
  92. filters.append('%s=1'%field)
  93. res_filt.append((field, '=', '1'))
  94. filt, rfilt = Publication._prepare_filters(filters)
  95. self.assertEqual(rfilt, [])
  96. self.assertEqual(filt, res_filt)
  97. #Mixed type filters (tuple and string)
  98. filters = [
  99. ('lodel_id', '<=', '0'),
  100. 'subordinate.parent = 2',
  101. ]
  102. filt, rfilt = Numero._prepare_filters(filters)
  103. self.assertEqual(filt, [('lodel_id', '<=', '0')])
  104. self.assertEqual(rfilt, [((leapi.leobject.REL_SUB,'parent'), '=', '2')])
  105. def test_prepare_filters_invalid(self):
  106. """ Testing the _prepare_filters() method """
  107. from dyncode import LeCrud, Publication, Numero, Personnes, LeObject
  108. #Numero fields filters but no letype nor leclass given
  109. filters = []
  110. res_filt = []
  111. for field in Numero._fields:
  112. filters.append('%s=1'%field)
  113. res_filt.append((field, '=', '1'))
  114. with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
  115. LeObject._prepare_filters(filters)
  116. #simply invalid filters
  117. filters = ['hello world !']
  118. with self.assertRaises(ValueError):
  119. Personnes._prepare_filters(filters)