Browse Source

Model construct enhancement with backend and MH inheritance

Every backend and MH are now subclasses of dummy ones. This allow to check the type with issubclass.
Yann Weber 9 years ago
parent
commit
06bd3b141d

+ 2
- 1
EditorialModel/backend/graphviz.py View File

6
 from EditorialModel.fieldgroups import EmFieldGroup
6
 from EditorialModel.fieldgroups import EmFieldGroup
7
 from EditorialModel.types import EmType
7
 from EditorialModel.types import EmType
8
 from Lodel.utils.mlstring import MlString
8
 from Lodel.utils.mlstring import MlString
9
+from EditorialModel.backend.dummy_backend import EmBackendDummy
9
 
10
 
10
-class EmBackendGraphviz(object):
11
+class EmBackendGraphviz(EmBackendDummy):
11
 
12
 
12
     ## @brief Constructor
13
     ## @brief Constructor
13
     # @param dot_fname str : The filename where we want to save the dot repr of the EM
14
     # @param dot_fname str : The filename where we want to save the dot repr of the EM

+ 2
- 1
EditorialModel/backend/json_backend.py View File

9
 import json
9
 import json
10
 import datetime
10
 import datetime
11
 from Lodel.utils.mlstring import MlString
11
 from Lodel.utils.mlstring import MlString
12
+from EditorialModel.backend.dummy_backend import EmBackendDummy
12
 
13
 
13
 
14
 
14
 def date_cast(date):
15
 def date_cast(date):
31
 
32
 
32
 
33
 
33
 ## Manages a Json file based backend structure
34
 ## Manages a Json file based backend structure
34
-class EmBackendJson(object):
35
+class EmBackendJson(EmBackendDummy):
35
 
36
 
36
     cast_methods = {
37
     cast_methods = {
37
         'uid': int,
38
         'uid': int,

+ 2
- 1
EditorialModel/migrationhandler/django.py View File

7
 from django.db import models
7
 from django.db import models
8
 from django.db.models.loading import cache as django_cache
8
 from django.db.models.loading import cache as django_cache
9
 from django.core.exceptions import ValidationError
9
 from django.core.exceptions import ValidationError
10
+from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
10
 
11
 
11
 from EditorialModel.exceptions import *
12
 from EditorialModel.exceptions import *
12
 
13
 
64
 #
65
 #
65
 # Create django models according to the editorial model
66
 # Create django models according to the editorial model
66
 
67
 
67
-class DjangoMigrationHandler(object):
68
+class DjangoMigrationHandler(DummyMigrationHandler):
68
 
69
 
69
     ## @brief Instanciate a new DjangoMigrationHandler
70
     ## @brief Instanciate a new DjangoMigrationHandler
70
     # @param app_name str : The django application name for models generation
71
     # @param app_name str : The django application name for models generation

+ 13
- 3
EditorialModel/model.py View File

28
     #
28
     #
29
     # @param backend unknown: A backend object instanciated from one of the classes in the backend module
29
     # @param backend unknown: A backend object instanciated from one of the classes in the backend module
30
     def __init__(self, backend, migration_handler=None):
30
     def __init__(self, backend, migration_handler=None):
31
-        self.migration_handler = DummyMigrationHandler() if migration_handler is None else migration_handler
32
-        self.backend = backend
31
+        if migration_handler is None:
32
+            self.migration_handler = DummyMigrationHandler()
33
+        elif issubclass(migration_handler.__class__, DummyMigrationHandler):
34
+            self.migration_handler = migration_handler
35
+        else:
36
+            raise TypeError("migration_handler should be an instance from a subclass of DummyMigrationhandler")
37
+        self.backend = None
38
+        self.set_backend(backend)
39
+
33
         self._components = {'uids': {}, 'EmClass': [], 'EmType': [], 'EmField': [], 'EmFieldGroup': []}
40
         self._components = {'uids': {}, 'EmClass': [], 'EmType': [], 'EmField': [], 'EmFieldGroup': []}
34
         self.load()
41
         self.load()
35
 
42
 
226
     #
233
     #
227
     # @param backend unknown: A backend object
234
     # @param backend unknown: A backend object
228
     def set_backend(self, backend):
235
     def set_backend(self, backend):
229
-        self.backend = backend
236
+        if issubclass(backend.__class__, EmBackendDummy):
237
+            self.backend = backend
238
+        else:
239
+            raise TypeError('Backend should be an instance of a EmBackednDummy subclass')
230
 
240
 
231
     ## Returns a list of all the EmClass objects of the model
241
     ## Returns a list of all the EmClass objects of the model
232
     def classes(self):
242
     def classes(self):

+ 12
- 10
EditorialModel/test/test_model.py View File

29
         model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('LodelTestInstance', debug=False, dryrun=True))
29
         model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('LodelTestInstance', debug=False, dryrun=True))
30
         self.assertTrue(isinstance(model, Model))
30
         self.assertTrue(isinstance(model, Model))
31
 
31
 
32
+    def test_bad_init(self):
33
+        """ Test initialisation with bad arguments """
34
+        for bad in [ None, int, EmBackendDummy, DummyMigrationHandler, 'foobar' ]:
35
+            with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a bad backend"):
36
+                Model(bad)
37
+        for bad in [ int, EmBackendDummy, DummyMigrationHandler, 'foobar' ]:
38
+            with self.assertRaises(TypeError, msg="Tried to instanciate a Model with a migration_handler"):
39
+                Model(EmBackendDummy(), bad)
40
+
32
     def test_components(self):
41
     def test_components(self):
33
         """ Test components fetching """
42
         """ Test components fetching """
34
         uid_l = list()
43
         uid_l = list()
196
     def test_set_backend(self):
205
     def test_set_backend(self):
197
         """ Test the set_backend method """
206
         """ Test the set_backend method """
198
 
207
 
199
-        for backend in [ None, EmBackendJson('EditorialModel/test/me.json'), EmBackendDummy() ]:
208
+        for backend in [ EmBackendJson('EditorialModel/test/me.json'), EmBackendDummy() ]:
200
             self.me.set_backend(backend)
209
             self.me.set_backend(backend)
201
             self.assertEqual(self.me.backend, backend)
210
             self.assertEqual(self.me.backend, backend)
202
 
211
 
203
-        for bad_backend in ['wow', int, EmBackendJson ]:
204
-            with self.assertRaises(AttributeError, msg="But bad argument (%s %s) was given"%(type(bad_backend),bad_backend)):
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)):
205
                 self.me.set_backend(bad_backend)
214
                 self.me.set_backend(bad_backend)
206
     ##
215
     ##
207
     # @todo Test selected fields application
216
     # @todo Test selected fields application
287
         for cls in [EmComponent, int, str]:
296
         for cls in [EmComponent, int, str]:
288
             self.assertFalse(Model.name_from_emclass(cls))
297
             self.assertFalse(Model.name_from_emclass(cls))
289
 
298
 
290
-    def test_load_save_invalid(self):
291
-        """ Test the behavior of a Model when no backend given but load and save are called """
292
-        bad_em = Model(None)
293
-        
294
-        self.assertFalse(bad_em.load())
295
-        self.assertFalse(bad_em.save())
296
-            
297
 
299
 

Loading…
Cancel
Save