summaryrefslogtreecommitdiff
path: root/tests/test_report.py
blob: 423082f680750f21f9c0fc85ed6dcfbe12c33c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import unittest
from unittest.mock import patch
from io import StringIO


from squad_client.report import Report, ReportContext, ReportGenerator
from squad_client.exceptions import InvalidSquadObject


class ReportContextTest(unittest.TestCase):
    def test_basics(self):
        context = {
            'var1': {
                'type': 'Build',
                'filters': {
                    'param1': 'val1'
                }
            }
        }

        report_context = ReportContext(context)
        self.assertEqual(1, len(report_context.context))

        c = report_context.context[0]
        self.assertEqual('var1', c.name)
        self.assertEqual('Build', c.type)
        self.assertEqual({'param1': 'val1'}, c.filters)

    def test_invalid_object_type(self):
        context = {
            'var1': {
                'type': 'InvalidType',
            }
        }
        report_context = ReportContext(context)
        with self.assertRaises(InvalidSquadObject):
            report_context.fill()


class ReportTest(unittest.TestCase):

    def test_basic_report_generation(self):
        template = 'This is the most basic template'
        report = Report(template)
        generated = report.generate()
        self.assertEqual(template, generated)

    @patch('squad_client.core.models.Squad.fetch')
    def test_basic_report_generation_with_context(self, squad_fetch):
        squad_fetch.return_value = 'fetched string'
        template = 'Report: {{ dummy }}'
        context = ReportContext({
            'dummy': {
                'type': 'Test',
                'filters': {
                    'param1': 'val1'
                }
            }
        })
        report = Report(template, context=context)
        generated = report.generate()
        self.assertEqual('Report: fetched string', generated)


class ReportGeneratorTest(unittest.TestCase):

    @patch('squad_client.core.models.Squad.fetch')
    def test_basics(self, squad_fetch):
        squad_fetch.return_value = 'fetched string'
        template = 'Report: {{ dummy }}'
        output = StringIO()
        context = {
            'dummy': {
                'type': 'Test',
                'filters': {
                    'param1': 'val1'
                }
            }
        }
        generator = ReportGenerator('http://example.com')
        generator.add_report('dummy report', template, output=output, context=context)
        reports = generator.generate()

        self.assertEqual(1, len(reports))

        self.assertEqual('Report: fetched string', output.getvalue())