설명 없음
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_classes.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """
  2. Tests for the EmClass class
  3. """
  4. import os
  5. import logging
  6. from unittest import TestCase
  7. import unittest
  8. import EditorialModel
  9. from EditorialModel.classes import EmClass
  10. from EditorialModel.classtypes import EmClassType
  11. from EditorialModel.types import EmType
  12. from EditorialModel.fields import EmField
  13. from EditorialModel.model import Model
  14. from EditorialModel.backend.json_backend import EmBackendJson
  15. from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  16. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
  17. EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
  18. EM_TEST_OBJECT = None
  19. ## run once for this module
  20. def setUpModule():
  21. global EM_TEST_OBJECT
  22. #EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST), migration_handler=DjandoMigrationHandler('LodelTestInstance'))
  23. EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST), migration_handler=DummyMigrationHandler())
  24. logging.basicConfig(level=logging.CRITICAL)
  25. class ClassesTestCase(TestCase):
  26. # run before every instanciation of the class
  27. @classmethod
  28. def setUpClass(cls):
  29. pass
  30. #sqlsetup.init_db()
  31. # run before every function of the class
  32. def setUp(self):
  33. pass
  34. # creating an new EmClass should
  35. # - create a table named like the created EmClass
  36. # - insert a new line in em_classes
  37. class TestEmClassCreation(ClassesTestCase):
  38. # create a new EmClass, then test on it
  39. def test_create(self):
  40. ClassesTestCase.setUpClass()
  41. test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testclass1', 'classtype': EmClassType.entity['name']})
  42. # We check that the class has been added in the right list in the model object
  43. class_components_records = EM_TEST_OBJECT.components(EmClass)
  44. self.assertIn(test_class, class_components_records)
  45. # the name should be the one given
  46. test_class = EM_TEST_OBJECT.component(test_class.uid)
  47. self.assertEqual(test_class.name, 'testclass1')
  48. # the classtype should have the name of the EmClassType
  49. test_class = EM_TEST_OBJECT.component(test_class.uid)
  50. self.assertEqual(test_class.classtype, EmClassType.entity['name'])
  51. def test_default_fields(self):
  52. """ Test if the default + common_fields are created when an EmClass is created """
  53. classtype = EmClassType.entity['name']
  54. test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testdefaultfieldclass', 'classtype': classtype})
  55. ctype = EditorialModel.classtypes.EmClassType.get(classtype)
  56. default_fields = ctype['default_fields']
  57. default_fields.update(EditorialModel.classtypes.common_fields)
  58. fnames = [ f.name for f in test_class.fields() ]
  59. self.assertEqual(sorted(fnames), sorted(list(default_fields.keys())))
  60. # Testing class deletion (and associated table drop)
  61. class TestEmClassDeletion(ClassesTestCase):
  62. def setUp(self):
  63. self.names = ['testClasse1', 'testClasse2', 'testClasse3']
  64. self.emclasses = []
  65. self.emclasses.append(EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': self.names[0], 'classtype': EmClassType.entity['name']}))
  66. self.emclasses.append(EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': self.names[1], 'classtype': EmClassType.entry['name']}))
  67. self.emclasses.append(EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': self.names[2], 'classtype': EmClassType.person['name']}))
  68. # tests if the table is deleted after a call to delete
  69. def test_table_delete(self):
  70. """ Test associated table deletion on EmClass deletion """
  71. for _, class_object in enumerate(self.emclasses):
  72. self.assertTrue(EM_TEST_OBJECT.delete_component(class_object.uid), "delete method didn't return True but the class has no fieldgroups")
  73. # TODO check : "table still exists but the class was deleted"
  74. # TODO check : "table doesn't exist but the class was not deleted"
  75. # tests if delete refuse to delete if a class had fieldgroups
  76. def test_table_refuse_delete(self):
  77. """ Test delete on an EmClass that has fieldgroup """
  78. test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testfgclass1', 'classtype': EmClassType.entity['name']})
  79. test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testClassFalseEmpty', 'classtype': EmClassType.entity['name']})
  80. foo_field = EM_TEST_OBJECT.create_component('EmField', {'name': 'ahah', 'fieldtype':'char', 'class_id':test_class.uid})
  81. self.assertFalse(EM_TEST_OBJECT.delete_component(test_class.uid), "delete method returns True but the class has a non-default field")
  82. # TODO check : "table has been deleted but the class has fieldgroup"
  83. try:
  84. EM_TEST_OBJECT.component(test_class.uid)
  85. except Exception:
  86. self.fail("The class has been deleted but it has non-default field in the default fieldgroup")
  87. # Interface to types
  88. class TestEmClassTypes(ClassesTestCase):
  89. @classmethod
  90. def setUpClass(cls):
  91. pass
  92. def setUp(self):
  93. ClassesTestCase.setUpClass()
  94. self.test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testClassType', 'classtype': EmClassType.entity['name']})
  95. # tests if types() returns a list of EmType
  96. def test_types(self):
  97. """ Tests if types method returns the right list of EmType """
  98. test_class = EM_TEST_OBJECT.component(self.test_class.uid)
  99. EM_TEST_OBJECT.create_component(EmType.__name__, {'name': 'testClassType1', 'class_id': test_class.uid})
  100. EM_TEST_OBJECT.create_component(EmType.__name__, {'name': 'testClassType2', 'class_id': test_class.uid})
  101. types = test_class.types()
  102. self.assertIsInstance(types, list)
  103. for t in types:
  104. self.assertIsInstance(t, EmType)
  105. # with no type, types() should return an empty list
  106. def test_no_types(self):
  107. """ Test types method on an EmClass with no associated types """
  108. test_class = EM_TEST_OBJECT.component(self.test_class.uid)
  109. types = test_class.types()
  110. self.assertEqual(types, [])
  111. # interface to fields
  112. class TestEmClassFields(ClassesTestCase):
  113. @classmethod
  114. def setUpClass(cls):
  115. pass
  116. def setUp(self):
  117. ClassesTestCase.setUpClass()
  118. self.test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testClassFields', 'classtype': EmClassType.entity['name']})
  119. # tests if fields() returns a list of EmField
  120. def test_fields(self):
  121. """ testing fields method """
  122. test_class = EM_TEST_OBJECT.component(self.test_class.uid)
  123. EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'f1', 'class_id': test_class.uid, 'fieldtype': 'char'})
  124. EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'f2', 'class_id': test_class.uid, 'fieldtype': 'char'})
  125. fields = test_class.fields()
  126. self.assertIsInstance(fields, list)
  127. for field in fields:
  128. self.assertIsInstance(field, EmField)
  129. # with no field fields() should return an empty list
  130. def test_default_fields(self):
  131. """ Testing fields method on an EmClass with only defaults fields """
  132. test_class = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testClassNoFields', 'classtype': EmClassType.entity['name']})
  133. fields = test_class.fields()
  134. for fname in [f.name for f in fields]:
  135. self.assertIn(fname, EmClassType.get(test_class.classtype)['default_fields'].keys())
  136. # Creating a new EmClass should :
  137. # - create a table named like the created EmClass
  138. # - insert a new line in em_classes
  139. class TestEmClassLinkType(ClassesTestCase):
  140. # create a new EmClass, then test on it
  141. @classmethod
  142. def setUpClass(cls):
  143. ClassesTestCase.setUpClass()
  144. test_entity = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testEntity', 'classtype': EmClassType.entity['name']})
  145. test_entry = EM_TEST_OBJECT.create_component(EmClass.__name__, {'name': 'testEntry', 'classtype': EmClassType.entry['name']})
  146. def testLinkedTypes(self):
  147. """ Test the EmClass.linked_types() method """
  148. for field, linked_type in [ (f, EM_TEST_OBJECT.component(f.rel_to_type_id)) for f in EM_TEST_OBJECT.components(EmField) if 'rel_to_type_id' in f.__dict__]:
  149. self.assertIn(linked_type, field.em_class.linked_types())