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 |
|
32
by William Grant
Date filtering. |
25 |
import uuid |
18
by William Grant
Betterer tests. |
26 |
|
27 |
from dateutil.tz import ( |
|
28 |
tzoffset, |
|
29 |
tzutc, |
|
30 |
)
|
|
31 |
from pycassa.util import convert_uuid_to_time |
|
13
by William Grant
Test add_message a little. |
32 |
|
21
by William Grant
Merge grackle.server into grackle. Alter Makefile to run all the tests. |
33 |
from grackle.model import ( |
18
by William Grant
Betterer tests. |
34 |
CassandraConnection, |
35 |
_parse_message, |
|
36 |
)
|
|
13
by William Grant
Test add_message a little. |
37 |
|
38 |
||
22
by William Grant
Template messages yay. |
39 |
TEMPLATE_MESSAGE = """\ |
13
by William Grant
Test add_message a little. |
40 |
From: sysadmin@example.com
|
41 |
To: developer@example.com
|
|
42 |
Subject: Everything is broken
|
|
22
by William Grant
Template messages yay. |
43 |
Date: {date} |
44 |
Message-Id: {id} |
|
13
by William Grant
Test add_message a little. |
45 |
|
46 |
Help, everything has just broken.
|
|
47 |
"""
|
|
48 |
||
22
by William Grant
Template messages yay. |
49 |
TEST_MESSAGE = TEMPLATE_MESSAGE.format( |
50 |
date='Sat, 1 Jan 2000 11:02:34 +1100', |
|
51 |
id='<aaaaaaaaaaaaa@example.com>') |
|
52 |
||
13
by William Grant
Test add_message a little. |
53 |
|
18
by William Grant
Betterer tests. |
54 |
class TestParseMessage(unittest.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 |
||
22
by William Grant
Template messages yay. |
71 |
class TestAddMessage(unittest.TestCase): |
18
by William Grant
Betterer tests. |
72 |
|
73 |
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. |
74 |
c = CassandraConnection( |
75 |
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160']) |
|
76 |
archive = next(_RandomNameSequence()) |
|
18
by William Grant
Betterer tests. |
77 |
|
78 |
# 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. |
79 |
key = c.add_message(archive, TEST_MESSAGE) |
13
by William Grant
Test add_message a little. |
80 |
cmsg = c.messages.get(key) |
18
by William Grant
Betterer tests. |
81 |
|
82 |
# The archive should contain a single message, a reference to
|
|
83 |
# 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. |
84 |
archive_messages = c.archive_messages.get(archive).items() |
18
by William Grant
Betterer tests. |
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
|
|
31
by William Grant
Use real UTC timestamps in the archive_message TimeUUIDs. pycassa's end up including the local timezone offset :/ |
89 |
# the message. There is no UTC equivalent of time.mktime, so we
|
90 |
# must subtract the offset.
|
|
18
by William Grant
Betterer tests. |
91 |
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 :/ |
92 |
2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple()) - time.timezone |
18
by William Grant
Betterer tests. |
93 |
self.assertEqual( |
94 |
utctime, |
|
95 |
convert_uuid_to_time(archive_messages[0][0])) |
|
96 |
||
97 |
# The stored message contains the full original text of the
|
|
98 |
# message, as well as interesting fields parsed out.
|
|
13
by William Grant
Test add_message a little. |
99 |
self.assertEqual(TEST_MESSAGE, cmsg['content']) |
18
by William Grant
Betterer tests. |
100 |
parsed_message = _parse_message(TEST_MESSAGE)[1] |
101 |
for key, value in parsed_message.iteritems(): |
|
102 |
self.assertEqual(value, cmsg[key]) |
|
25
by William Grant
get_messages tests. |
103 |
|
104 |
||
105 |
class TestGetMessages(unittest.TestCase): |
|
106 |
||
107 |
def assertMessages(self, expected_ids, messages): |
|
108 |
expected_msgids = [ |
|
109 |
'<message%d@example.com>' % id for id in expected_ids] |
|
110 |
actual_msgids = [msg['message-id'] for msg in messages] |
|
111 |
self.assertEqual(expected_msgids, actual_msgids) |
|
112 |
||
113 |
def makeMessages(self, conn, archive, count): |
|
114 |
return [ |
|
115 |
conn.add_message( |
|
116 |
archive, |
|
117 |
TEMPLATE_MESSAGE.format( |
|
118 |
date=formatdate(i * 100), |
|
119 |
id='<message%d@example.com>' % i)) |
|
120 |
for i in range(count)] |
|
121 |
||
122 |
def makeArchive(self): |
|
123 |
conn = CassandraConnection( |
|
124 |
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160']) |
|
125 |
archive = next(_RandomNameSequence()) |
|
126 |
return conn, archive |
|
127 |
||
128 |
def test_single_message(self): |
|
129 |
conn, archive = self.makeArchive() |
|
130 |
self.makeMessages(conn, archive, 1) |
|
131 |
self.assertMessages( |
|
26
by William Grant
Expose a backward memo too. |
132 |
[0], conn.get_messages(archive, 'date', 1, '')[1]) |
25
by William Grant
get_messages tests. |
133 |
|
134 |
def test_limit(self): |
|
135 |
conn, archive = self.makeArchive() |
|
136 |
self.makeMessages(conn, archive, 4) |
|
137 |
self.assertMessages( |
|
26
by William Grant
Expose a backward memo too. |
138 |
[0, 1], conn.get_messages(archive, 'date', 2, '')[1]) |
25
by William Grant
get_messages tests. |
139 |
|
140 |
def test_order(self): |
|
141 |
conn, archive = self.makeArchive() |
|
142 |
self.makeMessages(conn, archive, 4) |
|
143 |
self.assertMessages( |
|
26
by William Grant
Expose a backward memo too. |
144 |
[3, 2], conn.get_messages(archive, '-date', 2, '')[1]) |
25
by William Grant
get_messages tests. |
145 |
|
146 |
def test_batching_forward(self): |
|
147 |
conn, archive = self.makeArchive() |
|
148 |
self.makeMessages(conn, archive, 5) |
|
26
by William Grant
Expose a backward memo too. |
149 |
prev, messages, next = conn.get_messages(archive, 'date', 2, '') |
25
by William Grant
get_messages tests. |
150 |
self.assertMessages([0, 1], messages) |
26
by William Grant
Expose a backward memo too. |
151 |
prev, messages, next = conn.get_messages(archive, 'date', 2, next) |
25
by William Grant
get_messages tests. |
152 |
self.assertMessages([2, 3], messages) |
26
by William Grant
Expose a backward memo too. |
153 |
prev, messages, next = conn.get_messages(archive, 'date', 2, next) |
25
by William Grant
get_messages tests. |
154 |
self.assertMessages([4], messages) |
27
by William Grant
Fix batching to almost work backwards too. |
155 |
prev, messages, next = conn.get_messages(archive, 'date', 2, next) |
156 |
self.assertIs(None, prev) |
|
157 |
self.assertMessages([], messages) |
|
26
by William Grant
Expose a backward memo too. |
158 |
self.assertIs(None, next) |
27
by William Grant
Fix batching to almost work backwards too. |
159 |
|
160 |
def test_batching_backward(self): |
|
161 |
conn, archive = self.makeArchive() |
|
162 |
self.makeMessages(conn, archive, 5) |
|
163 |
prev, messages, next = conn.get_messages(archive, 'date', 2, '') |
|
164 |
self.assertMessages([0, 1], messages) |
|
165 |
prev, messages, next = conn.get_messages(archive, 'date', 2, next) |
|
166 |
self.assertMessages([2, 3], messages) |
|
29
by William Grant
Do backward correctly. |
167 |
prev, messages, next = conn.get_messages( |
168 |
archive, 'date', 2, prev, backward=True) |
|
169 |
self.assertMessages([0, 1], messages) |
|
170 |
prev, messages, next = conn.get_messages( |
|
171 |
archive, 'date', 2, prev, backward=True) |
|
172 |
self.assertIs(None, prev) |
|
173 |
self.assertMessages([], messages) |
|
174 |
self.assertIs(None, next) |
|
32
by William Grant
Date filtering. |
175 |
|
176 |
def test_date_filter(self): |
|
177 |
conn, archive = self.makeArchive() |
|
178 |
self.makeMessages(conn, archive, 10) |
|
179 |
start = datetime.datetime.utcfromtimestamp(250).replace( |
|
180 |
tzinfo=tzutc()) |
|
181 |
finish = datetime.datetime.utcfromtimestamp(500).replace( |
|
182 |
tzinfo=tzutc()) |
|
183 |
prev, messages, next = conn.get_messages( |
|
184 |
archive, 'date', 2, '', start_date=start, finish_date=finish) |
|
185 |
self.assertMessages([3, 4], messages) |
|
186 |
prev, messages, next = conn.get_messages( |
|
187 |
archive, 'date', 2, next, start_date=start, finish_date=finish) |
|
188 |
self.assertMessages([5], messages) |