~didrocks/unity/altf10

4 by Aaron Bentley
Initial test.
1
from BaseHTTPServer import (
2
    HTTPServer,
3
    BaseHTTPRequestHandler,
4
    )
6 by Aaron Bentley
Use constants.
5
import httplib
4 by Aaron Bentley
Initial test.
6
import os
5 by Aaron Bentley
Actual fake service working.
7
from signal import SIGKILL
13 by Aaron Bentley
Retrieve messages.
8
import simplejson
4 by Aaron Bentley
Initial test.
9
from StringIO import StringIO
3 by Aaron Bentley
Add test framework.
10
from unittest import TestCase
13 by Aaron Bentley
Retrieve messages.
11
from urlparse import urlparse
15 by Aaron Bentley
Test filtering by message-id.
12
from urlparse import parse_qs
3 by Aaron Bentley
Add test framework.
13
5 by Aaron Bentley
Actual fake service working.
14
from testtools import ExpectedException
15
8 by Aaron Bentley
Test message path.
16
from grackle.client import (
17
    GrackleClient,
21 by Aaron Bentley
Test unsupported orders.
18
    UnsupportedOrder,
8 by Aaron Bentley
Test message path.
19
    )
4 by Aaron Bentley
Initial test.
20
21
22 by Aaron Bentley
Order by thread subject.
22
def threaded_messages(messages):
23
    threads = {}
24
    count = 0
25
    pending = []
26
    for message in messages:
27
        if message.get('in_reply_to') is None:
28
            threads[message['message_id']] = [message]
29
            count += 1
30
        else:
31
            pending.append(message)
32
    for message in pending:
33
        threads[message['in_reply_to']].append(message)
34
    return threads.values()
35
36
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
37
class ForkedFake:
5 by Aaron Bentley
Actual fake service working.
38
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
39
    def __init__(self, port, messages=None):
5 by Aaron Bentley
Actual fake service working.
40
        self.pid = None
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
41
        self.port = port
42
        self.messages = messages
43
        self.read_end, self.write_end = os.pipe()
44
45
    def is_ready(self):
46
        os.write(self.write_end, 'asdf')
5 by Aaron Bentley
Actual fake service working.
47
48
    def __enter__(self):
49
        pid = os.fork()
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
50
        if pid == 0:
51
            self.start_server()
52
        self.pid = pid
53
        os.read(self.read_end, 1)
54
        return
5 by Aaron Bentley
Actual fake service working.
55
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
56
    def start_server(self):
57
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
58
        service.messages = self.messages
59
        self.is_ready()
60
        service.serve_forever()
5 by Aaron Bentley
Actual fake service working.
61
62
    def __exit__(self, exc_type, exc_val, traceback):
63
        os.kill(self.pid, SIGKILL)
64
65
23 by Aaron Bentley
Support thread_oldest order.
66
SUPPORTED_ORDERS = set(
24 by Aaron Bentley
Support thread_newest threading.
67
    ['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
68
     'thread_subject'])
21 by Aaron Bentley
Test unsupported orders.
69
70
5 by Aaron Bentley
Actual fake service working.
71
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
72
11 by Aaron Bentley
Start working on GET.
73
    def do_POST(self):
5 by Aaron Bentley
Actual fake service working.
74
        message = self.rfile.read(int(self.headers['content-length']))
75
        if message == 'This is a message':
6 by Aaron Bentley
Use constants.
76
            self.send_response(httplib.CREATED)
5 by Aaron Bentley
Actual fake service working.
77
            self.end_headers()
78
            self.wfile.close()
79
        else:
6 by Aaron Bentley
Use constants.
80
            self.send_error(httplib.BAD_REQUEST)
5 by Aaron Bentley
Actual fake service working.
81
13 by Aaron Bentley
Retrieve messages.
82
    def do_GET(self):
15 by Aaron Bentley
Test filtering by message-id.
83
        scheme, netloc, path, params, query_string, fragments = (
84
            urlparse(self.path))
26 by Aaron Bentley
Implement an archive namespace.
85
        parts = path.split('/')
86
        if parts[1] == 'archive':
87
            self.get_messages(parts[2], query_string)
88
89
    def get_messages(self, archive_id, query_string):
15 by Aaron Bentley
Test filtering by message-id.
90
        query = parse_qs(query_string)
91
        parameters = simplejson.loads(query['parameters'][0])
22 by Aaron Bentley
Order by thread subject.
92
        order = parameters.get('order')
26 by Aaron Bentley
Implement an archive namespace.
93
        messages = self.server.messages[archive_id]
22 by Aaron Bentley
Order by thread subject.
94
        if order is not None :
95
            if order not in SUPPORTED_ORDERS:
21 by Aaron Bentley
Test unsupported orders.
96
                self.send_response(httplib.BAD_REQUEST)
97
                self.wfile.write('Unsupported order')
98
                return
23 by Aaron Bentley
Support thread_oldest order.
99
            elif order.startswith('thread_'):
22 by Aaron Bentley
Order by thread subject.
100
                threaded = threaded_messages(messages)
101
                messages = []
23 by Aaron Bentley
Support thread_oldest order.
102
                if order == 'thread_subject':
103
                    threaded.sort(key=lambda t: t[0]['subject'])
104
                if order == 'thread_oldest':
105
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
24 by Aaron Bentley
Support thread_newest threading.
106
                if order == 'thread_newest':
107
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
22 by Aaron Bentley
Order by thread subject.
108
                for thread in threaded:
109
                    messages.extend(thread)
110
            else:
23 by Aaron Bentley
Support thread_oldest order.
111
                messages.sort(key=lambda m: m[order])
22 by Aaron Bentley
Order by thread subject.
112
        messages = [m for m in messages
113
                    if 'message_ids' not in parameters or
114
                    m['message_id'] in parameters['message_ids']]
21 by Aaron Bentley
Test unsupported orders.
115
        self.send_response(httplib.OK)
116
        self.end_headers()
19 by Aaron Bentley
Implement memo/limit support.
117
        limit = parameters.get('limit', 100)
118
        memo = parameters.get('memo')
119
        message_id_indices = dict(
120
            (m['message_id'], idx) for idx, m in enumerate(messages))
121
        if memo is None:
122
            start = 0
123
        else:
124
            start = message_id_indices[memo.encode('rot13')]
125
        if start > 0:
126
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
127
        else:
128
            previous_memo = None
129
        end = min(start + limit, len(messages))
130
        if end < len(messages):
131
            next_memo = messages[end]['message_id'].encode('rot13')
132
        else:
133
            next_memo = None
134
        messages = messages[start:end]
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
135
        response = {
136
            'messages': messages,
19 by Aaron Bentley
Implement memo/limit support.
137
            'next_memo': next_memo,
138
            'previous_memo': previous_memo
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
139
            }
140
        self.wfile.write(simplejson.dumps(response))
13 by Aaron Bentley
Retrieve messages.
141
5 by Aaron Bentley
Actual fake service working.
142
11 by Aaron Bentley
Start working on GET.
143
def fake_grackle_service(client, messages=None):
144
    if messages is None:
145
        messages = {}
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
146
    return ForkedFake(client.port, messages)
11 by Aaron Bentley
Start working on GET.
147
4 by Aaron Bentley
Initial test.
148
3 by Aaron Bentley
Add test framework.
149
class TestPutMessage(TestCase):
150
151
    def test_put_message(self):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
152
        client = GrackleClient('localhost', 8436)
11 by Aaron Bentley
Start working on GET.
153
        with fake_grackle_service(client):
7 by Aaron Bentley
Fix URLs etc.
154
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
155
            with ExpectedException(Exception, 'wtf'):
7 by Aaron Bentley
Fix URLs etc.
156
                client.put_message('arch1', 'asdf',
157
                    StringIO('This is not a message'))
11 by Aaron Bentley
Start working on GET.
158
159
160
class TestGetMessages(TestCase):
161
20 by Aaron Bentley
Support order by date
162
    def assertIDOrder(self, ids, messages):
163
        self.assertEqual(ids, [m['message_id'] for m in messages])
164
19 by Aaron Bentley
Implement memo/limit support.
165
    def assertMessageIDs(self, ids, messages):
20 by Aaron Bentley
Support order by date
166
        self.assertIDOrder(
167
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
19 by Aaron Bentley
Implement memo/limit support.
168
11 by Aaron Bentley
Start working on GET.
169
    def test_get_messages(self):
170
        client = GrackleClient('localhost', 8435)
171
        with fake_grackle_service(client,
172
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
173
            [{'message_id': 'foo'},
174
             {'message_id': 'bar'}]}):
15 by Aaron Bentley
Test filtering by message-id.
175
            response = client.get_messages('baz')
17 by Aaron Bentley
Switch hyphens to underscores.
176
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
177
            response['messages']))
178
        self.assertIs(None, response['next_memo'])
179
        self.assertIs(None, response['previous_memo'])
15 by Aaron Bentley
Test filtering by message-id.
180
181
    def test_get_messages_by_id(self):
182
        client = GrackleClient('localhost', 8437)
183
        with fake_grackle_service(client,
184
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
185
            [{'message_id': 'foo'},
186
             {'message_id': 'bar'}]}):
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
187
            response = client.get_messages('baz', message_ids=['foo'])
188
        message, = response['messages']
17 by Aaron Bentley
Switch hyphens to underscores.
189
        self.assertEqual('foo', message['message_id'])
19 by Aaron Bentley
Implement memo/limit support.
190
191
    def test_get_messages_batching(self):
20 by Aaron Bentley
Support order by date
192
        client = GrackleClient('localhost', 8438)
19 by Aaron Bentley
Implement memo/limit support.
193
        with fake_grackle_service(client,
194
            {'baz':
195
            [{'message_id': 'foo'},
196
             {'message_id': 'bar'}]}):
197
            response = client.get_messages('baz', limit=1)
198
            self.assertEqual(1, len(response['messages']))
199
            messages = response['messages']
200
            response = client.get_messages(
201
                'baz', limit=1, memo=response['next_memo'])
202
            self.assertEqual(1, len(response['messages']))
203
            messages.extend(response['messages'])
204
            self.assertMessageIDs(['foo', 'bar'], messages)
20 by Aaron Bentley
Support order by date
205
22 by Aaron Bentley
Order by thread subject.
206
    def get_messages_member_order_test(self, key):
20 by Aaron Bentley
Support order by date
207
        client = GrackleClient('localhost', 8439)
208
        with fake_grackle_service(client,
22 by Aaron Bentley
Order by thread subject.
209
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
210
                 {'message_id': 'bar', key: '2011-03-24'}]}):
20 by Aaron Bentley
Support order by date
211
            response = client.get_messages('baz')
212
            self.assertIDOrder(['foo', 'bar'], response['messages'])
22 by Aaron Bentley
Order by thread subject.
213
            response = client.get_messages('baz', order=key)
20 by Aaron Bentley
Support order by date
214
            self.assertIDOrder(['bar', 'foo'], response['messages'])
21 by Aaron Bentley
Test unsupported orders.
215
22 by Aaron Bentley
Order by thread subject.
216
    def test_get_messages_date_order(self):
217
        self.get_messages_member_order_test('date')
218
219
    def test_get_messages_author_order(self):
220
        self.get_messages_member_order_test('author')
221
222
    def test_get_messages_subject_order(self):
223
        self.get_messages_member_order_test('subject')
224
225
    def test_get_messages_thread_subject_order(self):
226
        client = GrackleClient('localhost', 8439)
227
        with fake_grackle_service(client, {'baz': [
228
            {'message_id': 'bar', 'subject': 'y'},
229
            {'message_id': 'qux', 'subject': 'z'},
230
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
231
            ]}):
232
            response = client.get_messages('baz')
233
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
234
            response = client.get_messages('baz', order='subject')
235
            self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
236
            response = client.get_messages('baz', order='thread_subject')
237
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
238
23 by Aaron Bentley
Support thread_oldest order.
239
    def test_get_messages_thread_oldest_order(self):
240
        client = GrackleClient('localhost', 8439)
241
        with fake_grackle_service(client, {'baz': [
242
            {'message_id': 'bar', 'date': 'x'},
243
            {'message_id': 'qux', 'date': 'z'},
244
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
245
            ]}):
246
            response = client.get_messages('baz')
247
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
248
            response = client.get_messages('baz', order='date')
249
            self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
250
            response = client.get_messages('baz', order='thread_oldest')
251
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
252
24 by Aaron Bentley
Support thread_newest threading.
253
    def test_get_messages_thread_newest_order(self):
254
        client = GrackleClient('localhost', 8439)
255
        with fake_grackle_service(client, {'baz': [
256
            {'message_id': 'bar', 'date': 'x'},
257
            {'message_id': 'qux', 'date': 'w'},
258
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
259
            {'message_id': 'baz', 'date': 'z', 'in_reply_to': 'qux'},
260
            ]}):
261
            response = client.get_messages('baz', order='date')
262
            self.assertIDOrder(
263
                ['qux', 'bar', 'foo', 'baz'], response['messages'])
264
            response = client.get_messages('baz', order='thread_newest')
265
            self.assertIDOrder(
266
                ['bar', 'foo', 'qux', 'baz'], response['messages'])
267
21 by Aaron Bentley
Test unsupported orders.
268
    def test_get_messages_unsupported_order(self):
269
        client = GrackleClient('localhost', 8439)
270
        with fake_grackle_service(client,
271
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
272
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
273
            with ExpectedException(UnsupportedOrder):
274
                client.get_messages('baz', order='nonsense')