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_reference.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import unittest
  2. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef
  3. from leapi.query.utils import init_dyncode
  4. from lodel.exceptions import *
  5. from lodel.leapi.leobject import LeObject
  6. dyncode = init_dyncode()
  7. obj1 = dyncode.Person(
  8. lodel_id = '1',
  9. lastname = "Foo",
  10. firstname = "Bar",
  11. alias = "Foobar")
  12. obj2 = dyncode.Collection(
  13. lodel_id = '3',
  14. title = "Foo)")
  15. obj3 = dyncode.Collection(
  16. lodel_id = '4',
  17. title = "Foo")
  18. class ReferenceTestCase(unittest.TestCase):
  19. def test_init_reference_class(self):
  20. reference = None
  21. try:
  22. reference = Reference()
  23. except NotImplementedError:
  24. self.assertNotIsInstance(reference, DataHandler)
  25. self.assertIsNone(datahandler)
  26. def test_reference_check_bad_data_value(self):
  27. test_ref = Reference((obj1, obj2))
  28. for test_value in ['toto']:
  29. with self.assertRaises(FieldValidationError):
  30. test_ref._check_data_value(test_value)
  31. def test_reference_check_good_data_value(self):
  32. test_ref = Reference((obj1,))
  33. for test_value in [obj3, 15]:
  34. value = test_ref._check_data_value(test_value)
  35. self.assertEqual(test_value, value)
  36. class MultipleRefTestCase(unittest.TestCase):
  37. def test_multiref_check_data_value_not_iter(self):
  38. test_multiref = MultipleRef(3)
  39. for test_value in [obj3]:
  40. with self.assertRaises(FieldValidationError):
  41. test_multiref._check_data_value(test_value)
  42. def test_multiref_check_data_multi_bad_value_error(self):
  43. test_multiref = MultipleRef(3)
  44. for test_value in [(obj3, 15, 'toto')]:
  45. with self.assertRaises(FieldValidationError) as cm:
  46. test_multiref._check_data_value(test_value)
  47. the_exception = cm.exception
  48. self.assertEqual(the_exception.args, ("MultipleRef have for invalid values [15,'toto'] :",))
  49. def test_multiref_check_data_too_max_lenght_iter_error(self):
  50. test_multiref = MultipleRef(3)
  51. for test_value in [(obj3, obj2, obj1, obj3)]:
  52. with self.assertRaises(FieldValidationError):
  53. test_multiref._check_data_value(test_value)
  54. def test_multiref_check_data_uid_multi_bad_value_error(self):
  55. test_multiref = MultipleRef(5, **{'allowed_classes' : [dyncode.Person, dyncode.Collection]})
  56. for test_value in [(obj3, obj2, 1, 15, 'toto')]:
  57. with self.assertRaises(FieldValidationError) as cm:
  58. test_multiref._check_data_value(test_value)
  59. the_exception = cm.exception
  60. self.assertEqual(the_exception.args, ("MultipleRef have for invalid values ['toto'] :",))
  61. def test_multiref_check_data_object_uid_multi_good_value_error(self):
  62. test_multiref = MultipleRef(5, **{'allowed_classes' : [dyncode.Person, dyncode.Collection]})
  63. for test_value in [(obj3, obj2, 1.2, 15)]:
  64. value = test_multiref._check_data_value(test_value)
  65. self.assertEqual(value, [obj3, obj2, 1, 15])