Skip to content

Commit

Permalink
Merge cf4348f into dc970ca
Browse files Browse the repository at this point in the history
  • Loading branch information
kukirokuk committed Dec 28, 2017
2 parents dc970ca + cf4348f commit bb188d8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
7 changes: 7 additions & 0 deletions buildout.cfg
@@ -1,4 +1,5 @@
[buildout]
extensions = mr.developer
extends = versions.cfg
newest = false
parts = test
Expand All @@ -13,4 +14,10 @@ recipe = zc.recipe.egg:scripts
dependent-scripts = true
eggs =
openregistry.api [test]
openprocurement.schemas.dgf
schematics-flexible
nose

[sources]
schematics-flexible = git https://github.com/openprocurement/schematics-flexible.git
openprocurement.schemas.dgf = git https://github.com/openprocurement/openprocurement.schemas.dgf.git
9 changes: 9 additions & 0 deletions openregistry/api/models/ocds.py
Expand Up @@ -7,6 +7,9 @@
from schematics.exceptions import ValidationError, ConversionError
from schematics.types.compound import ModelType, ListType
from schematics.types.serializable import serializable
from schematics_flexible.schematics_flexible import FlexibleModelType

from openprocurement.schemas.dgf.schemas_store import SchemaStore

from decimal import Decimal, InvalidOperation, ROUND_HALF_UP

Expand Down Expand Up @@ -185,7 +188,13 @@ class Item(Model):
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):
""" 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")

class ContactPoint(Model):
name = StringType(required=True)
Expand Down
47 changes: 46 additions & 1 deletion openregistry/api/tests/blanks/json_data.py
Expand Up @@ -30,6 +30,7 @@
}

test_item_data = {
"id": u"0",
"description": u"футляри до державних нагород",
"classification": {
"scheme": u"CAV",
Expand All @@ -56,6 +57,19 @@
"streetAddress": u"вул. Банкова 1"
}
}
schema_properties = {
u"code": "04000000-8",
u"version": "latest",
u"properties": {
u"totalArea": 200,
u"year": 1998,
u"floor": 3
}
}

test_item_data_with_schema = deepcopy(test_item_data)
test_item_data_with_schema['classification']['id'] = schema_properties['code']
test_item_data_with_schema['schema_properties'] = schema_properties

test_asset_basic_data = {
"title": u"Земля для космодрому",
Expand All @@ -81,7 +95,38 @@
"value": {
"amount": 100,
"currency": u"UAH"
}
}

test_asset_basic_data_with_schema = deepcopy(test_asset_basic_data)
test_asset_basic_data_with_schema['classification']['id'] = schema_properties['code']
test_asset_basic_data_with_schema['schema_properties'] = schema_properties

test_asset_basic_data_with_schema = {
"title": u"Земля для космодрому",
"assetType": "basic",
"assetCustodian": deepcopy(test_organization),
"classification": {
"scheme": u"CAV",
"id": u"04000000-8",
"description": u"Земельні ділянки"
},
"unit": {
"name": u"item",
"code": u"39513200-3"
},
"quantity": 5,
"address": {
"countryName": u"Україна",
"postalCode": "79000",
"region": u"м. Київ",
"locality": u"м. Київ",
"streetAddress": u"вул. Банкова 1"
},
"value": {
"amount": 1001,
"currency": u"UAH"
}
}

test_debt_data = {
Expand All @@ -101,7 +146,7 @@
test_asset_compound_data = deepcopy(test_asset_basic_data)
test_asset_compound_data['assetType'] = 'compound'

test_asset_compound_data['items'] = [test_item_data, test_item_data]
test_asset_compound_data['items'] = [test_item_data_with_schema, test_item_data_with_schema]

test_asset_claimrights_data = deepcopy(test_asset_compound_data)
test_asset_claimrights_data['assetType'] = 'claimRights'
Expand Down
47 changes: 14 additions & 33 deletions openregistry/api/tests/models.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import unittest
import mock
from copy import deepcopy
from datetime import datetime, timedelta
from schematics.exceptions import ConversionError, ValidationError, ModelValidationError
from decimal import Decimal
Expand All @@ -15,6 +16,7 @@
Item, Location, Unit, Value, ItemClassification, Classification,
Period, PeriodEndRequired, Document, DecimalType
)
from openregistry.api.tests.blanks.json_data import test_item_data_with_schema


now = get_now()
Expand Down Expand Up @@ -270,45 +272,24 @@ def test_Location_model(self):

def test_Item_model(self):

data = {
"id": u"0",
"description": u"футляри до державних нагород",
"classification": {
"scheme": u"CAV",
"id": u"32322000-6",
"description": u"Cartons"
},
"additionalClassifications": [
{
"scheme": u"ДКПП",
"id": u"17.21.1",
"description": u"папір і картон гофровані, паперова й картонна тара"
}
],
"unit": {
"name": u"item",
"code": u"39513200-3"
},
"quantity": Decimal('5.001'),
"address": {
"countryName": u"Україна",
"postalCode": "79000",
"region": u"м. Київ",
"locality": u"м. Київ",
"streetAddress": u"вул. Банкова 1"
}
}
item = Item(data)
item = Item(test_item_data_with_schema)
item.validate()
self.assertEqual(item.serialize()['schema_properties']['properties'], test_item_data_with_schema['schema_properties']['properties'])
self.assertEqual(item.serialize()['schema_properties']['code'][0:2], test_item_data_with_schema['schema_properties']['code'][:2])
self.assertEqual(item.serialize()['description'], test_item_data_with_schema['description'])
self.assertEqual(item.serialize()['classification'], test_item_data_with_schema['classification'])
self.assertEqual(item.serialize()['additionalClassifications'], test_item_data_with_schema['additionalClassifications'])
self.assertEqual(item.serialize()['address'], test_item_data_with_schema['address'])
self.assertEqual(item.serialize()['id'], test_item_data_with_schema['id'])
self.assertEqual(item.serialize()['unit'], test_item_data_with_schema['unit'])
self.assertEqual(item.serialize()['quantity'], test_item_data_with_schema['quantity'])

self.assertEqual(item.serialize(), data)
with self.assertRaisesRegexp(ValueError, 'Item Model has no role "test"'):
item.serialize('test')

data['location'] = {'latitude': '123', 'longitude': '567'}
item2 = Item(data)
test_item_data_with_schema['location'] = {'latitude': '123', 'longitude': '567'}
item2 = Item(test_item_data_with_schema)
item2.validate()
self.assertEqual(item2.serialize(), data)

self.assertNotEqual(item, item2)
item2.location = None
Expand Down

0 comments on commit bb188d8

Please sign in to comment.