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_reports.py

170 lines
6.4 KiB
Python
Raw Normal View History

2018-10-18 15:36:14 +00:00
import csv
2018-10-25 10:40:58 +00:00
from datetime import datetime
from io import StringIO
from pathlib import Path
2018-10-18 15:36:14 +00:00
2018-10-11 16:19:33 +00:00
from ereuse_devicehub.client import UserClient
2019-03-06 17:44:21 +00:00
from ereuse_devicehub.resources.documents import documents
2018-10-11 16:19:33 +00:00
from ereuse_devicehub.resources.event.models import Snapshot
from tests.conftest import file
def test_export_basic_snapshot(user: UserClient):
"""
Test export device information in a csv file
"""
2018-10-11 16:19:33 +00:00
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
2019-03-06 17:44:21 +00:00
csv_str, _ = user.get(res=documents.DocumentDef.t,
2019-02-28 17:21:24 +00:00
item='devices/',
2018-10-25 10:40:58 +00:00
accept='text/csv',
query=[('filter', {'type': ['Computer']})])
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
2019-03-06 17:44:21 +00:00
# Open fixture csv and transform to list
with Path(__file__).parent.joinpath('files').joinpath('basic.csv').open() as csv_file:
obj_csv = csv.reader(csv_file)
fixture_csv = list(obj_csv)
2019-03-06 17:44:21 +00:00
assert isinstance(datetime.strptime(export_csv[1][8], '%c'), datetime), \
2018-10-25 10:40:58 +00:00
'Register in field is not a datetime'
2018-10-25 10:40:58 +00:00
# Pop dates fields from csv lists to compare them
2019-03-06 17:44:21 +00:00
fixture_csv[1] = fixture_csv[1][:8] + fixture_csv[1][9:]
export_csv[1] = export_csv[1][:8] + export_csv[1][9:]
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
assert fixture_csv[1] == export_csv[1], 'Computer information are not equal'
def test_export_full_snapshot(user: UserClient):
"""
Test a export device with all information and a lot of components
"""
snapshot, _ = user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot)
2019-03-06 17:44:21 +00:00
csv_str, _ = user.get(res=documents.DocumentDef.t,
2019-02-28 17:21:24 +00:00
item='devices/',
2018-10-25 10:40:58 +00:00
accept='text/csv',
query=[('filter', {'type': ['Computer']})])
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
2019-03-06 17:44:21 +00:00
# Open fixture csv and transform to list
2018-10-25 10:40:58 +00:00
with Path(__file__).parent.joinpath('files').joinpath('real-eee-1001pxd.csv').open() \
as csv_file:
obj_csv = csv.reader(csv_file)
fixture_csv = list(obj_csv)
2019-03-06 17:44:21 +00:00
assert isinstance(datetime.strptime(export_csv[1][8], '%c'), datetime), \
2018-10-25 10:40:58 +00:00
'Register in field is not a datetime'
# Pop dates fields from csv lists to compare them
2019-03-06 17:44:21 +00:00
fixture_csv[1] = fixture_csv[1][:8] + fixture_csv[1][9:]
export_csv[1] = export_csv[1][:8] + export_csv[1][9:]
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
assert fixture_csv[1] == export_csv[1], 'Computer information are not equal'
2018-10-11 16:19:33 +00:00
def test_export_empty(user: UserClient):
"""
Test to check works correctly exporting csv without any information (no snapshot)
"""
2019-03-06 17:44:21 +00:00
csv_str, _ = user.get(res=documents.DocumentDef.t,
2019-02-28 17:21:24 +00:00
accept='text/csv',
item='devices/')
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
assert len(export_csv) == 0, 'Csv is not empty'
def test_export_computer_monitor(user: UserClient):
"""
Test a export device type computer monitor
"""
snapshot, _ = user.post(file('computer-monitor.snapshot'), res=Snapshot)
2019-03-06 17:44:21 +00:00
csv_str, _ = user.get(res=documents.DocumentDef.t,
2019-02-28 17:21:24 +00:00
item='devices/',
2018-10-25 10:40:58 +00:00
accept='text/csv',
query=[('filter', {'type': ['ComputerMonitor']})])
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
# Open fixture csv and transform to list
2018-10-25 10:40:58 +00:00
with Path(__file__).parent.joinpath('files').joinpath('computer-monitor.csv').open() \
as csv_file:
obj_csv = csv.reader(csv_file)
fixture_csv = list(obj_csv)
2018-10-25 10:40:58 +00:00
# Pop dates fields from csv lists to compare them
fixture_csv[1] = fixture_csv[1][:8]
export_csv[1] = export_csv[1][:8]
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
assert fixture_csv[1] == export_csv[1], 'Component information are not equal'
def test_export_keyboard(user: UserClient):
"""
Test a export device type keyboard
"""
snapshot, _ = user.post(file('keyboard.snapshot'), res=Snapshot)
2019-03-06 17:44:21 +00:00
csv_str, _ = user.get(res=documents.DocumentDef.t,
2019-02-28 17:21:24 +00:00
item='devices/',
2018-10-25 10:40:58 +00:00
accept='text/csv',
query=[('filter', {'type': ['Keyboard']})])
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
# Open fixture csv and transform to list
with Path(__file__).parent.joinpath('files').joinpath('keyboard.csv').open() as csv_file:
obj_csv = csv.reader(csv_file)
fixture_csv = list(obj_csv)
2018-10-25 10:40:58 +00:00
# Pop dates fields from csv lists to compare them
fixture_csv[1] = fixture_csv[1][:8]
export_csv[1] = export_csv[1][:8]
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
assert fixture_csv[1] == export_csv[1], 'Component information are not equal'
def test_export_multiple_devices(user: UserClient):
"""
2019-03-06 17:44:21 +00:00
Test a export multiple devices (Computers and other types) with different information
"""
2019-03-06 17:44:21 +00:00
# Post all devices snapshots
snapshot_pc, _ = user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot)
snapshot_empty, _ = user.post(file('basic.snapshot'), res=Snapshot)
snapshot_keyboard, _ = user.post(file('keyboard.snapshot'), res=Snapshot)
snapshot_monitor, _ = user.post(file('computer-monitor.snapshot'), res=Snapshot)
2018-10-25 10:40:58 +00:00
2019-03-06 17:44:21 +00:00
# need query param??
csv_str, _ = user.get(res=documents.DocumentDef.t,
item='devices/',
accept='text/csv')
f = StringIO(csv_str)
obj_csv = csv.reader(f, f)
export_csv = list(obj_csv)
2018-10-25 10:40:58 +00:00
2019-03-06 17:44:21 +00:00
# Open fixture csv and transform to list
with Path(__file__).parent.joinpath('files').joinpath('multiples_devices.csv').open() \
as csv_file:
obj_csv = csv.reader(csv_file)
fixture_csv = list(obj_csv)
2018-10-25 10:40:58 +00:00
2019-03-06 17:44:21 +00:00
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
2018-10-25 10:40:58 +00:00
2019-03-06 17:44:21 +00:00
max_range = max(len(export_csv), len(fixture_csv)) - 1
# check if all devices information is correct
for i in range(1, max_range):
if isinstance(datetime.strptime(export_csv[i][8], '%c'), datetime):
export_csv[i] = export_csv[i][:8] + export_csv[i][9:]
fixture_csv[i] = fixture_csv[i][:8] + fixture_csv[i][9:]
assert fixture_csv[i] == export_csv[i], 'Some fields are not equal'