~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 09:37:51 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120112093751-ltzymm8dbk34vixq
ImplementĀ memo/limitĀ support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
        messages = [m for m in self.server.messages[archive] if 'message_ids'
70
70
                    not in parameters or m['message_id'] in
71
71
                    parameters['message_ids']]
 
72
        limit = parameters.get('limit', 100)
 
73
        memo = parameters.get('memo')
 
74
        message_id_indices = dict(
 
75
            (m['message_id'], idx) for idx, m in enumerate(messages))
 
76
        if memo is None:
 
77
            start = 0
 
78
        else:
 
79
            start = message_id_indices[memo.encode('rot13')]
 
80
        if start > 0:
 
81
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
 
82
        else:
 
83
            previous_memo = None
 
84
        end = min(start + limit, len(messages))
 
85
        if end < len(messages):
 
86
            next_memo = messages[end]['message_id'].encode('rot13')
 
87
        else:
 
88
            next_memo = None
 
89
        messages = messages[start:end]
72
90
        response = {
73
91
            'messages': messages,
74
 
            'next_memo': None,
75
 
            'previous_memo': None
 
92
            'next_memo': next_memo,
 
93
            'previous_memo': previous_memo
76
94
            }
77
95
        self.wfile.write(simplejson.dumps(response))
78
96
 
96
114
 
97
115
class TestGetMessages(TestCase):
98
116
 
 
117
    def assertMessageIDs(self, ids, messages):
 
118
        self.assertEqual(
 
119
            sorted(ids), sorted(m['message_id'] for m in messages))
 
120
 
99
121
    def test_get_messages(self):
100
122
        client = GrackleClient('localhost', 8435)
101
123
        with fake_grackle_service(client,
117
139
            response = client.get_messages('baz', message_ids=['foo'])
118
140
        message, = response['messages']
119
141
        self.assertEqual('foo', message['message_id'])
 
142
 
 
143
    def test_get_messages_batching(self):
 
144
        client = GrackleClient('localhost', 8437)
 
145
        with fake_grackle_service(client,
 
146
            {'baz':
 
147
            [{'message_id': 'foo'},
 
148
             {'message_id': 'bar'}]}):
 
149
            response = client.get_messages('baz', limit=1)
 
150
            self.assertEqual(1, len(response['messages']))
 
151
            messages = response['messages']
 
152
            response = client.get_messages(
 
153
                'baz', limit=1, memo=response['next_memo'])
 
154
            self.assertEqual(1, len(response['messages']))
 
155
            messages.extend(response['messages'])
 
156
            self.assertMessageIDs(['foo', 'bar'], messages)