~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(
67
    ['date', 'author', 'subject', 'thread_oldest', 'thread_subject'])
21 by Aaron Bentley
Test unsupported orders.
68
69
5 by Aaron Bentley
Actual fake service working.
70
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
71
11 by Aaron Bentley
Start working on GET.
72
    def do_POST(self):
5 by Aaron Bentley
Actual fake service working.
73
        message = self.rfile.read(int(self.headers['content-length']))
74
        if message == 'This is a message':
6 by Aaron Bentley
Use constants.
75
            self.send_response(httplib.CREATED)
5 by Aaron Bentley
Actual fake service working.
76
            self.end_headers()
77
            self.wfile.close()
78
        else:
6 by Aaron Bentley
Use constants.
79
            self.send_error(httplib.BAD_REQUEST)
5 by Aaron Bentley
Actual fake service working.
80
13 by Aaron Bentley
Retrieve messages.
81
    def do_GET(self):
15 by Aaron Bentley
Test filtering by message-id.
82
        scheme, netloc, path, params, query_string, fragments = (
83
            urlparse(self.path))
13 by Aaron Bentley
Retrieve messages.
84
        archive = os.path.split(path)[1]
15 by Aaron Bentley
Test filtering by message-id.
85
        query = parse_qs(query_string)
86
        parameters = simplejson.loads(query['parameters'][0])
22 by Aaron Bentley
Order by thread subject.
87
        order = parameters.get('order')
88
        messages = self.server.messages[archive]
89
        if order is not None :
90
            if order not in SUPPORTED_ORDERS:
21 by Aaron Bentley
Test unsupported orders.
91
                self.send_response(httplib.BAD_REQUEST)
92
                self.wfile.write('Unsupported order')
93
                return
23 by Aaron Bentley
Support thread_oldest order.
94
            elif order.startswith('thread_'):
22 by Aaron Bentley
Order by thread subject.
95
                threaded = threaded_messages(messages)
96
                messages = []
23 by Aaron Bentley
Support thread_oldest order.
97
                if order == 'thread_subject':
98
                    threaded.sort(key=lambda t: t[0]['subject'])
99
                if order == 'thread_oldest':
100
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
22 by Aaron Bentley
Order by thread subject.
101
                for thread in threaded:
102
                    messages.extend(thread)
103
            else:
23 by Aaron Bentley
Support thread_oldest order.
104
                messages.sort(key=lambda m: m[order])
22 by Aaron Bentley
Order by thread subject.
105
        messages = [m for m in messages
106
                    if 'message_ids' not in parameters or
107
                    m['message_id'] in parameters['message_ids']]
21 by Aaron Bentley
Test unsupported orders.
108
        self.send_response(httplib.OK)
109
        self.end_headers()
19 by Aaron Bentley
Implement memo/limit support.
110
        limit = parameters.get('limit', 100)
111
        memo = parameters.get('memo')
112
        message_id_indices = dict(
113
            (m['message_id'], idx) for idx, m in enumerate(messages))
114
        if memo is None:
115
            start = 0
116
        else:
117
            start = message_id_indices[memo.encode('rot13')]
118
        if start > 0:
119
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
120
        else:
121
            previous_memo = None
122
        end = min(start + limit, len(messages))
123
        if end < len(messages):
124
            next_memo = messages[end]['message_id'].encode('rot13')
125
        else:
126
            next_memo = None
127
        messages = messages[start:end]
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
128
        response = {
129
            'messages': messages,
19 by Aaron Bentley
Implement memo/limit support.
130
            'next_memo': next_memo,
131
            'previous_memo': previous_memo
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
132
            }
133
        self.wfile.write(simplejson.dumps(response))
13 by Aaron Bentley
Retrieve messages.
134
5 by Aaron Bentley
Actual fake service working.
135
11 by Aaron Bentley
Start working on GET.
136
def fake_grackle_service(client, messages=None):
137
    if messages is None:
138
        messages = {}
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
139
    return ForkedFake(client.port, messages)
11 by Aaron Bentley
Start working on GET.
140
4 by Aaron Bentley
Initial test.
141
3 by Aaron Bentley
Add test framework.
142
class TestPutMessage(TestCase):
143
144
    def test_put_message(self):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
145
        client = GrackleClient('localhost', 8436)
11 by Aaron Bentley
Start working on GET.
146
        with fake_grackle_service(client):
7 by Aaron Bentley
Fix URLs etc.
147
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
148
            with ExpectedException(Exception, 'wtf'):
7 by Aaron Bentley
Fix URLs etc.
149
                client.put_message('arch1', 'asdf',
150
                    StringIO('This is not a message'))
11 by Aaron Bentley
Start working on GET.
151
152
153
class TestGetMessages(TestCase):
154
20 by Aaron Bentley
Support order by date
155
    def assertIDOrder(self, ids, messages):
156
        self.assertEqual(ids, [m['message_id'] for m in messages])
157
19 by Aaron Bentley
Implement memo/limit support.
158
    def assertMessageIDs(self, ids, messages):
20 by Aaron Bentley
Support order by date
159
        self.assertIDOrder(
160
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
19 by Aaron Bentley
Implement memo/limit support.
161
11 by Aaron Bentley
Start working on GET.
162
    def test_get_messages(self):
163
        client = GrackleClient('localhost', 8435)
164
        with fake_grackle_service(client,
165
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
166
            [{'message_id': 'foo'},
167
             {'message_id': 'bar'}]}):
15 by Aaron Bentley
Test filtering by message-id.
168
            response = client.get_messages('baz')
17 by Aaron Bentley
Switch hyphens to underscores.
169
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
170
            response['messages']))
171
        self.assertIs(None, response['next_memo'])
172
        self.assertIs(None, response['previous_memo'])
15 by Aaron Bentley
Test filtering by message-id.
173
174
    def test_get_messages_by_id(self):
175
        client = GrackleClient('localhost', 8437)
176
        with fake_grackle_service(client,
177
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
178
            [{'message_id': 'foo'},
179
             {'message_id': 'bar'}]}):
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
180
            response = client.get_messages('baz', message_ids=['foo'])
181
        message, = response['messages']
17 by Aaron Bentley
Switch hyphens to underscores.
182
        self.assertEqual('foo', message['message_id'])
19 by Aaron Bentley
Implement memo/limit support.
183
184
    def test_get_messages_batching(self):
20 by Aaron Bentley
Support order by date
185
        client = GrackleClient('localhost', 8438)
19 by Aaron Bentley
Implement memo/limit support.
186
        with fake_grackle_service(client,
187
            {'baz':
188
            [{'message_id': 'foo'},
189
             {'message_id': 'bar'}]}):
190
            response = client.get_messages('baz', limit=1)
191
            self.assertEqual(1, len(response['messages']))
192
            messages = response['messages']
193
            response = client.get_messages(
194
                'baz', limit=1, memo=response['next_memo'])
195
            self.assertEqual(1, len(response['messages']))
196
            messages.extend(response['messages'])
197
            self.assertMessageIDs(['foo', 'bar'], messages)
20 by Aaron Bentley
Support order by date
198
22 by Aaron Bentley
Order by thread subject.
199
    def get_messages_member_order_test(self, key):
20 by Aaron Bentley
Support order by date
200
        client = GrackleClient('localhost', 8439)
201
        with fake_grackle_service(client,
22 by Aaron Bentley
Order by thread subject.
202
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
203
                 {'message_id': 'bar', key: '2011-03-24'}]}):
20 by Aaron Bentley
Support order by date
204
            response = client.get_messages('baz')
205
            self.assertIDOrder(['foo', 'bar'], response['messages'])
22 by Aaron Bentley
Order by thread subject.
206
            response = client.get_messages('baz', order=key)
20 by Aaron Bentley
Support order by date
207
            self.assertIDOrder(['bar', 'foo'], response['messages'])
21 by Aaron Bentley
Test unsupported orders.
208
22 by Aaron Bentley
Order by thread subject.
209
    def test_get_messages_date_order(self):
210
        self.get_messages_member_order_test('date')
211
212
    def test_get_messages_author_order(self):
213
        self.get_messages_member_order_test('author')
214
215
    def test_get_messages_subject_order(self):
216
        self.get_messages_member_order_test('subject')
217
218
    def test_get_messages_thread_subject_order(self):
219
        client = GrackleClient('localhost', 8439)
220
        with fake_grackle_service(client, {'baz': [
221
            {'message_id': 'bar', 'subject': 'y'},
222
            {'message_id': 'qux', 'subject': 'z'},
223
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
224
            ]}):
225
            response = client.get_messages('baz')
226
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
227
            response = client.get_messages('baz', order='subject')
228
            self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
229
            response = client.get_messages('baz', order='thread_subject')
230
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
231
23 by Aaron Bentley
Support thread_oldest order.
232
    def test_get_messages_thread_oldest_order(self):
233
        client = GrackleClient('localhost', 8439)
234
        with fake_grackle_service(client, {'baz': [
235
            {'message_id': 'bar', 'date': 'x'},
236
            {'message_id': 'qux', 'date': 'z'},
237
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
238
            ]}):
239
            response = client.get_messages('baz')
240
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
241
            response = client.get_messages('baz', order='date')
242
            self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
243
            response = client.get_messages('baz', order='thread_oldest')
244
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
245
21 by Aaron Bentley
Test unsupported orders.
246
    def test_get_messages_unsupported_order(self):
247
        client = GrackleClient('localhost', 8439)
248
        with fake_grackle_service(client,
249
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
250
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
251
            with ExpectedException(UnsupportedOrder):
252
                client.get_messages('baz', order='nonsense')