~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-16 20:31:23 UTC
  • Revision ID: aaron@canonical.com-20120116203123-bxg29ktwtq19al75
Cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        parameters = simplejson.loads(query['parameters'][0])
55
55
        order = parameters.get('order')
56
56
        messages = self.messages[archive_id]
57
 
        if order is not None:
 
57
        if order is not None :
58
58
            if order not in SUPPORTED_ORDERS:
59
59
                raise UnsupportedOrder
60
60
            elif order.startswith('thread_'):
72
72
                messages.sort(key=lambda m: m[order])
73
73
        new_messages = []
74
74
        for message in messages:
75
 
            if (not parameters['include_hidden']
 
75
            if (
 
76
                not parameters['include_hidden']
76
77
                and message.get('hidden', False)):
77
78
                continue
78
79
 
79
 
            if ('message_ids' in parameters
80
 
                and message['message_id'] not in parameters['message_ids']):
 
80
            if ('message_ids' in parameters and
 
81
                message['message_id'] not in parameters['message_ids']):
81
82
                continue
82
83
            message = dict(message)
83
84
            if 'headers' in parameters:
117
118
        return response
118
119
 
119
120
 
120
 
class ForkedFakeService:
 
121
 
 
122
class ForkedFake:
121
123
    """A Grackle service fake, as a ContextManager."""
122
124
 
123
125
    def __init__(self, port, messages=None, write_logs=False):
124
126
        """Constructor.
125
 
 
126
 
        :param port: The tcp port to use.
 
127
        :param port: The tcp port to use
127
128
        :param messages: A dict of lists of dicts representing messages.  The
128
129
            outer dict represents the archive, the list represents the list of
129
130
            messages for that archive.
140
141
 
141
142
    @staticmethod
142
143
    def from_client(client, messages=None):
143
 
        """Instantiate a ForkedFakeService from the client.
 
144
        """Instantiate a ForkedFake from the client.
144
145
 
145
 
        :param port: The client to provide service for.
 
146
        :param port: The client  to provide service for.
146
147
        :param messages: A dict of lists of dicts representing messages.  The
147
148
            outer dict represents the archive, the list represents the list of
148
149
            messages for that archive.
149
150
        """
150
 
        return ForkedFakeService(client.port, messages)
 
151
        return ForkedFake(client.port, messages)
151
152
 
152
153
    def is_ready(self):
153
154
        """Tell the parent process that the server is ready for writes."""
225
226
    def log_message(self, format, *args):
226
227
        """Override log_message to use standard Python logging."""
227
228
        message = "%s - - [%s] %s\n" % (
228
 
            self.address_string(), self.log_date_time_string(), format % args)
 
229
            self.address_string(), self.log_date_time_string(), format%args)
229
230
        self.logger.info(message)
230
231
 
231
232
 
233
234
 
234
235
    def test_put_message(self):
235
236
        client = GrackleClient('localhost', 8436)
236
 
        with ForkedFakeService.from_client(client):
 
237
        with ForkedFake.from_client(client):
237
238
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
238
239
            with ExpectedException(Exception, 'wtf'):
239
240
                client.put_message('arch1', 'asdf',
247
248
 
248
249
    def assertMessageIDs(self, ids, messages):
249
250
        self.assertIDOrder(
250
 
            sorted(ids), sorted(messages, key=lambda m: m['message_id']))
 
251
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
251
252
 
252
253
    def test_get_messages(self):
253
254
        client = GrackleClient('localhost', 8435)
254
 
        with ForkedFakeService.from_client(client,
 
255
        with ForkedFake.from_client(client,
255
256
            {'baz':
256
257
            [{'message_id': 'foo'},
257
258
             {'message_id': 'bar'}]}):
263
264
 
264
265
    def test_get_messages_by_id(self):
265
266
        client = GrackleClient('localhost', 8437)
266
 
        with ForkedFakeService.from_client(client,
 
267
        with ForkedFake.from_client(client,
267
268
            {'baz':
268
269
            [{'message_id': 'foo'},
269
270
             {'message_id': 'bar'}]}):
273
274
 
274
275
    def test_get_messages_batching(self):
275
276
        client = GrackleClient('localhost', 8438)
276
 
        with ForkedFakeService.from_client(client,
 
277
        with ForkedFake.from_client(client,
277
278
            {'baz':
278
279
            [{'message_id': 'foo'},
279
280
             {'message_id': 'bar'}]}):
288
289
 
289
290
    def get_messages_member_order_test(self, key):
290
291
        client = GrackleClient('localhost', 8439)
291
 
        with ForkedFakeService.from_client(client,
 
292
        with ForkedFake.from_client(client,
292
293
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
293
294
                 {'message_id': 'bar', key: '2011-03-24'}]}):
294
295
            response = client.get_messages('baz')
307
308
 
308
309
    def test_get_messages_thread_subject_order(self):
309
310
        client = GrackleClient('localhost', 8439)
310
 
        with ForkedFakeService.from_client(client, {'baz': [
 
311
        with ForkedFake.from_client(client, {'baz': [
311
312
            {'message_id': 'bar', 'subject': 'y'},
312
313
            {'message_id': 'qux', 'subject': 'z'},
313
314
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
321
322
 
322
323
    def test_get_messages_thread_oldest_order(self):
323
324
        client = GrackleClient('localhost', 8439)
324
 
        with ForkedFakeService.from_client(client, {'baz': [
 
325
        with ForkedFake.from_client(client, {'baz': [
325
326
            {'message_id': 'bar', 'date': 'x'},
326
327
            {'message_id': 'qux', 'date': 'z'},
327
328
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
335
336
 
336
337
    def test_get_messages_thread_newest_order(self):
337
338
        client = GrackleClient('localhost', 8439)
338
 
        with ForkedFakeService.from_client(client, {'baz': [
 
339
        with ForkedFake.from_client(client, {'baz': [
339
340
            {'message_id': 'bar', 'date': 'x'},
340
341
            {'message_id': 'qux', 'date': 'w'},
341
342
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
350
351
 
351
352
    def test_get_messages_unsupported_order(self):
352
353
        client = GrackleClient('localhost', 8439)
353
 
        with ForkedFakeService.from_client(client,
 
354
        with ForkedFake.from_client(client,
354
355
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
355
356
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
356
 
            with ExpectedException(UnsupportedOrder, ''):
 
357
            with ExpectedException(UnsupportedOrder):
357
358
                client.get_messages('baz', order='nonsense')
358
359
 
359
360
    def test_get_messages_headers_no_headers(self):
360
361
        client = GrackleClient('localhost', 8440)
361
 
        with ForkedFakeService.from_client(client,
 
362
        with ForkedFake.from_client(client,
362
363
            {'baz': [
363
364
                {'message_id': 'foo'}
364
365
            ]}):
370
371
 
371
372
    def test_get_messages_headers_exclude_headers(self):
372
373
        client = GrackleClient('localhost', 8441)
373
 
        with ForkedFakeService.from_client(client,
 
374
        with ForkedFake.from_client(client,
374
375
            {'baz': [
375
376
                {'message_id': 'foo', 'headers': {'From': 'me'}}
376
377
            ]}):
382
383
 
383
384
    def test_get_messages_headers_include_headers(self):
384
385
        client = GrackleClient('localhost', 8442)
385
 
        with ForkedFakeService.from_client(client,
 
386
        with ForkedFake.from_client(client,
386
387
            {'baz': [
387
388
                {'message_id': 'foo', 'headers': {'From': 'me', 'To': 'you'}}
388
389
            ]}):
394
395
 
395
396
    def test_get_messages_max_body_length(self):
396
397
        client = GrackleClient('localhost', 8443)
397
 
        with ForkedFakeService.from_client(client,
 
398
        with ForkedFake.from_client(client,
398
399
            {'baz': [
399
400
                {'message_id': 'foo', 'body': u'abcdefghi'}
400
401
            ]}):
404
405
 
405
406
    def test_include_hidden(self):
406
407
        client = GrackleClient('localhost', 8444)
407
 
        with ForkedFakeService.from_client(client,
 
408
        with ForkedFake.from_client(client,
408
409
            {'baz': [
409
410
                {'message_id': 'foo', 'hidden': True},
410
411
                {'message_id': 'bar', 'hidden': False}
413
414
            self.assertMessageIDs(['bar', 'foo'], response['messages'])
414
415
            response = client.get_messages('baz', include_hidden=False)
415
416
            self.assertMessageIDs(['bar'], response['messages'])
 
417