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

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