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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import unittest
  2. from unittest import mock
  3. from unittest.mock import patch
  4. import tests.loader_utils
  5. from tests.leapi.query.utils import dyncode_module 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', 'LeObject', '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):
  57. caller.name2class(badname)
  58. def test_abstract_name2class(self):
  59. with self.assertRaises(NotImplementedError):
  60. LeObject.name2class('Person')
  61. with self.assertRaises(NotImplementedError):
  62. LeObject.name2class(42)
  63. def test_initilized(self):
  64. """ Testing initialized method """
  65. inst = dyncode.Person(
  66. lodel_id = 1, lastname="foo")
  67. self.assertFalse(inst.initialized)
  68. def test_uid_fieldname(self):
  69. self.assertEqual(dyncode.Person.uid_fieldname(), ["lodel_id"])
  70. def test_fieldnames_accessor(self):
  71. """ Testing fieldnames() accessor method """
  72. fnames = dyncode.Person.fieldnames(False)
  73. self.assertEqual(set(fnames),
  74. {'lastname', 'linked_texts', 'firstname', 'alias'})
  75. def test_insert(self):
  76. """ Testing insert method """
  77. dyncode.Person.insert({'lastname': 'foo', 'firstname': 'bar'})
  78. def test_bad_insert(self):
  79. """ Insert with bad arguments """
  80. badargs = [
  81. {},
  82. {'lodel_id': 1,'lastname': 'foo', 'firstname': 'bar'}]
  83. for arg in badargs:
  84. with self.assertRaises(LeApiDataCheckErrors):
  85. dyncode.Person.insert(arg)
  86. def test_delete_instance(self):
  87. """ Testing instance method delete """
  88. inst = dyncode.Person(
  89. lodel_id = 1, firstname = "foo", lastname = "bar")
  90. inst.delete()
  91. class LeObjectQueryMockTestCase(unittest.TestCase):
  92. """ Testing LeObject mocking LeQuery objects """
  93. def test_insert(self):
  94. datas = {'lastname': 'foo', 'firstname': 'bar'}
  95. with patch.object(
  96. LeInsertQuery, '__init__', return_value = None) as mock_init:
  97. dyncode.Person.insert(datas)
  98. mock_insert.assert_called_once_with(dyncode.Person)
  99. with patch.object(
  100. LeInsertQuery, 'execute', return_value = 42) as mock_insert:
  101. ret = dyncode.Person.insert(datas)
  102. self.AssertEqual(ret, 42, 'Bad return value forwarding')
  103. mock_insert.assert_called_once_with(datas)