adding live into snapshot and adding test
This commit is contained in:
parent
8999b6e9a5
commit
87704db271
|
@ -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()}'
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in New Issue