Browse Source

Some code formating

Yann Weber 9 years ago
parent
commit
9ff45c97a0
1 changed files with 43 additions and 31 deletions
  1. 43
    31
      EditorialModel/test/test_component.py

+ 43
- 31
EditorialModel/test/test_component.py View File

@@ -30,6 +30,10 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
30 30
 #=#############=#
31 31
 
32 32
 def setUpModule():
33
+    """ This function is run once for this module.
34
+        
35
+        The goal are to overwrtie Db configs, and prepare objects for test_case initialisation
36
+    """
33 37
     #Overwritting db confs to make tests
34 38
     settings.LODEL2SQLWRAPPER = {
35 39
         'default': {
@@ -42,7 +46,6 @@ def setUpModule():
42 46
     logging.basicConfig(level=logging.CRITICAL)
43 47
 
44 48
     #testDB setup
45
-    #   TODO May be slow
46 49
     sqls = SQLSetup()
47 50
     tables = sqls.get_schema()
48 51
     ttest = {   'name':'ttest',
@@ -59,9 +62,9 @@ def setUpModule():
59 62
             }
60 63
     tables.append(ttest)
61 64
 
62
-    
63 65
     globals()['dbwrapper'] = SqlWrapper(read_db='default', write_db = 'default', alchemy_logs=False)
64 66
     globals()['tables'] = tables
67
+    pass
65 68
 
66 69
 
67 70
 #A dummy EmComponent child class use to make tests
@@ -70,16 +73,19 @@ class EmTestComp(EmComponent):
70 73
     ranked_in = 'rank_fam'
71 74
     def __init__(self, ion):
72 75
         super(EmTestComp, self).__init__(ion)
76
+        pass
73 77
 
74 78
     def populate(self):
75 79
         row = super(EmTestComp, self).populate()
76 80
         self.rank_fam = row.rank_fam
81
+        pass
77 82
 
78 83
     def save(self):
79 84
         values = { 'rank_fam': self.rank_fam }
80
-        
81 85
         return super(EmTestComp, self).save(values)
82 86
 
87
+# The parent class of all other test cases for component
88
+# It defines a SetUp function and some utility functions for EmComponent tests
83 89
 class ComponentTestCase(TestCase):
84 90
 
85 91
     @classmethod
@@ -95,13 +101,14 @@ class ComponentTestCase(TestCase):
95 101
 
96 102
     def setUp(self):
97 103
         # Db RAZ
98
-        self.db.dropAll()
104
+        globals()['dbwrapper'].dropAll()
99 105
         # Db schema creation
100
-        self.db.createAllFromConf(self.tables);
101
-        self.dber = self.db.r_engine
102
-        self.dbew = self.db.w_engine
106
+        globals()['dbwrapper'].createAllFromConf(globals()['tables']);
107
+        #Db Engines init
108
+        self.dber = globals()['dbwrapper'].r_engine
109
+        self.dbew = globals()['dbwrapper'].w_engine
103 110
         
104
-        # Test Em creation
111
+        # Insertion of testings datas
105 112
         conn = self.dber.connect()
106 113
         test_table = sqla.Table(EmTestComp.table, sqlutils.meta(self.dber))
107 114
         uids_table = sqla.Table('uids', sqlutils.meta(self.dber))
@@ -131,7 +138,7 @@ class ComponentTestCase(TestCase):
131 138
         req = test_table.insert(values=self.test_values)
132 139
         conn.execute(req)
133 140
     
134
-        """#This commented block allow to dump the test values at each setup
141
+        """#Uncomment this block to dump the test values at each setup
135 142
         req = sqla.select([test_table])
136 143
         res = conn.execute(req).fetchall()
137 144
         print("\nDEBUG (dump inserted)")
@@ -142,7 +149,6 @@ class ComponentTestCase(TestCase):
142 149
             print(strrow)
143 150
         print("\n")
144 151
         """
145
-
146 152
         conn.close()
147 153
 
148 154
         footable = sqla.Table('em_class', sqlutils.meta(self.dber))
@@ -187,15 +193,16 @@ class ComponentTestCase(TestCase):
187 193
         
188 194
         super(ComponentTestCase, self).run(result)
189 195
 
190
-    #=#############=#
191
-    #  TESTS BEGIN  #
192
-    #=#############=#
196
+#=#############=#
197
+#  TESTS BEGIN  #
198
+#=#############=#
193 199
 
200
+#=======================#
201
+#   EmComponent.newUid  #
202
+#=======================#
194 203
 class TestUid(ComponentTestCase):
195 204
 
196
-    #=======================#
197
-    #   EmComponent.newUid  #
198
-    #=======================#
205
+    
199 206
     def test_newuid(self):
200 207
         """ Test valid calls for newUid method """
201 208
         for _ in range(10):
@@ -207,10 +214,10 @@ class TestUid(ComponentTestCase):
207 214
             rep = conn.execute(req)
208 215
             res = rep.fetchall()
209 216
         
210
-            self.assertEqual(len(res), 1)
217
+            self.assertEqual(len(res), 1, "Error when selecting : mutliple rows returned for 1 UID")
211 218
             res = res[0]
212
-            self.assertEqual(res.uid, nuid)
213
-            self.assertEqual(res.table, EmTestComp.table)
219
+            self.assertEqual(res.uid, nuid, "Selected UID didn't match created uid")
220
+            self.assertEqual(res.table, EmTestComp.table, "Table not match with class table : expected '"+res.table+"' but got '"+EmTestComp.table+"'")
214 221
         pass
215 222
     
216 223
     def test_newuid_abstract(self):
@@ -218,12 +225,13 @@ class TestUid(ComponentTestCase):
218 225
         with self.assertRaises(NotImplementedError):
219 226
             EmComponent.newUid()
220 227
         pass
221
-
228
+        
229
+#===========================#
230
+#   EmComponent.__init__    #
231
+#===========================#
222 232
 class TestInit(ComponentTestCase):
223 233
 
224
-    #===========================#
225
-    #   EmComponent.__init__    #
226
-    #===========================#
234
+
227 235
     def test_component_abstract_init(self):
228 236
         """ Test not valid call (from EmComponent) of __init__ """
229 237
         with self.assertRaises(EnvironmentError):
@@ -264,11 +272,12 @@ class TestInit(ComponentTestCase):
264 272
             with self.assertRaises(TypeError):
265 273
                 EmTestComp(badarg)
266 274
         pass
267
-
275
+        
276
+#=======================#
277
+#   EmComponent.save    #
278
+#=======================#
268 279
 class TestSave(ComponentTestCase):
269
-    #=======================#
270
-    #   EmComponent.save    #
271
-    #=======================#
280
+
272 281
 
273 282
     def _savecheck(self, test_comp, newval):
274 283
         """ Utility function for test_component_save_namechange """
@@ -334,9 +343,12 @@ class TestSave(ComponentTestCase):
334 343
 
335 344
         #change all
336 345
         with self.subTest("Save after name, help and string change"):
337
-            newval['name'] = test_comp.name = 'newnewname'
346
+            test_comp.name = newval['name'] = test_comp.name = 'newnewname'
338 347
             newval['help'] = '{"fr": "help fra", "en":"help eng", "es":"help esp"}'
348
+            test_comp.help = MlString.load(newval['help'])
339 349
             newval['string'] = '{"fr": "string FR", "en":"string EN", "es":"string ES", "foolang":"foofoobar"}'
350
+            test_comp.string = MlString.load(newval['string'])
351
+
340 352
             test_comp.save()
341 353
             self._savecheck(test_comp, newval)
342 354
 
@@ -405,10 +417,10 @@ class TestSave(ComponentTestCase):
405 417
                 """
406 418
         pass
407 419
     
420
+#===========================#
421
+# EmComponent.modify_rank   #
422
+#===========================#
408 423
 class TestModifyRank(ComponentTestCase):
409
-    #===========================#
410
-    # EmComponent.modify_rank   #
411
-    #===========================#
412 424
 
413 425
     def dump_ranks(self):
414 426
         names = [ v['name'] for v in self.test_values ]

Loading…
Cancel
Save