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_field.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os
  2. from unittest import TestCase
  3. from EditorialModel.fields import EmField
  4. from EditorialModel.model import Model
  5. from EditorialModel.backend.json_backend import EmBackendJson
  6. from EditorialModel.exceptions import EmComponentCheckError
  7. EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
  8. EM_TEST_OBJECT = None
  9. ## SetUpModule
  10. #
  11. # This function is called once for this module.
  12. def setUpModule():
  13. global EM_TEST_OBJECT
  14. EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST))
  15. def tearDownModule():
  16. #cleanDb(TEST_FIELD_DBNAME)
  17. pass
  18. ## FieldTestCase (Class)
  19. #
  20. # The parent class of all other test cases for the fields module.
  21. # It defines a SetUp function and some utility functions for EmField tests.
  22. class FieldTestCase(TestCase):
  23. @classmethod
  24. def setUpClass(cls):
  25. pass
  26. def setUp(self):
  27. self.test_fieldtype = 'integer'
  28. self.test_class = EM_TEST_OBJECT.components('EmClass')[0]
  29. ## TestField (Class)
  30. #
  31. # The test class for the fields module
  32. class TestField(FieldTestCase):
  33. ## Test_create (Function)
  34. #
  35. # tests the creation process of a field
  36. def test_create(self):
  37. field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testfield1', 'class_id': self.test_class.uid, 'fieldtype': self.test_fieldtype})
  38. # We check that the field has been added
  39. field_records = EM_TEST_OBJECT.component(field.uid)
  40. self.assertIsNot(field_records, False)
  41. # We check that the field has been added in the right list in the model object
  42. field_components_records = EM_TEST_OBJECT.components(EmField)
  43. self.assertIn(field, field_components_records)
  44. def test_invalid_internal(self):
  45. """ Test that internal='object' is reserved for common_fields """
  46. with self.assertRaises(ValueError, msg="Only common_fields should be internal='object'"):
  47. field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testbadinternal','internal': 'object', 'class_id': self.test_class.uid, 'fieldtype': self.test_fieldtype})
  48. def test_double_rel2type(self):
  49. """ Test the rel2type unicity """
  50. em = EM_TEST_OBJECT
  51. emtype = em.components('EmType')[0]
  52. emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
  53. f1 = em.create_component('EmField', {'name': 'testr2t', 'class_id': emclass.uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
  54. with self.assertRaises(EmComponentCheckError):
  55. f2 = em.create_component('EmField', {'name': 'testr2t2', 'class_id': emclass.uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
  56. def test_same_name(self):
  57. """ Test the name unicity is the same EmClass"""
  58. em = EM_TEST_OBJECT
  59. emtype = em.components('EmType')[0]
  60. emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
  61. f1 = em.create_component('EmField', {'name': 'samename', 'class_id': emclass.uid, 'fieldtype': 'char'})
  62. with self.assertRaises(EmComponentCheckError):
  63. f2 = em.create_component('EmField', {'name': 'samename', 'class_id': emclass.uid, 'fieldtype': 'integer'} )
  64. ## Test_Deletion
  65. #
  66. # tests the deletion process of a field
  67. def test_deletion(self):
  68. fields = []
  69. field_names = ['field1', 'field2']
  70. # We create the two fields
  71. for name in field_names:
  72. fields.append(EM_TEST_OBJECT.create_component(EmField.__name__, {'name': name, 'class_id': self.test_class.uid, 'fieldtype': self.test_fieldtype}))
  73. for field in fields:
  74. # We check if the delete process was performed to the end
  75. self.assertTrue(EM_TEST_OBJECT.delete_component(field.uid))
  76. # We check that the field object is not in the editorial model anymore
  77. self.assertFalse(EM_TEST_OBJECT.component(field.uid))
  78. # We check that the field object is not in the EmField components list
  79. field_components_records = EM_TEST_OBJECT.components(EmField)
  80. self.assertNotIn(field, field_components_records)
  81. def test_emclass(self):
  82. """ Test if the EmField.em_class @property method is correct """
  83. for field in EM_TEST_OBJECT.components(EmField):
  84. self.assertIn(field, field.em_class.fields())