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

« back to all changes in this revision

Viewing changes to grackle/tests/test_model.py

  • Committer: William Grant
  • Date: 2012-01-22 12:36:48 UTC
  • Revision ID: william.grant@canonical.com-20120122123648-15b1dhwyhin355jp
Fix wsgi to cope with memo changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
__metaclass__ = type
18
18
 
19
 
import calendar
20
19
import datetime
21
20
from email.utils import formatdate
22
21
import os
23
22
from tempfile import _RandomNameSequence
 
23
import time
24
24
import unittest
25
25
 
26
26
from dateutil.tz import (
31
31
 
32
32
from grackle.model import (
33
33
    CassandraConnection,
34
 
    _format_message,
35
34
    _parse_message,
36
35
    )
37
36
 
87
86
 
88
87
        # The key in archive_message is a TimeUUID for the Date field in
89
88
        # the message.
90
 
        utctime = calendar.timegm(datetime.datetime(
 
89
        utctime = time.mktime(datetime.datetime(
91
90
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
92
91
        self.assertEqual(
93
92
            utctime,
95
94
 
96
95
        # The stored message contains the full original text of the
97
96
        # message, as well as interesting fields parsed out.
98
 
        self.assertEqual(TEST_MESSAGE, cmsg['raw'])
 
97
        self.assertEqual(TEST_MESSAGE, cmsg['content'])
99
98
        parsed_message = _parse_message(TEST_MESSAGE)[1]
100
99
        for key, value in parsed_message.iteritems():
101
100
            self.assertEqual(value, cmsg[key])
106
105
    def assertMessages(self, expected_ids, messages):
107
106
        expected_msgids = [
108
107
            '<message%d@example.com>' % id for id in expected_ids]
109
 
        actual_msgids = [msg['headers']['message-id'] for msg in messages]
 
108
        actual_msgids = [msg['message-id'] for msg in messages]
110
109
        self.assertEqual(expected_msgids, actual_msgids)
111
110
 
112
111
    def makeMessages(self, conn, archive, count):
127
126
    def test_single_message(self):
128
127
        conn, archive = self.makeArchive()
129
128
        self.makeMessages(conn, archive, 1)
130
 
 
131
 
        # We get a single message when we ask for it.
132
 
        messages = conn.get_messages(archive, 'date', 1, '')[1]
133
 
        self.assertMessages([0], messages)
134
 
        cmsg = messages[0]
135
 
 
136
 
        # The raw message matches what we generated, and the result is
137
 
        # formatted according to the default settings.
138
 
        expected_raw = TEMPLATE_MESSAGE.format(
139
 
            date=formatdate(0), id='<message0@example.com>')
140
 
        self.assertEqual(expected_raw, messages[0]['raw'])
141
 
        pmsg = _parse_message(expected_raw)[1]
142
 
        pmsg['raw'] = expected_raw
143
 
        self.assertEqual(
144
 
            _format_message(
145
 
                pmsg,
146
 
                headers=['date', 'from', 'subject', 'message-id'],
147
 
                include_raw=True),
148
 
            cmsg)
 
129
        self.assertMessages(
 
130
            [0], conn.get_messages(archive, 'date', 1, '')[1])
149
131
 
150
132
    def test_limit(self):
151
133
        conn, archive = self.makeArchive()
180
162
        self.assertMessages([0, 1], messages)
181
163
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
182
164
        self.assertMessages([2, 3], messages)
183
 
        prev, messages, next = conn.get_messages(
184
 
            archive, 'date', 2, prev, backward=True)
185
 
        self.assertMessages([0, 1], messages)
186
 
        prev, messages, next = conn.get_messages(
187
 
            archive, 'date', 2, prev, backward=True)
188
 
        self.assertIs(None, prev)
189
 
        self.assertMessages([], messages)
190
 
        self.assertIs(None, next)
191
 
 
192
 
    def test_date_filter(self):
193
 
        conn, archive = self.makeArchive()
194
 
        self.makeMessages(conn, archive, 10)
195
 
        start = datetime.datetime.utcfromtimestamp(250).replace(
196
 
            tzinfo=tzutc())
197
 
        finish = datetime.datetime.utcfromtimestamp(500).replace(
198
 
            tzinfo=tzutc())
199
 
        prev, messages, next = conn.get_messages(
200
 
            archive, 'date', 2, '', start_date=start, finish_date=finish)
201
 
        self.assertMessages([3, 4], messages)
202
 
        prev, messages, next = conn.get_messages(
203
 
            archive, 'date', 2, next, start_date=start, finish_date=finish)
204
 
        self.assertMessages([5], messages)
205
 
 
206
 
 
207
 
class TestMessageFormatter(unittest.TestCase):
208
 
 
209
 
    def test_all(self):
210
 
        parsed = _parse_message(TEST_MESSAGE)[1]
211
 
        parsed['raw'] = TEST_MESSAGE
212
 
        formatted = _format_message(
213
 
            parsed,
214
 
            headers=['date', 'from', 'subject', 'message-id'],
215
 
            include_raw=True)
216
 
        expected = {
217
 
            'headers': {
218
 
                'date': '2000-01-01T11:02:34+11:00',
219
 
                'from': 'sysadmin@example.com',
220
 
                'message-id': '<aaaaaaaaaaaaa@example.com>',
221
 
                'subject': 'Everything is broken',
222
 
                },
223
 
            'raw': TEST_MESSAGE,
224
 
            }
225
 
        self.assertEqual(expected, formatted)
 
165
        # XXX: We shouldn't have to reverse the sort order. There should
 
166
        # be a flag to work backwards, but I'm not sure if we can get
 
167
        # that from Cassandra without reversing in Python.
 
168
        prev, messages, next = conn.get_messages(archive, '-date', 2, prev)
 
169
        self.assertMessages([1, 0], messages)