From 87704db27142c1b3c0143e313d0e0235253d8be3 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 24 Nov 2020 13:44:49 +0100 Subject: [PATCH] adding live into snapshot and adding test --- .../e93aec8fc41f_added_assigned_action.py | 2 +- ereuse_devicehub/resources/action/models.py | 2 +- ereuse_devicehub/resources/action/views.py | 19 +++++++- tests/test_action.py | 48 ++++++++++--------- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py index 59010a74..08d6c52c 100644 --- a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py +++ b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py @@ -59,7 +59,7 @@ def upgrade(): sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('serial_number', sa.Unicode(), nullable=True, comment='The serial number of the Hard Disk in lower case.'), - sa.Column('time', sa.Interval(), nullable=True), + sa.Column('time', sa.SmallInteger(), nullable=True), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 416e612c..a32ebbdd 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1297,7 +1297,7 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice): """ serial_number = Column(Unicode(), check_lower('serial_number')) serial_number.comment = """The serial number of the Hard Disk in lower case.""" - time = Column(Interval, nullable=True) + time = Column(SmallInteger, nullable=False) class Organize(JoinedTableMixin, ActionWithMultipleDevices): diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 1e0908cb..61ed9e16 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -16,7 +16,7 @@ from teal.resource import View from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, - InitTransfer) + InitTransfer, Live) from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.user.exceptions import InsufficientPermission @@ -160,6 +160,7 @@ class ActionView(View): # Check if HID is null and add Severity:Warning to Snapshot if snapshot.device.hid is None: snapshot.severity = Severity.Warning + self.live(snapshot) db.session.add(snapshot) db.session().final_flush() ret = self.schema.jsonify(snapshot) # transform it back @@ -167,6 +168,22 @@ class ActionView(View): db.session.commit() return ret + def live(self, snapshot): + test_hdd= [a for a in snapshot.actions if a.type == "TestDataStorage"] + if not (test_hdd and snapshot.device.allocated): + return + + test_hdd = test_hdd[0] + time = test_hdd.power_cycle_count + if time: + data_live = {'time': time, + 'serial_number': test_hdd.device.serial_number, + 'device': snapshot.device + } + live = Live(**data_live) + snapshot.actions.add(live) + + def transfer_ownership(self): """Perform a InitTransfer action to change author_id of device""" pass diff --git a/tests/test_action.py b/tests/test_action.py index d6fab3f2..4d2a7231 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -10,8 +10,9 @@ from flask import current_app as app, g from sqlalchemy.util import OrderedSet from teal.enums import Currency, Subdivision -from ereuse_devicehub.client import UserClient from ereuse_devicehub.db import db +from ereuse_devicehub.client import UserClient +from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.resources import enums from ereuse_devicehub.resources.action import models from ereuse_devicehub.resources.device import states @@ -245,29 +246,30 @@ def test_generic_action(action_model_state: Tuple[models.Action, states.Trading] @pytest.mark.mvp -@pytest.mark.usefixtures(conftest.auth_app_context.__name__) -def test_live(): +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_live(user: UserClient, app: Devicehub): """Tests inserting a Live into the database and GETting it.""" - db_live = models.Live(ip=ipaddress.ip_address('79.147.10.10'), - subdivision_confidence=84, - subdivision=Subdivision['ES-CA'], - city='barcelona', - city_confidence=20, - isp='acme', - device=Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', - chassis=ComputerChassis.Docking), - organization='acme1', - organization_type='acme1bis') - db.session.add(db_live) - db.session.commit() - client = UserClient(app, 'foo@foo.com', 'foo', response_wrapper=app.response_class) - client.login() - live, _ = client.get(res=models.Action, item=str(db_live.id)) - assert live['ip'] == '79.147.10.10' - assert live['subdivision'] == 'ES-CA' - assert live['country'] == 'ES' - device, _ = client.get(res=Device, item=live['device']['id']) - assert device['usage'] == states.Usage.InUse.name + acer = file('acer.happy.battery.snapshot') + snapshot, _ = user.post(acer, res=models.Snapshot) + device_id = snapshot['device']['id'] + db_device = Device.query.filter_by(id=1).one() + post_request = {"Transaction": "ccc", "name": "John", "end_users": 1, + "devices": [device_id], "description": "aaa", + "start_time": "2020-11-01T02:00:00+00:00", + "end_time": "2020-12-01T02:00:00+00:00" + } + + user.post(res=models.Allocate, data=post_request) + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3" + 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['powerCycleCount'] += 1000 + snapshot, _ = user.post(acer, res=models.Snapshot) + db_device = Device.query.filter_by(id=1).one() + action_live = [a for a in db_device.actions if a.type == 'Live'] + assert len(action_live) == 1 + assert action_live[0].time == 6293 + assert action_live[0].serial_number == 'wd-wx11a80w7430' @pytest.mark.mvp