Browse Source

PEP8/PyLint on test_model.py

Roland Haroutiounian 9 years ago
parent
commit
233ce905fc
1 changed files with 42 additions and 43 deletions
  1. 42
    43
      EditorialModel/test/test_model.py

+ 42
- 43
EditorialModel/test/test_model.py View File

16
 from EditorialModel.migrationhandler.django import DjangoMigrationHandler
16
 from EditorialModel.migrationhandler.django import DjangoMigrationHandler
17
 from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
17
 from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
18
 
18
 
19
+
19
 class TestModel(unittest.TestCase):
20
 class TestModel(unittest.TestCase):
20
 
21
 
21
     def setUp(self):
22
     def setUp(self):
31
 
32
 
32
     def test_bad_init(self):
33
     def test_bad_init(self):
33
         """ Test initialisation with bad arguments """
34
         """ Test initialisation with bad arguments """
34
-        for bad in [ None, int, EmBackendDummy, DummyMigrationHandler, 'foobar' ]:
35
+        for bad in [None, int, EmBackendDummy, DummyMigrationHandler, 'foobar']:
35
             with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a bad backend"):
36
             with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a bad backend"):
36
                 Model(bad)
37
                 Model(bad)
37
-        for bad in [ int, EmBackendDummy, DummyMigrationHandler, 'foobar' ]:
38
+        for bad in [int, EmBackendDummy, DummyMigrationHandler, 'foobar']:
38
             with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a migration_handler"):
39
             with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a migration_handler"):
39
                 Model(EmBackendDummy(), bad)
40
                 Model(EmBackendDummy(), bad)
40
 
41
 
47
             for component in comp_l:
48
             for component in comp_l:
48
                 self.assertTrue(isinstance(component, comp_class), "Model.components method doesn't return EmComponent of the right type. Asked for {} but got {}".format(type(comp_class), type(component)))
49
                 self.assertTrue(isinstance(component, comp_class), "Model.components method doesn't return EmComponent of the right type. Asked for {} but got {}".format(type(comp_class), type(component)))
49
                 uid_l.append(component.uid)
50
                 uid_l.append(component.uid)
50
-        
51
+
51
         #Testing that we have fetched all the components
52
         #Testing that we have fetched all the components
52
         for uid in self.me._components['uids']:
53
         for uid in self.me._components['uids']:
53
-            self.assertIn(uid, uid_l, "Component with uid %d was not fetched"%uid)
54
+            self.assertIn(uid, uid_l, "Component with uid %d was not fetched" % uid)
54
 
55
 
55
         #Testing components method without parameters
56
         #Testing components method without parameters
56
-        uid_l = [ comp.uid for comp in self.me.components() ]
57
+        uid_l = [comp.uid for comp in self.me.components()]
57
 
58
 
58
         for uid in self.me._components['uids']:
59
         for uid in self.me._components['uids']:
59
-            self.assertIn(uid, uid_l, "Component with uid %d was not fetched using me.components()"%uid)
60
+            self.assertIn(uid, uid_l, "Component with uid %d was not fetched using me.components()" % uid)
60
 
61
 
61
         self.assertFalse(self.me.components(EmComponent))
62
         self.assertFalse(self.me.components(EmComponent))
62
         self.assertFalse(self.me.components(int))
63
         self.assertFalse(self.me.components(int))
67
         for comp in self.me.components():
68
         for comp in self.me.components():
68
             self.assertEqual(comp, self.me.component(comp.uid))
69
             self.assertEqual(comp, self.me.component(comp.uid))
69
 
70
 
70
-        for baduid in [-1, 0xffffff, "hello" ]:
71
+        for baduid in [-1, 0xffffff, "hello"]:
71
             self.assertFalse(self.me.component(baduid))
72
             self.assertFalse(self.me.component(baduid))
72
 
73
 
73
     def test_sort_components(self):
74
     def test_sort_components(self):
89
 
90
 
90
         testDatas = {
91
         testDatas = {
91
             'name': 'FooBar',
92
             'name': 'FooBar',
92
-            'classtype':'entity',
93
+            'classtype': 'entity',
93
             'help_text': None,
94
             'help_text': None,
94
             'string': None,
95
             'string': None,
95
         }
96
         }
96
 
97
 
97
         #Invalid uid
98
         #Invalid uid
98
         used_uid = self.me.components()[0].uid
99
         used_uid = self.me.components()[0].uid
99
-        for bad_uid in [ used_uid, -1, -1000, 'HelloWorld']:
100
-            with self.assertRaises(ValueError, msg="The component was created but the given uid (%s) whas invalid (allready used, negative or WTF)"%bad_uid):
101
-                self.me.create_component('EmClass', testDatas, uid = bad_uid)
100
+        for bad_uid in [used_uid, -1, -1000, 'HelloWorld']:
101
+            with self.assertRaises(ValueError, msg="The component was created but the given uid (%s) whas invalid (allready used, negative or WTF)" % bad_uid):
102
+                self.me.create_component('EmClass', testDatas, uid=bad_uid)
102
 
103
 
103
         #Invalid component_type
104
         #Invalid component_type
104
         for bad_comp_name in ['EmComponent', 'EmFoobar', 'int', int]:
105
         for bad_comp_name in ['EmComponent', 'EmFoobar', 'int', int]:
105
-            with self.assertRaises(ValueError, msg="The create_component don't raise when an invalid classname (%s) is given as parameter"%bad_comp_name):
106
+            with self.assertRaises(ValueError, msg="The create_component don't raise when an invalid classname (%s) is given as parameter" % bad_comp_name):
106
                 self.me.create_component(bad_comp_name, testDatas)
107
                 self.me.create_component(bad_comp_name, testDatas)
107
 
108
 
108
         #Invalid rank
109
         #Invalid rank
109
         for invalid_rank in [-1, 10000]:
110
         for invalid_rank in [-1, 10000]:
110
-            with self.assertRaises(ValueError, msg="A invalid rank (%s) was given"%invalid_rank):
111
+            with self.assertRaises(ValueError, msg="A invalid rank (%s) was given" % invalid_rank):
111
                 foodat = testDatas.copy()
112
                 foodat = testDatas.copy()
112
                 foodat['rank'] = invalid_rank
113
                 foodat['rank'] = invalid_rank
113
                 self.me.create_component('EmClass', foodat)
114
                 self.me.create_component('EmClass', foodat)
117
             self.me.create_component('EmClass', foodat)
118
             self.me.create_component('EmClass', foodat)
118
 
119
 
119
         #Invalid datas
120
         #Invalid datas
120
-        for invalid_datas in [ dict(), [1,2,3,4], ('hello', 'world') ]:
121
+        for invalid_datas in [dict(), [1, 2, 3, 4], ('hello', 'world')]:
121
             with self.assertRaises(TypeError, msg="Invalid datas was given in parameters"):
122
             with self.assertRaises(TypeError, msg="Invalid datas was given in parameters"):
122
                 self.me.create_component('EmClass', invalid_datas)
123
                 self.me.create_component('EmClass', invalid_datas)
123
 
124
 
124
     def test_create_components(self):
125
     def test_create_components(self):
125
         """ Test the create_component method mocking the EmComponent constructors """
126
         """ Test the create_component method mocking the EmComponent constructors """
126
-        
127
+
127
         params = {
128
         params = {
128
-            'EmClass' : {
129
-                'cls' : EmClass,
130
-                'cdats' : {
129
+            'EmClass': {
130
+                'cls': EmClass,
131
+                'cdats': {
131
                     'name': 'FooClass',
132
                     'name': 'FooClass',
132
-                    'classtype':'entity',
133
+                    'classtype': 'entity',
133
                 }
134
                 }
134
             },
135
             },
135
-            'EmType' : {
136
+            'EmType': {
136
                 'cls': EmType,
137
                 'cls': EmType,
137
-                'cdats' : {
138
-                    'name' : 'FooType',
138
+                'cdats': {
139
+                    'name': 'FooType',
139
                     'class_id': self.me.components(EmClass)[0].uid,
140
                     'class_id': self.me.components(EmClass)[0].uid,
140
                     'fields_list': [],
141
                     'fields_list': [],
141
                 }
142
                 }
142
             },
143
             },
143
             'EmFieldGroup': {
144
             'EmFieldGroup': {
144
                 'cls': EmFieldGroup,
145
                 'cls': EmFieldGroup,
145
-                'cdats' : {
146
-                    'name' : 'FooFG',
146
+                'cdats': {
147
+                    'name': 'FooFG',
147
                     'class_id': self.me.components(EmClass)[0].uid,
148
                     'class_id': self.me.components(EmClass)[0].uid,
148
                 },
149
                 },
149
             },
150
             },
154
                     'fieldgroup_id': self.me.components(EmFieldGroup)[0].uid,
155
                     'fieldgroup_id': self.me.components(EmFieldGroup)[0].uid,
155
                     'fieldtype': 'char',
156
                     'fieldtype': 'char',
156
                     'max_length': 64,
157
                     'max_length': 64,
157
-                    'optional':True,
158
+                    'optional': True,
158
                     'internal': False,
159
                     'internal': False,
159
-                    'rel_field_id':None,
160
+                    'rel_field_id': None,
160
                     'icon': '0',
161
                     'icon': '0',
161
                     'nullable': False,
162
                     'nullable': False,
162
                     'uniq': True,
163
                     'uniq': True,
172
             with patch.object(params[n]['cls'], '__init__', return_value=None) as initmock:
173
             with patch.object(params[n]['cls'], '__init__', return_value=None) as initmock:
173
                 try:
174
                 try:
174
                     self.me.create_component(n, params[n]['cdats'])
175
                     self.me.create_component(n, params[n]['cdats'])
175
-                except AttributeError: #Raises because the component is a MagicMock
176
+                except AttributeError:  # Raises because the component is a MagicMock
176
                     pass
177
                     pass
177
                 cdats['uid'] = tmpuid
178
                 cdats['uid'] = tmpuid
178
                 cdats['model'] = self.me
179
                 cdats['model'] = self.me
201
                 self.assertTrue(ret)
202
                 self.assertTrue(ret)
202
                 self.assertNotIn(cuid, new_em._components['uids'])
203
                 self.assertNotIn(cuid, new_em._components['uids'])
203
                 self.assertNotIn(comp, new_em._components[cname])
204
                 self.assertNotIn(comp, new_em._components[cname])
204
-    
205
+
205
     def test_set_backend(self):
206
     def test_set_backend(self):
206
         """ Test the set_backend method """
207
         """ Test the set_backend method """
207
 
208
 
208
-        for backend in [ EmBackendJson('EditorialModel/test/me.json'), EmBackendDummy() ]:
209
+        for backend in [EmBackendJson('EditorialModel/test/me.json'), EmBackendDummy()]:
209
             self.me.set_backend(backend)
210
             self.me.set_backend(backend)
210
             self.assertEqual(self.me.backend, backend)
211
             self.assertEqual(self.me.backend, backend)
211
 
212
 
212
-        for bad_backend in [None, 'wow', int, EmBackendJson ]:
213
-            with self.assertRaises(TypeError, msg="But bad argument (%s %s) was given"%(type(bad_backend),bad_backend)):
213
+        for bad_backend in [None, 'wow', int, EmBackendJson]:
214
+            with self.assertRaises(TypeError, msg="But bad argument (%s %s) was given" % (type(bad_backend), bad_backend)):
214
                 self.me.set_backend(bad_backend)
215
                 self.me.set_backend(bad_backend)
216
+
215
     ##
217
     ##
216
     # @todo Test selected fields application
218
     # @todo Test selected fields application
217
     # @todo Test types hierarchy application
219
     # @todo Test types hierarchy application
218
     def test_migrate_handler(self):
220
     def test_migrate_handler(self):
219
         """ Test that the migrate_handler() method create component in a good order """
221
         """ Test that the migrate_handler() method create component in a good order """
220
-        
222
+
221
         #Testing component creation
223
         #Testing component creation
222
         with patch.object(Model, 'create_component', return_value=None) as create_mock:
224
         with patch.object(Model, 'create_component', return_value=None) as create_mock:
223
             try:
225
             try:
224
                 self.me.migrate_handler(None)
226
                 self.me.migrate_handler(None)
225
-            except AttributeError: #Raises because of the mock
227
+            except AttributeError:  # Raises because of the mock
226
                 pass
228
                 pass
227
-            order_comp = ['EmClass', 'EmType', 'EmFieldGroup', 'EmField'] #Excpected creation order
229
+            order_comp = ['EmClass', 'EmType', 'EmFieldGroup', 'EmField']  # Excpected creation order
228
             cur_comp = 0
230
             cur_comp = 0
229
             for mcall in create_mock.mock_calls:
231
             for mcall in create_mock.mock_calls:
230
                 #Testing EmComponent order of creation
232
                 #Testing EmComponent order of creation
231
                 while order_comp[cur_comp] != mcall[1][0]:
233
                 while order_comp[cur_comp] != mcall[1][0]:
232
-                    cur_comp +=1
234
+                    cur_comp += 1
233
                     if cur_comp >= len(order_comp):
235
                     if cur_comp >= len(order_comp):
234
-                        self.assertTrue(False, 'The order of create_component() calls was not respected by migrate_handler() methods');
236
+                        self.assertTrue(False, 'The order of create_component() calls was not respected by migrate_handler() methods')
235
 
237
 
236
                 #Testing uid
238
                 #Testing uid
237
                 comp = self.me.component(mcall[1][2])
239
                 comp = self.me.component(mcall[1][2])
257
             last_call = mh_mock.mock_calls[-1]
259
             last_call = mh_mock.mock_calls[-1]
258
             self.assertEqual(hash(last_call[1][0]), hash(self.me))
260
             self.assertEqual(hash(last_call[1][0]), hash(self.me))
259
 
261
 
260
-
261
     def test_hash(self):
262
     def test_hash(self):
262
         """ Test that __hash__ and __eq__ work properly on models """
263
         """ Test that __hash__ and __eq__ work properly on models """
263
         me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
264
         me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
280
 
281
 
281
     def test_compclass_getter(self):
282
     def test_compclass_getter(self):
282
         """ Test the Model methods that handles name <-> EmComponent conversion """
283
         """ Test the Model methods that handles name <-> EmComponent conversion """
283
-        for classname in [ 'EmField', 'EmClass', 'EmFieldGroup', 'EmType' ]:
284
+        for classname in ['EmField', 'EmClass', 'EmFieldGroup', 'EmType']:
284
             cls = Model.emclass_from_name(classname)
285
             cls = Model.emclass_from_name(classname)
285
-            self.assertNotEqual(cls, False, "emclass_from_name return False when '%s' given as parameter"%classname)
286
+            self.assertNotEqual(cls, False, "emclass_from_name return False when '%s' given as parameter" % classname)
286
             self.assertEqual(cls.__name__, classname)
287
             self.assertEqual(cls.__name__, classname)
287
 
288
 
288
-        for classname in ['EmComponent', 'EmFoobar' ]:
289
-            self.assertFalse( Model.emclass_from_name(classname))
289
+        for classname in ['EmComponent', 'EmFoobar']:
290
+            self.assertFalse(Model.emclass_from_name(classname))
290
 
291
 
291
         for comp_cls in [EmClass, EmFieldGroup, EmType]:
292
         for comp_cls in [EmClass, EmFieldGroup, EmType]:
292
             self.assertEqual(Model.name_from_emclass(comp_cls), comp_cls.__name__)
293
             self.assertEqual(Model.name_from_emclass(comp_cls), comp_cls.__name__)
295
 
296
 
296
         for cls in [EmComponent, int, str]:
297
         for cls in [EmComponent, int, str]:
297
             self.assertFalse(Model.name_from_emclass(cls))
298
             self.assertFalse(Model.name_from_emclass(cls))
298
-
299
-

Loading…
Cancel
Save