43
80
self.send_error(httplib.BAD_REQUEST)
47
service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
48
service.serve_forever()
83
scheme, netloc, path, params, query_string, fragments = (
85
archive = os.path.split(path)[1]
86
query = parse_qs(query_string)
87
parameters = simplejson.loads(query['parameters'][0])
88
order = parameters.get('order')
89
messages = self.server.messages[archive]
90
if order is not None :
91
if order not in SUPPORTED_ORDERS:
92
self.send_response(httplib.BAD_REQUEST)
93
self.wfile.write('Unsupported order')
95
elif order.startswith('thread_'):
96
threaded = threaded_messages(messages)
98
if order == 'thread_subject':
99
threaded.sort(key=lambda t: t[0]['subject'])
100
if order == 'thread_oldest':
101
threaded.sort(key=lambda t: min(m['date'] for m in t))
102
if order == 'thread_newest':
103
threaded.sort(key=lambda t: max(m['date'] for m in t))
104
for thread in threaded:
105
messages.extend(thread)
107
messages.sort(key=lambda m: m[order])
108
messages = [m for m in messages
109
if 'message_ids' not in parameters or
110
m['message_id'] in parameters['message_ids']]
111
self.send_response(httplib.OK)
113
limit = parameters.get('limit', 100)
114
memo = parameters.get('memo')
115
message_id_indices = dict(
116
(m['message_id'], idx) for idx, m in enumerate(messages))
120
start = message_id_indices[memo.encode('rot13')]
122
previous_memo = messages[start - 1]['message_id'].encode('rot13')
125
end = min(start + limit, len(messages))
126
if end < len(messages):
127
next_memo = messages[end]['message_id'].encode('rot13')
130
messages = messages[start:end]
132
'messages': messages,
133
'next_memo': next_memo,
134
'previous_memo': previous_memo
136
self.wfile.write(simplejson.dumps(response))
139
def fake_grackle_service(client, messages=None):
142
return ForkedFake(client.port, messages)
52
145
class TestPutMessage(TestCase):
54
147
def test_put_message(self):
55
with Forked(run_service):
56
client.put_message('arch1', StringIO('This is a message'))
148
client = GrackleClient('localhost', 8436)
149
with fake_grackle_service(client):
150
client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
151
with ExpectedException(Exception, 'wtf'):
58
client.put_message('arch1', StringIO('This is not a message'))
152
client.put_message('arch1', 'asdf',
153
StringIO('This is not a message'))
156
class TestGetMessages(TestCase):
158
def assertIDOrder(self, ids, messages):
159
self.assertEqual(ids, [m['message_id'] for m in messages])
161
def assertMessageIDs(self, ids, messages):
163
sorted(ids), sorted(messages, key=lambda m:m['message_id']))
165
def test_get_messages(self):
166
client = GrackleClient('localhost', 8435)
167
with fake_grackle_service(client,
169
[{'message_id': 'foo'},
170
{'message_id': 'bar'}]}):
171
response = client.get_messages('baz')
172
self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
173
response['messages']))
174
self.assertIs(None, response['next_memo'])
175
self.assertIs(None, response['previous_memo'])
177
def test_get_messages_by_id(self):
178
client = GrackleClient('localhost', 8437)
179
with fake_grackle_service(client,
181
[{'message_id': 'foo'},
182
{'message_id': 'bar'}]}):
183
response = client.get_messages('baz', message_ids=['foo'])
184
message, = response['messages']
185
self.assertEqual('foo', message['message_id'])
187
def test_get_messages_batching(self):
188
client = GrackleClient('localhost', 8438)
189
with fake_grackle_service(client,
191
[{'message_id': 'foo'},
192
{'message_id': 'bar'}]}):
193
response = client.get_messages('baz', limit=1)
194
self.assertEqual(1, len(response['messages']))
195
messages = response['messages']
196
response = client.get_messages(
197
'baz', limit=1, memo=response['next_memo'])
198
self.assertEqual(1, len(response['messages']))
199
messages.extend(response['messages'])
200
self.assertMessageIDs(['foo', 'bar'], messages)
202
def get_messages_member_order_test(self, key):
203
client = GrackleClient('localhost', 8439)
204
with fake_grackle_service(client,
205
{'baz': [{'message_id': 'foo', key: '2011-03-25'},
206
{'message_id': 'bar', key: '2011-03-24'}]}):
207
response = client.get_messages('baz')
208
self.assertIDOrder(['foo', 'bar'], response['messages'])
209
response = client.get_messages('baz', order=key)
210
self.assertIDOrder(['bar', 'foo'], response['messages'])
212
def test_get_messages_date_order(self):
213
self.get_messages_member_order_test('date')
215
def test_get_messages_author_order(self):
216
self.get_messages_member_order_test('author')
218
def test_get_messages_subject_order(self):
219
self.get_messages_member_order_test('subject')
221
def test_get_messages_thread_subject_order(self):
222
client = GrackleClient('localhost', 8439)
223
with fake_grackle_service(client, {'baz': [
224
{'message_id': 'bar', 'subject': 'y'},
225
{'message_id': 'qux', 'subject': 'z'},
226
{'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
228
response = client.get_messages('baz')
229
self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
230
response = client.get_messages('baz', order='subject')
231
self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
232
response = client.get_messages('baz', order='thread_subject')
233
self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
235
def test_get_messages_thread_oldest_order(self):
236
client = GrackleClient('localhost', 8439)
237
with fake_grackle_service(client, {'baz': [
238
{'message_id': 'bar', 'date': 'x'},
239
{'message_id': 'qux', 'date': 'z'},
240
{'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
242
response = client.get_messages('baz')
243
self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
244
response = client.get_messages('baz', order='date')
245
self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
246
response = client.get_messages('baz', order='thread_oldest')
247
self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
249
def test_get_messages_thread_newest_order(self):
250
client = GrackleClient('localhost', 8439)
251
with fake_grackle_service(client, {'baz': [
252
{'message_id': 'bar', 'date': 'x'},
253
{'message_id': 'qux', 'date': 'w'},
254
{'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
255
{'message_id': 'baz', 'date': 'z', 'in_reply_to': 'qux'},
257
response = client.get_messages('baz', order='date')
259
['qux', 'bar', 'foo', 'baz'], response['messages'])
260
response = client.get_messages('baz', order='thread_newest')
262
['bar', 'foo', 'qux', 'baz'], response['messages'])
264
def test_get_messages_unsupported_order(self):
265
client = GrackleClient('localhost', 8439)
266
with fake_grackle_service(client,
267
{'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
268
{'message_id': 'bar', 'date': '2011-03-24'}]}):
269
with ExpectedException(UnsupportedOrder):
270
client.get_messages('baz', order='nonsense')