~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 11:39:14 UTC
  • Revision ID: william.grant@canonical.com-20120122113914-2x6ghrw3fflrdd0b
Python get_messages now takes a string memo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
__metaclass__ = type
18
18
 
19
 
import fixtures
20
 
 
21
 
from grackle.server.testing.cassandra import TemporaryDB
22
 
from grackle.server.model import CassandraConnection
23
 
 
24
 
 
25
 
TEST_MESSAGE = """\
 
19
import datetime
 
20
import os
 
21
from tempfile import _RandomNameSequence
 
22
import time
 
23
import unittest
 
24
 
 
25
from dateutil.tz import (
 
26
    tzoffset,
 
27
    tzutc,
 
28
    )
 
29
from pycassa.util import convert_uuid_to_time
 
30
 
 
31
from grackle.model import (
 
32
    CassandraConnection,
 
33
    _parse_message,
 
34
    )
 
35
 
 
36
 
 
37
TEMPLATE_MESSAGE = """\
26
38
From: sysadmin@example.com
27
39
To: developer@example.com
28
40
Subject: Everything is broken
29
 
Date: Sat, 1 Jan 2000 11:02:34 +1100
30
 
Message-Id: <aaaaaaaaaaaaa@example.com>
 
41
Date: {date}
 
42
Message-Id: {id}
31
43
 
32
44
Help, everything has just broken.
33
45
"""
34
46
 
35
 
 
36
 
class TestModel(fixtures.TestWithFixtures):
37
 
 
38
 
    def test_add_message(self):
39
 
        keyspace = self.useFixture(TemporaryDB()).keyspace
40
 
        c = CassandraConnection(keyspace, ['localhost:9160'])
41
 
        c.add_message('foo', TEST_MESSAGE)
42
 
 
43
 
 
44
 
class TestModel(fixtures.TestWithFixtures):
45
 
 
46
 
    def test_add_message(self):
47
 
        keyspace = self.useFixture(TemporaryDB()).keyspace
48
 
        c = CassandraConnection(keyspace, ['localhost:9160'])
49
 
        key = c.add_message('foo', TEST_MESSAGE)
 
47
TEST_MESSAGE = TEMPLATE_MESSAGE.format(
 
48
    date='Sat, 1 Jan 2000 11:02:34 +1100',
 
49
    id='<aaaaaaaaaaaaa@example.com>')
 
50
 
 
51
 
 
52
class TestParseMessage(unittest.TestCase):
 
53
 
 
54
    def test_works(self):
 
55
        # _parse_message extracts interesting fields. It also parses the
 
56
        # date and returns it separately.
 
57
        date, msg = _parse_message(TEST_MESSAGE)
 
58
        self.assertEqual('sysadmin@example.com', msg['from'])
 
59
        self.assertEqual('developer@example.com', msg['to'])
 
60
        self.assertEqual('Everything is broken', msg['subject'])
 
61
        self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
 
62
        self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
 
63
        self.assertEqual(
 
64
            datetime.datetime(
 
65
                2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
 
66
            date)
 
67
 
 
68
 
 
69
class TestAddMessage(unittest.TestCase):
 
70
 
 
71
    def test_add_message(self):
 
72
        c = CassandraConnection(
 
73
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
 
74
        archive = next(_RandomNameSequence())
 
75
 
 
76
        # Write the message out to Cassandra, and read it back in.
 
77
        key = c.add_message(archive, TEST_MESSAGE)
50
78
        cmsg = c.messages.get(key)
 
79
 
 
80
        # The archive should contain a single message, a reference to
 
81
        # our new key.
 
82
        archive_messages = c.archive_messages.get(archive).items()
 
83
        self.assertEqual(1, len(archive_messages))
 
84
        self.assertEqual(key, archive_messages[0][1])
 
85
 
 
86
        # The key in archive_message is a TimeUUID for the Date field in
 
87
        # the message.
 
88
        utctime = time.mktime(datetime.datetime(
 
89
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
 
90
        self.assertEqual(
 
91
            utctime,
 
92
            convert_uuid_to_time(archive_messages[0][0]))
 
93
 
 
94
        # The stored message contains the full original text of the
 
95
        # message, as well as interesting fields parsed out.
51
96
        self.assertEqual(TEST_MESSAGE, cmsg['content'])
52
 
        self.assertEqual('sysadmin@example.com', cmsg['from'])
53
 
        self.assertEqual('developer@example.com', cmsg['to'])
54
 
        self.assertEqual('Everything is broken', cmsg['subject'])
55
 
        self.assertEqual('2000-01-01T11:02:34+11:00', cmsg['date'])
56
 
        self.assertEqual('<aaaaaaaaaaaaa@example.com>', cmsg['message-id'])
 
97
        parsed_message = _parse_message(TEST_MESSAGE)[1]
 
98
        for key, value in parsed_message.iteritems():
 
99
            self.assertEqual(value, cmsg[key])