From 7bb514d23c7aa2446d7a5382d815287a35db2beb Mon Sep 17 00:00:00 2001 From: nad Date: Thu, 2 May 2019 17:49:15 +0200 Subject: [PATCH] rewrite some tests to rate v2 structure --- .../dummy/files/smartphone.snapshot.yaml | 6 +-- tests/test_rate.py | 52 +++++++++++++++++-- tests/test_snapshot.py | 17 +++--- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/ereuse_devicehub/dummy/files/smartphone.snapshot.yaml b/ereuse_devicehub/dummy/files/smartphone.snapshot.yaml index c7586a6f..04c4874f 100644 --- a/ereuse_devicehub/dummy/files/smartphone.snapshot.yaml +++ b/ereuse_devicehub/dummy/files/smartphone.snapshot.yaml @@ -9,6 +9,6 @@ device: imei: 35686800-004141-20 events: - type: TestVisual - appearanceRange: A - functionalityRange: B - labelling: False + appearanceRange: A + functionalityRange: B + labelling: False diff --git a/tests/test_rate.py b/tests/test_rate.py index b62ea640..3407cf45 100644 --- a/tests/test_rate.py +++ b/tests/test_rate.py @@ -3,22 +3,25 @@ from distutils.version import StrictVersion import pytest +from ereuse_devicehub.client import UserClient from ereuse_devicehub.db import db from ereuse_devicehub.resources.device.models import Computer, Desktop, HardDrive, Processor, \ RamModule from ereuse_devicehub.resources.enums import AppearanceRange, ComputerChassis, \ FunctionalityRange from ereuse_devicehub.resources.event.models import BenchmarkDataStorage, \ - BenchmarkProcessor, EreusePrice, RateComputer, TestVisual + BenchmarkProcessor, RateComputer, TestVisual, Snapshot +from ereuse_devicehub.resources.event.rate.workbench.v1_0 import CannotRate from tests import conftest +from tests.conftest import file @pytest.mark.usefixtures(conftest.auth_app_context.__name__) def test_workbench_rate_db(): rate = RateComputer(processor=0.1, ram=1.0, - graphic_card=0.1, data_storage=4.1, + graphic_card=0.1, version=StrictVersion('1.0'), device=Computer(serial_number='24', chassis=ComputerChassis.Tower)) db.session.add(rate) @@ -64,9 +67,8 @@ def test_rate(): pc.events_one.add(visual_test) rate, price = RateComputer.compute(pc) - # TODO why events_one?? how to rewrite correctly this tests?? - events = pc.events - price = next(e for e in events if isinstance(e, EreusePrice)) + # events = pc.events + # price = next(e for e in events if isinstance(e, EreusePrice)) assert price.price == Decimal('92.2001') assert price.retailer.standard.amount == Decimal('40.9714') assert price.platform.standard.amount == Decimal('18.8434') @@ -78,3 +80,43 @@ def test_rate(): assert price.platform.warranty2.amount == Decimal('25.4357') assert price.refurbisher.warranty2.amount == Decimal('43.7259') assert price.warranty2 == Decimal('124.47') + + +def test_no_rate_if_no_workbench(user: UserClient): + """ + Checks if compute a rate from snapshot software is not from Workbench + """ + # Upload a basic snapshot + device_no_wb = file('basic.snapshot') + # Change snapshot software source + device_no_wb['software'] = 'Web' + del device_no_wb['uuid'] + del device_no_wb['elapsed'] + del device_no_wb['components'] + # Try to compute rate + user.post(device_no_wb, res=Snapshot) + # How to assert CannotRate Exception + assert CannotRate + + +def test_no_rate_if_no_visual_test(user: UserClient): + """ + Checks if a rate is calculated from a snapshot without visual test + """ + # Upload a basic snapshot + device = file('basic.snapshot') + # Delete snapshot device events + del device['device']['events'] + user.post(device, res=Snapshot) + # How to assert CannotRate Exception + assert CannotRate + + +def test_no_rate_if_device_is_not_computer(user: UserClient): + """ + Checks if a rate is calculated from a device that is not a computer. + """ + # Upload a basic snapshot of a device type + device = file('keyboard.snapshot') + user.post(device, res=Snapshot) + assert CannotRate diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 8784d233..6d86574b 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -13,11 +13,12 @@ from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.resources.device import models as m from ereuse_devicehub.resources.device.exceptions import NeedsId +from ereuse_devicehub.resources.device.models import SolidStateDrive from ereuse_devicehub.resources.device.sync import MismatchBetweenProperties, \ MismatchBetweenTagsAndHid from ereuse_devicehub.resources.enums import ComputerChassis, SnapshotSoftware from ereuse_devicehub.resources.event.models import BenchmarkProcessor, \ - EraseSectors, Event, Snapshot, SnapshotRequest, RateComputer, Rate, TestVisual, BenchmarkDataStorage + EraseSectors, Event, Snapshot, SnapshotRequest, RateComputer, TestVisual, BenchmarkDataStorage from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.user.models import User from tests.conftest import file @@ -241,7 +242,6 @@ def test_snapshot_tag_inner_tag(tag_id: str, user: UserClient, app: Devicehub): b = file('basic.snapshot') b['device']['tags'] = [{'type': 'Tag', 'id': tag_id}] - # TODO fix assert fail expected 3 and len(snapshot['events']) == 2; why? need param perform?? snapshot_and_check(user, b, event_types=( RateComputer.t, @@ -316,19 +316,18 @@ def test_erase_privacy_standards(user: UserClient): BenchmarkDataStorage.t, BenchmarkProcessor.t ), perform_second_snapshot=True) - # TODO fix order snapshot[events] ?? cause change in every execution?? why KeyError?? - assert '2018-06-01T07:12:06+00:00' == snapshot['events'][0]['endTime'] - storage, *_ = snapshot['components'] - assert storage['type'] == 'SolidStateDrive', 'Components must be ordered by input order' + erase = next(e for e in snapshot['events'] if e['type'] == EraseSectors.t) + assert '2018-06-01T07:12:06+00:00' == erase['endTime'] + storage = next(e for e in snapshot['components'] if e['type'] == SolidStateDrive.t) storage, _ = user.get(res=m.Device, item=storage['id']) # Let's get storage events too - # order: creation time descending - # TODO fix same order for storage['events']?? - erasure1, benchmark_data_storage1, _snapshot1, benchmark_data_storage2, erasure2, _snapshot2 = storage['events'] + # order: creation time ascending + erasure1, benchmark_data_storage1, _snapshot1, erasure2, benchmark_data_storage2, _snapshot2 = storage['events'] assert erasure1['type'] == erasure2['type'] == 'EraseSectors' assert benchmark_data_storage1['type'] == benchmark_data_storage2['type'] == 'BenchmarkDataStorage' assert _snapshot1['type'] == _snapshot2['type'] == 'Snapshot' get_snapshot, _ = user.get(res=Event, item=_snapshot2['id']) assert get_snapshot['events'][0]['endTime'] == '2018-06-01T07:12:06+00:00' + # TODO events order are different between snapshots assert snapshot == get_snapshot erasure, _ = user.get(res=Event, item=erasure1['id']) assert len(erasure['steps']) == 2