~unity-2d-team/unity-2d/Shell-MultiMonitor

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-12 11:31:51 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120112113151-qe327t15vwogee6t
Order by thread subject.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import httplib
6
6
import os
7
7
from signal import SIGKILL
 
8
import simplejson
8
9
from StringIO import StringIO
9
10
from unittest import TestCase
 
11
from urlparse import urlparse
 
12
from urlparse import parse_qs
10
13
 
11
14
from testtools import ExpectedException
12
15
 
13
 
from grackle import client
14
 
 
15
 
 
16
 
class Forked:
17
 
 
18
 
    def __init__(self, func_or_method):
19
 
        self.func_or_method = func_or_method
 
16
from grackle.client import (
 
17
    GrackleClient,
 
18
    UnsupportedOrder,
 
19
    )
 
20
 
 
21
 
 
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
 
 
37
class ForkedFake:
 
38
 
 
39
    def __init__(self, port, messages=None):
20
40
        self.pid = None
 
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')
21
47
 
22
48
    def __enter__(self):
23
49
        pid = os.fork()
24
 
        if pid != 0:
25
 
            self.pid = pid
26
 
            return
27
 
        self.func_or_method()
 
50
        if pid == 0:
 
51
            self.start_server()
 
52
        self.pid = pid
 
53
        os.read(self.read_end, 1)
 
54
        return
28
55
 
 
56
    def start_server(self):
 
57
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
 
58
        service.messages = self.messages
 
59
        self.is_ready()
 
60
        service.serve_forever()
29
61
 
30
62
    def __exit__(self, exc_type, exc_val, traceback):
31
63
        os.kill(self.pid, SIGKILL)
32
64
 
33
65
 
 
66
SUPPORTED_ORDERS = set(['date', 'author', 'subject', 'thread_subject'])
 
67
 
 
68
 
34
69
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
35
70
 
36
71
    def do_POST(self):
42
77
        else:
43
78
            self.send_error(httplib.BAD_REQUEST)
44
79
 
45
 
 
46
 
def run_service():
47
 
    service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
48
 
    service.serve_forever()
49
 
 
 
80
    def do_GET(self):
 
81
        scheme, netloc, path, params, query_string, fragments = (
 
82
            urlparse(self.path))
 
83
        archive = os.path.split(path)[1]
 
84
        query = parse_qs(query_string)
 
85
        parameters = simplejson.loads(query['parameters'][0])
 
86
        order = parameters.get('order')
 
87
        messages = self.server.messages[archive]
 
88
        if order is not None :
 
89
            if order not in SUPPORTED_ORDERS:
 
90
                self.send_response(httplib.BAD_REQUEST)
 
91
                self.wfile.write('Unsupported order')
 
92
                return
 
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']]
 
105
        self.send_response(httplib.OK)
 
106
        self.end_headers()
 
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]
 
125
        response = {
 
126
            'messages': messages,
 
127
            'next_memo': next_memo,
 
128
            'previous_memo': previous_memo
 
129
            }
 
130
        self.wfile.write(simplejson.dumps(response))
 
131
 
 
132
 
 
133
def fake_grackle_service(client, messages=None):
 
134
    if messages is None:
 
135
        messages = {}
 
136
    return ForkedFake(client.port, messages)
50
137
 
51
138
 
52
139
class TestPutMessage(TestCase):
53
140
 
54
141
    def test_put_message(self):
55
 
        with Forked(run_service):
56
 
            client.put_message('arch1', StringIO('This is a message'))
 
142
        client = GrackleClient('localhost', 8436)
 
143
        with fake_grackle_service(client):
 
144
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
145
            with ExpectedException(Exception, 'wtf'):
58
 
                client.put_message('arch1', StringIO('This is not a message'))
 
146
                client.put_message('arch1', 'asdf',
 
147
                    StringIO('This is not a message'))
 
148
 
 
149
 
 
150
class TestGetMessages(TestCase):
 
151
 
 
152
    def assertIDOrder(self, ids, messages):
 
153
        self.assertEqual(ids, [m['message_id'] for m in messages])
 
154
 
 
155
    def assertMessageIDs(self, ids, messages):
 
156
        self.assertIDOrder(
 
157
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
 
158
 
 
159
    def test_get_messages(self):
 
160
        client = GrackleClient('localhost', 8435)
 
161
        with fake_grackle_service(client,
 
162
            {'baz':
 
163
            [{'message_id': 'foo'},
 
164
             {'message_id': 'bar'}]}):
 
165
            response = client.get_messages('baz')
 
166
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
 
167
            response['messages']))
 
168
        self.assertIs(None, response['next_memo'])
 
169
        self.assertIs(None, response['previous_memo'])
 
170
 
 
171
    def test_get_messages_by_id(self):
 
172
        client = GrackleClient('localhost', 8437)
 
173
        with fake_grackle_service(client,
 
174
            {'baz':
 
175
            [{'message_id': 'foo'},
 
176
             {'message_id': 'bar'}]}):
 
177
            response = client.get_messages('baz', message_ids=['foo'])
 
178
        message, = response['messages']
 
179
        self.assertEqual('foo', message['message_id'])
 
180
 
 
181
    def test_get_messages_batching(self):
 
182
        client = GrackleClient('localhost', 8438)
 
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)
 
195
 
 
196
    def get_messages_member_order_test(self, key):
 
197
        client = GrackleClient('localhost', 8439)
 
198
        with fake_grackle_service(client,
 
199
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
 
200
                 {'message_id': 'bar', key: '2011-03-24'}]}):
 
201
            response = client.get_messages('baz')
 
202
            self.assertIDOrder(['foo', 'bar'], response['messages'])
 
203
            response = client.get_messages('baz', order=key)
 
204
            self.assertIDOrder(['bar', 'foo'], response['messages'])
 
205
 
 
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
 
 
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')