Bez popisu
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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import unittest
  2. from unittest import mock
  3. from unittest.mock import patch
  4. import tests.loader_utils
  5. import leapi_dyncode as dyncode
  6. from lodel.leapi.leobject import LeObject
  7. from lodel.leapi.query import LeDeleteQuery, LeUpdateQuery, LeGetQuery, \
  8. LeInsertQuery
  9. from lodel.leapi.exceptions import *
  10. class LeObjectDummyTestCase(unittest.TestCase):
  11. """ Testing LeObject method with a dummy datasource """
  12. def test_init(self):
  13. """ Testing LeObject child class __init__ """
  14. dyncode.Person(
  15. lodel_id = '1',
  16. lastname = "Foo",
  17. firstname = "Bar",
  18. alias = "Foobar")
  19. def test_init_abstract(self):
  20. """ Testing init abstract LeObject childs """
  21. abstract_classes = [
  22. dyncode.Entitie, dyncode.Indexabs]
  23. for cls in abstract_classes:
  24. with self.assertRaises(NotImplementedError):
  25. cls(lodel_id = 1)
  26. def test_init_bad_fields(self):
  27. """ Testing init with bad arguments """
  28. with self.assertRaises(LeApiErrors):
  29. dyncode.Person(
  30. lodel_id = 1,
  31. foobar = "barfoo")
  32. with self.assertRaises(LeApiError):
  33. dyncode.Person(lastname = "foo", firstname = "bar")
  34. def test_data_accessor(self):
  35. """ Testing data accessor method """
  36. inst = dyncode.Person(lodel_id = 1, lastname = "foo")
  37. self.assertEqual(inst.data('lodel_id'), 1)
  38. self.assertEqual(inst.data('lastname'), 'foo')
  39. def test_data_accessor_fails(self):
  40. """ Testing that data accessor detects unitialized fields """
  41. inst = dyncode.Person(lodel_id = 1, lastname = "foo")
  42. with self.assertRaises(RuntimeError):
  43. inst.data('firstname')
  44. def test_name2class(self):
  45. """ Testing the class method that returns a dynamic object given it's
  46. name """
  47. self.assertEqual(dyncode.Object.name2class('Person'), dyncode.Person)
  48. self.assertEqual(dyncode.Object.name2class('Object'), dyncode.Object)
  49. def test_bad_name2class(self):
  50. """ Testing failures of the class method that returns a dynamic object
  51. given it's name """
  52. badnames = ['foobar', 'str', str, None, 42]
  53. callers = [dyncode.Object, dyncode.Person, dyncode.Entitie]
  54. for caller in callers:
  55. for badname in badnames:
  56. with self.assertRaises(LeApiError, msg="LeApiError not raised \
  57. but invalid name %s was given" % badname):
  58. caller.name2class(badname)
  59. def test_abstract_name2class(self):
  60. with self.assertRaises(NotImplementedError):
  61. LeObject.name2class('Person')
  62. with self.assertRaises(NotImplementedError):
  63. LeObject.name2class(42)
  64. def test_initilized(self):
  65. """ Testing initialized method """
  66. inst = dyncode.Person(
  67. lodel_id = 1, lastname="foo")
  68. self.assertFalse(inst.initialized)
  69. def test_uid_fieldname(self):
  70. self.assertEqual(dyncode.Person.uid_fieldname(), ["lodel_id"])
  71. def test_fieldnames_accessor(self):
  72. """ Testing fieldnames() accessor method """
  73. fnames = dyncode.Person.fieldnames(False)
  74. self.assertEqual(set(fnames),
  75. {'lastname', 'linked_texts', 'firstname', 'alias'})
  76. def test_bad_insert(self):
  77. """ Insert with bad arguments """
  78. badargs = [
  79. {},
  80. {'lodel_id': 1,'lastname': 'foo', 'firstname': 'bar'}]
  81. for arg in badargs:
  82. with self.assertRaises(LeApiDataCheckErrors):
  83. dyncode.Person.insert(arg)
  84. def test_delete_instance(self):
  85. """ Testing instance method delete """
  86. inst = dyncode.Person(
  87. lodel_id = 1, firstname = "foo", lastname = "bar")
  88. inst.delete()
  89. class LeObjectQueryMockTestCase(unittest.TestCase):
  90. """ Testing LeObject mocking LeQuery objects """
  91. def test_insert(self):
  92. """ Checking that LeObject insert method calls LeInsertQuery
  93. correctly """
  94. datas = {'lastname': 'foo', 'firstname': 'bar'}
  95. with patch.object(
  96. LeInsertQuery, '__init__', return_value = None) as mock_init:
  97. try:
  98. dyncode.Person.insert(datas)
  99. except AttributeError:
  100. pass #Because of mock
  101. mock_init.assert_called_once_with(dyncode.Person)
  102. with patch.object(
  103. LeInsertQuery, 'execute', return_value = 42) as mock_insert:
  104. ret = dyncode.Person.insert(datas)
  105. self.assertEqual(ret, 42, 'Bad return value forwarding')
  106. mock_insert.assert_called_once_with(datas)
  107. def test_delete(self):
  108. """ Checking that LeObject delete method calls LeDeleteQuery
  109. correctly """
  110. with patch.object(
  111. LeDeleteQuery, '__init__', return_value = None) as mock_init:
  112. inst = dyncode.Person(
  113. lodel_id = 1, firstname = "foo", lastname = "bar")
  114. try:
  115. inst.delete()
  116. except AttributeError:
  117. pass
  118. mock_init.assert_called_once_with(
  119. dyncode.Person, [('lodel_id', '=', 1)])
  120. with patch.object(
  121. LeDeleteQuery, 'execute', return_value = 1) as mock_execute:
  122. inst = dyncode.Person(
  123. lodel_id = 1, firstname = "foo", lastname = "bar")
  124. ret = inst.delete()
  125. self.assertEqual(ret, 1, 'Bad return value forwarding')
  126. mock_execute.assert_called_once_with()
  127. def test_delete_bundle(self):
  128. """ Checking that LeObject delete_bundle method calls LeDeleteQuery
  129. correctly """
  130. with patch.object(
  131. LeDeleteQuery, '__init__', return_value = None) as mock_init:
  132. try:
  133. dyncode.Person.delete_bundle(['lodel_id > 1'])
  134. except AttributeError:
  135. pass
  136. mock_init.assert_called_once_with(
  137. dyncode.Person, ['lodel_id > 1'])
  138. with patch.object(
  139. LeDeleteQuery, 'execute', return_value = 1) as mock_execute:
  140. dyncode.Person.delete_bundle(['lodel_id > 1'])
  141. mock_execute.assert_called_once_with()
  142. def test_update_instance(self):
  143. """ Checking that LeObject update method calls LeUpdateQuery
  144. correctly """
  145. with patch.object(
  146. LeUpdateQuery, '__init__', return_value = None) as mock_init:
  147. with patch.object(
  148. LeObject, 'datas', return_value = {
  149. 'lodel_id': 1, 'firstname': 'foo', 'lastname': 'bar',
  150. 'fullname': 'Foo Bar', 'alias': None }) as mock_datas:
  151. inst = dyncode.Person(
  152. lodel_id = 1, firstname = "foo", lastname = "bar")
  153. try:
  154. inst.update()
  155. except AttributeError:
  156. pass
  157. mock_init.assert_called_once_with(
  158. dyncode.Person, [('lodel_id', '=', 1)])
  159. with patch.object(
  160. LeUpdateQuery, 'execute', return_value = None) as mock_update:
  161. with patch.object(
  162. LeObject, 'datas', return_value = {
  163. 'lodel_id': 1, 'firstname': 'foo', 'lastname': 'bar',
  164. 'fullname': 'Foo Bar', 'alias': None }) as mock_datas:
  165. inst = dyncode.Person(
  166. lodel_id = 1, firstname = "foo", lastname = "bar")
  167. inst.update()
  168. mock_update.assert_called_once_with({
  169. 'lodel_id': 1, 'firstname': 'foo', 'lastname': 'bar',
  170. 'fullname': 'Foo Bar', 'alias': None })
  171. def test_get(self):
  172. """ Checking that LeObject.get method calls LeGetQuery
  173. correctly """
  174. get_args = {
  175. 'query_filters': ['lodel_id = 1'],
  176. 'field_list': ['firstname'],
  177. 'order': ['firstname'],
  178. 'group': ['alias'],
  179. 'limit': 42,
  180. 'offset': 24}
  181. with patch.object(
  182. LeGetQuery, '__init__', return_value = None) as mock_init:
  183. try:
  184. dyncode.Person.get(**get_args)
  185. except AttributeError:
  186. pass
  187. mock_init.assert_called_once_with(
  188. dyncode.Person,
  189. **get_args)
  190. ret_val = [{
  191. 'lodel_id': 1,
  192. 'firstname': 'foo',
  193. 'lastname': 'bar',
  194. 'fullname': 'foo bar',
  195. 'alias': None,
  196. 'classname': 'Person'}]
  197. with patch.object(
  198. LeGetQuery, 'execute', return_value = ret_val) as mock_execute:
  199. results = dyncode.Person.get(**get_args)
  200. mock_execute.assert_called_once_with()
  201. res = results[0]
  202. self.assertEqual(res.d.lodel_id, 1)
  203. self.assertEqual(res.d.firstname, 'foo')
  204. self.assertEqual(res.d.lastname, 'bar')
  205. def test_get_mini(self):
  206. """ Checking that LeObject.get method calls LeGetQuery correctly
  207. when called with minimum args """
  208. with patch.object(
  209. LeGetQuery, '__init__', return_value = None) as mock_init:
  210. try:
  211. dyncode.Person.get(['lodel_id = 1'])
  212. except AttributeError:
  213. pass
  214. mock_init.assert_called_once_with(
  215. dyncode.Person,
  216. query_filters = ['lodel_id = 1'],
  217. field_list = None,
  218. order = None, group = None, limit = None, offset = 0)
  219. with patch.object(
  220. LeGetQuery, 'execute', return_value = []) as mock_exec:
  221. dyncode.Person.get(['lodel_id = 1'])
  222. mock_exec.assert_called_once_with()