~cjwatson/python-oops/py3

« back to all changes in this revision

Viewing changes to oops/tests/test_publishers.py

  • Committer: James Westby
  • Date: 2012-06-26 21:14:45 UTC
  • mfrom: (28.1.13 refactor-publishing)
  • Revision ID: james.westby@canonical.com-20120626211445-tjwwenguepcwzig8
Refactor publication to better support publish_only_new.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
__metaclass__ = type
19
19
 
20
20
import testtools
21
 
from testtools.matchers import Is
22
 
 
23
 
from oops import publish_new_only
24
 
 
25
 
class TestPublishers(testtools.TestCase):
 
21
 
 
22
from oops import (
 
23
    convert_result_to_list,
 
24
    publish_new_only,
 
25
    publish_to_many,
 
26
    publish_with_fallback,
 
27
    )
 
28
 
 
29
 
 
30
class TestPublisherNewOnly(testtools.TestCase):
26
31
 
27
32
    def test_publish_new_only_id_in_report(self):
28
33
        def pub_fail(report):
35
40
        publisher = publish_new_only(calls.append)
36
41
        publisher({'foo': 'bar'})
37
42
        self.assertEqual([{'foo': 'bar'}], calls)
 
43
 
 
44
 
 
45
class TestPublishToMany(testtools.TestCase):
 
46
 
 
47
    def test_publishes_to_one(self):
 
48
        calls = []
 
49
        def capture(report):
 
50
            calls.append(report)
 
51
            return []
 
52
        publisher = publish_to_many(capture)
 
53
        publisher(dict(foo='bar'))
 
54
        self.assertEqual([dict(foo='bar')], calls)
 
55
 
 
56
    def test_publishes_to_two(self):
 
57
        calls1 = []
 
58
        calls2 = []
 
59
        def capture1(report):
 
60
            calls1.append(report)
 
61
            return []
 
62
        def capture2(report):
 
63
            calls2.append(report)
 
64
            return []
 
65
        publisher = publish_to_many(capture1, capture2)
 
66
        publisher(dict(foo='bar'))
 
67
        self.assertEqual([dict(foo='bar')], calls1)
 
68
        self.assertEqual([dict(foo='bar')], calls2)
 
69
 
 
70
    def test_returns_empty_list_with_no_publishers(self):
 
71
        publisher = publish_to_many()
 
72
        self.assertEqual([], publisher({}))
 
73
 
 
74
    def test_adds_ids_from_publishers(self):
 
75
        first_ids = ['a', 'b']
 
76
        def first_success(report):
 
77
            return first_ids
 
78
        second_ids = ['c', 'd']
 
79
        def second_success(report):
 
80
            return second_ids
 
81
        publisher = publish_to_many(first_success, second_success)
 
82
        self.assertEqual(first_ids + second_ids, publisher({}))
 
83
 
 
84
    def test_puts_id_in_report(self):
 
85
        the_id = 'b'
 
86
        def first(report):
 
87
            return ['a', the_id]
 
88
        def second(report):
 
89
            self.assertEqual(the_id, report['id'])
 
90
            return []
 
91
        publisher = publish_to_many(first, second)
 
92
        publisher({})
 
93
 
 
94
    def test_puts_nothing_in_report_for_unpublished(self):
 
95
        def first(report):
 
96
            return []
 
97
        def second(report):
 
98
            if 'id' in report:
 
99
                self.fail("id set to %s when previous publisher "
 
100
                    "didn't publish" % report['id'])
 
101
            return []
 
102
        publisher = publish_to_many(first, second)
 
103
        publisher({})
 
104
 
 
105
 
 
106
class TestPublishWithFallback(testtools.TestCase):
 
107
 
 
108
    def test_publishes_to_one(self):
 
109
        calls = []
 
110
        def capture(report):
 
111
            calls.append(report)
 
112
            return []
 
113
        publisher = publish_with_fallback(capture)
 
114
        publisher(dict(foo='bar'))
 
115
        self.assertEqual([dict(foo='bar')], calls)
 
116
 
 
117
    def test_publishes_to_two(self):
 
118
        calls1 = []
 
119
        calls2 = []
 
120
        def capture1(report):
 
121
            calls1.append(report)
 
122
            return []
 
123
        def capture2(report):
 
124
            calls2.append(report)
 
125
            return []
 
126
        publisher = publish_with_fallback(capture1, capture2)
 
127
        publisher(dict(foo='bar'))
 
128
        self.assertEqual([dict(foo='bar')], calls1)
 
129
        self.assertEqual([dict(foo='bar')], calls2)
 
130
 
 
131
    def test_returns_ids_from_publisher(self):
 
132
        ids = ['a', 'b']
 
133
        def success(report):
 
134
            return ids
 
135
        publisher = publish_with_fallback(success)
 
136
        self.assertEqual(ids, publisher({}))
 
137
 
 
138
    def test_publishes_new(self):
 
139
        def failure(report):
 
140
            return []
 
141
        calls = []
 
142
        def capture(report):
 
143
            calls.append(report)
 
144
            return []
 
145
        publisher = publish_with_fallback(failure, capture)
 
146
        self.assertEqual([], publisher({}))
 
147
        self.assertEqual(1, len(calls))
 
148
 
 
149
    def test_publish_stops_when_a_publisher_succeeds(self):
 
150
        def success(report):
 
151
            return ['the id']
 
152
        def fail(report):
 
153
            self.fail("Called fallback when primary succeeded.")
 
154
        publisher = publish_with_fallback(success, fail)
 
155
        self.assertEqual(['the id'], publisher({}))
 
156
 
 
157
 
 
158
class ConvertResultToListTests(testtools.TestCase):
 
159
 
 
160
    def test_converts_False_to_empty_list(self):
 
161
        # A false-ish value gets turned in to an empty list
 
162
        def falseish(report):
 
163
            return False
 
164
        self.assertEqual([], convert_result_to_list(falseish)({}))
 
165
 
 
166
    def test_converts_True_to_list(self):
 
167
        # A true-ish value gets turned in to an empty list
 
168
        def trueish(report):
 
169
            return "aaa"
 
170
        self.assertEqual(["aaa"], convert_result_to_list(trueish)({}))