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