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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
  7. EM_TEST_OBJECT = None
  8. ## SetUpModule
  9. #
  10. # This function is called once for this module.
  11. def setUpModule():
  12. global EM_TEST_OBJECT
  13. EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST))
  14. def tearDownModule():
  15. #cleanDb(TEST_FIELD_DBNAME)
  16. pass
  17. ## FieldTestCase (Class)
  18. #
  19. # The parent class of all other test cases for the fields module.
  20. # It defines a SetUp function and some utility functions for EmField tests.
  21. class FieldTestCase(TestCase):
  22. @classmethod
  23. def setUpClass(cls):
  24. pass
  25. def setUp(self):
  26. self.test_fieldtype = 'integer'
  27. self.test_fieldgroup = EM_TEST_OBJECT.component(3)
  28. ## TestField (Class)
  29. #
  30. # The test class for the fields module
  31. class TestField(FieldTestCase):
  32. ## Test_create (Function)
  33. #
  34. # tests the creation process of a field
  35. def test_create(self):
  36. field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testfield1', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
  37. # We check that the field has been added
  38. field_records = EM_TEST_OBJECT.component(field.uid)
  39. self.assertIsNot(field_records, False)
  40. # We check that the field has been added in the right list in the model object
  41. field_components_records = EM_TEST_OBJECT.components(EmField)
  42. self.assertIn(field, field_components_records)
  43. def test_invalid_internal(self):
  44. """ Test that internal='object' is reserved for common_fields """
  45. with self.assertRaises(ValueError, msg="Only common_fields should be internal='object'"):
  46. field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testbadinternal','internal': 'object', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
  47. ## Test_Deletion
  48. #
  49. # tests the deletion process of a field
  50. def test_deletion(self):
  51. fields = []
  52. field_names = ['field1', 'field2']
  53. # We create the two fields
  54. for name in field_names:
  55. fields.append(EM_TEST_OBJECT.create_component(EmField.__name__, {'name': name, 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype}))
  56. for field in fields:
  57. # We check if the delete process was performed to the end
  58. self.assertTrue(EM_TEST_OBJECT.delete_component(field.uid))
  59. # We check that the field object is not in the editorial model anymore
  60. self.assertFalse(EM_TEST_OBJECT.component(field.uid))
  61. # We check that the field object is not in the EmField components list
  62. field_components_records = EM_TEST_OBJECT.components(EmField)
  63. self.assertNotIn(field, field_components_records)
  64. def test_emclass(self):
  65. """ Test if the EmField.em_class @property method is correct """
  66. for field in EM_TEST_OBJECT.components(EmField):
  67. self.assertIn(field, field.em_class.fields())
  68. self.assertIn(field.fieldgroup, field.em_class.fieldgroups())