Browse Source

Add the two files for testing leobject package (that I forget to add in previous commit -_- )

Yann Weber 9 years ago
parent
commit
b257d6e16d
2 changed files with 156 additions and 0 deletions
  1. 125
    0
      leobject/test/test_lefactory.py
  2. 31
    0
      leobject/test/utils.py

+ 125
- 0
leobject/test/test_lefactory.py View File

1
+import unittest
2
+from unittest import TestCase
3
+
4
+import tempfile
5
+import sys
6
+import shutil
7
+
8
+import EditorialModel
9
+import leobject
10
+import leobject.test.utils
11
+from leobject.lefactory import LeFactory
12
+
13
+class TestLeFactorySyntax(TestCase):
14
+
15
+    def test_generated_code_syntax(self):
16
+        py = LeFactory.generate_python(**leobject.test.utils.genepy_args)
17
+        pyc = compile(py, "dyn.py", 'exec')
18
+        exec(pyc, globals())
19
+
20
+class TestLeFactory(TestCase):
21
+    
22
+    @classmethod
23
+    def setUpClass(cls):
24
+        """ Write the generated code in a temporary directory and import it """
25
+        cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
26
+    @classmethod
27
+    def tearDownClass(cls):
28
+        """ Remove the temporary directory created at class setup """
29
+        leobject.test.utils.cleanup(cls.tmpdir)
30
+
31
+    def setUp(self):
32
+        backend=leobject.test.utils.genepy_args['backend_cls'](**leobject.test.utils.genepy_args['backend_args'])
33
+        self.model = EditorialModel.model.Model(backend = backend)
34
+
35
+    def test_leobject(self):
36
+        """ Testing the generated LeObject class """
37
+        import dyncode
38
+        self.assertTrue(hasattr(dyncode, 'LeObject'))
39
+
40
+        for uid, cls in dyncode.LeObject._me_uid.items():
41
+            if leobject.letype.LeType in cls.__bases__:
42
+                self.assertNotEqual(cls, leobject.letype.LeType)
43
+                self.assertEqual(cls._type_id, uid)
44
+            elif leobject.leclass.LeClass in cls.__bases__:
45
+                self.assertNotEqual(cls, leobject.leclass.LeClass)
46
+                self.assertEqual(cls._class_id, uid)
47
+            else:
48
+                self.fail("Bad instance type for _me_uid values : %s"%type(cls))
49
+
50
+    ## @todo Testing _fieldtypes attribute but we need an __hash__ method on fieldtypes
51
+    def test_leclass(self):
52
+        """ Testing generated LeClass childs classes """
53
+        import dyncode
54
+
55
+        for emclass in self.model.components(EditorialModel.classes.EmClass):
56
+            leclass_name = LeFactory.name2classname(emclass.name)
57
+            self.assertTrue(hasattr(dyncode, leclass_name))
58
+
59
+            leclass = getattr(dyncode, leclass_name)
60
+            self.assertEqual(leclass._class_id, emclass.uid)
61
+            
62
+            #Testing inheritance
63
+            self.assertEqual(set(leclass.__bases__), set([dyncode.LeObject, leobject.leclass.LeClass]))
64
+            
65
+            #Testing _fieldgroups attr
66
+            self.assertEqual(
67
+                set([ fg.name for fg in emclass.fieldgroups()]),
68
+                set(leclass._fieldgroups.keys())
69
+            )
70
+            for fgroup in emclass.fieldgroups():
71
+                self.assertEqual(
72
+                    set([ f.name for f in fgroup.fields()]),
73
+                    set(leclass._fieldgroups[fgroup.name])
74
+                )
75
+            
76
+            #Testing _linked_types attr
77
+            self.assertEqual(
78
+                set([ LeFactory.name2classname(lt.name) for lt in emclass.linked_types()]),
79
+                set([ t.__name__ for t in leclass._linked_types ])
80
+            )
81
+
82
+            #Testing fieldtypes
83
+            self.assertEqual(
84
+                set([ f.name for f in emclass.fields()]),
85
+                set(leclass._fieldtypes.keys())
86
+            )
87
+            for field in emclass.fields():
88
+                self.assertEqual(
89
+                    hash(field.fieldtype_instance()),
90
+                    hash(leclass._fieldtypes[field.name])
91
+                )
92
+
93
+
94
+    def test_letype(self):
95
+        """ Testing generated LeType childs classes """
96
+        import dyncode
97
+
98
+        for emtype in self.model.components(EditorialModel.types.EmType):
99
+            letype_name = LeFactory.name2classname(emtype.name)
100
+            self.assertTrue(hasattr(dyncode, letype_name))
101
+
102
+            letype = getattr(dyncode, letype_name)
103
+            self.assertEqual(letype._type_id, emtype.uid)
104
+            self.assertEqual(letype._leclass._class_id, emtype.em_class.uid)
105
+
106
+            #Testing inheritance
107
+            self.assertEqual(
108
+                set(letype.__bases__),
109
+                set([leobject.letype.LeType, letype._leclass])
110
+            )
111
+
112
+            #Testing _fields
113
+            self.assertEqual(
114
+                set([ f.name for f in emtype.fields() ]),
115
+                set(letype._fields)
116
+            )
117
+
118
+            #Testing superiors
119
+            for nat, sups in emtype.superiors().items():
120
+                self.assertIn(nat, letype._superiors.keys())
121
+                self.assertEqual(
122
+                    set([ s.__name__ for s in letype._superiors[nat]]),
123
+                    set([ LeFactory.name2classname(s.name) for s in sups])
124
+                )
125
+

+ 31
- 0
leobject/test/utils.py View File

1
+import tempfile
2
+import shutil
3
+import sys
4
+
5
+import EditorialModel
6
+import leobject
7
+from EditorialModel.backend.json_backend import EmBackendJson
8
+from leobject.datasources.dummy import DummyDatasource
9
+from leobject.lefactory import LeFactory
10
+
11
+
12
+
13
+genepy_args = {
14
+    'backend_cls': EmBackendJson,
15
+    'backend_args': {'json_file': 'EditorialModel/test/me.json'},
16
+    'datasource_cls': DummyDatasource,
17
+    'datasource_args': {}
18
+}
19
+
20
+def tmp_load_factory_code(name='dyncode'):
21
+    tmpdir = tempfile.mkdtemp('_lodel2_test_dyncode')
22
+    fname = tmpdir+'/%s.py'%name
23
+    with open(fname, 'w+') as dynfp:
24
+        dynfp.write(LeFactory.generate_python(**genepy_args))
25
+    sys.path.append(tmpdir)
26
+    LeFactory.modname = name
27
+    return tmpdir
28
+
29
+
30
+def cleanup(tmpdir):
31
+    shutil.rmtree(tmpdir)

Loading…
Cancel
Save