コード例 #1
0
    def setUp(self):
        classification_id = u"0341"
        version = '001'

        store = SchemaStore()
        store.load()
        schema_tuple = store.get_schema(classification_id, version)
        self.schema_tuple = schema_tuple
        self.schema = schema_tuple.schema
コード例 #2
0
class Asset(BaseAsset):
    value = ModelType(Value)
    classification = ModelType(ItemClassification, required=True)
    additionalClassifications = ListType(ModelType(Classification), default=list())
    unit = ModelType(Unit)  # Description of the unit which the good comes in e.g. hours, kilograms
    quantity = DecimalType()  # The number of units required
    address = ModelType(Address)
    location = ModelType(Location)
    schema_properties = FlexibleModelType(SchemaStore())

    def validate_schema_properties(self, data, new_schema_properties):
        validate_schema_properties(data, new_schema_properties)
コード例 #3
0
class Item(BaseItem):
    """A good, service, or work to be contracted."""
    class Options:
        roles = {
            'create':
            blacklist('deliveryLocation', 'deliveryAddress', 'deliveryDate'),
            'edit_active.tendering':
            blacklist('deliveryLocation', 'deliveryAddress', 'deliveryDate'),
        }

    classification = ModelType(CAVClassification, required=True)
    additionalClassifications = ListType(ModelType(Classification),
                                         default=list())
    address = ModelType(Address)
    location = ModelType(Location)
    schema_properties = FlexibleModelType(SchemaStore())

    def validate_schema_properties(self, data, new_schema_properties):
        """ Raise validation error if code in schema_properties mismatch
            with classification id """
        if new_schema_properties and not data['classification'][
                'id'].startswith(new_schema_properties['code']):
            raise ValidationError(
                "classification id mismatch with schema_properties code")
    def test_validation(self):
        store = SchemaStore()
        store.load()
        schema_tuple = store.get_schema(u'04000000-8', u'latest')

        exc = None

        test_properties = {}
        try:
            exc = None
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(e.message, '{} is a required property'.format("u'totalArea'"))

        test_properties = {u'totalArea': 200}

        # Test constructionTechnology
        available_value = [u"monolithicFrame", u"panel", u"insulatedPanel", u"brick", u"other"]
        for i in range(len(available_value)):
            for ct in combinations(available_value, i):
                test_properties[u'constructionTechnology'] = list(ct)
                schema_tuple.schema.validate(test_properties)


        try:
            test_properties[u'constructionTechnology'] = [u"wrong_value"]
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'constructionTechnology')
        self.assertEqual(e.message, 'u\'{}\' is not one of {}'.format(u"wrong_value", available_value))

        try:
            exc = None
            test_properties[u'constructionTechnology'] = [u"panel", u"panel"]
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'constructionTechnology')
        self.assertEqual(e.message, '{} has non-unique elements'.format(test_properties[u'constructionTechnology']))

        test_properties[u'totalArea'] = -200
        test_properties[u'constructionTechnology'] = []

        # Test if totalArea less than 0
        try:
            exc = None
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'totalArea')
        self.assertEqual(e.message, '{} is less than the minimum of 0'.format(test_properties[u'totalArea']))

        test_properties[u'totalArea'] = 0
        test_properties[u'livingArea'] = 0
        test_properties[u'kitchenArea'] = 0
        test_properties[u'landArea'] = 0
        schema_tuple.schema.validate(test_properties)

        # Test if livingArea less than 0
        test_properties[u'livingArea'] = -200
        try:
            exc = None
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'livingArea')
        self.assertEqual(e.message, '{} is less than the minimum of 0'.format(test_properties[u'livingArea']))

        # Test if kitchenArea less than 0
        test_properties[u'livingArea'] = 0
        test_properties[u'kitchenArea'] = -200
        try:
            exc = None
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'kitchenArea')
        self.assertEqual(e.message, '{} is less than the minimum of 0'.format(test_properties[u'kitchenArea']))

        # Test if landArea less than 0
        test_properties[u'kitchenArea'] = 0
        test_properties[u'landArea'] = -200
        try:
            exc = None
            schema_tuple.schema.validate(test_properties)
        except ValidationError as e:
            exc = e
        self.assertNotEqual(exc, None)
        self.assertEqual(exc.relative_path[0], u'landArea')
        self.assertEqual(e.message, '{} is less than the minimum of 0'.format(test_properties[u'landArea']))

        # Test if landArea less than 0
        test_properties[u'landArea'] = 0
        test_properties[u'floor'] = -200
        schema_tuple.schema.validate(test_properties)

        test_properties[u'floor'] = 200
        schema_tuple.schema.validate(test_properties)