Browse Source

[Broken tests] Add some tests for xmlfile EM translator

Yann Weber 8 years ago
parent
commit
373987ab9f

+ 3
- 3
lodel/editorial_model/translator/xmlfile.py View File

@@ -176,9 +176,9 @@ def write_emclass_xml(etree, elem, uid, name, help_text, group, fields, parents,
176 176
 ##@brief Loads a model from a xml file
177 177
 # @param model EditorialModel : the model to load
178 178
 # @return a new EditorialModel object
179
-def load(**kwargs):
179
+def load(filename):
180 180
 
181
-    Em = etree.parse(kwargs['filename'])
181
+    Em = etree.parse(filename)
182 182
     emodel = Em.getroot()
183 183
     name = emodel.find('name')
184 184
     description = emodel.find('description')
@@ -346,4 +346,4 @@ def load_mlstring_xml(elem):
346 346
 
347 347
 
348 348
 
349
-        
349
+        

+ 168
- 2
tests/editorial_model/test_translator_xmlfile.py View File

@@ -5,6 +5,7 @@ import tempfile
5 5
 import os
6 6
 
7 7
 import tests.loader_utils
8
+from lodel.editorial_model.translator import picklefile
8 9
 from lodel.editorial_model.translator import xmlfile
9 10
 from lodel.editorial_model.model import EditorialModel
10 11
 from lodel.editorial_model.components import *
@@ -12,12 +13,28 @@ from lodel.editorial_model.exceptions import *
12 13
 
13 14
 class XmlFileTestCase(unittest.TestCase):
14 15
     
16
+    def __init__(self, *args):
17
+        super().__init__(*args)
18
+        self.tmpfile = None
19
+
20
+    def setUp(self):
21
+        if self.tmpfile is not None:
22
+            os.unlink(self.tmpfile)
23
+        f_tmp, self.tmpfile = tempfile.mkstemp()
24
+        os.close(f_tmp)
25
+
26
+    def tearDown(self):
27
+        if self.tmpfile is not None:
28
+            os.unlink(self.tmpfile)
29
+
15 30
     def test_save(self):
16 31
         emmodel = EditorialModel("test model", description = "Test EM")
17
-        cls1 = emmodel.new_class('testclass1', display_name = 'Classe de test 1', help_text = 'super aide')
32
+        cls1 = emmodel.new_class(   'testclass1',
33
+                                    display_name = 'Classe de test 1',
34
+                                    help_text = 'super aide')
18 35
         c1f1 = cls1.new_field('testfield1', data_handler = 'varchar')
19 36
         c1f2 = cls1.new_field('testfield2', data_handler = 'varchar')
20
-        cls2 = emmodel.new_class('testclass2') #, display_name = 'Classe de test 2', help_text = 'super aide')
37
+        cls2 = emmodel.new_class('testclass2')
21 38
         c2f1 = cls2.new_field('testfield1', data_handler = 'varchar')
22 39
         c2f2 = cls2.new_field('testfield2', data_handler = 'varchar')
23 40
 
@@ -46,5 +63,154 @@ class XmlFileTestCase(unittest.TestCase):
46 63
         
47 64
         self.assertEqual(new_model.d_hash(), emmodel.d_hash())
48 65
 
66
+    def test_abstract_classes(self):
67
+        """ Testing xmlfile abtract class handling """
68
+        emmodel = EditorialModel("em_test", description = "Test model")
69
+        cls1 = emmodel.new_class(   'testclass1',
70
+                                    display_name = "test class 1",
71
+                                    abstract = True)
72
+
73
+        emmodel.save(xmlfile, filename=self.tmpfile)
74
+
75
+        emmodel_loaded = xmlfile.load(self.tmpfile)
76
+        self.assertEqual(   emmodel.d_hash(),
77
+                            emmodel_loaded.d_hash())
78
+    
79
+    def test_groups(self):
80
+        """ Testing xmlfile groups handling """
81
+        emmodel = EditorialModel("em_test", description = "test model")
82
+        emmodel.new_group(  'test_grp',
83
+                            display_name = "Test group")
84
+
85
+        emmodel.save(xmlfile, filename=self.tmpfile)
86
+        
87
+        emmodel_loaded = xmlfile.load(self.tmpfile)
88
+        self.assertEqual(   emmodel.d_hash(),
89
+                            emmodel_loaded.d_hash())
90
+
91
+    def test_groups_population(self):
92
+        """ Testing xmlfile groups population handling """
93
+        emmodel = EditorialModel("em_test", description = "test model")
94
+        cls1 = emmodel.new_class(   'testclass1',
95
+                                    display_name = "test class 1")
96
+        cls2 = emmodel.new_class(   'testclass2',
97
+                                    display_name = "test class 2")
98
+        cls1f1 = cls1.new_field(    'testfield1',
99
+                                    data_handler = 'varchar')
100
+        cls2f1 = cls2.new_field(    'testfield2',
101
+                                    data_handler = 'varchar')
102
+        cls2f2 = cls2.new_field(    'testfield3',
103
+                                    data_handler = 'varchar')
104
+        grp1 = emmodel.new_group(  'test_grp',
105
+                                    display_name = "Test group")
106
+        grp2 = emmodel.new_group(  'test_grp2',
107
+                                    display_name = "Test group2")
108
+        grp1.add_components([cls1,cls2])
109
+        grp2.add_components([cls1f1,cls2f1, cls2f2])
110
+        emmodel.save(xmlfile, filename=self.tmpfile)
111
+        
112
+        emmodel_loaded = xmlfile.load(self.tmpfile)
113
+        self.assertEqual(   emmodel.d_hash(),
114
+                            emmodel_loaded.d_hash())
115
+
116
+    def test_groups_dependencies(self):
117
+        """ Testing xmlfile groups population dependencies """
118
+        emmodel = EditorialModel("em_test", description = "test model")
119
+        grp1 = emmodel.new_group(  'test_grp',
120
+                                    display_name = "Test group")
121
+        grp2 = emmodel.new_group(  'test_grp2',
122
+                                   display_name = "Test group2")
123
+        grp3 = emmodel.new_group(  'test_grp2',
124
+                                   display_name = "Test group2",
125
+                                   depends = [grp1, grp2])
126
+        emmodel.save(xmlfile, filename=self.tmpfile)
127
+        
128
+        emmodel_loaded = xmlfile.load(self.tmpfile)
129
+        self.assertEqual(   emmodel.d_hash(),
130
+                            emmodel_loaded.d_hash())
131
+
132
+    def test_emfield_with_prop_bool(self):
133
+        """ Testing xmlfile with bool as property for datahandler """
134
+        emmodel = EditorialModel("em_test", description = "test model")
135
+        cls1 = emmodel.new_class(   'testclass1',
136
+                                    display_name = "test class 1")
137
+        cls1f1 = cls1.new_field(    'testfield1',
138
+                                    data_handler = 'varchar',
139
+                                    nullalble = True)
140
+        emmodel.save(xmlfile, filename=self.tmpfile)
141
+        
142
+        emmodel_loaded = xmlfile.load(self.tmpfile)
143
+        self.assertEqual(   emmodel.d_hash(),
144
+                            emmodel_loaded.d_hash())
145
+
146
+    def test_emfield_with_prop_tuple(self):
147
+        """ Testing xmlfile with iterable as property for datahandler """
148
+        emmodel = EditorialModel("em_test", description = "test model")
149
+        cls1 = emmodel.new_class(   'testclass1',
150
+                                    display_name = "test class 1")
151
+        cls2 = emmodel.new_class(   'testclass2',
152
+                                    display_name = "test class 2")
153
+        cls1f1 = cls1.new_field(    'testfield1',
154
+                                    data_handler = 'varchar')
155
+        cls2f1 = cls2.new_field(    'testfield2',
156
+                                    data_handler = 'list',
157
+                                    allowed_classes = [cls1, cls2])
158
+        cls2f1 = cls2.new_field(    'testfield3',
159
+                                    data_handler = 'varchar',
160
+                                    back_reference = (  'testclass2',
161
+                                                        'testfield1'))
162
+        emmodel.save(xmlfile, filename=self.tmpfile)
163
+        
164
+        emmodel_loaded = xmlfile.load(self.tmpfile)
165
+        self.assertEqual(   emmodel.d_hash(),
166
+                            emmodel_loaded.d_hash())
167
+
168
+    def test_emclass_with_ml_strings(self):
169
+        """ Testing xmlfile mlstring handling in classes"""
170
+        emmodel = EditorialModel("em_test", description = "test model")
171
+        cls1 = emmodel.new_class(   'testclass1',
172
+                                    display_name = {    'eng': "test class 1",
173
+                                                        'fre': 'classe de test 1'})
174
+        emmodel.save(xmlfile, filename=self.tmpfile)
175
+        
176
+        emmodel_loaded = xmlfile.load(self.tmpfile)
177
+        self.assertEqual(   emmodel.d_hash(),
178
+                            emmodel_loaded.d_hash())
179
+
180
+    def test_emfield_with_ml_strings(self):
181
+        """ Testing xmlfile mlstring handling in data handlers """
182
+        emmodel = EditorialModel("em_test", description = "test model")
183
+        cls1 = emmodel.new_class(   'testclass1',
184
+                                    display_name = "test class 1")
185
+        cls1f1 = cls1.new_field(    'testfield1',
186
+                                    display_name = {    'eng': 'test1',
187
+                                                        'fre': 'test1'},
188
+                                    data_handler = 'varchar')
189
+        emmodel.save(xmlfile, filename=self.tmpfile)
190
+        
191
+        emmodel_loaded = xmlfile.load(self.tmpfile)
192
+        self.assertEqual(   emmodel.d_hash(),
193
+                            emmodel_loaded.d_hash())
194
+
195
+    def test_groups_with_ml_strings(self):
196
+        """ Testing xmlfile groups handling """
197
+        emmodel = EditorialModel("em_test", description = "test model")
198
+        emmodel.new_group(  'test_grp',
199
+                            display_name = {    'eng': "Test group",
200
+                                                'fre': "Groupe de test"})
201
+
202
+        emmodel.save(xmlfile, filename=self.tmpfile)
203
+        
204
+        emmodel_loaded = xmlfile.load(self.tmpfile)
205
+        self.assertEqual(   emmodel.d_hash(),
206
+                            emmodel_loaded.d_hash())
207
+
208
+    def test_em_test(self):
209
+        """ Testing xmlfile with the test editorial model """
210
+        emmodel = picklefile.load('tests/editorial_model.pickle')
49 211
 
212
+        emmodel.save(xmlfile, filename=self.tmpfile)
50 213
 
214
+        emmodel_loaded = xmlfile.load(self.tmpfile)
215
+        self.assertEqual(   emmodel.d_hash(),
216
+                            emmodel_loaded.d_hash())

Loading…
Cancel
Save