2
Unit tests for DocumentLoader
5
from decimal import Decimal
6
from StringIO import StringIO
8
from linaro_json import ValidationError
9
from pkg_resources import (resource_string, resource_stream)
10
from testscenarios import TestWithScenarios
11
from testtools import TestCase
13
from linaro_dashboard_bundle import (DocumentIO, DocumentFormatError)
15
class DocumentIOLoadTests(TestCase):
17
Various tests checking how DocumentIO load() and loads() operate
21
super(DocumentIOLoadTests, self).setUp()
22
self.text = '{"format": "Dashboard Bundle Format 1.0"}'
23
self.expected_fmt = "Dashboard Bundle Format 1.0"
24
self.expected_doc = {"format": "Dashboard Bundle Format 1.0"}
26
def test_loads_return_value(self):
27
fmt, doc = DocumentIO.loads(self.text)
28
self.assertEqual(fmt, self.expected_fmt)
29
self.assertEqual(doc, self.expected_doc)
31
def test_load_return_value(self):
32
stream = StringIO(self.text)
33
fmt, doc = DocumentIO.load(stream)
34
self.assertEqual(fmt, self.expected_fmt)
35
self.assertEqual(doc, self.expected_doc)
38
class DocumentIODumpTests(TestCase):
42
super(DocumentIODumpTests, self).setUp()
43
self.doc = {"format": "Dashboard Bundle Format 1.0"}
44
self.expected_json = '{\n "format": "Dashboard Bundle Format 1.0"\n}'
46
def test_dump_produces_output(self):
48
DocumentIO.dump(stream, self.doc)
49
self.assertEqual(stream.getvalue(), self.expected_json)
52
class DocumentIOParsingTests(TestCase):
54
def test_loader_uses_decimal_to_parse_numbers(self):
57
"format": "Dashboard Bundle Format 1.0",
60
"test_id": "NOT RELEVANT",
61
"analyzer_assigned_date": "2010-11-14T01:03:06Z",
62
"analyzer_assigned_uuid": "NOT RELEVANT",
63
"time_check_performed": false,
66
"test_case_id": "NOT RELEVANT",
75
fmt, doc = DocumentIO.loads(text)
76
measurement = doc["test_runs"][0]["test_results"][0]["measurement"]
77
self.assertEqual(measurement, Decimal("1.5"))
78
self.assertTrue(isinstance(measurement, Decimal))
81
class DocumentIOCheckTests(TestCase):
83
def test_unknown_format_raises_DocumentFormatError(self):
84
doc = {"format": "Bad Format"}
85
ex = self.assertRaises(DocumentFormatError, DocumentIO.check, doc)
86
self.assertEqual(str(ex), "Unrecognized or missing document format")
87
self.assertEqual(ex.format, "Bad Format")
89
def test_validator_finds_schema_mismatch(self):
91
"format": "Dashboard Bundle Format 1.0",
92
"property_that_does_not_belong_here": 1
94
self.assertRaises(ValidationError, DocumentIO.check, doc)
97
class DocumentIORegressionTests(TestWithScenarios, TestCase):
99
A set of tests ensuring that it's possible to load each of the file
100
from the test_documents directory.
102
Each test is defined as a scenario
105
('smallest_bundle', {'filename': 'smallest_bundle.json'}),
106
('everything_in_one_bundle', {'filename': 'everything_in_one_bundle.json'}),
109
def test_load_document(self):
110
# Note: resource_string uses posix-style paths
111
# regardless of the actual system paths
112
fmt, doc = DocumentIO.load(
113
resource_stream('linaro_dashboard_bundle',
114
'test_documents/' + self.filename))
115
self.assertIsNot(doc, None)