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

« back to all changes in this revision

Viewing changes to grackle/tests/test_model.py

  • Committer: Aaron Bentley
  • Date: 2012-01-13 11:46:14 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120113114614-lw5t8rcn4cidlb3o
Support thread_newest threading.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2012 Canonical Ltd
2
 
#
3
 
# This program is free software: you can redistribute it and/or modify
4
 
# it under the terms of the GNU Affero General Public License as published by
5
 
# the Free Software Foundation, either version 3 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU Affero General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU Affero General Public
14
 
# License along with this program. If not, see
15
 
# <http://www.gnu.org/licenses/>.
16
 
 
17
 
__metaclass__ = type
18
 
 
19
 
import calendar
20
 
import datetime
21
 
from email.utils import formatdate
22
 
import os
23
 
from tempfile import _RandomNameSequence
24
 
 
25
 
from dateutil.tz import (
26
 
    tzoffset,
27
 
    tzutc,
28
 
    )
29
 
from pycassa.util import convert_uuid_to_time
30
 
import testtools
31
 
 
32
 
from grackle.model import (
33
 
    CassandraConnection,
34
 
    _format_message,
35
 
    _parse_message,
36
 
    )
37
 
 
38
 
 
39
 
TEMPLATE_MESSAGE = """\
40
 
From: sysadmin@example.com
41
 
To: developer@example.com
42
 
Subject: Everything is broken
43
 
Date: {date}
44
 
Message-Id: {id}
45
 
 
46
 
Help, everything has just broken.
47
 
"""
48
 
 
49
 
TEST_MESSAGE = TEMPLATE_MESSAGE.format(
50
 
    date='Sat, 1 Jan 2000 11:02:34 +1100',
51
 
    id='<aaaaaaaaaaaaa@example.com>')
52
 
 
53
 
 
54
 
class TestParseMessage(testtools.TestCase):
55
 
 
56
 
    def test_works(self):
57
 
        # _parse_message extracts interesting fields. It also parses the
58
 
        # date and returns it separately.
59
 
        date, msg = _parse_message(TEST_MESSAGE)
60
 
        self.assertEqual('sysadmin@example.com', msg['from'])
61
 
        self.assertEqual('developer@example.com', msg['to'])
62
 
        self.assertEqual('Everything is broken', msg['subject'])
63
 
        self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
64
 
        self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
65
 
        self.assertEqual(
66
 
            datetime.datetime(
67
 
                2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
68
 
            date)
69
 
 
70
 
 
71
 
class TestAddMessage(testtools.TestCase):
72
 
 
73
 
    def test_add_message(self):
74
 
        c = CassandraConnection(
75
 
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
76
 
        archive = next(_RandomNameSequence())
77
 
 
78
 
        # Write the message out to Cassandra, and read it back in.
79
 
        key = c.add_message(archive, TEST_MESSAGE)
80
 
        cmsg = c.messages.get(key)
81
 
 
82
 
        # The archive should contain a single message, a reference to
83
 
        # our new key.
84
 
        archive_messages = c.archive_messages.get(archive).items()
85
 
        self.assertEqual(1, len(archive_messages))
86
 
        self.assertEqual(key, archive_messages[0][1])
87
 
 
88
 
        # The key in archive_message is a TimeUUID for the Date field in
89
 
        # the message.
90
 
        utctime = calendar.timegm(datetime.datetime(
91
 
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
92
 
        self.assertEqual(
93
 
            utctime,
94
 
            convert_uuid_to_time(archive_messages[0][0]))
95
 
 
96
 
        # The stored message contains the full original text of the
97
 
        # message, as well as interesting fields parsed out.
98
 
        self.assertEqual(TEST_MESSAGE, cmsg['raw'])
99
 
        parsed_message = _parse_message(TEST_MESSAGE)[1]
100
 
        for key, value in parsed_message.iteritems():
101
 
            self.assertEqual(value, cmsg[key])
102
 
 
103
 
 
104
 
class TestGetMessages(testtools.TestCase):
105
 
 
106
 
    def assertMessages(self, expected_ids, messages):
107
 
        expected_msgids = [
108
 
            '<message%d@example.com>' % id for id in expected_ids]
109
 
        actual_msgids = [msg['headers']['message-id'] for msg in messages]
110
 
        self.assertEqual(expected_msgids, actual_msgids)
111
 
 
112
 
    def makeMessages(self, conn, archive, count):
113
 
        return [
114
 
            conn.add_message(
115
 
                archive,
116
 
                TEMPLATE_MESSAGE.format(
117
 
                    date=formatdate(i * 100),
118
 
                    id='<message%d@example.com>' % i))
119
 
            for i in range(count)]
120
 
 
121
 
    def makeArchive(self):
122
 
        conn = CassandraConnection(
123
 
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
124
 
        archive = next(_RandomNameSequence())
125
 
        return conn, archive
126
 
 
127
 
    def test_single_message(self):
128
 
        conn, archive = self.makeArchive()
129
 
        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)
149
 
 
150
 
    def test_limit(self):
151
 
        conn, archive = self.makeArchive()
152
 
        self.makeMessages(conn, archive, 4)
153
 
        self.assertMessages(
154
 
            [0, 1], conn.get_messages(archive, 'date', 2, '')[1])
155
 
 
156
 
    def test_order(self):
157
 
        conn, archive = self.makeArchive()
158
 
        self.makeMessages(conn, archive, 4)
159
 
        self.assertMessages(
160
 
            [3, 2], conn.get_messages(archive, '-date', 2, '')[1])
161
 
 
162
 
    def test_batching_forward(self):
163
 
        conn, archive = self.makeArchive()
164
 
        self.makeMessages(conn, archive, 5)
165
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
166
 
        self.assertMessages([0, 1], messages)
167
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
168
 
        self.assertMessages([2, 3], messages)
169
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
170
 
        self.assertMessages([4], messages)
171
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
172
 
        self.assertIs(None, prev)
173
 
        self.assertMessages([], messages)
174
 
        self.assertIs(None, next)
175
 
 
176
 
    def test_batching_backward(self):
177
 
        conn, archive = self.makeArchive()
178
 
        self.makeMessages(conn, archive, 5)
179
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
180
 
        self.assertMessages([0, 1], messages)
181
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
182
 
        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(testtools.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)