2018-08-08 19:25:53 +00:00
|
|
|
import pytest
|
|
|
|
from flask import g
|
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
from ereuse_devicehub.client import UserClient
|
2018-08-08 19:25:53 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2018-09-20 16:25:47 +00:00
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2018-10-08 15:32:45 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Desktop, Device, GraphicCard
|
2018-08-08 19:25:53 +00:00
|
|
|
from ereuse_devicehub.resources.enums import ComputerChassis
|
2018-08-09 19:46:54 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, LotDevice
|
2018-08-08 19:25:53 +00:00
|
|
|
from tests import conftest
|
|
|
|
|
2018-08-09 19:46:54 +00:00
|
|
|
"""
|
|
|
|
In case of error, debug with:
|
|
|
|
|
|
|
|
try:
|
|
|
|
with db.session.begin_nested():
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
db.session.commit()
|
|
|
|
print(e)
|
|
|
|
a=1
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-11-11 20:52:55 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
|
|
|
def test_lot_model_children():
|
|
|
|
"""Tests the property Lot.children
|
|
|
|
|
|
|
|
l1
|
|
|
|
|
|
|
|
|
l2
|
|
|
|
|
|
|
|
|
l3
|
|
|
|
"""
|
|
|
|
lots = Lot('1'), Lot('2'), Lot('3')
|
|
|
|
l1, l2, l3 = lots
|
|
|
|
db.session.add_all(lots)
|
|
|
|
db.session.flush()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert not l1.children
|
|
|
|
assert not l1.parents
|
|
|
|
assert not l2.children
|
|
|
|
assert not l2.parents
|
|
|
|
assert not l3.parents
|
|
|
|
assert not l3.children
|
|
|
|
|
|
|
|
l1.add_children(l2)
|
|
|
|
assert l1.children == {l2}
|
|
|
|
assert l2.parents == {l1}
|
|
|
|
|
|
|
|
l2.add_children(l3)
|
|
|
|
assert l1.children == {l2}
|
|
|
|
assert l2.parents == {l1}
|
|
|
|
assert l2.children == {l3}
|
|
|
|
assert l3.parents == {l2}
|
2018-11-11 20:52:55 +00:00
|
|
|
|
|
|
|
l2.delete()
|
|
|
|
db.session.flush()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert not l1.children
|
|
|
|
assert not l3.parents
|
2018-11-11 20:52:55 +00:00
|
|
|
|
|
|
|
l1.delete()
|
|
|
|
db.session.flush()
|
|
|
|
l3b = Lot.query.one()
|
|
|
|
assert l3 == l3b
|
2018-11-13 14:52:27 +00:00
|
|
|
assert not l3.parents
|
2018-11-11 20:52:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_lot_modify_patch_endpoint_and_delete(user: UserClient):
|
2018-10-19 08:35:23 +00:00
|
|
|
"""Creates and modifies lot properties through the endpoint"""
|
2018-11-06 17:08:57 +00:00
|
|
|
l, _ = user.post({'name': 'foo', 'description': 'baz'}, res=Lot)
|
2018-10-19 08:35:23 +00:00
|
|
|
assert l['name'] == 'foo'
|
2018-11-06 17:08:57 +00:00
|
|
|
assert l['description'] == 'baz'
|
|
|
|
user.patch({'name': 'bar', 'description': 'bax'}, res=Lot, item=l['id'], status=204)
|
2018-10-19 08:35:23 +00:00
|
|
|
l_after, _ = user.get(res=Lot, item=l['id'])
|
|
|
|
assert l_after['name'] == 'bar'
|
2018-11-06 17:08:57 +00:00
|
|
|
assert l_after['description'] == 'bax'
|
2019-02-07 12:47:42 +00:00
|
|
|
user.patch({'description': 'bax'}, res=Lot, item=l['id'], status=204)
|
2018-11-11 20:52:55 +00:00
|
|
|
user.delete(res=Lot, item=l['id'], status=204)
|
|
|
|
user.get(res=Lot, item=l['id'], status=404)
|
2018-11-06 17:08:57 +00:00
|
|
|
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
|
|
|
def test_lot_device_relationship():
|
|
|
|
device = Desktop(serial_number='foo',
|
|
|
|
model='bar',
|
|
|
|
manufacturer='foobar',
|
|
|
|
chassis=ComputerChassis.Lunchbox)
|
2018-11-11 20:52:55 +00:00
|
|
|
device.components.add(GraphicCard(serial_number='foo', model='bar1', manufacturer='baz'))
|
2018-11-04 21:40:14 +00:00
|
|
|
child = Lot('child')
|
|
|
|
child.devices.add(device)
|
|
|
|
db.session.add(child)
|
2018-08-08 19:25:53 +00:00
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
lot_device = LotDevice.query.one() # type: LotDevice
|
|
|
|
assert lot_device.device_id == device.id
|
2018-11-04 21:40:14 +00:00
|
|
|
assert lot_device.lot_id == child.id
|
2018-08-08 19:25:53 +00:00
|
|
|
assert lot_device.created
|
|
|
|
assert lot_device.author_id == g.user.id
|
2018-11-04 21:40:14 +00:00
|
|
|
assert device.lots == {child}
|
|
|
|
assert device in child
|
2018-11-13 14:52:27 +00:00
|
|
|
assert device in child.all_devices
|
2018-10-08 15:32:45 +00:00
|
|
|
|
|
|
|
graphic = GraphicCard(serial_number='foo', model='bar')
|
|
|
|
device.components.add(graphic)
|
|
|
|
db.session.flush()
|
2018-11-04 21:40:14 +00:00
|
|
|
assert graphic in child
|
|
|
|
|
|
|
|
parent = Lot('parent')
|
|
|
|
db.session.add(parent)
|
|
|
|
db.session.flush()
|
2018-11-13 14:52:27 +00:00
|
|
|
parent.add_children(child)
|
2018-11-04 21:40:14 +00:00
|
|
|
assert child in parent
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
|
|
|
def test_add_edge():
|
2018-08-09 19:46:54 +00:00
|
|
|
"""Tests creating an edge between child - parent - grandparent."""
|
|
|
|
child = Lot('child')
|
|
|
|
parent = Lot('parent')
|
2018-08-08 19:25:53 +00:00
|
|
|
db.session.add(child)
|
|
|
|
db.session.add(parent)
|
|
|
|
db.session.flush()
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
parent.add_children(child)
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
assert child in parent
|
2018-08-27 14:32:45 +00:00
|
|
|
assert len(child.paths) == 1
|
|
|
|
assert len(parent.paths) == 1
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
parent.remove_children(child)
|
2018-08-08 19:25:53 +00:00
|
|
|
assert child not in parent
|
2018-08-27 14:32:45 +00:00
|
|
|
assert len(child.paths) == 1
|
|
|
|
assert len(parent.paths) == 1
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-08-09 19:46:54 +00:00
|
|
|
grandparent = Lot('grandparent')
|
2018-08-08 19:25:53 +00:00
|
|
|
db.session.add(grandparent)
|
|
|
|
db.session.flush()
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
grandparent.add_children(parent)
|
|
|
|
parent.add_children(child)
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
assert parent in grandparent
|
|
|
|
assert child in parent
|
|
|
|
assert child in grandparent
|
2018-08-09 19:46:54 +00:00
|
|
|
|
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
def test_lot_multiple_parents(auth_app_context):
|
2018-08-09 19:46:54 +00:00
|
|
|
"""Tests creating a lot with two parent lots:
|
|
|
|
|
|
|
|
grandparent1 grandparent2
|
|
|
|
\ /
|
|
|
|
parent
|
|
|
|
|
|
|
|
|
child
|
|
|
|
"""
|
|
|
|
lots = Lot('child'), Lot('parent'), Lot('grandparent1'), Lot('grandparent2')
|
|
|
|
child, parent, grandparent1, grandparent2 = lots
|
|
|
|
db.session.add_all(lots)
|
|
|
|
db.session.flush()
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
grandparent1.add_children(parent)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert parent in grandparent1
|
2018-11-13 14:52:27 +00:00
|
|
|
parent.add_children(child)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert child in parent
|
|
|
|
assert child in grandparent1
|
2018-11-13 14:52:27 +00:00
|
|
|
grandparent2.add_children(parent)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert parent in grandparent1
|
|
|
|
assert parent in grandparent2
|
|
|
|
assert child in parent
|
|
|
|
assert child in grandparent1
|
|
|
|
assert child in grandparent2
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
p = parent.id
|
|
|
|
c = child.id
|
|
|
|
gp1 = grandparent1.id
|
|
|
|
gp2 = grandparent2.id
|
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
nodes = auth_app_context.resources[Lot.t].VIEW.ui_tree()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[0]['id'] == gp1
|
|
|
|
assert nodes[0]['nodes'][0]['id'] == p
|
|
|
|
assert nodes[0]['nodes'][0]['nodes'][0]['id'] == c
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[0]['nodes'][0]['nodes'][0]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[1]['id'] == gp2
|
|
|
|
assert nodes[1]['nodes'][0]['id'] == p
|
|
|
|
assert nodes[1]['nodes'][0]['nodes'][0]['id'] == c
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[1]['nodes'][0]['nodes'][0]['nodes'] == []
|
|
|
|
|
|
|
|
# Now remove all childs
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
grandparent1.remove_children(parent)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert parent not in grandparent1
|
|
|
|
assert child in parent
|
|
|
|
assert parent in grandparent2
|
|
|
|
assert child not in grandparent1
|
|
|
|
assert child in grandparent2
|
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
nodes = auth_app_context.resources[Lot.t].VIEW.ui_tree()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[0]['id'] == gp1
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[0]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[1]['id'] == gp2
|
|
|
|
assert nodes[1]['nodes'][0]['id'] == p
|
|
|
|
assert nodes[1]['nodes'][0]['nodes'][0]['id'] == c
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[1]['nodes'][0]['nodes'][0]['nodes'] == []
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
grandparent2.remove_children(parent)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert parent not in grandparent2
|
|
|
|
assert parent not in grandparent1
|
|
|
|
assert child not in grandparent2
|
|
|
|
assert child not in grandparent1
|
|
|
|
assert child in parent
|
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
nodes = auth_app_context.resources[Lot.t].VIEW.ui_tree()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[0]['id'] == gp1
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[0]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[1]['id'] == gp2
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[1]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[2]['id'] == p
|
|
|
|
assert nodes[2]['nodes'][0]['id'] == c
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[2]['nodes'][0]['nodes'] == []
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
parent.remove_children(child)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert child not in parent
|
2018-08-27 14:32:45 +00:00
|
|
|
assert len(child.paths) == 1
|
|
|
|
assert len(parent.paths) == 1
|
2018-08-09 19:46:54 +00:00
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
nodes = auth_app_context.resources[Lot.t].VIEW.ui_tree()
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[0]['id'] == gp1
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[0]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[1]['id'] == gp2
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[1]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[2]['id'] == p
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[2]['nodes'] == []
|
2018-11-13 14:52:27 +00:00
|
|
|
assert nodes[3]['id'] == c
|
2018-10-11 09:22:59 +00:00
|
|
|
assert nodes[3]['nodes'] == []
|
|
|
|
|
2018-08-09 19:46:54 +00:00
|
|
|
|
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-10-05 16:57:35 +00:00
|
|
|
def test_lot_unite_graphs_and_find():
|
2018-08-09 19:46:54 +00:00
|
|
|
"""Adds and removes children uniting already existing graphs.
|
|
|
|
|
|
|
|
1 3
|
|
|
|
\/
|
|
|
|
2
|
|
|
|
|
|
|
|
4
|
|
|
|
| \
|
|
|
|
| 6
|
|
|
|
\ /
|
|
|
|
5
|
|
|
|
| \
|
|
|
|
7 8
|
|
|
|
|
|
|
|
This builds the graph and then unites 2 - 4.
|
|
|
|
"""
|
|
|
|
|
|
|
|
lots = tuple(Lot(str(i)) for i in range(1, 9))
|
|
|
|
l1, l2, l3, l4, l5, l6, l7, l8 = lots
|
|
|
|
db.session.add_all(lots)
|
|
|
|
db.session.flush()
|
|
|
|
|
2018-11-13 14:52:27 +00:00
|
|
|
l1.add_children(l2)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l2 in l1
|
2018-11-13 14:52:27 +00:00
|
|
|
l3.add_children(l2)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l2 in l3
|
2018-11-13 14:52:27 +00:00
|
|
|
l5.add_children(l7)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l7 in l5
|
2018-11-13 14:52:27 +00:00
|
|
|
l4.add_children(l5)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l5 in l4
|
|
|
|
assert l7 in l4
|
2018-11-13 14:52:27 +00:00
|
|
|
l5.add_children(l8)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l8 in l5
|
2018-11-13 14:52:27 +00:00
|
|
|
l4.add_children(l6)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l6 in l4
|
2018-11-13 14:52:27 +00:00
|
|
|
l6.add_children(l5)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l5 in l6 and l5 in l4
|
|
|
|
|
|
|
|
# We unite the two graphs
|
2018-11-13 14:52:27 +00:00
|
|
|
l2.add_children(l4)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l4 in l2 and l5 in l2 and l6 in l2 and l7 in l2 and l8 in l2
|
|
|
|
assert l4 in l3 and l5 in l3 and l6 in l3 and l7 in l3 and l8 in l3
|
|
|
|
|
|
|
|
# We remove the union
|
2018-11-13 14:52:27 +00:00
|
|
|
l2.remove_children(l4)
|
2018-08-09 19:46:54 +00:00
|
|
|
assert l4 not in l2 and l5 not in l2 and l6 not in l2 and l7 not in l2 and l8 not in l2
|
|
|
|
assert l4 not in l3 and l5 not in l3 and l6 not in l3 and l7 not in l3 and l8 not in l3
|
2018-08-09 22:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
|
|
|
def test_lot_roots():
|
|
|
|
"""Tests getting the method Lot.roots."""
|
|
|
|
lots = Lot('1'), Lot('2'), Lot('3')
|
|
|
|
l1, l2, l3 = lots
|
|
|
|
db.session.add_all(lots)
|
|
|
|
db.session.flush()
|
|
|
|
|
2018-09-11 20:51:13 +00:00
|
|
|
assert set(Lot.roots()) == {l1, l2, l3}
|
2018-11-13 14:52:27 +00:00
|
|
|
l1.add_children(l2)
|
2018-09-11 20:51:13 +00:00
|
|
|
assert set(Lot.roots()) == {l1, l3}
|
2018-09-11 19:50:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_post_get_lot(user: UserClient):
|
|
|
|
"""Tests submitting and retreiving a basic lot."""
|
|
|
|
l, _ = user.post({'name': 'Foo'}, res=Lot)
|
|
|
|
assert l['name'] == 'Foo'
|
|
|
|
l, _ = user.get(res=Lot, item=l['id'])
|
|
|
|
assert l['name'] == 'Foo'
|
|
|
|
assert not l['children']
|
|
|
|
|
|
|
|
|
2018-10-14 09:37:10 +00:00
|
|
|
def test_lot_post_add_children_view_ui_tree_normal(user: UserClient):
|
2018-10-08 15:32:45 +00:00
|
|
|
"""Tests adding children lots to a lot through the view and
|
|
|
|
GETting the results."""
|
2018-09-12 12:53:14 +00:00
|
|
|
parent, _ = user.post(({'name': 'Parent'}), res=Lot)
|
2018-09-11 19:50:40 +00:00
|
|
|
child, _ = user.post(({'name': 'Child'}), res=Lot)
|
2018-09-12 12:53:14 +00:00
|
|
|
parent, _ = user.post({},
|
|
|
|
res=Lot,
|
|
|
|
item='{}/children'.format(parent['id']),
|
|
|
|
query=[('id', child['id'])])
|
|
|
|
assert parent['children'][0]['id'] == child['id']
|
|
|
|
child, _ = user.get(res=Lot, item=child['id'])
|
|
|
|
assert child['parents'][0]['id'] == parent['id']
|
2018-10-05 16:57:35 +00:00
|
|
|
|
2018-10-08 15:32:45 +00:00
|
|
|
# Format UiTree
|
2018-11-13 14:52:27 +00:00
|
|
|
r = user.get(res=Lot, query=[('format', 'UiTree')])[0]
|
|
|
|
lots, nodes = r['items'], r['tree']
|
|
|
|
assert 1 == len(nodes)
|
|
|
|
assert nodes[0]['id'] == parent['id']
|
|
|
|
assert len(nodes[0]['nodes']) == 1
|
|
|
|
assert nodes[0]['nodes'][0]['id'] == child['id']
|
|
|
|
assert 2 == len(lots)
|
|
|
|
assert 'Parent' == lots[parent['id']]['name']
|
|
|
|
assert 'Child' == lots[child['id']]['name']
|
|
|
|
assert lots[child['id']]['parents'][0]['name'] == 'Parent'
|
2018-09-20 16:25:47 +00:00
|
|
|
|
2018-10-08 15:32:45 +00:00
|
|
|
# Normal list format
|
|
|
|
lots = user.get(res=Lot)[0]['items']
|
2018-10-31 10:40:45 +00:00
|
|
|
assert 2 == len(lots)
|
2018-10-08 15:32:45 +00:00
|
|
|
assert lots[0]['name'] == 'Parent'
|
|
|
|
assert lots[1]['name'] == 'Child'
|
|
|
|
|
|
|
|
# List format with a filter
|
2018-10-08 15:51:51 +00:00
|
|
|
lots = user.get(res=Lot, query=[('search', 'pa')])[0]['items']
|
2018-10-31 10:40:45 +00:00
|
|
|
assert 1 == len(lots)
|
2018-10-08 15:32:45 +00:00
|
|
|
assert lots[0]['name'] == 'Parent'
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
|
|
|
|
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."""
|
2018-10-08 15:32:45 +00:00
|
|
|
# todo check with components
|
2018-09-20 16:25:47 +00:00
|
|
|
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)])
|
2018-10-08 15:32:45 +00:00
|
|
|
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'
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
# Remove the device
|
|
|
|
lot, _ = user.delete(res=Lot,
|
|
|
|
item='{}/devices'.format(parent['id']),
|
|
|
|
query=[('id', device_id)],
|
|
|
|
status=200)
|
|
|
|
assert not len(lot['devices'])
|