100
97
parsed_message = _parse_message(TEST_MESSAGE)[1]
101
98
for key, value in parsed_message.iteritems():
102
99
self.assertEqual(value, cmsg[key])
105
class TestGetMessages(unittest.TestCase):
107
def assertMessages(self, expected_ids, messages):
109
'<message%d@example.com>' % id for id in expected_ids]
110
actual_msgids = [msg['message-id'] for msg in messages]
111
self.assertEqual(expected_msgids, actual_msgids)
113
def makeMessages(self, conn, archive, count):
117
TEMPLATE_MESSAGE.format(
118
date=formatdate(i * 100),
119
id='<message%d@example.com>' % i))
120
for i in range(count)]
122
def makeArchive(self):
123
conn = CassandraConnection(
124
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
125
archive = next(_RandomNameSequence())
128
def test_single_message(self):
129
conn, archive = self.makeArchive()
130
self.makeMessages(conn, archive, 1)
132
[0], conn.get_messages(archive, 'date', 1, '')[1])
134
def test_limit(self):
135
conn, archive = self.makeArchive()
136
self.makeMessages(conn, archive, 4)
138
[0, 1], conn.get_messages(archive, 'date', 2, '')[1])
140
def test_order(self):
141
conn, archive = self.makeArchive()
142
self.makeMessages(conn, archive, 4)
144
[3, 2], conn.get_messages(archive, '-date', 2, '')[1])
146
def test_batching_forward(self):
147
conn, archive = self.makeArchive()
148
self.makeMessages(conn, archive, 5)
149
prev, messages, next = conn.get_messages(archive, 'date', 2, '')
150
self.assertMessages([0, 1], messages)
151
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
152
self.assertMessages([2, 3], messages)
153
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
154
self.assertMessages([4], messages)
155
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
156
self.assertIs(None, prev)
157
self.assertMessages([], messages)
158
self.assertIs(None, next)
160
def test_batching_backward(self):
161
conn, archive = self.makeArchive()
162
self.makeMessages(conn, archive, 5)
163
prev, messages, next = conn.get_messages(archive, 'date', 2, '')
164
self.assertMessages([0, 1], messages)
165
prev, messages, next = conn.get_messages(archive, 'date', 2, next)
166
self.assertMessages([2, 3], messages)
167
prev, messages, next = conn.get_messages(
168
archive, 'date', 2, prev, backward=True)
169
self.assertMessages([0, 1], messages)
170
prev, messages, next = conn.get_messages(
171
archive, 'date', 2, prev, backward=True)
172
self.assertIs(None, prev)
173
self.assertMessages([], messages)
174
self.assertIs(None, next)
176
def test_date_filter(self):
177
conn, archive = self.makeArchive()
178
self.makeMessages(conn, archive, 10)
179
start = datetime.datetime.utcfromtimestamp(250).replace(
181
finish = datetime.datetime.utcfromtimestamp(500).replace(
183
prev, messages, next = conn.get_messages(
184
archive, 'date', 2, '', start_date=start, finish_date=finish)
185
self.assertMessages([3, 4], messages)
186
prev, messages, next = conn.get_messages(
187
archive, 'date', 2, next, start_date=start, finish_date=finish)
188
self.assertMessages([5], messages)