コード例 #1
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_add_remove():
    # Original state:
    # pc has c1 and c2
    # pc2 has c3
    # c4 is not with any pc
    values = conftest.file('pc-components.db')
    pc = values['device']
    c1, c2 = (d.Component(**c) for c in values['components'])
    pc = d.Desktop(**pc, components=OrderedSet([c1, c2]))
    db.session.add(pc)
    c3 = d.Component(serial_number='nc1')
    pc2 = d.Desktop(serial_number='s2',
                    components=OrderedSet([c3]),
                    chassis=ComputerChassis.Microtower)
    c4 = d.Component(serial_number='c4s')
    db.session.add(pc2)
    db.session.add(c4)
    db.session.commit()

    # Test:
    # pc has only c3
    events = Sync.add_remove(device=pc, components={c3, c4})
    db.session.add_all(events)
    db.session.commit()  # We enforce the appliance of order_by
    assert len(events) == 1
    assert isinstance(events[0], Remove)
    assert events[0].device == pc2
    assert events[0].components == OrderedSet([c3])
コード例 #2
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_add_remove():
    # Original state:
    # pc has c1 and c2
    # pc2 has c3
    # c4 is not with any pc
    user = User.query.filter().first()
    values = yaml2json('pc-components.db')
    pc = values['device']
    c1, c2 = (d.Component(**c) for c in values['components'])
    pc = d.Desktop(**pc, components=OrderedSet([c1, c2]))
    db.session.add(pc)
    c3 = d.Component(serial_number='nc1', owner_id=user.id)
    pc2 = d.Desktop(serial_number='s2',
                    components=OrderedSet([c3]),
                    chassis=ComputerChassis.Microtower)
    c4 = d.Component(serial_number='c4s', owner_id=user.id)
    db.session.add(pc2)
    db.session.add(c4)
    db.session.commit()

    # Test:
    # pc has only c3
    actions = Sync.add_remove(device=pc, components={c3, c4})
    db.session.add_all(actions)
    db.session.commit()  # We enforce the appliance of order_by
    assert len(actions) == 1
    assert isinstance(actions[0], Remove)
    assert actions[0].device == pc2
    assert actions[0].components == OrderedSet([c3])
コード例 #3
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_get_devices(app: Devicehub, user: UserClient):
    """Checks GETting multiple devices."""
    with app.app_context():
        pc = d.Desktop(model='p1mo',
                       manufacturer='p1ma',
                       serial_number='p1s',
                       chassis=ComputerChassis.Tower)
        pc.components = OrderedSet([
            d.NetworkAdapter(model='c1mo',
                             manufacturer='c1ma',
                             serial_number='c1s'),
            d.GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500)
        ])
        pc1 = d.Desktop(model='p2mo',
                        manufacturer='p2ma',
                        serial_number='p2s',
                        chassis=ComputerChassis.Tower)
        pc2 = d.Laptop(model='p3mo',
                       manufacturer='p3ma',
                       serial_number='p3s',
                       chassis=ComputerChassis.Netbook)
        db.session.add_all((pc, pc1, pc2))
        db.session.commit()
    devices, _ = user.get(res=d.Device)
    assert tuple(dev['id'] for dev in devices['items']) == (1, 2, 3, 4, 5)
    assert tuple(
        dev['type']
        for dev in devices['items']) == (d.Desktop.t, d.Desktop.t, d.Laptop.t,
                                         d.NetworkAdapter.t, d.GraphicCard.t)
コード例 #4
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_sync_execute_register_desktop_existing_no_tag():
    """
    Syncs an existing d.Desktop with HID and without a tag.
    """
    pc = d.Desktop(**conftest.file('pc-components.db')['device'])
    db.session.add(pc)
    db.session.commit()

    pc = d.Desktop(**conftest.file('pc-components.db')
                   ['device'])  # Create a new transient non-db object
    # 1: device exists on DB
    db_pc = Sync().execute_register(pc)
    assert pc.physical_properties == db_pc.physical_properties
コード例 #5
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_desktop_existing_no_tag():
    """Syncs an existing d.Desktop with HID and without a tag."""
    pc = d.Desktop(**yaml2json('pc-components.db')['device'])
    db.session.add(pc)
    db.session.commit()

    pc = d.Desktop(**yaml2json('pc-components.db')
                   ['device'])  # Create a new transient non-db object
    # 1: device exists on DB
    db_pc = Sync().execute_register(pc)
    pc.amount = 0
    pc.owner_id = db_pc.owner_id
    pc.transfer_state = TransferState.Initial
    assert pc.physical_properties == db_pc.physical_properties
コード例 #6
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_component_similar_one():
    user = User.query.filter().first()
    snapshot = yaml2json('pc-components.db')
    pc = snapshot['device']
    snapshot['components'][0]['serial_number'] = snapshot['components'][1][
        'serial_number'] = None
    pc = d.Desktop(**pc,
                   components=OrderedSet(
                       d.Component(**c) for c in snapshot['components']))
    component1, component2 = pc.components  # type: d.Component
    db.session.add(pc)
    db.session.flush()
    # Let's create a new component named 'A' similar to 1
    componentA = d.Component(model=component1.model,
                             manufacturer=component1.manufacturer,
                             owner_id=user.id)
    similar_to_a = componentA.similar_one(pc, set())
    assert similar_to_a == component1
    # d.Component B does not have the same model
    componentB = d.Component(model='nope',
                             manufacturer=component1.manufacturer)
    with pytest.raises(ResourceNotFound):
        assert componentB.similar_one(pc, set())
    # If we blacklist component A we won't get anything
    with pytest.raises(ResourceNotFound):
        assert componentA.similar_one(pc, blacklist={componentA.id})
コード例 #7
0
def test_snapshot_model():
    """
    Tests creating a Snapshot with its relationships ensuring correct
    DB mapping.
    """
    device = m.Desktop(serial_number='a1', chassis=ComputerChassis.Tower)
    # noinspection PyArgumentList
    snapshot = Snapshot(uuid=uuid4(),
                        end_time=datetime.now(timezone.utc),
                        version='1.0',
                        software=SnapshotSoftware.DesktopApp,
                        elapsed=timedelta(seconds=25))
    snapshot.device = device
    snapshot.request = SnapshotRequest(request={'foo': 'bar'})
    db.session.add(snapshot)
    db.session.commit()
    device = m.Desktop.query.one()  # type: m.Desktop
    e1 = device.events[0]
    assert isinstance(e1, Snapshot), 'Creation order must be preserved: 1. snapshot, 2. WR'
    db.session.delete(device)
    db.session.commit()
    assert Snapshot.query.one_or_none() is None
    assert SnapshotRequest.query.one_or_none() is None
    assert User.query.one() is not None
    assert m.Desktop.query.one_or_none() is None
    assert m.Device.query.one_or_none() is None
    # Check properties
    assert device.url == urlutils.URL('http://localhost/devices/1')
コード例 #8
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_run_components_none():
    """Syncs a device that has a None components. The system should
    keep all the components from the device.
    """
    s = yaml2json('pc-components.db')
    pc = d.Desktop(**s['device'],
                   components=OrderedSet(
                       d.Component(**c) for c in s['components']))
    db.session.add(pc)
    db.session.commit()

    # Create a new transient non-db synced object
    transient_pc = d.Desktop(**s['device'])
    db_pc, _ = Sync().run(transient_pc, components=None)
    assert db_pc.components
    assert db_pc.components == pc.components
コード例 #9
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_run_components_empty():
    """Syncs a device that has an empty components list. The system should
    remove all the components from the device.
    """
    s = yaml2json('pc-components.db')
    pc = d.Desktop(**s['device'],
                   components=OrderedSet(
                       d.Component(**c) for c in s['components']))
    db.session.add(pc)
    db.session.commit()

    # Create a new transient non-db synced object
    pc = d.Desktop(**s['device'])
    db_pc, _ = Sync().run(pc, components=OrderedSet())
    assert not db_pc.components
    assert not pc.components
コード例 #10
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_tag_linked_same_device():
    """If the tag is linked to the device, regardless if it has HID,
    the system should match the device through the tag.
    (If it has HID it validates both HID and tag point at the same
    device, this his checked in ).
    """
    orig_pc = d.Desktop(**yaml2json('pc-components.db')['device'])
    db.session.add(Tag(id='foo', device=orig_pc))
    db.session.commit()

    pc = d.Desktop(**yaml2json('pc-components.db')
                   ['device'])  # Create a new transient non-db object
    pc.tags.add(Tag(id='foo'))
    db_pc = Sync().execute_register(pc)
    assert db_pc.id == orig_pc.id
    assert len(db_pc.tags) == 2
    for tag in db_pc.tags:
        assert tag.id in ['foo', db_pc.devicehub_id]
コード例 #11
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_sync_execute_register_desktop_new_desktop_no_tag():
    """
    Syncs a new d.Desktop with HID and without a tag, creating it.
    :return:
    """
    # Case 1: device does not exist on DB
    pc = d.Desktop(**conftest.file('pc-components.db')['device'])
    db_pc = Sync().execute_register(pc)
    assert pc.physical_properties == db_pc.physical_properties
コード例 #12
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_tag_linked_other_device_mismatch_between_tags():
    """Checks that sync raises an error if finds that at least two passed-in
    tags are not linked to the same device.
    """
    pc1 = d.Desktop(**yaml2json('pc-components.db')['device'])
    db.session.add(Tag(id='foo-1', device=pc1))
    pc2 = d.Desktop(**yaml2json('pc-components.db')['device'])
    pc2.serial_number = 'pc2-serial'
    pc2.hid = Naming.hid(pc2.type, pc2.manufacturer, pc2.model,
                         pc2.serial_number)
    db.session.add(Tag(id='foo-2', device=pc2))
    db.session.commit()

    pc1 = d.Desktop(**yaml2json('pc-components.db')
                    ['device'])  # Create a new transient non-db object
    pc1.tags.add(Tag(id='foo-1'))
    pc1.tags.add(Tag(id='foo-2'))
    with raises(MismatchBetweenTags):
        Sync().execute_register(pc1)
コード例 #13
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_mismatch_between_tags_and_hid():
    """Checks that sync raises an error if it finds that the HID does
    not point at the same device as the tag does.

    In this case we set HID -> pc1 but tag -> pc2
    """
    pc1 = d.Desktop(**yaml2json('pc-components.db')['device'])
    db.session.add(Tag(id='foo-1', device=pc1))
    pc2 = d.Desktop(**yaml2json('pc-components.db')['device'])
    pc2.serial_number = 'pc2-serial'
    pc2.hid = Naming.hid(pc2.type, pc2.manufacturer, pc2.model,
                         pc2.serial_number)
    db.session.add(Tag(id='foo-2', device=pc2))
    db.session.commit()

    pc1 = d.Desktop(**yaml2json('pc-components.db')
                    ['device'])  # Create a new transient non-db object
    pc1.tags.add(Tag(id='foo-2'))
    with raises(MismatchBetweenTagsAndHid):
        Sync().execute_register(pc1)
コード例 #14
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_desktop_no_hid_no_tag(user: UserClient):
    """Syncs a d.Desktop without HID and no tag.
    This should not fail as we don't have a way to identify it.
    """
    device = yaml2json('pc-components.db')['device']
    device['owner_id'] = user.user['id']
    pc = d.Desktop(**device)
    # 1: device has no HID
    pc.hid = pc.model = None
    returned_pc = Sync().execute_register(pc)
    assert returned_pc == pc
コード例 #15
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_sync_execute_register_desktop_no_hid_no_tag():
    """
    Syncs a d.Desktop without HID and no tag.

    This should fail as we don't have a way to identify it.
    """
    pc = d.Desktop(**conftest.file('pc-components.db')['device'])
    # 1: device has no HID
    pc.hid = pc.model = None
    with pytest.raises(NeedsId):
        Sync().execute_register(pc)
コード例 #16
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_sync_execute_register_tag_does_not_exist():
    """
    Ensures not being able to register if the tag does not exist,
    even if the device has HID or it existed before.

    Tags have to be created before trying to link them through a Snapshot.
    """
    pc = d.Desktop(**conftest.file('pc-components.db')['device'],
                   tags=OrderedSet([Tag('foo')]))
    with raises(ResourceNotFound):
        Sync().execute_register(pc)
コード例 #17
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_get_devices(app: Devicehub, user: UserClient):
    """Checks GETting multiple devices."""
    g.user = User.query.one()
    pc = d.Desktop(model='p1mo',
                   manufacturer='p1ma',
                   serial_number='p1s',
                   chassis=ComputerChassis.Tower,
                   owner_id=user.user['id'])
    pc.components = OrderedSet([
        d.NetworkAdapter(model='c1mo',
                         manufacturer='c1ma',
                         serial_number='c1s',
                         owner_id=user.user['id']),
        d.GraphicCard(model='c2mo',
                      manufacturer='c2ma',
                      memory=1500,
                      owner_id=user.user['id'])
    ])
    pc1 = d.Desktop(model='p2mo',
                    manufacturer='p2ma',
                    serial_number='p2s',
                    chassis=ComputerChassis.Tower,
                    owner_id=user.user['id'])
    pc2 = d.Laptop(model='p3mo',
                   manufacturer='p3ma',
                   serial_number='p3s',
                   chassis=ComputerChassis.Netbook,
                   owner_id=user.user['id'])
    db.session.add_all((pc, pc1, pc2))
    db.session.commit()
    devices, _ = user.get(res=d.Device)
    ids = (pc.id, pc1.id, pc2.id, pc.components[0].id, pc.components[1].id)
    assert tuple(dev['id'] for dev in devices['items']) == ids
    assert tuple(
        dev['type']
        for dev in devices['items']) == (d.Desktop.t, d.Desktop.t, d.Laptop.t,
                                         d.NetworkAdapter.t, d.GraphicCard.t)
コード例 #18
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_sync_execute_register_desktop_tag_not_linked():
    """Syncs a new d.Desktop with HID and a non-linked tag.

    It is OK if the tag was not linked, it will be linked in this process.
    """
    tag = Tag(id='foo')
    db.session.add(tag)
    db.session.commit()

    # Create a new transient non-db object
    pc = d.Desktop(**yaml2json('pc-components.db')['device'],
                   tags=OrderedSet([Tag(id='foo')]))
    returned_pc = Sync().execute_register(pc)
    assert returned_pc == pc
    assert tag.device == pc, 'Tag has to be linked'
    assert d.Desktop.query.one() == pc, 'd.Desktop had to be set to db'
コード例 #19
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_device_model():
    """
    Tests that the correctness of the device model and its relationships.
    """
    pc = d.Desktop(model='p1mo',
                   manufacturer='p1ma',
                   serial_number='p1s',
                   chassis=ComputerChassis.Tower)
    net = d.NetworkAdapter(model='c1mo',
                           manufacturer='c1ma',
                           serial_number='c1s')
    graphic = d.GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500)
    pc.components.add(net)
    pc.components.add(graphic)
    db.session.add(pc)
    db.session.commit()
    pc = d.Desktop.query.one()
    assert pc.serial_number == 'p1s'
    assert pc.components == OrderedSet([net, graphic])
    network_adapter = d.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 = d.Device.query.first(
    )  # this is the same as querying for d.Desktop directly
    assert pc.components == {graphic}
    network_adapter = d.NetworkAdapter.query.one()
    assert network_adapter not in pc.components
    assert network_adapter.parent is None

    # Deleting the pc deletes everything
    gcard = d.GraphicCard.query.one()
    db.session.delete(pc)
    db.session.flush()
    assert pc.id == 1
    assert d.Desktop.query.first() is None
    db.session.commit()
    assert d.Desktop.query.first() is None
    assert network_adapter.id == 2
    assert d.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 d.GraphicCard.query.first(
    ) is None, 'We should have deleted it –it was inside the pc'
コード例 #20
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_get_device(user: UserClient):
    """Checks GETting a d.Desktop with its components."""
    g.user = User.query.one()
    pc = d.Desktop(model='p1mo',
                   manufacturer='p1ma',
                   serial_number='p1s',
                   chassis=ComputerChassis.Tower,
                   owner_id=user.user['id'])
    pc.components = OrderedSet([
        d.NetworkAdapter(model='c1mo',
                         manufacturer='c1ma',
                         serial_number='c1s',
                         owner_id=user.user['id']),
        d.GraphicCard(model='c2mo',
                      manufacturer='c2ma',
                      memory=1500,
                      owner_id=user.user['id'])
    ])
    db.session.add(pc)
    # todo test is an abstract class. replace with another one
    db.session.add(
        TestConnectivity(device=pc,
                         severity=Severity.Info,
                         agent=Person(name='Timmy'),
                         author=User(email='*****@*****.**')))
    db.session.commit()
    pc_api, _ = user.get(res=d.Device, item=pc.devicehub_id)
    assert len(pc_api['actions']) == 1
    assert pc_api['actions'][0]['type'] == 'TestConnectivity'
    assert pc_api['actions'][0]['device'] == pc.id
    assert pc_api['actions'][0]['severity'] == 'Info'
    assert UUID(pc_api['actions'][0]['author'])
    assert 'actions_components' not in pc_api, 'actions_components are internal use only'
    assert 'actions_one' not in pc_api, 'they are internal use only'
    assert 'author' not in pc_api
    assert tuple(c['id'] for c in pc_api['components']) == tuple(
        c.id for c in pc.components)
    assert pc_api['hid'] == 'desktop-p1ma-p1mo-p1s'
    assert pc_api['model'] == 'p1mo'
    assert pc_api['manufacturer'] == 'p1ma'
    assert pc_api['serialNumber'] == 'p1s'
    assert pc_api['type'] == d.Desktop.t
コード例 #21
0
ファイル: test_device.py プロジェクト: eReuse/devicehub-teal
def test_physical_properties():
    c = d.Motherboard(slots=2,
                      usb=3,
                      serial_number='sn',
                      model='ml',
                      manufacturer='mr',
                      width=2.0,
                      color=Color())
    pc = d.Desktop(chassis=ComputerChassis.Tower,
                   model='foo',
                   manufacturer='bar',
                   serial_number='foo-bar',
                   weight=2.8,
                   width=1.4,
                   height=2.1,
                   color=Color('LightSeaGreen'))
    pc.components.add(c)
    db.session.add(pc)
    db.session.commit()
    assert c.physical_properties == {
        'usb': 3,
        'serial_number': 'sn',
        'pcmcia': None,
        'model': 'ml',
        'slots': 2,
        'serial': None,
        'firewire': None,
        'manufacturer': 'mr',
        'bios_date': None,
        'ram_max_size': None,
        'ram_slots': None
    }
    assert pc.physical_properties == {
        'chassis': ComputerChassis.Tower,
        'amount': 0,
        'manufacturer': 'bar',
        'model': 'foo',
        'receiver_id': None,
        'serial_number': 'foo-bar',
        'transfer_state': TransferState.Initial
    }
コード例 #22
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_sync_execute_register_no_hid_tag_not_linked(tag_id: str):
    """
    Validates registering a d.Desktop without HID and a non-linked tag.

    In this case it is ok still, as the non-linked tag proves that
    the d.Desktop was not existing before (otherwise the tag would
    be linked), and thus it creates a new d.Desktop.
    """
    tag = Tag(id=tag_id)
    pc = d.Desktop(**conftest.file('pc-components.db')['device'],
                   tags=OrderedSet([tag]))
    returned_pc = Sync().execute_register(pc)
    db.session.commit()
    assert returned_pc == pc
    db_tag = next(iter(returned_pc.tags))
    # they are not the same tags though
    # tag is a transient obj and db_tag the one from the db
    # they have the same pk though
    assert tag != db_tag, 'They are not the same tags though'
    assert db_tag.id == tag.id
    assert d.Desktop.query.one() == pc, 'd.Desktop had to be set to db'
コード例 #23
0
ファイル: test_device.py プロジェクト: slamora/devicehub-teal
def test_get_device(app: Devicehub, user: UserClient):
    """Checks GETting a d.Desktop with its components."""
    with app.app_context():
        pc = d.Desktop(model='p1mo',
                       manufacturer='p1ma',
                       serial_number='p1s',
                       chassis=ComputerChassis.Tower)
        pc.components = OrderedSet([
            d.NetworkAdapter(model='c1mo',
                             manufacturer='c1ma',
                             serial_number='c1s'),
            d.GraphicCard(model='c2mo', manufacturer='c2ma', memory=1500)
        ])
        db.session.add(pc)
        db.session.add(
            Test(device=pc,
                 elapsed=timedelta(seconds=4),
                 severity=Severity.Info,
                 agent=Person(name='Timmy'),
                 author=User(email='*****@*****.**')))
        db.session.commit()
    pc, _ = user.get(res=d.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 pc['events'][0]['severity'] == 'Info'
    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'] == 'desktop-p1ma-p1mo-p1s'
    assert pc['model'] == 'p1mo'
    assert pc['manufacturer'] == 'p1ma'
    assert pc['serialNumber'] == 'p1s'
    assert pc['type'] == d.Desktop.t