35
40
publisher = publish_new_only(calls.append)
36
41
publisher({'foo': 'bar'})
37
42
self.assertEqual([{'foo': 'bar'}], calls)
45
class TestPublishToMany(testtools.TestCase):
47
def test_publishes_to_one(self):
52
publisher = publish_to_many(capture)
53
publisher(dict(foo='bar'))
54
self.assertEqual([dict(foo='bar')], calls)
56
def test_publishes_to_two(self):
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)
70
def test_returns_empty_list_with_no_publishers(self):
71
publisher = publish_to_many()
72
self.assertEqual([], publisher({}))
74
def test_adds_ids_from_publishers(self):
75
first_ids = ['a', 'b']
76
def first_success(report):
78
second_ids = ['c', 'd']
79
def second_success(report):
81
publisher = publish_to_many(first_success, second_success)
82
self.assertEqual(first_ids + second_ids, publisher({}))
84
def test_puts_id_in_report(self):
89
self.assertEqual(the_id, report['id'])
91
publisher = publish_to_many(first, second)
94
def test_puts_nothing_in_report_for_unpublished(self):
99
self.fail("id set to %s when previous publisher "
100
"didn't publish" % report['id'])
102
publisher = publish_to_many(first, second)
106
class TestPublishWithFallback(testtools.TestCase):
108
def test_publishes_to_one(self):
113
publisher = publish_with_fallback(capture)
114
publisher(dict(foo='bar'))
115
self.assertEqual([dict(foo='bar')], calls)
117
def test_publishes_to_two(self):
120
def capture1(report):
121
calls1.append(report)
123
def capture2(report):
124
calls2.append(report)
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)
131
def test_returns_ids_from_publisher(self):
135
publisher = publish_with_fallback(success)
136
self.assertEqual(ids, publisher({}))
138
def test_publishes_new(self):
145
publisher = publish_with_fallback(failure, capture)
146
self.assertEqual([], publisher({}))
147
self.assertEqual(1, len(calls))
149
def test_publish_stops_when_a_publisher_succeeds(self):
153
self.fail("Called fallback when primary succeeded.")
154
publisher = publish_with_fallback(success, fail)
155
self.assertEqual(['the id'], publisher({}))
158
class ConvertResultToListTests(testtools.TestCase):
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):
164
self.assertEqual([], convert_result_to_list(falseish)({}))
166
def test_converts_True_to_list(self):
167
# A true-ish value gets turned in to an empty list
170
self.assertEqual(["aaa"], convert_result_to_list(trueish)({}))