From 9b2936c7951a4a122ca744a79d49f97c25180681 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 3 Aug 2021 16:40:17 +0200 Subject: [PATCH] adding weight to trade documents --- ...c1897ce_adding_weight_to_tradedocuments.py | 31 ++++++++++++++++++ .../resources/tradedocument/models.py | 2 ++ .../resources/tradedocument/schemas.py | 3 +- tests/test_documents.py | 32 +++++++++++++++++-- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py diff --git a/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py b/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py new file mode 100644 index 00000000..bb5d4b6f --- /dev/null +++ b/ereuse_devicehub/migrations/versions/3ac2bc1897ce_adding_weight_to_tradedocuments.py @@ -0,0 +1,31 @@ +"""adding weight to tradedocuments + +Revision ID: 3ac2bc1897ce +Revises: 3a3601ac8224 +Create Date: 2021-08-03 16:28:38.719686 + +""" +from alembic import op +import sqlalchemy as sa +from alembic import context + + +# revision identifiers, used by Alembic. +revision = '3ac2bc1897ce' +down_revision = '0103a9c96b2d' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + +def upgrade(): + op.add_column("trade_document", sa.Column("weight", sa.Float(decimal_return_scale=2), nullable=True), schema=f'{get_inv()}') + + +def downgrade(): + op.drop_column('trade_document', 'weight', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 9c743244..e1326613 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -71,6 +71,8 @@ class TradeDocument(Thing): file_hash.comment = """This is the hash of the file produced from frontend.""" url = db.Column(URL()) url.comment = """This is the url where resides the document.""" + weight = db.Column(db.Float(nullable=True)) + weight.comment = """This is the weight of one container than this document express.""" __table_args__ = ( db.Index('document_id', id, postgresql_using='hash'), diff --git a/ereuse_devicehub/resources/tradedocument/schemas.py b/ereuse_devicehub/resources/tradedocument/schemas.py index fab95dc4..cfa74637 100644 --- a/ereuse_devicehub/resources/tradedocument/schemas.py +++ b/ereuse_devicehub/resources/tradedocument/schemas.py @@ -1,4 +1,4 @@ -from marshmallow.fields import DateTime, Integer, validate +from marshmallow.fields import DateTime, Integer, Float, validate from teal.marshmallow import SanitizedStr, URL # from marshmallow import ValidationError, validates_schema @@ -29,3 +29,4 @@ class TradeDocument(Thing): url = URL(description=m.TradeDocument.url.comment) lot = NestedOn('Lot', only_query='id', description=m.TradeDocument.lot.__doc__) trading = SanitizedStr(dump_only=True, description='') + weight = Float(required=False, description=m.TradeDocument.weight.comment) diff --git a/tests/test_documents.py b/tests/test_documents.py index 6cc77e38..b227f280 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -16,7 +16,7 @@ from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.resources.user.models import Session from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Live from ereuse_devicehub.resources.documents import documents -from ereuse_devicehub.resources.tradedocument.definitions import TradeDocumentDef +from ereuse_devicehub.resources.tradedocument.models import TradeDocument from ereuse_devicehub.resources.device import models as d from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag @@ -682,4 +682,32 @@ def test_get_wbconf(user: UserClient): def test_trade_documents(user: UserClient): """Tests upload one document""" - doc, _ = user.post(res=TradeDocumentDef.t, item='wbconf/usodyrate', accept=ANY) + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + request_post = { + 'filename': 'test.pdf', + 'hash': 'bbbbbbbb', + 'url': 'http://www.ereuse.org/', + 'lot': lot['id'] + } + doc, _ = user.post(res=TradeDocument, data=request_post) + assert doc['filename'] == request_post['filename'] + assert doc['url'] == request_post['url'] + assert doc['hash'] == request_post['hash'] + assert doc['lot']['name'] == lot['name'] + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_documents_with_weight(user: UserClient): + """Tests upload one document""" + + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + request_post = { + 'filename': 'test.pdf', + 'hash': 'bbbbbbbb', + 'url': 'http://www.ereuse.org/', + 'weight': 15, + 'lot': lot['id'] + } + doc, _ = user.post(res=TradeDocument, data=request_post) + assert doc['weight'] == request_post['weight']