|
@@ -16,6 +16,7 @@ from EditorialModel.backend.dummy_backend import EmBackendDummy
|
16
|
16
|
from EditorialModel.migrationhandler.django import DjangoMigrationHandler
|
17
|
17
|
from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
|
18
|
18
|
|
|
19
|
+
|
19
|
20
|
class TestModel(unittest.TestCase):
|
20
|
21
|
|
21
|
22
|
def setUp(self):
|
|
@@ -31,10 +32,10 @@ class TestModel(unittest.TestCase):
|
31
|
32
|
|
32
|
33
|
def test_bad_init(self):
|
33
|
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
|
36
|
with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a bad backend"):
|
36
|
37
|
Model(bad)
|
37
|
|
- for bad in [ int, EmBackendDummy, DummyMigrationHandler, 'foobar' ]:
|
|
38
|
+ for bad in [int, EmBackendDummy, DummyMigrationHandler, 'foobar']:
|
38
|
39
|
with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a migration_handler"):
|
39
|
40
|
Model(EmBackendDummy(), bad)
|
40
|
41
|
|
|
@@ -47,16 +48,16 @@ class TestModel(unittest.TestCase):
|
47
|
48
|
for component in comp_l:
|
48
|
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
|
50
|
uid_l.append(component.uid)
|
50
|
|
-
|
|
51
|
+
|
51
|
52
|
#Testing that we have fetched all the components
|
52
|
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
|
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
|
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
|
62
|
self.assertFalse(self.me.components(EmComponent))
|
62
|
63
|
self.assertFalse(self.me.components(int))
|
|
@@ -67,7 +68,7 @@ class TestModel(unittest.TestCase):
|
67
|
68
|
for comp in self.me.components():
|
68
|
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
|
72
|
self.assertFalse(self.me.component(baduid))
|
72
|
73
|
|
73
|
74
|
def test_sort_components(self):
|
|
@@ -89,25 +90,25 @@ class TestModel(unittest.TestCase):
|
89
|
90
|
|
90
|
91
|
testDatas = {
|
91
|
92
|
'name': 'FooBar',
|
92
|
|
- 'classtype':'entity',
|
|
93
|
+ 'classtype': 'entity',
|
93
|
94
|
'help_text': None,
|
94
|
95
|
'string': None,
|
95
|
96
|
}
|
96
|
97
|
|
97
|
98
|
#Invalid uid
|
98
|
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
|
104
|
#Invalid component_type
|
104
|
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
|
107
|
self.me.create_component(bad_comp_name, testDatas)
|
107
|
108
|
|
108
|
109
|
#Invalid rank
|
109
|
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
|
112
|
foodat = testDatas.copy()
|
112
|
113
|
foodat['rank'] = invalid_rank
|
113
|
114
|
self.me.create_component('EmClass', foodat)
|
|
@@ -117,33 +118,33 @@ class TestModel(unittest.TestCase):
|
117
|
118
|
self.me.create_component('EmClass', foodat)
|
118
|
119
|
|
119
|
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
|
122
|
with self.assertRaises(TypeError, msg="Invalid datas was given in parameters"):
|
122
|
123
|
self.me.create_component('EmClass', invalid_datas)
|
123
|
124
|
|
124
|
125
|
def test_create_components(self):
|
125
|
126
|
""" Test the create_component method mocking the EmComponent constructors """
|
126
|
|
-
|
|
127
|
+
|
127
|
128
|
params = {
|
128
|
|
- 'EmClass' : {
|
129
|
|
- 'cls' : EmClass,
|
130
|
|
- 'cdats' : {
|
|
129
|
+ 'EmClass': {
|
|
130
|
+ 'cls': EmClass,
|
|
131
|
+ 'cdats': {
|
131
|
132
|
'name': 'FooClass',
|
132
|
|
- 'classtype':'entity',
|
|
133
|
+ 'classtype': 'entity',
|
133
|
134
|
}
|
134
|
135
|
},
|
135
|
|
- 'EmType' : {
|
|
136
|
+ 'EmType': {
|
136
|
137
|
'cls': EmType,
|
137
|
|
- 'cdats' : {
|
138
|
|
- 'name' : 'FooType',
|
|
138
|
+ 'cdats': {
|
|
139
|
+ 'name': 'FooType',
|
139
|
140
|
'class_id': self.me.components(EmClass)[0].uid,
|
140
|
141
|
'fields_list': [],
|
141
|
142
|
}
|
142
|
143
|
},
|
143
|
144
|
'EmFieldGroup': {
|
144
|
145
|
'cls': EmFieldGroup,
|
145
|
|
- 'cdats' : {
|
146
|
|
- 'name' : 'FooFG',
|
|
146
|
+ 'cdats': {
|
|
147
|
+ 'name': 'FooFG',
|
147
|
148
|
'class_id': self.me.components(EmClass)[0].uid,
|
148
|
149
|
},
|
149
|
150
|
},
|
|
@@ -154,9 +155,9 @@ class TestModel(unittest.TestCase):
|
154
|
155
|
'fieldgroup_id': self.me.components(EmFieldGroup)[0].uid,
|
155
|
156
|
'fieldtype': 'char',
|
156
|
157
|
'max_length': 64,
|
157
|
|
- 'optional':True,
|
|
158
|
+ 'optional': True,
|
158
|
159
|
'internal': False,
|
159
|
|
- 'rel_field_id':None,
|
|
160
|
+ 'rel_field_id': None,
|
160
|
161
|
'icon': '0',
|
161
|
162
|
'nullable': False,
|
162
|
163
|
'uniq': True,
|
|
@@ -172,7 +173,7 @@ class TestModel(unittest.TestCase):
|
172
|
173
|
with patch.object(params[n]['cls'], '__init__', return_value=None) as initmock:
|
173
|
174
|
try:
|
174
|
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
|
177
|
pass
|
177
|
178
|
cdats['uid'] = tmpuid
|
178
|
179
|
cdats['model'] = self.me
|
|
@@ -201,37 +202,38 @@ class TestModel(unittest.TestCase):
|
201
|
202
|
self.assertTrue(ret)
|
202
|
203
|
self.assertNotIn(cuid, new_em._components['uids'])
|
203
|
204
|
self.assertNotIn(comp, new_em._components[cname])
|
204
|
|
-
|
|
205
|
+
|
205
|
206
|
def test_set_backend(self):
|
206
|
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
|
210
|
self.me.set_backend(backend)
|
210
|
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
|
215
|
self.me.set_backend(bad_backend)
|
|
216
|
+
|
215
|
217
|
##
|
216
|
218
|
# @todo Test selected fields application
|
217
|
219
|
# @todo Test types hierarchy application
|
218
|
220
|
def test_migrate_handler(self):
|
219
|
221
|
""" Test that the migrate_handler() method create component in a good order """
|
220
|
|
-
|
|
222
|
+
|
221
|
223
|
#Testing component creation
|
222
|
224
|
with patch.object(Model, 'create_component', return_value=None) as create_mock:
|
223
|
225
|
try:
|
224
|
226
|
self.me.migrate_handler(None)
|
225
|
|
- except AttributeError: #Raises because of the mock
|
|
227
|
+ except AttributeError: # Raises because of the mock
|
226
|
228
|
pass
|
227
|
|
- order_comp = ['EmClass', 'EmType', 'EmFieldGroup', 'EmField'] #Excpected creation order
|
|
229
|
+ order_comp = ['EmClass', 'EmType', 'EmFieldGroup', 'EmField'] # Excpected creation order
|
228
|
230
|
cur_comp = 0
|
229
|
231
|
for mcall in create_mock.mock_calls:
|
230
|
232
|
#Testing EmComponent order of creation
|
231
|
233
|
while order_comp[cur_comp] != mcall[1][0]:
|
232
|
|
- cur_comp +=1
|
|
234
|
+ cur_comp += 1
|
233
|
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
|
238
|
#Testing uid
|
237
|
239
|
comp = self.me.component(mcall[1][2])
|
|
@@ -257,7 +259,6 @@ class TestModel(unittest.TestCase):
|
257
|
259
|
last_call = mh_mock.mock_calls[-1]
|
258
|
260
|
self.assertEqual(hash(last_call[1][0]), hash(self.me))
|
259
|
261
|
|
260
|
|
-
|
261
|
262
|
def test_hash(self):
|
262
|
263
|
""" Test that __hash__ and __eq__ work properly on models """
|
263
|
264
|
me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
|
|
@@ -280,13 +281,13 @@ class TestModel(unittest.TestCase):
|
280
|
281
|
|
281
|
282
|
def test_compclass_getter(self):
|
282
|
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
|
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
|
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
|
292
|
for comp_cls in [EmClass, EmFieldGroup, EmType]:
|
292
|
293
|
self.assertEqual(Model.name_from_emclass(comp_cls), comp_cls.__name__)
|
|
@@ -295,5 +296,3 @@ class TestModel(unittest.TestCase):
|
295
|
296
|
|
296
|
297
|
for cls in [EmComponent, int, str]:
|
297
|
298
|
self.assertFalse(Model.name_from_emclass(cls))
|
298
|
|
-
|
299
|
|
-
|