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

13 by William Grant
Test add_message a little.
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
18 by William Grant
Betterer tests.
19
import datetime
25 by William Grant
get_messages tests.
20
from email.utils import formatdate
20 by William Grant
Creating a keyspace and schema for each test (or even test run) is too slow -- it takes about 2s. Instead use a random archivename.
21
import os
22
from tempfile import _RandomNameSequence
18 by William Grant
Betterer tests.
23
import time
24
import unittest
25
26
from dateutil.tz import (
27
    tzoffset,
28
    tzutc,
29
    )
30
from pycassa.util import convert_uuid_to_time
13 by William Grant
Test add_message a little.
31
21 by William Grant
Merge grackle.server into grackle. Alter Makefile to run all the tests.
32
from grackle.model import (
18 by William Grant
Betterer tests.
33
    CassandraConnection,
34
    _parse_message,
35
    )
13 by William Grant
Test add_message a little.
36
37
22 by William Grant
Template messages yay.
38
TEMPLATE_MESSAGE = """\
13 by William Grant
Test add_message a little.
39
From: sysadmin@example.com
40
To: developer@example.com
41
Subject: Everything is broken
22 by William Grant
Template messages yay.
42
Date: {date}
43
Message-Id: {id}
13 by William Grant
Test add_message a little.
44
45
Help, everything has just broken.
46
"""
47
22 by William Grant
Template messages yay.
48
TEST_MESSAGE = TEMPLATE_MESSAGE.format(
49
    date='Sat, 1 Jan 2000 11:02:34 +1100',
50
    id='<aaaaaaaaaaaaa@example.com>')
51
13 by William Grant
Test add_message a little.
52
18 by William Grant
Betterer tests.
53
class TestParseMessage(unittest.TestCase):
54
55
    def test_works(self):
56
        # _parse_message extracts interesting fields. It also parses the
57
        # date and returns it separately.
58
        date, msg = _parse_message(TEST_MESSAGE)
59
        self.assertEqual('sysadmin@example.com', msg['from'])
60
        self.assertEqual('developer@example.com', msg['to'])
61
        self.assertEqual('Everything is broken', msg['subject'])
62
        self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
63
        self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
64
        self.assertEqual(
65
            datetime.datetime(
66
                2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
67
            date)
68
69
22 by William Grant
Template messages yay.
70
class TestAddMessage(unittest.TestCase):
18 by William Grant
Betterer tests.
71
72
    def test_add_message(self):
20 by William Grant
Creating a keyspace and schema for each test (or even test run) is too slow -- it takes about 2s. Instead use a random archivename.
73
        c = CassandraConnection(
74
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
75
        archive = next(_RandomNameSequence())
18 by William Grant
Betterer tests.
76
77
        # Write the message out to Cassandra, and read it back in.
20 by William Grant
Creating a keyspace and schema for each test (or even test run) is too slow -- it takes about 2s. Instead use a random archivename.
78
        key = c.add_message(archive, TEST_MESSAGE)
13 by William Grant
Test add_message a little.
79
        cmsg = c.messages.get(key)
18 by William Grant
Betterer tests.
80
81
        # The archive should contain a single message, a reference to
82
        # our new key.
20 by William Grant
Creating a keyspace and schema for each test (or even test run) is too slow -- it takes about 2s. Instead use a random archivename.
83
        archive_messages = c.archive_messages.get(archive).items()
18 by William Grant
Betterer tests.
84
        self.assertEqual(1, len(archive_messages))
85
        self.assertEqual(key, archive_messages[0][1])
86
87
        # The key in archive_message is a TimeUUID for the Date field in
31 by William Grant
Use real UTC timestamps in the archive_message TimeUUIDs. pycassa's end up including the local timezone offset :/
88
        # the message. There is no UTC equivalent of time.mktime, so we
89
        # must subtract the offset.
18 by William Grant
Betterer tests.
90
        utctime = time.mktime(datetime.datetime(
31 by William Grant
Use real UTC timestamps in the archive_message TimeUUIDs. pycassa's end up including the local timezone offset :/
91
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple()) - time.timezone
18 by William Grant
Betterer tests.
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.
13 by William Grant
Test add_message a little.
98
        self.assertEqual(TEST_MESSAGE, cmsg['content'])
18 by William Grant
Betterer tests.
99
        parsed_message = _parse_message(TEST_MESSAGE)[1]
100
        for key, value in parsed_message.iteritems():
101
            self.assertEqual(value, cmsg[key])
25 by William Grant
get_messages tests.
102
103
104
class TestGetMessages(unittest.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['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)
36 by William Grant
Test the format.
130
        messages = conn.get_messages(archive, 'date', 1, '')[1]
131
        self.assertMessages([0], messages)
132
        cmsg = messages[0]
133
        expected_content = TEMPLATE_MESSAGE.format(
134
            date=formatdate(0), id='<message0@example.com>')
135
        self.assertEqual(expected_content, messages[0]['content'])
136
        pmsg = _parse_message(expected_content)[1]
137
        self.assertEqual(pmsg['date'], cmsg['date'])
138
        self.assertEqual(pmsg['from'], cmsg['from'])
139
        self.assertEqual(pmsg['subject'], cmsg['subject'])
140
        self.assertEqual(pmsg['message-id'], cmsg['message-id'])
25 by William Grant
get_messages tests.
141
142
    def test_limit(self):
143
        conn, archive = self.makeArchive()
144
        self.makeMessages(conn, archive, 4)
145
        self.assertMessages(
26 by William Grant
Expose a backward memo too.
146
            [0, 1], conn.get_messages(archive, 'date', 2, '')[1])
25 by William Grant
get_messages tests.
147
148
    def test_order(self):
149
        conn, archive = self.makeArchive()
150
        self.makeMessages(conn, archive, 4)
151
        self.assertMessages(
26 by William Grant
Expose a backward memo too.
152
            [3, 2], conn.get_messages(archive, '-date', 2, '')[1])
25 by William Grant
get_messages tests.
153
154
    def test_batching_forward(self):
155
        conn, archive = self.makeArchive()
156
        self.makeMessages(conn, archive, 5)
26 by William Grant
Expose a backward memo too.
157
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
25 by William Grant
get_messages tests.
158
        self.assertMessages([0, 1], messages)
26 by William Grant
Expose a backward memo too.
159
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
25 by William Grant
get_messages tests.
160
        self.assertMessages([2, 3], messages)
26 by William Grant
Expose a backward memo too.
161
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
25 by William Grant
get_messages tests.
162
        self.assertMessages([4], messages)
27 by William Grant
Fix batching to almost work backwards too.
163
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
164
        self.assertIs(None, prev)
165
        self.assertMessages([], messages)
26 by William Grant
Expose a backward memo too.
166
        self.assertIs(None, next)
27 by William Grant
Fix batching to almost work backwards too.
167
168
    def test_batching_backward(self):
169
        conn, archive = self.makeArchive()
170
        self.makeMessages(conn, archive, 5)
171
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
172
        self.assertMessages([0, 1], messages)
173
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
174
        self.assertMessages([2, 3], messages)
29 by William Grant
Do backward correctly.
175
        prev, messages, next = conn.get_messages(
176
            archive, 'date', 2, prev, backward=True)
177
        self.assertMessages([0, 1], messages)
178
        prev, messages, next = conn.get_messages(
179
            archive, 'date', 2, prev, backward=True)
180
        self.assertIs(None, prev)
181
        self.assertMessages([], messages)
182
        self.assertIs(None, next)
32 by William Grant
Date filtering.
183
184
    def test_date_filter(self):
185
        conn, archive = self.makeArchive()
186
        self.makeMessages(conn, archive, 10)
187
        start = datetime.datetime.utcfromtimestamp(250).replace(
188
            tzinfo=tzutc())
189
        finish = datetime.datetime.utcfromtimestamp(500).replace(
190
            tzinfo=tzutc())
191
        prev, messages, next = conn.get_messages(
192
            archive, 'date', 2, '', start_date=start, finish_date=finish)
193
        self.assertMessages([3, 4], messages)
194
        prev, messages, next = conn.get_messages(
195
            archive, 'date', 2, next, start_date=start, finish_date=finish)
196
        self.assertMessages([5], messages)