~stylesen/linaro-python-dashboard-bundle/evolve-bundle-stream-1.6

« back to all changes in this revision

Viewing changes to linaro_dashboard_bundle/tests.py

  • Committer: Zygmunt Krynicki
  • Date: 2010-11-15 10:04:04 UTC
  • Revision ID: zygmunt.krynicki@linaro.org-20101115100404-xa69upxqf6sbgz7m
Alpha release of linaro_dashboard_bundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Unit tests for DocumentLoader
 
3
"""
 
4
 
 
5
from decimal import Decimal
 
6
from StringIO import StringIO
 
7
 
 
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
 
12
 
 
13
from linaro_dashboard_bundle import (DocumentIO, DocumentFormatError)
 
14
 
 
15
class DocumentIOLoadTests(TestCase):
 
16
    """
 
17
    Various tests checking how DocumentIO load() and loads() operate
 
18
    """
 
19
 
 
20
    def setUp(self):
 
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"}
 
25
 
 
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)
 
30
 
 
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)
 
36
 
 
37
 
 
38
class DocumentIODumpTests(TestCase):
 
39
 
 
40
 
 
41
    def setUp(self):
 
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}'
 
45
 
 
46
    def test_dump_produces_output(self):
 
47
        stream = StringIO()
 
48
        DocumentIO.dump(stream, self.doc)
 
49
        self.assertEqual(stream.getvalue(), self.expected_json)
 
50
 
 
51
 
 
52
class DocumentIOParsingTests(TestCase):
 
53
 
 
54
    def test_loader_uses_decimal_to_parse_numbers(self):
 
55
        text = '''
 
56
        {
 
57
            "format": "Dashboard Bundle Format 1.0",
 
58
            "test_runs": [
 
59
                {
 
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,
 
64
                    "test_results": [
 
65
                        {
 
66
                            "test_case_id": "NOT RELEVANT",
 
67
                            "result": "unknown",
 
68
                            "measurement": 1.5
 
69
                        }
 
70
                    ]
 
71
                }
 
72
            ]
 
73
        }
 
74
        '''
 
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))
 
79
 
 
80
 
 
81
class DocumentIOCheckTests(TestCase):
 
82
 
 
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")
 
88
 
 
89
    def test_validator_finds_schema_mismatch(self):
 
90
        doc = {
 
91
            "format": "Dashboard Bundle Format 1.0",
 
92
            "property_that_does_not_belong_here": 1
 
93
        }
 
94
        self.assertRaises(ValidationError, DocumentIO.check, doc)
 
95
 
 
96
 
 
97
class DocumentIORegressionTests(TestWithScenarios, TestCase):
 
98
    """
 
99
    A set of tests ensuring that it's possible to load each of the file
 
100
    from the test_documents directory.
 
101
 
 
102
    Each test is defined as a scenario
 
103
    """
 
104
    scenarios = [
 
105
        ('smallest_bundle', {'filename': 'smallest_bundle.json'}),
 
106
        ('everything_in_one_bundle', {'filename': 'everything_in_one_bundle.json'}),
 
107
    ]
 
108
 
 
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)