Browse Source

Add checks in EmField, add tests for EmField + bugfix in check in Model.create_component

Checks for EmField :
- checks that the name is uniq in an EmClass
- checks that a relation EmClass <- rel2type -> EmType is uniq
Yann Weber 9 years ago
parent
commit
cb7a37e7a2
3 changed files with 43 additions and 3 deletions
  1. 14
    0
      EditorialModel/fields.py
  2. 4
    3
      EditorialModel/model.py
  3. 25
    0
      EditorialModel/test/test_field.py

+ 14
- 0
EditorialModel/fields.py View File

@@ -103,14 +103,28 @@ class EmField(EmComponent):
103 103
         return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
104 104
 
105 105
     ## Check if the EmField is valid
106
+    # 
107
+    # Check multiple things :
108
+    # - the fieldgroup_id is valid
109
+    # - the name is uniq in the EmClass
110
+    # - if its a rel2type check that the linked_type is uniq in the EmClass
106 111
     # @return True if valid False if not
107 112
     def check(self):
108 113
         super(EmField, self).check()
114
+        #Fieldgroup check
109 115
         em_fieldgroup = self.model.component(self.fieldgroup_id)
110 116
         if not em_fieldgroup:
111 117
             raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
112 118
         if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
113 119
             raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
120
+        #Uniq Name check
121
+        if len([ f for f in self.em_class.fields() if f.name == self.name]) > 1:
122
+            raise EmComponentCheckError("The field %d has a name '%s' that is not uniq in the EmClass %d"%(self.uid, self.name, self.em_class.uid))
123
+        #rel2type uniq check
124
+        if self.fieldtype == 'rel2type':
125
+            if len([f for f in self.em_class.fields() if f.fieldtype == 'rel2type' and f.rel_to_type_id == self.rel_to_type_id]) > 1:
126
+                raise EmComponentCheckError("The rel2type %d is not uniq, another field is linked to the same type '%s' in the same class '%s'"%(self.uid, self.model.component(self.rel_to_type_id).name, self.em_class.name))
127
+
114 128
 
115 129
     ## @brief Delete a field if it's not linked
116 130
     # @return bool : True if deleted False if deletion aborded

+ 4
- 3
EditorialModel/model.py View File

@@ -213,11 +213,12 @@ class Model(object):
213 213
 
214 214
         self.migration_handler.register_model_state(self, hash(self))
215 215
 
216
-        if uid is None and component_type == 'EmClass':
217
-            # !!! If uid is not None it means that we shouldn't create components automatically !!!
218
-            self.add_default_class_fields(em_component.uid)
216
+        if uid is None:
219 217
             #Checking the component
220 218
             em_component.check()
219
+            if component_type == 'EmClass':
220
+               # !!! If uid is not None it means that we shouldn't create components automatically !!!
221
+                self.add_default_class_fields(em_component.uid)
221 222
 
222 223
         return em_component
223 224
 

+ 25
- 0
EditorialModel/test/test_field.py View File

@@ -4,6 +4,7 @@ from unittest import TestCase
4 4
 from EditorialModel.fields import EmField
5 5
 from EditorialModel.model import Model
6 6
 from EditorialModel.backend.json_backend import EmBackendJson
7
+from EditorialModel.exceptions import EmComponentCheckError
7 8
 
8 9
 EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
9 10
 EM_TEST_OBJECT = None
@@ -62,6 +63,30 @@ class TestField(FieldTestCase):
62 63
         with self.assertRaises(ValueError, msg="Only common_fields should be internal='object'"):
63 64
             field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testbadinternal','internal': 'object', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
64 65
 
66
+    def test_double_rel2type(self):
67
+        """ Test the rel2type unicity """
68
+        em = EM_TEST_OBJECT
69
+        emtype = em.components('EmType')[0]
70
+        emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
71
+
72
+        f1 = em.create_component('EmField', {'name': 'testr2t', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
73
+
74
+        with self.assertRaises(EmComponentCheckError):
75
+            f2 = em.create_component('EmField', {'name': 'testr2t2', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
76
+
77
+    def test_same_name(self):
78
+        """ Test the name unicity is the same EmClass"""
79
+        em = EM_TEST_OBJECT
80
+        emtype = em.components('EmType')[0]
81
+        emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
82
+
83
+        f1 = em.create_component('EmField', {'name': 'samename', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'char'})
84
+
85
+        with self.assertRaises(EmComponentCheckError):
86
+            f2 = em.create_component('EmField', {'name': 'samename', 'fieldgroup_id': emclass.fieldgroups()[1].uid, 'fieldtype': 'integer'} )
87
+
88
+        
89
+
65 90
     ## Test_Deletion
66 91
     #
67 92
     # tests the deletion process of a field

Loading…
Cancel
Save