|
@@ -1,10 +1,12 @@
|
1
|
1
|
import unittest
|
|
2
|
+from unittest.mock import MagicMock, patch
|
2
|
3
|
|
3
|
4
|
from EditorialModel.model import Model
|
4
|
5
|
from EditorialModel.classes import EmClass
|
5
|
6
|
from EditorialModel.types import EmType
|
6
|
7
|
from EditorialModel.fieldgroups import EmFieldGroup
|
7
|
8
|
from EditorialModel.fields import EmField
|
|
9
|
+from EditorialModel.components import EmComponent
|
8
|
10
|
|
9
|
11
|
from EditorialModel.backend.json_backend import EmBackendJson
|
10
|
12
|
from EditorialModel.migrationhandler.django import DjangoMigrationHandler
|
|
@@ -19,15 +21,40 @@ class TestModel(unittest.TestCase):
|
19
|
21
|
model = Model(EmBackendJson('EditorialModel/test/me.json'))
|
20
|
22
|
self.assertTrue(isinstance(model, Model))
|
21
|
23
|
|
22
|
|
- model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('LodelTestInstance', debug=True, dryrun=True))
|
|
24
|
+ model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('LodelTestInstance', debug=False, dryrun=True))
|
23
|
25
|
self.assertTrue(isinstance(model, Model))
|
24
|
26
|
|
25
|
27
|
def test_components(self):
|
26
|
28
|
""" Test components fetching """
|
|
29
|
+ uid_l = list()
|
27
|
30
|
for comp_class in [EmClass, EmType, EmField, EmFieldGroup]:
|
28
|
31
|
comp_l = self.me.components(comp_class)
|
|
32
|
+ #Testing types of returned components
|
29
|
33
|
for component in comp_l:
|
30
|
34
|
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)))
|
|
35
|
+ uid_l.append(component.uid)
|
|
36
|
+
|
|
37
|
+ #Testing that we have fetched all the components
|
|
38
|
+ for uid in self.me._components['uids']:
|
|
39
|
+ self.assertIn(uid, uid_l, "Component with uid %d was not fetched"%uid)
|
|
40
|
+
|
|
41
|
+ #Testing components method without parameters
|
|
42
|
+ uid_l = [ comp.uid for comp in self.me.components() ]
|
|
43
|
+
|
|
44
|
+ for uid in self.me._components['uids']:
|
|
45
|
+ self.assertIn(uid, uid_l, "Component with uid %d was not fetched using me.components()"%uid)
|
|
46
|
+
|
|
47
|
+ self.assertFalse(self.me.components(EmComponent))
|
|
48
|
+ self.assertFalse(self.me.components(int))
|
|
49
|
+
|
|
50
|
+ def test_component(self):
|
|
51
|
+ """ Test component fetching by uid """
|
|
52
|
+ #assert that __hash__, __eq__ and components() are corrects
|
|
53
|
+ for comp in self.me.components():
|
|
54
|
+ self.assertEqual(comp, self.me.component(comp.uid))
|
|
55
|
+
|
|
56
|
+ for baduid in [-1, 0xffffff, "hello" ]:
|
|
57
|
+ self.assertFalse(self.me.component(baduid))
|
31
|
58
|
|
32
|
59
|
def test_sort_components(self):
|
33
|
60
|
""" Test that Model.sort_components method actually sort components """
|
|
@@ -43,6 +70,64 @@ class TestModel(unittest.TestCase):
|
43
|
70
|
new_uid = self.me.new_uid()
|
44
|
71
|
self.assertNotIn(new_uid, self.me._components['uids'].keys())
|
45
|
72
|
|
|
73
|
+ def test_create_component_fails(self):
|
|
74
|
+ """ Test the create_component method with invalid arguments """
|
|
75
|
+
|
|
76
|
+ testDatas = {
|
|
77
|
+ 'name': 'FooBar',
|
|
78
|
+ 'classtype':'entity',
|
|
79
|
+ 'help_text': None,
|
|
80
|
+ 'string': None,
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ #Invalid uid
|
|
84
|
+ used_uid = self.me.components()[0].uid
|
|
85
|
+ for bad_uid in [ used_uid, -1, -1000, 'HelloWorld']:
|
|
86
|
+ with self.assertRaises(ValueError, msg="The component was created but the given uid (%s) whas invalid (allready used, negative or WTF)"%bad_uid):
|
|
87
|
+ self.me.create_component('EmClass', testDatas, uid = bad_uid)
|
|
88
|
+
|
|
89
|
+ #Invalid component_type
|
|
90
|
+ for bad_comp_name in ['EmComponent', 'EmFoobar', 'int', int]:
|
|
91
|
+ with self.assertRaises(ValueError, msg="The create_component don't raise when an invalid classname (%s) is given as parameter"%bad_comp_name):
|
|
92
|
+ self.me.create_component(bad_comp_name, testDatas)
|
|
93
|
+
|
|
94
|
+ #Invalid rank
|
|
95
|
+ for invalid_rank in [-1, 10000, 'laaaaast', 'tsrif']:
|
|
96
|
+ with self.assertRaises(ValueError, msg="A invalid rank (%s) was given"%invalid_rank):
|
|
97
|
+ foodat = testDatas.copy()
|
|
98
|
+ foodat['rank'] = invalid_rank
|
|
99
|
+ self.me.create_component('EmClass', testDatas)
|
|
100
|
+
|
|
101
|
+ #Invalid datas
|
|
102
|
+ for invalid_datas in [ dict(), [1,2,3,4], ('hello', 'world') ]:
|
|
103
|
+ with self.assertRaises(ValueError, msg="Invalid datas was given in parameters"):
|
|
104
|
+ self.me.create_component('EmClass', invalid_datas)
|
|
105
|
+
|
|
106
|
+ def test_create_class(self):
|
|
107
|
+ """ Test the create_component method mocking the EmComponent constructors """
|
|
108
|
+
|
|
109
|
+ cdats = {
|
|
110
|
+ 'name': 'FooBar',
|
|
111
|
+ 'classtype':'entity',
|
|
112
|
+ 'help_text': None,
|
|
113
|
+ 'string': None,
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ tmpuid = self.me.new_uid()
|
|
117
|
+ #with patch.object(EmClass, '__init__', return_value=EmClass(model=self.me, uid=tmpuid, name=cdats['name'], classtype=cdats['classtype'], help_text = cdats['help_text'], string = cdats['string'])) as initmock:
|
|
118
|
+ with patch.object(EmClass, '__init__', return_value=None) as initmock:
|
|
119
|
+ with self.assertRaises(AttributeError): #Raises because mocking
|
|
120
|
+ self.me.create_component('EmClass', cdats)
|
|
121
|
+ initmock.assert_called_once_with(
|
|
122
|
+ uid=tmpuid,
|
|
123
|
+ model=self.me,
|
|
124
|
+ name=cdats['name'],
|
|
125
|
+ classtype=cdats['classtype'],
|
|
126
|
+ help_text = cdats['help_text'],
|
|
127
|
+ string = cdats['string'],
|
|
128
|
+ )
|
|
129
|
+
|
|
130
|
+
|
46
|
131
|
def test_hash(self):
|
47
|
132
|
""" Test that __hash__ and __eq__ work properly on models """
|
48
|
133
|
me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
|
|
@@ -62,3 +147,30 @@ class TestModel(unittest.TestCase):
|
62
|
147
|
|
63
|
148
|
self.assertEqual(hash(me1), hash(me2), "After doing sames modifications in the two models the hashes differs")
|
64
|
149
|
self.assertTrue(me1.__eq__(me2))
|
|
150
|
+
|
|
151
|
+ def test_compclass_getter(self):
|
|
152
|
+ """ Test the Model methods that handles name <-> EmComponent conversion """
|
|
153
|
+ for classname in [ 'EmField', 'EmClass', 'EmFieldGroup', 'EmType' ]:
|
|
154
|
+ cls = Model.emclass_from_name(classname)
|
|
155
|
+ self.assertNotEqual(cls, False, "emclass_from_name return False when '%s' given as parameter"%classname)
|
|
156
|
+ self.assertEqual(cls.__name__, classname)
|
|
157
|
+
|
|
158
|
+ for classname in ['EmComponent', 'EmFoobar' ]:
|
|
159
|
+ self.assertFalse( Model.emclass_from_name(classname))
|
|
160
|
+
|
|
161
|
+ for comp_cls in [EmClass, EmFieldGroup, EmType]:
|
|
162
|
+ self.assertEqual(Model.name_from_emclass(comp_cls), comp_cls.__name__)
|
|
163
|
+ for comp in self.me.components(EmField):
|
|
164
|
+ self.assertEqual(Model.name_from_emclass(comp.__class__), 'EmField')
|
|
165
|
+
|
|
166
|
+ for cls in [EmComponent, int, str]:
|
|
167
|
+ self.assertFalse(Model.name_from_emclass(cls))
|
|
168
|
+
|
|
169
|
+ def test_load_save_invalid(self):
|
|
170
|
+ """ Test the behavior of a Model when no backend given but load and save are called """
|
|
171
|
+ bad_em = Model(None)
|
|
172
|
+
|
|
173
|
+ self.assertFalse(bad_em.load())
|
|
174
|
+ self.assertFalse(bad_em.save())
|
|
175
|
+
|
|
176
|
+
|