From d974d3d78fb64298d0fa1e6f979058ce9f6f01b1 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 14 Jan 2021 17:32:03 +0100 Subject: [PATCH] ereuse_devicehub/resources/action/models.py --- ereuse_devicehub/resources/device/models.py | 17 ++++++++------- tests/test_documents.py | 23 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a3f825e4..7081ce49 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -325,16 +325,15 @@ class Device(Thing): actions = copy.copy(self.actions) actions.sort(key=lambda x: x.created) allocates = [] - from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Deallocate, Live for act in actions: - if isinstance(act, Snapshot): + if act.type == 'Snapshot': snapshot = act lifestimes = snapshot.get_last_lifetimes() lifetime = 0 if lifestimes: lifetime = lifestimes[0]['lifetime'] - if isinstance(act, Allocate): + if act.type == 'Allocate': allo = {'type': 'Allocate', 'userCode': act.final_user_code, 'numUsers': act.end_users, @@ -345,15 +344,19 @@ class Device(Thing): 'allocateLifetime': lifetime} allocates.append(allo) - if isinstance(act, Live): + if act.type == 'Live': allocate = copy.copy(allo) - lifetime = act.usage_time_hdd + lifetime = act.usage_time_hdd.total_seconds() allocate['type'] = 'Live' allocate['liveCreate'] = act.created + for hd in lifestimes: + if hd['serial_number'] == act.serial_number: + lifetime = hd['lifetime'] + break allocate['liveLifetime'] = lifetime allocates.append(allocate) - if isinstance(act, Deallocate): + if act.type == 'Deallocate': deallo = {'type': 'Deallocate', 'userCode': '', 'numUsers': '', @@ -364,7 +367,7 @@ class Device(Thing): 'allocateLifetime': 0} allocates.append(deallo) - return allocates + return allocates def __lt__(self, other): return self.id < other.id diff --git a/tests/test_documents.py b/tests/test_documents.py index bb7d802b..f76ca0ed 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -11,7 +11,7 @@ from ereuse_utils.test import ANY from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.devicehub import Devicehub -from ereuse_devicehub.resources.action.models import Snapshot +from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Live from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.device import models as d from ereuse_devicehub.resources.lot.models import Lot @@ -110,7 +110,24 @@ def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Clie @pytest.mark.mvp def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client): """Test export device information in a csv file with others users.""" - snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot) + acer = file('acer.happy.battery.snapshot') + snapshot, _ = user.post(acer, res=Snapshot) + device_id = snapshot['device']['id'] + post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, + "devices": [device_id], "description": "aaa", + "finalUserCode": "abcdefjhi", + "startTime": "2020-11-01T02:00:00+00:00", + "endTime": "2020-12-01T02:00:00+00:00" + } + + user.post(res=Allocate, data=post_request) + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['lifetime'] += 1000 + acer.pop('elapsed') + acer['licence_version'] = '1.0.0' + snapshot, _ = client.post(acer, res=Live) + csv_user, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', @@ -131,7 +148,7 @@ def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client) assert len(csv_user2) == 0 -@pytest.mark.mvp +@pytest.mark.mvp def test_export_basic_snapshot(user: UserClient): """Test export device information in a csv file.""" snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)