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.5KB

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