Ver código fonte

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 anos atrás
pai
commit
06bd3b141d

+ 2
- 1
EditorialModel/backend/graphviz.py Ver arquivo

@@ -6,8 +6,9 @@ from EditorialModel.classtypes import EmClassType
6 6
 from EditorialModel.fieldgroups import EmFieldGroup
7 7
 from EditorialModel.types import EmType
8 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 13
     ## @brief Constructor
13 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 Ver arquivo

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

+ 2
- 1
EditorialModel/migrationhandler/django.py Ver arquivo

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

+ 13
- 3
EditorialModel/model.py Ver arquivo

@@ -28,8 +28,15 @@ class Model(object):
28 28
     #
29 29
     # @param backend unknown: A backend object instanciated from one of the classes in the backend module
30 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 40
         self._components = {'uids': {}, 'EmClass': [], 'EmType': [], 'EmField': [], 'EmFieldGroup': []}
34 41
         self.load()
35 42
 
@@ -226,7 +233,10 @@ class Model(object):
226 233
     #
227 234
     # @param backend unknown: A backend object
228 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 241
     ## Returns a list of all the EmClass objects of the model
232 242
     def classes(self):

+ 12
- 10
EditorialModel/test/test_model.py Ver arquivo

@@ -29,6 +29,15 @@ class TestModel(unittest.TestCase):
29 29
         model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('LodelTestInstance', debug=False, dryrun=True))
30 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 41
     def test_components(self):
33 42
         """ Test components fetching """
34 43
         uid_l = list()
@@ -196,12 +205,12 @@ class TestModel(unittest.TestCase):
196 205
     def test_set_backend(self):
197 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 209
             self.me.set_backend(backend)
201 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 214
                 self.me.set_backend(bad_backend)
206 215
     ##
207 216
     # @todo Test selected fields application
@@ -287,11 +296,4 @@ class TestModel(unittest.TestCase):
287 296
         for cls in [EmComponent, int, str]:
288 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
 

Carregando…
Cancelar
Salvar