This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/tests/test_rate.py

201 lines
6.9 KiB
Python
Raw Normal View History

2018-10-15 09:21:21 +00:00
from decimal import Decimal
2018-06-10 16:47:49 +00:00
from distutils.version import StrictVersion
2019-05-14 18:32:24 +00:00
import math
2018-06-10 16:47:49 +00:00
import pytest
from ereuse_devicehub.client import UserClient
2018-06-10 16:47:49 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import Action, BenchmarkDataStorage, \
BenchmarkProcessor, RateComputer, Snapshot, VisualTest
from ereuse_devicehub.resources.device.models import Computer, Desktop, Device, HardDrive, \
2019-05-14 18:32:24 +00:00
Processor, RamModule, SolidStateDrive
2019-04-23 19:30:08 +00:00
from ereuse_devicehub.resources.enums import AppearanceRange, ComputerChassis, \
FunctionalityRange
2018-08-08 19:25:53 +00:00
from tests import conftest
from tests.conftest import file
2018-06-10 16:47:49 +00:00
2018-08-08 19:25:53 +00:00
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2018-07-14 14:41:22 +00:00
def test_workbench_rate_db():
2019-04-23 19:30:08 +00:00
rate = RateComputer(processor=0.1,
ram=1.0,
data_storage=4.1,
graphic_card=0.1,
2019-04-23 19:30:08 +00:00
version=StrictVersion('1.0'),
device=Computer(serial_number='24', chassis=ComputerChassis.Tower))
2018-06-10 16:47:49 +00:00
db.session.add(rate)
db.session.commit()
2019-05-20 17:32:29 +00:00
@pytest.mark.xfail(reason='Rate algorithm v1 have the limitation of not '
'recompute rate when post new visual test')
2019-05-14 18:32:24 +00:00
def test_rate_with_multiple_visual_tests(user: UserClient):
"""Perform a ComputerRate and then update the device with a new VisualTest.
2019-05-14 18:32:24 +00:00
Devicehub must make the final rate with the first computer rate
plus the new visual test, without considering the appearance /
functionality values of the computer rate.
"""
s = file('real-hp.snapshot.11')
snapshot, _ = user.post(s, res=Snapshot)
device, _ = user.get(res=Device, item=snapshot['device']['id'])
2019-05-14 18:32:24 +00:00
visual_test = next(e for e in reversed(device['actions']) if e['type'] == VisualTest.t)
assert visual_test['appearanceRange'] == 'B'
assert visual_test['functionalityRange'] == 'D'
assert device['rate']['rating'] == 2
# Adding new visual test
user.post({
2019-05-14 18:32:24 +00:00
'type': 'VisualTest',
'device': device['id'],
'appearanceRange': 'A',
'functionalityRange': 'A'
}, res=Action)
device, _ = user.get(res=Device, item=snapshot['device']['id'])
2019-05-14 18:32:24 +00:00
visual_test = next(e for e in reversed(device['actions']) if e['type'] == VisualTest.t)
assert visual_test['appearanceRange'] == 'A'
assert visual_test['functionalityRange'] == 'A'
assert device['rate']['rating'] == 3.7
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_price_from_rate():
"""Tests the price generated from the rate."""
2018-10-18 08:09:10 +00:00
pc = Desktop(chassis=ComputerChassis.Tower)
hdd = HardDrive(size=476940)
hdd.actions_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8))
cpu = Processor(cores=2, speed=3.4)
cpu.actions_one.add(BenchmarkProcessor(rate=27136.44))
pc.components |= {
hdd,
RamModule(size=4096, speed=1600),
RamModule(size=2048, speed=1067),
cpu
}
2019-04-23 19:30:08 +00:00
# Add test visual with functionality and appearance range
2019-05-08 17:12:05 +00:00
VisualTest(appearance_range=AppearanceRange.A,
functionality_range=FunctionalityRange.A,
device=pc)
2019-05-14 18:32:24 +00:00
_, price = RateComputer.compute(pc)
2018-10-15 09:21:21 +00:00
assert price.price == Decimal('92.2001')
assert price.retailer.standard.amount == Decimal('40.9714')
assert price.platform.standard.amount == Decimal('18.8434')
assert price.refurbisher.standard.amount == Decimal('32.3853')
2019-05-14 18:32:24 +00:00
assert price.price >= price.retailer.standard.amount + price.platform.standard.amount \
+ price.refurbisher.standard.amount
2018-10-15 09:21:21 +00:00
assert price.retailer.warranty2.amount == Decimal('55.3085')
assert price.platform.warranty2.amount == Decimal('25.4357')
assert price.refurbisher.warranty2.amount == Decimal('43.7259')
assert price.warranty2 == Decimal('124.47')
2019-05-14 18:32:24 +00:00
def test_when_rate_must_not_compute(user: UserClient):
"""
2019-05-14 18:32:24 +00:00
Test to check if rate is computed in case of should not be calculated:
1. Snapshot haven't visual test
2. Snapshot software aren't Workbench
3. Device type are not Computer
...
"""
2019-05-14 18:32:24 +00:00
# Checking case 1
s = file('basic.snapshot')
# Delete snapshot device actions to delete VisualTest
del s['device']['actions']
2019-05-14 18:32:24 +00:00
# Post to compute rate and check to didn't do it
snapshot, _ = user.post(s, res=Snapshot)
assert 'rate' not in snapshot['device']
2019-05-14 18:32:24 +00:00
# Checking case 2
s = file('basic.snapshot')
2019-05-14 18:32:24 +00:00
# Change snapshot software source
s['software'] = 'Web'
del s['uuid']
del s['elapsed']
del s['components']
# Post to compute rate and check to didn't do it
snapshot, _ = user.post(s, res=Snapshot)
assert 'rate' not in snapshot['device']
2019-05-14 18:32:24 +00:00
# Checking case 3
s = file('keyboard.snapshot')
2019-05-14 18:32:24 +00:00
# Post to compute rate and check to didn't do it
snapshot, _ = user.post(s, res=Snapshot)
assert 'rate' not in snapshot['device']
2019-05-08 17:12:05 +00:00
2019-05-14 18:32:24 +00:00
@pytest.mark.usefixtures(conftest.app_context.__name__)
2019-05-08 17:12:05 +00:00
def test_multiple_rates(user: UserClient):
"""Tests submitting two rates from Workbench,
ensuring that the tests / benchmarks...
from the first rate do not contaminate the second rate.
2019-06-19 09:41:22 +00:00
This ensures that rates only takes all the correct actions
and components rates in case device have new tests/benchmarks.
2019-05-08 17:12:05 +00:00
"""
2019-05-14 18:32:24 +00:00
pc = Desktop(chassis=ComputerChassis.Tower)
hdd = HardDrive(size=476940)
hdd.actions_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8))
cpu = Processor(cores=2, speed=3.4)
cpu.actions_one.add(BenchmarkProcessor(rate=27136.44))
pc.components = {
hdd,
RamModule(size=4096, speed=1600),
RamModule(size=2048, speed=1600),
cpu
}
# Add test visual with functionality and appearance range
VisualTest(appearance_range=AppearanceRange.A,
functionality_range=FunctionalityRange.A,
device=pc)
rate1, price1 = RateComputer.compute(pc)
# asserts rate1 ...
assert rate1.data_storage == 4.02
assert rate1.processor == 3.95
assert rate1.ram == 3.8
assert rate1.appearance == 0.3
assert rate1.functionality == 0.4
assert rate1.rating == 4.62
assert price1.price == Decimal('92.4001')
cpu.actions_one.add(BenchmarkProcessor(rate=16069.44))
2019-06-19 09:41:22 +00:00
ssd = SolidStateDrive(size=476940)
ssd.actions_one.add(BenchmarkDataStorage(read_speed=222, write_speed=111))
2019-05-20 17:32:29 +00:00
pc.components |= {
2019-06-19 09:41:22 +00:00
ssd,
2019-05-14 18:32:24 +00:00
RamModule(size=2048, speed=1067),
RamModule(size=2048, speed=1067),
}
# Add test visual with functionality and appearance range
VisualTest(appearance_range=AppearanceRange.B,
functionality_range=FunctionalityRange.B,
device=pc)
rate2, price2 = RateComputer.compute(pc)
2019-06-19 09:41:22 +00:00
assert rate2.data_storage == 4.3
assert rate2.processor == 3.78
assert rate2.ram == 3.95
2019-05-14 18:32:24 +00:00
assert rate2.appearance == 0
assert rate2.functionality == -0.5
2019-06-19 09:41:22 +00:00
assert rate2.rating == 3.43
2019-05-14 18:32:24 +00:00
2019-06-19 09:41:22 +00:00
assert price2.price == Decimal('68.6001')