1
# Copyright (c) 2012 Canonical Ltd
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.
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.
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/>.
21
from tempfile import _RandomNameSequence
25
from dateutil.tz import (
30
from pycassa.util import convert_uuid_to_time
32
from grackle.model import (
39
From: sysadmin@example.com
40
To: developer@example.com
41
Subject: Everything is broken
42
Date: Sat, 1 Jan 2000 11:02:34 +1100
43
Message-Id: <aaaaaaaaaaaaa@example.com>
45
Help, everything has just broken.
49
class TestParseMessage(unittest.TestCase):
52
# _parse_message extracts interesting fields. It also parses the
53
# date and returns it separately.
54
date, msg = _parse_message(TEST_MESSAGE)
55
self.assertEqual('sysadmin@example.com', msg['from'])
56
self.assertEqual('developer@example.com', msg['to'])
57
self.assertEqual('Everything is broken', msg['subject'])
58
self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
59
self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
62
2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
66
class TestCassandra(fixtures.TestWithFixtures):
68
def test_add_message(self):
69
c = CassandraConnection(
70
os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
72
archive = next(_RandomNameSequence())
74
# Write the message out to Cassandra, and read it back in.
75
key = c.add_message(archive, TEST_MESSAGE)
76
cmsg = c.messages.get(key)
78
# The archive should contain a single message, a reference to
80
archive_messages = c.archive_messages.get(archive).items()
81
self.assertEqual(1, len(archive_messages))
82
self.assertEqual(key, archive_messages[0][1])
84
# The key in archive_message is a TimeUUID for the Date field in
86
utctime = time.mktime(datetime.datetime(
87
2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
90
convert_uuid_to_time(archive_messages[0][0]))
92
# The stored message contains the full original text of the
93
# message, as well as interesting fields parsed out.
94
self.assertEqual(TEST_MESSAGE, cmsg['content'])
95
parsed_message = _parse_message(TEST_MESSAGE)[1]
96
for key, value in parsed_message.iteritems():
97
self.assertEqual(value, cmsg[key])