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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.leobject import _LeObject
  11. ## Testing methods that need the generated code
  12. class LeObjectTestCase(TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. """ Write the generated code in a temporary directory and import it """
  16. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  17. @classmethod
  18. def tearDownClass(cls):
  19. """ Remove the temporary directory created at class setup """
  20. leapi.test.utils.cleanup(cls.tmpdir)
  21. def test_uid2leobj(self):
  22. """ Testing _Leobject.uid2leobj() """
  23. import dyncode
  24. from dyncode import LeType, LeClass
  25. for i in dyncode.LeObject._me_uid.keys():
  26. cls = dyncode.LeObject.uid2leobj(i)
  27. if LeType in cls.__bases__:
  28. self.assertEqual(i, cls._type_id)
  29. elif LeClass in cls.__bases__:
  30. self.assertEqual(i, cls._class_id)
  31. else:
  32. self.fail("Bad value returned : '%s'"%cls)
  33. i=10
  34. while i in dyncode.LeObject._me_uid.keys():
  35. i+=1
  36. with self.assertRaises(KeyError):
  37. dyncode.LeObject.uid2leobj(i)
  38. @unittest.skip("Obsolete but may be usefull for datasources tests")
  39. def test_prepare_targets(self):
  40. """ Testing _prepare_targets() method """
  41. from dyncode import Publication, Numero, LeObject
  42. test_v = {
  43. (None, None) : (None, None),
  44. (Publication, Numero): (Publication, Numero),
  45. (Publication, None): (Publication, None),
  46. (None, Numero): (Publication, Numero),
  47. (Publication,'Numero'): (Publication, Numero),
  48. ('Publication', Numero): (Publication, Numero),
  49. ('Publication', 'Numero'): (Publication, Numero),
  50. ('Publication', None): (Publication, None),
  51. (None, 'Numero'): (Publication, Numero),
  52. }
  53. for (leclass, letype), (rleclass, rletype) in test_v.items():
  54. self.assertEqual((rletype,rleclass), LeObject._prepare_targets(letype, leclass))
  55. @unittest.skip("Obsolete but may be usefull for datasources tests")
  56. def test_invalid_prepare_targets(self):
  57. """ Testing _prepare_targets() method with invalid arguments """
  58. from dyncode import Publication, Numero, LeObject, Personnes
  59. test_v = [
  60. ('',''),
  61. (Personnes, Numero),
  62. (leapi.leclass.LeClass, Numero),
  63. (Publication, leapi.letype.LeType),
  64. ('foobar', Numero),
  65. (Publication, 'foobar'),
  66. (Numero, Numero),
  67. (Publication, Publication),
  68. (None, Publication),
  69. ('foobar', 'foobar'),
  70. (42,1337),
  71. (type, Numero),
  72. (LeObject, Numero),
  73. (LeObject, LeObject),
  74. (Publication, LeObject),
  75. ]
  76. for (leclass, letype) in test_v:
  77. with self.assertRaises(ValueError):
  78. LeObject._prepare_targets(letype, leclass)
  79. class LeObjectMockDatasourceTestCase(TestCase):
  80. """ Testing _LeObject using a mock on the datasource """
  81. @classmethod
  82. def setUpClass(cls):
  83. """ Write the generated code in a temporary directory and import it """
  84. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  85. @classmethod
  86. def tearDownClass(cls):
  87. """ Remove the temporary directory created at class setup """
  88. leapi.test.utils.cleanup(cls.tmpdir)
  89. @patch('leapi.datasources.dummy.DummyDatasource.insert')
  90. def test_insert(self, dsmock):
  91. from dyncode import Publication, Numero, LeObject
  92. ndatas = [
  93. {'titre' : 'FooBar'},
  94. {'titre':'hello'},
  95. {'titre':'world'},
  96. ]
  97. for ndats in ndatas:
  98. Publication.insert(ndats, classname='Numero')
  99. dsmock.assert_called_once_with(Numero, **ndats)
  100. dsmock.reset_mock()
  101. LeObject.insert(ndats, classname = 'Numero')
  102. dsmock.assert_called_once_with(Numero, **ndats)
  103. dsmock.reset_mock()
  104. @unittest.skip("Wait FieldTypes modification (cf. #90) and classmethod capabilities for update")
  105. @patch('leapi.datasources.dummy.DummyDatasource.update')
  106. def test_update(self, dsmock):
  107. from dyncode import Publication, Numero, LeObject
  108. args = [
  109. ( ['lodel_id = 1'],
  110. {'titre':'foobar'},
  111. [('lodel_id','=','1')],
  112. []
  113. ),
  114. ( ['superior.parent in [1,2,3,4,5,6]', 'titre != "FooBar"'],
  115. {'titre':'FooBar'},
  116. [( 'titre','!=','"FooBar"')],
  117. [( (leapi.leobject.REL_SUP, 'parent') ,' in ', '[1,2,3,4,5,6]')]
  118. ),
  119. ]
  120. for filters, datas, ds_filters, ds_relfilters in args:
  121. LeObject.update(Numero, filters, datas)
  122. dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters, datas)
  123. dsmock.reset_mock()
  124. LeObject.update('Numero', filters, datas)
  125. dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters, datas)
  126. dsmock.reset_mock()
  127. @patch('leapi.datasources.dummy.DummyDatasource.delete')
  128. def test_delete(self, dsmock):
  129. from dyncode import Publication, Numero, LeObject, LeType
  130. args = [
  131. (
  132. 'Numero',
  133. ['lodel_id=1'],
  134. [('lodel_id', '=', '1'), ('type_id', '=', Numero._type_id)],
  135. []
  136. ),
  137. (
  138. 'Publication',
  139. ['subordinate.parent not in [1,2,3]', 'titre = "titre nul"'],
  140. [('titre','=', '"titre nul"'), ('class_id', '=', Publication._class_id)],
  141. [( (leapi.leobject.REL_SUB, 'parent'), ' not in ', '[1,2,3]')]
  142. ),
  143. ]
  144. for classname, filters, ds_filters, ds_relfilters in args:
  145. ccls = LeObject.name2class(classname)
  146. LeObject.delete(filters, classname)
  147. dsmock.assert_called_once_with(ccls, ds_filters, ds_relfilters)
  148. dsmock.reset_mock()
  149. if not (LeType in ccls.__bases__): #tests for calls with a LeClass child
  150. ccls.delete(filters)
  151. dsmock.assert_called_once_with(ccls, ds_filters, ds_relfilters)
  152. dsmock.reset_mock()