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

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