Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_classes.py 9.9KB

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