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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef
  21. from leapi.query.utils import init_dyncode
  22. from lodel.exceptions import *
  23. from lodel.leapi.leobject import LeObject
  24. dyncode = init_dyncode()
  25. obj1 = dyncode.Person(
  26. lodel_id = '1',
  27. lastname = "Foo",
  28. firstname = "Bar",
  29. alias = "Foobar")
  30. obj2 = dyncode.Collection(
  31. lodel_id = '3',
  32. title = "Foo)")
  33. obj3 = dyncode.Collection(
  34. lodel_id = '4',
  35. title = "Foo")
  36. class ReferenceTestCase(unittest.TestCase):
  37. def test_init_reference_class(self):
  38. reference = None
  39. try:
  40. reference = Reference()
  41. except NotImplementedError:
  42. self.assertNotIsInstance(reference, DataHandler)
  43. self.assertIsNone(datahandler)
  44. def test_reference_check_bad_data_value(self):
  45. test_ref = Reference((obj1, obj2))
  46. for test_value in ['toto']:
  47. with self.assertRaises(FieldValidationError):
  48. test_ref._check_data_value(test_value)
  49. def test_reference_check_good_data_value(self):
  50. test_ref = Reference((obj1,))
  51. for test_value in [obj3, 15]:
  52. value = test_ref._check_data_value(test_value)
  53. self.assertEqual(test_value, value)
  54. class MultipleRefTestCase(unittest.TestCase):
  55. def test_multiref_check_data_value_not_iter(self):
  56. test_multiref = MultipleRef(3)
  57. for test_value in [obj3]:
  58. with self.assertRaises(FieldValidationError):
  59. test_multiref._check_data_value(test_value)
  60. def test_multiref_check_data_multi_bad_value_error(self):
  61. test_multiref = MultipleRef(3)
  62. for test_value in [(obj3, 15, 'toto')]:
  63. with self.assertRaises(FieldValidationError) as cm:
  64. test_multiref._check_data_value(test_value)
  65. the_exception = cm.exception
  66. self.assertEqual(the_exception.args, ("MultipleRef have for invalid values [15,'toto'] :",))
  67. def test_multiref_check_data_too_max_lenght_iter_error(self):
  68. test_multiref = MultipleRef(3)
  69. for test_value in [(obj3, obj2, obj1, obj3)]:
  70. with self.assertRaises(FieldValidationError):
  71. test_multiref._check_data_value(test_value)
  72. def test_multiref_check_data_uid_multi_bad_value_error(self):
  73. test_multiref = MultipleRef(5, **{'allowed_classes' : [dyncode.Person, dyncode.Collection]})
  74. for test_value in [(obj3, obj2, 1, 15, 'toto')]:
  75. with self.assertRaises(FieldValidationError) as cm:
  76. test_multiref._check_data_value(test_value)
  77. the_exception = cm.exception
  78. self.assertEqual(the_exception.args, ("MultipleRef have for invalid values ['toto'] :",))
  79. def test_multiref_check_data_object_uid_multi_good_value_error(self):
  80. test_multiref = MultipleRef(5, **{'allowed_classes' : [dyncode.Person, dyncode.Collection]})
  81. for test_value in [(obj3, obj2, 1.2, 15)]:
  82. value = test_multiref._check_data_value(test_value)
  83. self.assertEqual(value, [obj3, obj2, 1, 15])