Browse Source

Added tests for EmField.delete() and EmClass.delete()

Also add a new function is sqlutils to execute ddl
Yann Weber 9 years ago
parent
commit
6359adc0f4
3 changed files with 71 additions and 0 deletions
  1. 8
    0
      Database/sqlutils.py
  2. 44
    0
      EditorialModel/test/test_classes.py
  3. 19
    0
      EditorialModel/test/test_field.py

+ 8
- 0
Database/sqlutils.py View File

@@ -91,3 +91,11 @@ def getTable(cls):
91 91
     engine = cls.getDbE()
92 92
     return sqla.Table(cls.table, meta(engine))
93 93
 
94
+def ddl_execute(ddl, db_engine):
95
+    conn = db_engine.connect()
96
+    req = str(ddl.compile(dialect=db_engine.dialect))
97
+    logger.debug("Executing custom raw SQL query : '"+req+"'")
98
+    ret = conn.execute(req)
99
+    conn.close()
100
+    return bool(ret)
101
+

+ 44
- 0
EditorialModel/test/test_classes.py View File

@@ -5,8 +5,10 @@
5 5
 import os
6 6
 
7 7
 from unittest import TestCase
8
+import unittest
8 9
 
9 10
 from django.conf import settings
11
+from EditorialModel.components import EmComponentNotExistError
10 12
 from EditorialModel.classes import EmClass
11 13
 from EditorialModel.classtypes import EmClassType
12 14
 from EditorialModel.fieldgroups import EmFieldGroup
@@ -80,6 +82,47 @@ class TestEmClassCreation(ClassesTestCase):
80 82
         cl = EmClass('testClass')
81 83
         self.assertEqual(cl.classtype, EmClassType.entity['name'])
82 84
 
85
+# Testing class deletion (and associated table drop)
86
+class TestEmClassDeletion(ClassesTestCase):
87
+    
88
+    def setUp(self):
89
+        self.names = ['testClass1', 'testClass2', 'testClass3']
90
+        EmClass.create(self.names[0], EmClassType.entity)
91
+        EmClass.create(self.names[1], EmClassType.entry)
92
+        EmClass.create(self.names[2], EmClassType.person)
93
+        pass
94
+    
95
+    # test if the table is deleted after a call to delete
96
+    def test_table_delete(self):
97
+        dbe = sqlutils.getEngine()
98
+        for i,class_name in enumerate(self.names):
99
+            cur_class = EmClass(class_name)
100
+            self.assertTrue(cur_class.delete(), "delete method didn't return True but the class has no fieldgroups")
101
+            meta = sqlutils.meta(dbe)
102
+            table_list = meta.tables.keys()
103
+            for deleted_name in self.names[:i+1]:
104
+                self.assertNotIn(deleted_name, table_list, "Table still exist but the class was deleted")
105
+            for not_deleted_name in self.names[i+1:]:
106
+                self.assertIn(not_deleted_name, table_list, "Table don't exist but the class was NOT deleted")
107
+            with self.assertRaises(EmComponentNotExistError,msg="This EmClass should be deleted"):
108
+                EmClass(class_name)
109
+        pass
110
+    
111
+    # test if delete refuse to delete if a class had fieldgroups
112
+    def test_table_refuse_delete(self):
113
+        test_class = EmClass(self.names[0])
114
+        fieldgroup = EmFieldGroup.create('fooFieldGroup', test_class)
115
+        self.assertFalse(test_class.delete(), "delete method returns True but the class has fieldgroup")
116
+        dbe = sqlutils.getEngine()
117
+        meta = sqlutils.meta(dbe)
118
+        self.assertIn(self.names[0], meta.tables, "Table has been deleted but the class has fieldgroup")
119
+        try:
120
+            EmClass(self.names[0])
121
+        except EmComponentNotExistError:
122
+            self.fail("The class has been deleted but it has fieldgroups")
123
+        pass
124
+
125
+
83 126
 # interface to fieldGroups
84 127
 class TestEmClassFieldgroups(ClassesTestCase):
85 128
 
@@ -168,6 +211,7 @@ class TestEmClassFields(ClassesTestCase):
168 211
 # creating an new EmClass should
169 212
 # - create a table named like the created EmClass
170 213
 # - insert a new line in em_classes
214
+@unittest.skip("Not implemented yet")
171 215
 class TestEmClassLinkType(ClassesTestCase):
172 216
 
173 217
     # create a new EmClass, then test on it

+ 19
- 0
EditorialModel/test/test_field.py View File

@@ -128,3 +128,22 @@ class TestField(FieldTestCase):
128 128
         field_column = sqla.Column(**field_column_args)
129 129
         self.assertIn(field_column.name, field_table_columns)
130 130
         pass
131
+
132
+    def test_deletion(self):
133
+        field_names = ['field1', 'field2']
134
+        for name in field_names:
135
+            EmField.create(name=name, fieldgroup=self.testFieldgroup, fieldtype = self.testFieldType)
136
+
137
+        for i,name in enumerate(field_names):
138
+            test_field = EmField(name)
139
+            self.assertTrue(test_field.delete())
140
+
141
+            cols = self.get_table_columns(self.testClass.name)
142
+            for deleted_name in field_names[:i+1]:
143
+                self.assertNotIn(deleted_name, cols, "Column is  not deleted")
144
+            for not_deleted_name in field_names[i+1:]:
145
+                self.assertIn(deleted_name, cols, "A bad column was deleted")
146
+                
147
+            with self.assertRaises(EmComponentNotExistError, msg="This field should be deleted"):
148
+                EmField(name)
149
+                

Loading…
Cancel
Save