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

« back to all changes in this revision

Viewing changes to grackle/server/tests/test_model.py

  • Committer: William Grant
  • Date: 2012-01-22 09:40:20 UTC
  • Revision ID: william.grant@canonical.com-20120122094020-nxzwb4dcgppegynu
BettererĀ tests.

Show diffs side-by-side

added added

removed removed

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