From 5510417125dcab2d269f875564543965db8c3752 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 18 Nov 2021 11:48:57 +0100 Subject: [PATCH 1/4] reduce latency for timeout for lots --- ereuse_devicehub/resources/lot/schemas.py | 31 ++++++++++++-- tests/test_device.py | 6 ++- tests/test_lot.py | 50 ++++++++++++----------- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/ereuse_devicehub/resources/lot/schemas.py b/ereuse_devicehub/resources/lot/schemas.py index 5604cc2e..9a5a58aa 100644 --- a/ereuse_devicehub/resources/lot/schemas.py +++ b/ereuse_devicehub/resources/lot/schemas.py @@ -5,14 +5,32 @@ from ereuse_devicehub.marshmallow import NestedOn from ereuse_devicehub.resources.deliverynote import schemas as s_deliverynote from ereuse_devicehub.resources.device import schemas as s_device from ereuse_devicehub.resources.action import schemas as s_action -from ereuse_devicehub.resources.tradedocument import schemas as s_document from ereuse_devicehub.resources.enums import TransferState from ereuse_devicehub.resources.lot import models as m from ereuse_devicehub.resources.models import STR_SIZE from ereuse_devicehub.resources.schemas import Thing -class Lot(Thing): +TRADE_VALUES = ( + 'id', + 'user_from.email', + 'user_to.email', + 'user_from.id', + 'user_to.id', + 'user_to.code', + 'user_from.code' +) + + +DOCUMENTS_VALUES = ( + 'id', + 'file_name', + 'total_weight', + 'trading' +) + + +class Old_Lot(Thing): id = f.UUID(dump_only=True) name = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True) description = SanitizedStr(description=m.Lot.description.comment) @@ -29,4 +47,11 @@ class Lot(Thing): receiver_address = SanitizedStr(validate=f.validate.Length(max=42)) deliverynote = NestedOn(s_deliverynote.Deliverynote, dump_only=True) documents = NestedOn('TradeDocument', many=True, dump_only=True) - trade = NestedOn(s_action.Trade, dump_only=True) + + +class Lot(Thing): + id = f.UUID(dump_only=True) + name = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True) + description = SanitizedStr(description=m.Lot.description.comment) + trade = f.Nested(s_action.Trade, dump_only=True, only=TRADE_VALUES) + documents = f.Nested('TradeDocument', many=True, dump_only=True, only=DOCUMENTS_VALUES) diff --git a/tests/test_device.py b/tests/test_device.py index 3ffef3a9..7ea400a0 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -506,7 +506,8 @@ def test_get_devices_permissions(app: Devicehub, user: UserClient, user2: UserCl @pytest.mark.mvp -def test_get_devices_unassigned(app: Devicehub, user: UserClient): +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_get_devices_unassigned(user: UserClient): """Checks GETting multiple devices.""" user.post(file('asus-eee-1000h.snapshot.11'), res=m.Snapshot) @@ -529,7 +530,8 @@ def test_get_devices_unassigned(app: Devicehub, user: UserClient): res=Lot, item='{}/devices'.format(my_lot['id']), query=[('id', device_id)]) - assert lot['devices'][0]['id'] == device_id, 'Lot contains device' + lot = Lot.query.filter_by(id=lot['id']).one() + assert next(iter(lot.devices)).id == device_id url = '/devices/?filter={"type":["Computer"]}&unassign=0' diff --git a/tests/test_lot.py b/tests/test_lot.py index 3f1f0731..44889825 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -313,7 +313,6 @@ def test_post_get_lot(user: UserClient): assert l['name'] == 'Foo' l, _ = user.get(res=Lot, item=l['id']) assert l['name'] == 'Foo' - assert not l['children'] def test_lot_post_add_children_view_ui_tree_normal(user: UserClient): @@ -355,38 +354,40 @@ def test_lot_post_add_children_view_ui_tree_normal(user: UserClient): @pytest.mark.mvp -def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_lot_post_add_remove_device_view(user: UserClient): """Tests adding a device to a lot using POST and removing it with DELETE. """ # todo check with components - with app.app_context(): - g.user = User.query.one() - device = Desktop(serial_number='foo', - model='bar', - manufacturer='foobar', - chassis=ComputerChassis.Lunchbox, - owner_id=user.user['id']) - db.session.add(device) - db.session.commit() - device_id = device.id - devicehub_id = device.devicehub_id + g.user = User.query.one() + device = Desktop(serial_number='foo', + model='bar', + manufacturer='foobar', + chassis=ComputerChassis.Lunchbox, + owner_id=user.user['id']) + db.session.add(device) + db.session.commit() + device_id = device.id + devicehub_id = device.devicehub_id parent, _ = user.post(({'name': 'lot'}), res=Lot) lot, _ = user.post({}, res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)]) - assert lot['devices'][0]['id'] == device_id, 'Lot contains device' - device, _ = user.get(res=Device, item=devicehub_id) - assert len(device['lots']) == 1 - assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot' + lot = Lot.query.filter_by(id=lot['id']).one() + assert list(lot.devices)[0].id == device_id, 'Lot contains device' + device = Device.query.filter_by(devicehub_id=devicehub_id).one() + assert len(device.lots) == 1 + # assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot' + assert list(device.lots)[0].id == lot.id, 'Device is inside lot' # Remove the device - lot, _ = user.delete(res=Lot, - item='{}/devices'.format(parent['id']), - query=[('id', device_id)], - status=200) - assert not len(lot['devices']) + user.delete(res=Lot, + item='{}/devices'.format(parent['id']), + query=[('id', device_id)], + status=200) + assert not len(lot.devices) @pytest.mark.mvp @@ -416,8 +417,9 @@ def test_lot_error_add_device_from_other_user(user: UserClient): res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)]) - assert lot['devices'] == [], 'Lot contains device' - assert len(lot['devices']) == 0 + lot = Lot.query.filter_by(id=lot['id']).one() + assert list(lot.devices) == [], 'Lot contains device' + assert len(lot.devices) == 0 @pytest.mark.mvp From 63f7adf69c55fde5851c5009535a84bbf1897bf3 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 18 Nov 2021 11:50:29 +0100 Subject: [PATCH 2/4] try to use only_fields for devices but not used --- ereuse_devicehub/resources/device/schemas.py | 74 +++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index bc18b995..f28fba72 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -14,6 +14,7 @@ from ereuse_devicehub.resources import enums from ereuse_devicehub.resources.device import models as m, states from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE from ereuse_devicehub.resources.schemas import Thing, UnitCodes +# from ereuse_devicehub.resources.action.schemas import Action as s_action class Device(Thing): @@ -96,6 +97,77 @@ class Device(Thing): field_names=['actions']) +ACTIONS_VALUE = ( + 'id', + 'type', + 'created' +) + + +TAGS_VALUE = ( + 'id', + 'printable', + 'code', + 'device', + 'type', + 'url' +) + + +class Device2(Thing): + __doc__ = m.Device.__doc__ + id = Integer(description=m.Device.id.comment, dump_only=True) + hid = SanitizedStr(lower=True, description=m.Device.hid.comment) + tags = f.Nested('Tag', + many=True, + collection_class=OrderedSet, + description='A set of tags that identify the device.', + only=TAGS_VALUE) + model = SanitizedStr(lower=True, + validate=Length(max=STR_BIG_SIZE), + description=m.Device.model.comment) + manufacturer = SanitizedStr(lower=True, + validate=Length(max=STR_SIZE), + description=m.Device.manufacturer.comment) + serial_number = SanitizedStr(lower=True, + validate=Length(max=STR_BIG_SIZE), + data_key='serialNumber') + # actions = NestedOn('Action', many=True, dump_only=True, description=m.Device.actions.__doc__) + actions = f.Nested('Action', only=ACTIONS_VALUE, many=True) + # actions = f.Nested(s_actions.Action(only=ACTIONS_VALUE), + # many=True, + # dump_only=True, + # description=m.Device.actions.__doc__) + # actions_one = NestedOn('Action', many=True, load_only=True, collection_class=OrderedSet) + url = URL(dump_only=True, description=m.Device.url.__doc__) + # lots = NestedOn('Lot', + # many=True, + # dump_only=True, + # description='The lots where this device is directly under.') + tradings = Dict(dump_only=True, description='') + traking = EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__) + revoke = UUID(dump_only=True) + allocated = Boolean(description=m.Device.allocated.comment) + devicehub_id = SanitizedStr(data_key='devicehubID', + description=m.Device.devicehub_id.comment) + + +COMPONENTS_VALUES = ( + 'type', + 'manufacturer', + 'model' +) + + +class Computer2(Device): + __doc__ = m.Computer.__doc__ + # components = f.Nested(Component(only=COMPONENTS_VALUES), + # many=True, + # dump_only=True, + # collection_class=OrderedSet, + # description='The components that are inside this computer.') + + class Computer(Device): __doc__ = m.Computer.__doc__ components = NestedOn('Component', @@ -128,7 +200,7 @@ class Computer(Device): description=m.Computer.privacy.__doc__) amount = Integer(validate=f.validate.Range(min=0, max=100), description=m.Computer.amount.__doc__) - # author_id = NestedOn(s_user.User,only_query='author_id') + # author_id = NestedOn(s_user.User, only_query='author_id') owner_id = UUID(data_key='ownerID') transfer_state = EnumField(enums.TransferState, description=m.Computer.transfer_state.comment) receiver_id = UUID(data_key='receiverID') From d8382f833138c863f887f44672ba3207e811f2bb Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 18 Nov 2021 11:52:59 +0100 Subject: [PATCH 3/4] drop assert for lot[devices] in dummy --- ereuse_devicehub/dummy/dummy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/dummy/dummy.py b/ereuse_devicehub/dummy/dummy.py index 04fbdaf3..5b6ab47a 100644 --- a/ereuse_devicehub/dummy/dummy.py +++ b/ereuse_devicehub/dummy/dummy.py @@ -139,7 +139,7 @@ class Dummy: res=Lot, item='{}/devices'.format(lot_user['id']), query=[('id', pc) for pc in itertools.islice(pcs, 1, 4)]) - assert len(lot['devices']) + # assert len(lot['devices']) lot2, _ = user2.post({}, res=Lot, From aa17d4b492c89ae7f080142d89302dd9c2147a08 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 19 Nov 2021 11:34:13 +0100 Subject: [PATCH 4/4] clean code test --- ereuse_devicehub/resources/device/schemas.py | 76 ++------------------ 1 file changed, 4 insertions(+), 72 deletions(-) diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index f28fba72..0c265d78 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -14,7 +14,6 @@ from ereuse_devicehub.resources import enums from ereuse_devicehub.resources.device import models as m, states from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE from ereuse_devicehub.resources.schemas import Thing, UnitCodes -# from ereuse_devicehub.resources.action.schemas import Action as s_action class Device(Thing): @@ -41,10 +40,13 @@ class Device(Thing): width = Float(validate=Range(0.1, 5), unit=UnitCodes.m, description=m.Device.width.comment) height = Float(validate=Range(0.1, 5), unit=UnitCodes.m, description=m.Device.height.comment) depth = Float(validate=Range(0.1, 5), unit=UnitCodes.m, description=m.Device.depth.comment) + # TODO TimeOut 2. Comment actions and lots if there are time out. actions = NestedOn('Action', many=True, dump_only=True, description=m.Device.actions.__doc__) + # TODO TimeOut 2. Comment actions_one and lots if there are time out. actions_one = NestedOn('Action', many=True, load_only=True, collection_class=OrderedSet) problems = NestedOn('Action', many=True, dump_only=True, description=m.Device.problems.__doc__) url = URL(dump_only=True, description=m.Device.url.__doc__) + # TODO TimeOut 2. Comment actions and lots if there are time out. lots = NestedOn('Lot', many=True, dump_only=True, @@ -97,79 +99,9 @@ class Device(Thing): field_names=['actions']) -ACTIONS_VALUE = ( - 'id', - 'type', - 'created' -) - - -TAGS_VALUE = ( - 'id', - 'printable', - 'code', - 'device', - 'type', - 'url' -) - - -class Device2(Thing): - __doc__ = m.Device.__doc__ - id = Integer(description=m.Device.id.comment, dump_only=True) - hid = SanitizedStr(lower=True, description=m.Device.hid.comment) - tags = f.Nested('Tag', - many=True, - collection_class=OrderedSet, - description='A set of tags that identify the device.', - only=TAGS_VALUE) - model = SanitizedStr(lower=True, - validate=Length(max=STR_BIG_SIZE), - description=m.Device.model.comment) - manufacturer = SanitizedStr(lower=True, - validate=Length(max=STR_SIZE), - description=m.Device.manufacturer.comment) - serial_number = SanitizedStr(lower=True, - validate=Length(max=STR_BIG_SIZE), - data_key='serialNumber') - # actions = NestedOn('Action', many=True, dump_only=True, description=m.Device.actions.__doc__) - actions = f.Nested('Action', only=ACTIONS_VALUE, many=True) - # actions = f.Nested(s_actions.Action(only=ACTIONS_VALUE), - # many=True, - # dump_only=True, - # description=m.Device.actions.__doc__) - # actions_one = NestedOn('Action', many=True, load_only=True, collection_class=OrderedSet) - url = URL(dump_only=True, description=m.Device.url.__doc__) - # lots = NestedOn('Lot', - # many=True, - # dump_only=True, - # description='The lots where this device is directly under.') - tradings = Dict(dump_only=True, description='') - traking = EnumField(states.Traking, dump_only=True, description=m.Device.physical.__doc__) - revoke = UUID(dump_only=True) - allocated = Boolean(description=m.Device.allocated.comment) - devicehub_id = SanitizedStr(data_key='devicehubID', - description=m.Device.devicehub_id.comment) - - -COMPONENTS_VALUES = ( - 'type', - 'manufacturer', - 'model' -) - - -class Computer2(Device): - __doc__ = m.Computer.__doc__ - # components = f.Nested(Component(only=COMPONENTS_VALUES), - # many=True, - # dump_only=True, - # collection_class=OrderedSet, - # description='The components that are inside this computer.') - - class Computer(Device): __doc__ = m.Computer.__doc__ + # TODO TimeOut 1. Comment components if there are time out. components = NestedOn('Component', many=True, dump_only=True,