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_leobject.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """
  2. Tests for _LeObject
  3. """
  4. import unittest
  5. from unittest import TestCase
  6. import EditorialModel
  7. import leobject
  8. import leobject.test.utils
  9. from leobject.leobject import _LeObject
  10. ## Testing static methods that don't need the generated code
  11. class _LeObjectTestCase(TestCase):
  12. def test_split_query_filter(self):
  13. """ Tests the _split_filter() classmethod """
  14. query_results = {
  15. 'Hello = world' : ('Hello', '=', 'world'),
  16. 'hello <= "world"': ('hello', '<=', '"world"'),
  17. '_he42_ll-o >= \'world"': ('_he42_ll-o', '>=', '\'world"'),
  18. 'foo in ["foo", 42, \'bar\']': ('foo', ' in ', '["foo", 42, \'bar\']'),
  19. ' bar42 < 42': ('bar42', '<', '42'),
  20. ' _hidden > 1337': ('_hidden', '>', '1337'),
  21. '_42 not in foobar': ('_42', ' not in ', 'foobar'),
  22. 'hello in foo':('hello', ' in ', 'foo'),
  23. "\t\t\thello\t\t\nin\nfoo\t\t\n\t":('hello', ' in ', 'foo'),
  24. "hello \nnot\tin \nfoo":('hello', ' not in ', 'foo'),
  25. 'hello != bar':('hello', '!=', 'bar'),
  26. 'hello = "world>= <= != in not in"': ('hello', '=', '"world>= <= != in not in"'),
  27. 'superior.parent = 13': ('superior.parent', '=', '13'),
  28. }
  29. for query, result in query_results.items():
  30. res = _LeObject._split_filter(query)
  31. self.assertEqual(res, result, "When parsing the query : '%s' the returned value is different from the expected '%s'"%(query, result))
  32. def test_invalid_split_query_filter(self):
  33. """ Testing the _split_filter() method with invalid queries """
  34. invalid_queries = [
  35. '42 = 42',
  36. '4hello = foo',
  37. 'foo == bar',
  38. 'hello >> world',
  39. 'hello = ',
  40. ' = world',
  41. '=',
  42. '42',
  43. '"hello" = world',
  44. 'foo.bar = 15',
  45. ]
  46. for query in invalid_queries:
  47. with self.assertRaises(ValueError, msg='But the query was not valid : "%s"'%query):
  48. _LeObject._split_filter(query)
  49. ## Testing methods that need the generated code
  50. # @todo mock the datasource to test the get, update, delete and insert methods
  51. class LeObjectTestCase(TestCase):
  52. @classmethod
  53. def setUpClass(cls):
  54. """ Write the generated code in a temporary directory and import it """
  55. cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
  56. @classmethod
  57. def tearDownClass(cls):
  58. """ Remove the temporary directory created at class setup """
  59. leobject.test.utils.cleanup(cls.tmpdir)
  60. def test_uid2leobj(self):
  61. """ Testing _Leobject.uid2leobj() """
  62. import dyncode
  63. for i in dyncode.LeObject._me_uid.keys():
  64. cls = dyncode.LeObject.uid2leobj(i)
  65. if leobject.letype.LeType in cls.__bases__:
  66. self.assertEqual(i, cls._type_id)
  67. elif leobject.leclass.LeClass in cls.__bases__:
  68. self.assertEqual(i, cls._class_id)
  69. else:
  70. self.fail("Bad value returned : '%s'"%cls)
  71. i=10
  72. while i in dyncode.LeObject._me_uid.keys():
  73. i+=1
  74. with self.assertRaises(KeyError):
  75. dyncode.LeObject.uid2leobj(i)
  76. def test_prepare_targets(self):
  77. """ Testing _prepare_targets() method """
  78. from dyncode import Publication, Numero, LeObject
  79. test_v = {
  80. (None, None) : (None, None),
  81. (Publication, Numero): (Publication, Numero),
  82. (Publication, None): (Publication, None),
  83. (None, Numero): (Publication, Numero),
  84. (Publication,'Numero'): (Publication, Numero),
  85. ('Publication', Numero): (Publication, Numero),
  86. ('Publication', 'Numero'): (Publication, Numero),
  87. ('Publication', None): (Publication, None),
  88. (None, 'Numero'): (Publication, Numero),
  89. }
  90. for (leclass, letype), (rleclass, rletype) in test_v.items():
  91. self.assertEqual((rletype,rleclass), LeObject._prepare_targets(letype, leclass))
  92. def test_invalid_prepare_targets(self):
  93. """ Testing _prepare_targets() method with invalid arguments """
  94. from dyncode import Publication, Numero, LeObject, Personnes
  95. test_v = [
  96. ('',''),
  97. (Personnes, Numero),
  98. (leobject.leclass.LeClass, Numero),
  99. (Publication, leobject.letype.LeType),
  100. ('foobar', Numero),
  101. (Publication, 'foobar'),
  102. (Numero, Numero),
  103. (Publication, Publication),
  104. (None, Publication),
  105. ('foobar', 'foobar'),
  106. (42,1337),
  107. (type, Numero),
  108. (LeObject, Numero),
  109. (LeObject, LeObject),
  110. (Publication, LeObject),
  111. ]
  112. for (leclass, letype) in test_v:
  113. with self.assertRaises(ValueError):
  114. LeObject._prepare_targets(letype, leclass)
  115. def test_check_fields(self):
  116. """ Testing the _check_fields() method """
  117. from dyncode import Publication, Numero, LeObject, Personnes
  118. #Valid fields given
  119. LeObject._check_fields(None, Publication, Publication._fieldtypes.keys())
  120. LeObject._check_fields(Numero, None, Numero._fields)
  121. #Specials fields
  122. LeObject._check_fields(Numero, Publication, ['lodel_id'])
  123. #Common fields
  124. LeObject._check_fields(None, None, EditorialModel.classtypes.common_fields.keys())
  125. #Invalid fields
  126. with self.assertRaises(leobject.leobject.LeObjectQueryError):
  127. LeObject._check_fields(None, None, Numero._fields)
  128. def test_prepare_filters(self):
  129. """ Testing the _prepare_filters() method """
  130. from dyncode import Publication, Numero, LeObject, Personnes
  131. #Simple filters
  132. filters = [
  133. 'lodel_id = 1',
  134. 'superior.parent > 2'
  135. ]
  136. filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
  137. self.assertEqual(filt, [('lodel_id', '=', '1')])
  138. self.assertEqual(rfilt, [((leobject.leobject.REL_SUP,'parent'), '>', '2')])
  139. #All fields, no relationnal and class given
  140. filters = []
  141. res_filt = []
  142. for field in Numero._fields:
  143. filters.append('%s=1'%field)
  144. res_filt.append((field, '=', '1'))
  145. filt, rfilt = LeObject._prepare_filters(filters, None, Publication)
  146. self.assertEqual(rfilt, [])
  147. self.assertEqual(filt, res_filt)
  148. #Mixed type filters (tuple and string)
  149. filters = [
  150. ('lodel_id', '<=', '0'),
  151. 'subordinate.parent = 2',
  152. ]
  153. filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
  154. self.assertEqual(filt, [('lodel_id', '<=', '0')])
  155. self.assertEqual(rfilt, [((leobject.leobject.REL_SUB,'parent'), '=', '2')])
  156. def test_prepare_filters_invalid(self):
  157. """ Testing the _prepare_filters() method """
  158. from dyncode import Publication, Numero, LeObject, Personnes
  159. #Numero fields filters but no letype nor leclass given
  160. filters = []
  161. res_filt = []
  162. for field in Numero._fields:
  163. filters.append('%s=1'%field)
  164. res_filt.append((field, '=', '1'))
  165. with self.assertRaises(leobject.leobject.LeObjectQueryError):
  166. LeObject._prepare_filters(filters, None, None)
  167. #simply invalid filters
  168. filters = ['hello world !']
  169. with self.assertRaises(ValueError):
  170. LeObject._prepare_filters(filters, None, None)