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_classes.py 11KB

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