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

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