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.2KB

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