1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-03-17 08:42:01 +01:00

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
This commit is contained in:
Yann 2015-11-04 10:40:36 +01:00
commit cb7a37e7a2
3 changed files with 43 additions and 3 deletions

View file

@ -103,14 +103,28 @@ class EmField(EmComponent):
return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
## Check if the EmField is valid
#
# Check multiple things :
# - the fieldgroup_id is valid
# - the name is uniq in the EmClass
# - if its a rel2type check that the linked_type is uniq in the EmClass
# @return True if valid False if not
def check(self):
super(EmField, self).check()
#Fieldgroup check
em_fieldgroup = self.model.component(self.fieldgroup_id)
if not em_fieldgroup:
raise EmComponentCheckError("fieldgroup_id contains a non existing uid : '%d'" % self.fieldgroup_id)
if not isinstance(em_fieldgroup, EditorialModel.fieldgroups.EmFieldGroup):
raise EmComponentCheckError("fieldgroup_id contains an uid from a component that is not an EmFieldGroup but a %s" % str(type(em_fieldgroup)))
#Uniq Name check
if len([ f for f in self.em_class.fields() if f.name == self.name]) > 1:
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))
#rel2type uniq check
if self.fieldtype == 'rel2type':
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:
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))
## @brief Delete a field if it's not linked
# @return bool : True if deleted False if deletion aborded

View file

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

View file

@ -4,6 +4,7 @@ from unittest import TestCase
from EditorialModel.fields import EmField
from EditorialModel.model import Model
from EditorialModel.backend.json_backend import EmBackendJson
from EditorialModel.exceptions import EmComponentCheckError
EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
EM_TEST_OBJECT = None
@ -62,6 +63,30 @@ class TestField(FieldTestCase):
with self.assertRaises(ValueError, msg="Only common_fields should be internal='object'"):
field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testbadinternal','internal': 'object', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
def test_double_rel2type(self):
""" Test the rel2type unicity """
em = EM_TEST_OBJECT
emtype = em.components('EmType')[0]
emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
f1 = em.create_component('EmField', {'name': 'testr2t', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
with self.assertRaises(EmComponentCheckError):
f2 = em.create_component('EmField', {'name': 'testr2t2', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'rel2type', 'rel_to_type_id': emtype.uid})
def test_same_name(self):
""" Test the name unicity is the same EmClass"""
em = EM_TEST_OBJECT
emtype = em.components('EmType')[0]
emclass = [c for c in em.components('EmClass') if c != emtype.em_class][0]
f1 = em.create_component('EmField', {'name': 'samename', 'fieldgroup_id': emclass.fieldgroups()[0].uid, 'fieldtype': 'char'})
with self.assertRaises(EmComponentCheckError):
f2 = em.create_component('EmField', {'name': 'samename', 'fieldgroup_id': emclass.fieldgroups()[1].uid, 'fieldtype': 'integer'} )
## Test_Deletion
#
# tests the deletion process of a field