def test_tag_get_device_from_tag_endpoint_multiple_tags(app: Devicehub, user: UserClient, user2: UserClient, client: Client): """As above, but when there are two tags with the secondary ID, the system should not return any of both (to be deterministic) so it should raise an exception. """ g.user = User.query.all()[0] db.session.add(Tag(id='foo', secondary='bar', owner_id=user.user['id'])) db.session.commit() db.session.add(Tag(id='foo', secondary='bar', owner_id=user2.user['id'])) db.session.commit() db.session.add(Tag(id='foo2', secondary='bar', owner_id=user.user['id'])) with raises(DBError): db.session.commit() db.session.rollback() tag1 = Tag.from_an_id('foo').filter_by(owner_id=user.user['id']).one() tag2 = Tag.from_an_id('foo').filter_by(owner_id=user2.user['id']).one() pc1 = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) pc2 = Desktop(serial_number='sn2', chassis=ComputerChassis.Tower, owner_id=user2.user['id']) pc1.tags.add(tag1) pc2.tags.add(tag2) db.session.add(pc1) db.session.add(pc2) db.session.commit() computer, _ = user.get(res=Tag, item='foo/device') assert computer['serialNumber'] == 'sn1' computer, _ = user2.get(res=Tag, item='foo/device') assert computer['serialNumber'] == 'sn2' _, status = client.get(res=Tag, item='foo/device', status=MultipleResourcesFound) assert status.status_code == 422
def test_get_device(app: Devicehub, user: UserClient): """Checks GETting a Desktop with its components.""" with app.app_context(): pc = Desktop(model='p1mo', manufacturer='p1ma', serial_number='p1s') pc.components = OrderedSet([ NetworkAdapter(model='c1mo', manufacturer='c1ma', serial_number='c1s'), GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500) ]) db.session.add(pc) db.session.add(Test(device=pc, elapsed=timedelta(seconds=4), error=False, author=User(email='*****@*****.**'))) db.session.commit() pc, _ = user.get(res=Device, item=1) assert len(pc['events']) == 1 assert pc['events'][0]['type'] == 'Test' assert pc['events'][0]['device'] == 1 assert pc['events'][0]['elapsed'] == 4 assert not pc['events'][0]['error'] assert UUID(pc['events'][0]['author']) assert 'events_components' not in pc, 'events_components are internal use only' assert 'events_one' not in pc, 'they are internal use only' assert 'author' not in pc assert tuple(c['id'] for c in pc['components']) == (2, 3) assert pc['hid'] == 'p1ma-p1s-p1mo' assert pc['model'] == 'p1mo' assert pc['manufacturer'] == 'p1ma' assert pc['serialNumber'] == 'p1s' assert pc['type'] == 'Desktop'
def test_live(): """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', response_wrapper=app.response_class) client.login() live, _ = client.get(res=models.Event, 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['physical'] == states.Physical.InUse.name
def test_main(): rate = WorkbenchRate( appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A ) pc = Desktop() hdd = HardDrive(size=476940) hdd.events_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) cpu = Processor(cores=2, speed=3.4) cpu.events_one.add(BenchmarkProcessor(rate=27136.44)) pc.components |= { hdd, RamModule(size=4096, speed=1600), RamModule(size=2048, speed=1067), cpu } rate.device = pc events = main.main(rate, RatingSoftware.ECost, StrictVersion('1.0')) price = next(e for e in events if isinstance(e, EreusePrice)) assert price.price == 92.2 assert price.retailer.standard.amount == 40.97 assert price.platform.standard.amount == 18.84 assert price.refurbisher.standard.amount == 32.38 assert price.price >= price.retailer.standard.amount \ + price.platform.standard.amount \ + price.refurbisher.standard.amount assert price.retailer.warranty2.amount == 55.30 assert price.platform.warranty2.amount == 25.43 assert price.refurbisher.warranty2.amount == 43.72 assert price.warranty2 == 124.45 # Checks relationships workbench_rate = next(e for e in events if isinstance(e, WorkbenchRate)) aggregate_rate = next(e for e in events if isinstance(e, AggregateRate)) assert price.rating == aggregate_rate assert aggregate_rate.ratings[0] == workbench_rate
def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): """Tests adding a device to a lot using POST and removing it with DELETE.""" # todo check with components with app.app_context(): device = Desktop(serial_number='foo', model='bar', manufacturer='foobar', chassis=ComputerChassis.Lunchbox) db.session.add(device) db.session.commit() device_id = device.id parent, _ = user.post(({'name': 'lot'}), res=Lot) lot, _ = user.post({}, res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)]) assert lot['devices'][0]['id'] == device_id, 'Lot contains device' device, _ = user.get(res=Device, item=device_id) assert len(device['lots']) == 1 assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot' # Remove the device lot, _ = user.delete(res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)], status=200) assert not len(lot['devices'])
def test_update_components_event_one(): computer = Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', chassis=ComputerChassis.Tower) hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar') computer.components.add(hdd) # Add event test = models.StressTest(elapsed=timedelta(seconds=1)) computer.events_one.add(test) assert test.device == computer assert next(iter( test.components)) == hdd, 'Event has to have new components' # Remove event computer.events_one.clear() assert not test.device assert not test.components, 'Event has to loose the components' # If we add a component to a device AFTER assigning the event # to the device, the event doesn't get the new component computer.events_one.add(test) ram = RamModule() computer.components.add(ram) assert len(test.components) == 1
def test_lot_error_add_device_from_other_user(user: UserClient): # TODO """Tests adding a device to a lot using POST and removing it with DELETE. """ g.user = User.query.one() user2 = User(email='*****@*****.**', password='******') user2.individuals.add(Person(name='Tommy')) db.session.add(user2) db.session.commit() device = Desktop(serial_number='foo', model='bar', manufacturer='foobar', chassis=ComputerChassis.Lunchbox, owner_id=user2.id) db.session.add(device) db.session.commit() device_id = device.id parent, _ = user.post(({'name': 'lot'}), res=Lot) lot, _ = user.post({}, res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)]) lot = Lot.query.filter_by(id=lot['id']).one() assert list(lot.devices) == [], 'Lot contains device' assert len(lot.devices) == 0
def test_get_devices(app: Devicehub, user: UserClient): """Checks GETting multiple devices.""" with app.app_context(): pc = Desktop(model='p1mo', manufacturer='p1ma', serial_number='p1s') pc.components = OrderedSet([ NetworkAdapter(model='c1mo', manufacturer='c1ma', serial_number='c1s'), GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500) ]) pc1 = Microtower(model='p2mo', manufacturer='p2ma', serial_number='p2s') pc2 = Laptop(model='p3mo', manufacturer='p3ma', serial_number='p3s') db.session.add_all((pc, pc1, pc2)) db.session.commit() devices, _ = user.get(res=Device) assert tuple(d['id'] for d in devices) == (1, 2, 3, 4, 5) assert tuple(d['type'] for d in devices) == ('Desktop', 'Microtower', 'Laptop', 'NetworkAdapter', 'GraphicCard')
def test_update_components_event_multiple(): computer = Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', chassis=ComputerChassis.Tower) hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar') computer.components.add(hdd) ready = models.ReadyToUse() assert not ready.devices assert not ready.components # Add computer.events_multiple.add(ready) assert ready.devices == OrderedSet([computer]) assert next(iter(ready.components)) == hdd # Remove computer.events_multiple.remove(ready) assert not ready.devices assert not ready.components # init / replace collection ready.devices = OrderedSet([computer]) assert ready.devices assert ready.components
def test_lot_device_relationship(): device = Desktop(serial_number='foo', model='bar', manufacturer='foobar', chassis=ComputerChassis.Lunchbox) device.components.add( GraphicCard(serial_number='foo', model='bar1', manufacturer='baz')) child = Lot('child') child.devices.add(device) db.session.add(child) db.session.flush() lot_device = LotDevice.query.one() # type: LotDevice assert lot_device.device_id == device.id assert lot_device.lot_id == child.id assert lot_device.created assert lot_device.author_id == g.user.id assert device.lots == {child} assert device in child assert device in child.all_devices graphic = GraphicCard(serial_number='foo', model='bar') device.components.add(graphic) db.session.flush() assert graphic in child parent = Lot('parent') db.session.add(parent) db.session.flush() parent.add_children(child) assert child in parent
def test_lot_post_add_remove_device_view(user: UserClient): """Tests adding a device to a lot using POST and removing it with DELETE. """ # todo check with components g.user = User.query.one() device = Desktop(serial_number='foo', model='bar', manufacturer='foobar', chassis=ComputerChassis.Lunchbox, owner_id=user.user['id']) db.session.add(device) db.session.commit() device_id = device.id devicehub_id = device.devicehub_id parent, _ = user.post(({'name': 'lot'}), res=Lot) lot, _ = user.post({}, res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)]) lot = Lot.query.filter_by(id=lot['id']).one() assert list(lot.devices)[0].id == device_id, 'Lot contains device' device = Device.query.filter_by(devicehub_id=devicehub_id).one() assert len(device.lots) == 1 # assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot' assert list(device.lots)[0].id == lot.id, 'Device is inside lot' # Remove the device user.delete(res=Lot, item='{}/devices'.format(parent['id']), query=[('id', device_id)], status=200) assert not len(lot.devices)
def test_price_from_rate(): """Tests the price generated from the rate.""" 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 } # Add test visual with functionality and appearance range VisualTest(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A, device=pc) _, price = RateComputer.compute(pc) assert price.price == Decimal('78.2001') assert price.retailer.standard.amount == Decimal('34.7502') assert price.platform.standard.amount == Decimal('15.9821') assert price.refurbisher.standard.amount == Decimal('27.4678') assert price.price >= price.retailer.standard.amount + price.platform.standard.amount \ + price.refurbisher.standard.amount assert price.retailer.warranty2.amount == Decimal('46.9103') assert price.platform.warranty2.amount == Decimal('21.5735') assert price.refurbisher.warranty2.amount == Decimal('37.0864') assert price.warranty2 == Decimal('105.57')
def test_tag_manual_link_search(app: Devicehub, user: UserClient): """Tests linking manually a tag through PUT /tags/<id>/device/<id> Checks search has the term. """ with app.app_context(): g.user = User.query.one() db.session.add(Tag('foo-bar', secondary='foo-sec', owner_id=user.user['id'])) desktop = Desktop(serial_number='foo', chassis=ComputerChassis.AllInOne, owner_id=user.user['id']) db.session.add(desktop) db.session.commit() desktop_id = desktop.id devicehub_id = desktop.devicehub_id user.put({}, res=Tag, item='foo-bar/device/{}'.format(desktop_id), status=204) device, _ = user.get(res=Device, item=devicehub_id) assert 'foo-bar' in [x['id'] for x in device['tags']] # Device already linked # Just returns an OK to conform to PUT as anything changes user.put({}, res=Tag, item='foo-sec/device/{}'.format(desktop_id), status=204) # Secondary IDs are case insensitive user.put({}, res=Tag, item='FOO-BAR/device/{}'.format(desktop_id), status=204) user.put({}, res=Tag, item='FOO-SEC/device/{}'.format(desktop_id), status=204) # cannot link to another device when already linked user.put({}, res=Tag, item='foo-bar/device/99', status=LinkedToAnotherDevice) i, _ = user.get(res=Device, query=[('search', 'foo-bar')]) assert i['items'] i, _ = user.get(res=Device, query=[('search', 'foo-sec')]) assert i['items'] i, _ = user.get(res=Device, query=[('search', 'foo')]) assert i['items']
def device_query_dummy(app: Devicehub): """ 3 computers, where: 1. s1 Desktop with a Processor 2. s2 Desktop with an SSD 3. s3 Laptop 4. s4 Server with another SSD :param app: :return: """ with app.app_context(): devices = ( # The order matters ;-) Desktop(serial_number='1', model='ml1', manufacturer='mr1', chassis=ComputerChassis.Tower), Desktop(serial_number='2', model='ml2', manufacturer='mr2', chassis=ComputerChassis.Microtower), Laptop(serial_number='3', model='ml3', manufacturer='mr3', chassis=ComputerChassis.Detachable), Server(serial_number='4', model='ml4', manufacturer='mr4', chassis=ComputerChassis.Tower), ) devices[0].components.add( GraphicCard(serial_number='1-gc', model='s1ml', manufacturer='s1mr')) devices[1].components.add( SolidStateDrive(serial_number='2-ssd', model='s2ml', manufacturer='s2mr')) devices[-1].components.add( SolidStateDrive(serial_number='4-ssd', model='s4ml', manufacturer='s4mr')) db.session.add_all(devices) db.session.commit()
def inventory_query_dummy(app: Devicehub): with app.app_context(): db.session.add_all(( # The order matters ;-) Desktop(serial_number='s1', model='ml1', manufacturer='mr1'), Laptop(serial_number='s3', model='ml3', manufacturer='mr3'), Microtower(serial_number='s2', model='ml2', manufacturer='mr2'), SolidStateDrive(serial_number='s4', model='ml4', manufacturer='mr4'))) db.session.commit()
def test_rate_computer_multiple_ram_module(): """Test rate computer characteristics: - only 1 module ram - processor 2 cores Data get it from R score from DH pc with id = 79 pc_79 = Computer() price = VeryLow hdd_81 = HardDrive(size=76319) hdd_81.actions_one.add(BenchmarkDataStorage(read_speed=72.2, write_speed=24.3)) ram1 = RamModule(size=512, speed=667) ram2 = RamModule(size=512, speed=800) ram3 = RamModule(size=512, speed=667) ram4 = RamModule(size=512, speed=533) cpu = Processor(cores=1, speed=1.6) cpu.actions_one.add(BenchmarkProcessor(rate=3192.34)) pc_79.components.add(hdd_81, ram1, ram2, ram3, ram4, cpu) # add functionality and appearance range rate_pc_79 = WorkbenchRate(appearance_range=AppearanceRange.C, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 2.61 RAM_rate = 1.99 Processor_rate = 1 Rating = 1.58 """ pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=76319) data_storage.actions_one.add( BenchmarkDataStorage(read_speed=72.2, write_speed=24.3)) cpu = Processor(cores=1, speed=1.6) cpu.actions_one.add(BenchmarkProcessor(rate=3192.34)) pc_test.components |= { data_storage, RamModule(size=512, speed=667), RamModule(size=512, speed=800), RamModule(size=512, speed=667), RamModule(size=512, speed=533), cpu } # Add test visual with functionality and appearance range VisualTest(appearance_range=AppearanceRange.C, functionality_range=FunctionalityRange.A, device=pc_test) # Compute all components rates and general rating rate_pc = RateAlgorithm().compute(pc_test) assert math.isclose(rate_pc.ram, 1.99, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 2.61, rel_tol=0.001) assert math.isclose(rate_pc.processor, 1, rel_tol=0.001) assert rate_pc.rating == 1.37
def test_tag_get_device_from_tag_endpoint(app: Devicehub, user: UserClient): """Checks getting a linked device from a tag endpoint""" with app.app_context(): # Create a pc with a tag g.user = User.query.one() tag = Tag(id='foo-bar', owner_id=user.user['id']) pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) pc.tags.add(tag) db.session.add(pc) db.session.commit() computer, _ = user.get(res=Tag, item='foo-bar/device') assert computer['serialNumber'] == 'sn1'
def test_rate_computer_1193(): """Test rate computer characteristics: - 2 module ram - processor with 2 cores Data get it from R score from DH pc with id = 1193 pc_1193 = Computer() price = 92.2 hdd_1969 = HardDrive(size=476940) hdd_1969.actions_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) ram1 = RamModule(size=4096, speed=1600) ram2 = RamModule(size=2048, speed=1067) cpu = Processor(cores=2, speed=3.4) cpu.actions_one.add(BenchmarkProcessor(rate=27136.44)) pc_1193.components.add(hdd_1969, ram1, ram2, cpu) rate_pc_1193 = WorkbenchRate(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A) HDD_rate = 4.02 RAM_rate = 3.79 Processor_rate = 3.95 Rating = 4.61 """ pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=476940) data_storage.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_test.components |= { data_storage, RamModule(size=4096, speed=1600), RamModule(size=2048, speed=1067), cpu } # Add test visual with functionality and appearance range VisualTest(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A, device=pc_test) # Compute all components rates and general rating rate_pc = RateAlgorithm().compute(pc_test) assert math.isclose(rate_pc.ram, 3.79, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 4.02, rel_tol=0.001) assert math.isclose(rate_pc.processor, 3.95, rel_tol=0.001) assert math.isclose(rate_pc.rating, 3.91, rel_tol=0.001)
def test_create_tag_with_device(user: UserClient): """Creates a tag specifying linked with one device.""" g.user = User.query.one() pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) db.session.add(pc) db.session.commit() tag = Tag(id='bar', owner_id=user.user['id']) db.session.add(tag) db.session.commit() data = '{tag_id}/device/{device_id}'.format(tag_id=tag.id, device_id=pc.id) user.put({}, res=Tag, item=data, status=204) user.get(res=Tag, item='{}/device'.format(tag.id)) user.delete({}, res=Tag, item=data, status=204) res, _ = user.get(res=Tag, item='{}/device'.format(tag.id), status=422) assert res['type'] == 'TagNotLinked'
def test_rate_computer_one_ram_module(): """Test rate computer characteristics: - only 1 module ram - processor 2 cores Data get it from R score from DH pc with id = 798 pc_798 = Computer() price = 50 hdd_1556 = HardDrive(size=152587) hdd_1556.actions_one.add(BenchmarkDataStorage(read_speed=78.1, write_speed=24.4)) ram0 = RamModule(size=0, speed=None) cpu = Processor(cores=2, speed=2.5) cpu.actions_one.add(BenchmarkProcessor(rate=9974.3)) pc_798.components.add(hdd_1556, ram0, cpu) # add functionality and appearance range rate_pc_798 = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 3.7 RAM_rate = 1 Processor_rate = 4.09 Rating = 2.5 """ pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=152587) data_storage.actions_one.add( BenchmarkDataStorage(read_speed=78.1, write_speed=24.4)) cpu = Processor(cores=2, speed=2.5) cpu.actions_one.add(BenchmarkProcessor(rate=9974.3)) pc_test.components |= {data_storage, RamModule(size=0, speed=None), cpu} # Add test visual with functionality and appearance range VisualTest(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A, device=pc_test) # Compute all components rates and general rating rate_pc = RateAlgorithm().compute(pc_test) assert math.isclose(rate_pc.ram, 1, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 3.7, rel_tol=0.001) assert math.isclose(rate_pc.processor, 4.09, rel_tol=0.001) assert math.isclose(rate_pc.rating, 2.1, rel_tol=0.001)
def test_update_parent(): computer = Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', chassis=ComputerChassis.Tower) hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar') computer.components.add(hdd) # Add benchmark = models.BenchmarkDataStorage() benchmark.device = hdd assert benchmark.parent == computer assert not benchmark.components # Remove benchmark.device = None assert not benchmark.parent
def test_rate_computer_1201(): """Test rate computer characteristics: - only 1 module ram - processor 2 cores Data get it from R score from DH pc with id = 1201 pc_1201 = Computer() price = 69.6 hdd_3054 = HardDrive(size=476940) hdd_3054.actions_one.add(BenchmarkDataStorage(read_speed=158, write_speed=34.7)) ram1 = RamModule(size=2048, speed=1333) cpu = Processor(cores=2, speed=3.3) cpu.actions_one.add(BenchmarkProcessor(rate=26339.48)) pc_1201.components.add(hdd_3054, ram1, cpu) rate_pc_1201 = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) HDD_rate = 4.07 RAM_rate = 2.02 Processor_rate = 3.93 Rating = 3.48 """ pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=476940) data_storage.actions_one.add( BenchmarkDataStorage(read_speed=158, write_speed=34.7)) cpu = Processor(cores=2, speed=3.3) cpu.actions_one.add(BenchmarkProcessor(rate=26339.48)) pc_test.components |= {data_storage, RamModule(size=2048, speed=1333), cpu} # Add test visual with functionality and appearance range VisualTest(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A, device=pc_test) # Compute all components rates and general rating rate_pc = RateAlgorithm().compute(pc_test) assert math.isclose(rate_pc.ram, 2.02, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 4.07, rel_tol=0.001) assert math.isclose(rate_pc.processor, 3.93, rel_tol=0.001) assert math.isclose(rate_pc.rating, 3.08, rel_tol=0.001)
def test_rate(): """Test generating an AggregateRate for a given PC / components / WorkbenchRate ensuring results and relationships between pc - rate - workbenchRate - price. """ rate = WorkbenchRate(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A) pc = Desktop(chassis=ComputerChassis.Tower) hdd = HardDrive(size=476940) hdd.events_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) cpu = Processor(cores=2, speed=3.4) cpu.events_one.add(BenchmarkProcessor(rate=27136.44)) pc.components |= { hdd, RamModule(size=4096, speed=1600), RamModule(size=2048, speed=1067), cpu } rate.device = pc events = main.main(rate, RatingSoftware.ECost, StrictVersion('1.0')) 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') assert price.refurbisher.standard.amount == Decimal('32.3853') assert price.price >= price.retailer.standard.amount \ + price.platform.standard.amount \ + price.refurbisher.standard.amount 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') # Checks relationships workbench_rate = next(e for e in events if isinstance(e, WorkbenchRate)) aggregate_rate = next(e for e in events if isinstance(e, AggregateRate)) assert price.rating == aggregate_rate assert aggregate_rate.workbench == workbench_rate assert aggregate_rate.rating == workbench_rate.rating == 4.61 assert aggregate_rate.software == workbench_rate.software == RatingSoftware.ECost assert aggregate_rate.version == StrictVersion('1.0') assert aggregate_rate.appearance == workbench_rate.appearance assert aggregate_rate.functionality == workbench_rate.functionality assert aggregate_rate.rating_range == workbench_rate.rating_range assert cpu.rate == pc.rate == hdd.rate == aggregate_rate assert cpu.price == pc.price == aggregate_rate.price == hdd.price == price
def test_get_tag_permissions(app: Devicehub, user: UserClient, user2: UserClient): """Creates a tag specifying a custom organization.""" with app.app_context(): # Create a pc with a tag g.user = User.query.all()[0] tag = Tag(id='foo-bar', owner_id=user.user['id']) pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) pc.tags.add(tag) db.session.add(pc) db.session.commit() computer, res = user.get(res=Tag, item='foo-bar/device') url = "/tags/?foo-bar/device" computer, res = user.get(url, None) computer2, res2 = user2.get(url, None) assert res.status_code == 200 assert res2.status_code == 200 assert len(computer['items']) == 2 assert len(computer2['items']) == 0
def test_delete_tags(user: UserClient, client: Client): """Delete a named tag.""" # Delete Tag Named g.user = User.query.one() pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) db.session.add(pc) db.session.commit() tag = Tag(id='bar', owner_id=user.user['id'], device_id=pc.id) db.session.add(tag) db.session.commit() tag = Tag.query.all()[-1] assert tag.id == 'bar' # Is not possible delete one tag linked to one device res, _ = user.delete(res=Tag, item=tag.id, status=422) msg = 'The tag bar is linked to device' assert msg in res['message'][0] tag.device_id = None db.session.add(tag) db.session.commit() # Is not possible delete one tag from an anonymous user client.delete(res=Tag, item=tag.id, status=401) # Is possible delete one normal tag user.delete(res=Tag, item=tag.id) user.get(res=Tag, item=tag.id, status=404) # Delete Tag UnNamed org = Organization(name='bar', tax_id='bartax') tag = Tag(id='bar-1', org=org, provider=URL('http://foo.bar'), owner_id=user.user['id']) db.session.add(tag) db.session.commit() tag = Tag.query.all()[-1] assert tag.id == 'bar-1' res, _ = user.delete(res=Tag, item=tag.id, status=422) msg = 'This tag {} is unnamed tag. It is imposible delete.'.format(tag.id) assert msg in res['message'] tag = Tag.query.all()[-1] assert tag.id == 'bar-1'
def test_device_model(): """ Tests that the correctness of the device model and its relationships. """ pc = Desktop(model='p1mo', manufacturer='p1ma', serial_number='p1s') net = NetworkAdapter(model='c1mo', manufacturer='c1ma', serial_number='c1s') graphic = GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500) pc.components.add(net) pc.components.add(graphic) db.session.add(pc) db.session.commit() pc = Desktop.query.one() assert pc.serial_number == 'p1s' assert pc.components == OrderedSet([net, graphic]) network_adapter = NetworkAdapter.query.one() assert network_adapter.parent == pc # Removing a component from pc doesn't delete the component pc.components.remove(net) db.session.commit() pc = Device.query.first() # this is the same as querying for Desktop directly assert pc.components == {graphic} network_adapter = NetworkAdapter.query.one() assert network_adapter not in pc.components assert network_adapter.parent is None # Deleting the pc deletes everything gcard = GraphicCard.query.one() db.session.delete(pc) db.session.flush() assert pc.id == 1 assert Desktop.query.first() is None db.session.commit() assert Desktop.query.first() is None assert network_adapter.id == 2 assert NetworkAdapter.query.first() is not None, 'We removed the network adaptor' assert gcard.id == 3, 'We should still hold a reference to a zombie graphic card' assert GraphicCard.query.first() is None, 'We should have deleted it –it was inside the pc'
def test_price_custom(): computer = Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', chassis=ComputerChassis.Docking) price = models.Price(price=Decimal(25.25), currency=Currency.EUR) price.device = computer assert computer.price == price db.session.add(computer) db.session.commit() client = UserClient(app, '*****@*****.**', 'foo', response_wrapper=app.response_class) client.login() p, _ = client.get(res=models.Event, item=str(price.id)) assert p['device']['id'] == price.device.id == computer.id assert p['price'] == 25.25 assert p['currency'] == Currency.EUR.name == 'EUR' c, _ = client.get(res=Device, item=computer.id) assert c['price']['id'] == p['id']
def test_rate(): """ Test main rate function """ pc = Desktop() hdd = HardDrive(size=476940) hdd.events_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) cpu = Processor(cores=2, speed=3.4) cpu.events_one.add(BenchmarkProcessor(rate=27136.44)) pc.components |= { hdd, RamModule(size=4096, speed=1600), RamModule(size=2048, speed=1067), cpu } rate = WorkbenchRate(appearance_range=AppearanceRange.A, software=RatingSoftware.ECost, version=StrictVersion('1.0'), functionality_range=FunctionalityRange.A) rate.algorithm_version = StrictVersion('1.0') main.rate(pc, rate) assert rate.rating == 4.61
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. This ensures that rates only takes all the correct actions and components rates in case device have new tests/benchmarks. """ 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 is None assert rate1.functionality is None assert rate1.rating == 3.92 assert price1.price == Decimal('78.4001') cpu.actions_one.add(BenchmarkProcessor(rate=16069.44)) ssd = SolidStateDrive(size=476940) ssd.actions_one.add(BenchmarkDataStorage(read_speed=222, write_speed=111)) pc.components |= { ssd, 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) assert rate2.data_storage == 4.3 assert rate2.processor == 3.78 assert rate2.ram == 3.95 assert rate2.appearance is None assert rate2.functionality is None assert rate2.rating == 3.93 assert price2.price == Decimal('78.6001')
def test_rate_computer_rate(): """ Test rate v1 pc_1193 = Computer() price = 92.2 # add components characteristics of pc with id = 1193 hdd_1969 = HardDrive(size=476940) hdd_1969.events_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) ram1 = RamModule(size=4096, speed=1600) ram2 = RamModule(size=2048, speed=1067) cpu = Processor(cores=2, speed=3.4) cpu.events_one.add(BenchmarkProcessor(rate=27136.44)) pc_1193.components.add(hdd_1969, ram1, ram2, cpu) # add functionality and appearance range rate_pc_1193 = WorkbenchRate(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 4.02 RAM_rate = 3.79 Processor_rate = 3.95 Rating = 4.61 pc_1201 = Computer() price = 69.6 hdd_3054 = HardDrive(size=476940) hdd_3054.events_one.add(BenchmarkDataStorage(read_speed=158, write_speed=34.7)) ram1 = RamModule(size=2048, speed=1333) cpu = Processor(cores=2, speed=3.3) cpu.events_one.add(BenchmarkProcessor(rate=26339.48)) pc_1201.components.add(hdd_3054, ram1, cpu) # add functionality and appearance range rate_pc_1201 = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 4.07 RAM_rate = 2.02 Processor_rate = 3.93 Rating = 3.48 pc_79 = Computer() price = VeryLow hdd_81 = HardDrive(size=76319) hdd_81.events_one.add(BenchmarkDataStorage(read_speed=72.2, write_speed=24.3)) ram1 = RamModule(size=512, speed=667) ram2 = RamModule(size=512, speed=800) ram3 = RamModule(size=512, speed=667) ram4 = RamModule(size=512, speed=533) cpu = Processor(cores=1, speed=1.6) cpu.events_one.add(BenchmarkProcessor(rate=3192.34)) pc_79.components.add(hdd_81, ram1, ram2, ram3, ram4, cpu) # add functionality and appearance range rate_pc_79 = WorkbenchRate(appearance_range=AppearanceRange.C, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 2.61 RAM_rate = 1.99 Processor_rate = 1 Rating = 1.58 pc_798 = Computer() price = 50 hdd_1556 = HardDrive(size=152587) hdd_1556.events_one.add(BenchmarkDataStorage(read_speed=78.1, write_speed=24.4)) ram0 = RamModule(size=0, speed=None) cpu = Processor(cores=2, speed=2.5) cpu.events_one.add(BenchmarkProcessor(rate=9974.3)) pc_798.components.add(hdd_1556, ram0, cpu) # add functionality and appearance range rate_pc_798 = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) # add component rate HDD_rate = 3.7 RAM_rate = 1 Processor_rate = 4.09 Rating = 2.5 """ # Create a new Computer with components characteristics of pc with id = 1193 pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=476940) data_storage.events_one.add(BenchmarkDataStorage(read_speed=126, write_speed=29.8)) cpu = Processor(cores=2, speed=3.4) cpu.events_one.add(BenchmarkProcessor(rate=27136.44)) pc_test.components |= { data_storage, RamModule(size=4096, speed=1600), RamModule(size=2048, speed=1067), cpu } # add functionality and appearance range rate_pc = WorkbenchRate(appearance_range=AppearanceRange.A, functionality_range=FunctionalityRange.A) # Compute all components rates and general rating Rate().compute(pc_test, rate_pc) assert math.isclose(rate_pc.ram, 3.79, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 4.02, rel_tol=0.001) assert math.isclose(rate_pc.processor, 3.95, rel_tol=0.001) assert math.isclose(rate_pc.rating, 4.61, rel_tol=0.001) # Create a new Computer with components characteristics of pc with id = 1201 pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=476940) data_storage.events_one.add(BenchmarkDataStorage(read_speed=158, write_speed=34.7)) cpu = Processor(cores=2, speed=3.3) cpu.events_one.add(BenchmarkProcessor(rate=26339.48)) pc_test.components |= { data_storage, RamModule(size=2048, speed=1333), cpu } # add functionality and appearance range rate_pc = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) # Compute all components rates and general rating Rate().compute(pc_test, rate_pc) assert math.isclose(rate_pc.ram, 2.02, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 4.07, rel_tol=0.001) assert math.isclose(rate_pc.processor, 3.93, rel_tol=0.001) assert math.isclose(rate_pc.rating, 3.48, rel_tol=0.001) # Create a new Computer with components characteristics of pc with id = 79 pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=76319) data_storage.events_one.add(BenchmarkDataStorage(read_speed=72.2, write_speed=24.3)) cpu = Processor(cores=1, speed=1.6) cpu.events_one.add(BenchmarkProcessor(rate=3192.34)) pc_test.components |= { data_storage, RamModule(size=512, speed=667), RamModule(size=512, speed=800), RamModule(size=512, speed=667), RamModule(size=512, speed=533), cpu } # add functionality and appearance range rate_pc = WorkbenchRate(appearance_range=AppearanceRange.C, functionality_range=FunctionalityRange.A) # Compute all components rates and general rating Rate().compute(pc_test, rate_pc) assert math.isclose(rate_pc.ram, 1.99, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 2.61, rel_tol=0.001) assert math.isclose(rate_pc.processor, 1, rel_tol=0.001) assert math.isclose(rate_pc.rating, 1.58, rel_tol=0.001) # Create a new Computer with components characteristics of pc with id = 798 pc_test = Desktop(chassis=ComputerChassis.Tower) data_storage = HardDrive(size=152587) data_storage.events_one.add(BenchmarkDataStorage(read_speed=78.1, write_speed=24.4)) cpu = Processor(cores=2, speed=2.5) cpu.events_one.add(BenchmarkProcessor(rate=9974.3)) pc_test.components |= { data_storage, RamModule(size=0, speed=None), cpu } # add functionality and appearance range rate_pc = WorkbenchRate(appearance_range=AppearanceRange.B, functionality_range=FunctionalityRange.A) # Compute all components rates and general rating Rate().compute(pc_test, rate_pc) assert math.isclose(rate_pc.ram, 1, rel_tol=0.001) assert math.isclose(rate_pc.data_storage, 3.7, rel_tol=0.001) assert math.isclose(rate_pc.processor, 4.09, rel_tol=0.001) assert math.isclose(rate_pc.rating, 2.5, rel_tol=0.001)