~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 10:39:15 UTC
  • Revision ID: william.grant@canonical.com-20120122103915-lqjw4jsaw3ug1q2g
Merge grackle.server into grackle. Alter Makefile to run all the 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 os
 
21
from tempfile import _RandomNameSequence
 
22
import time
 
23
import unittest
 
24
 
 
25
from dateutil.tz import (
 
26
    tzoffset,
 
27
    tzutc,
 
28
    )
19
29
import fixtures
 
30
from pycassa.util import convert_uuid_to_time
20
31
 
21
 
from grackle.server.testing.cassandra import TemporaryDB
22
 
from grackle.server.model import CassandraConnection
 
32
from grackle.model import (
 
33
    CassandraConnection,
 
34
    _parse_message,
 
35
    )
23
36
 
24
37
 
25
38
TEST_MESSAGE = """\
33
46
"""
34
47
 
35
48
 
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)
 
49
class TestParseMessage(unittest.TestCase):
 
50
 
 
51
    def test_works(self):
 
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'])
 
60
        self.assertEqual(
 
61
            datetime.datetime(
 
62
                2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
 
63
            date)
 
64
 
 
65
 
 
66
class TestCassandra(fixtures.TestWithFixtures):
 
67
 
 
68
    def test_add_message(self):
 
69
        c = CassandraConnection(
 
70
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
 
71
 
 
72
        archive = next(_RandomNameSequence())
 
73
 
 
74
        # Write the message out to Cassandra, and read it back in.
 
75
        key = c.add_message(archive, TEST_MESSAGE)
50
76
        cmsg = c.messages.get(key)
 
77
 
 
78
        # The archive should contain a single message, a reference to
 
79
        # our new key.
 
80
        archive_messages = c.archive_messages.get(archive).items()
 
81
        self.assertEqual(1, len(archive_messages))
 
82
        self.assertEqual(key, archive_messages[0][1])
 
83
 
 
84
        # The key in archive_message is a TimeUUID for the Date field in
 
85
        # the message.
 
86
        utctime = time.mktime(datetime.datetime(
 
87
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
 
88
        self.assertEqual(
 
89
            utctime,
 
90
            convert_uuid_to_time(archive_messages[0][0]))
 
91
 
 
92
        # The stored message contains the full original text of the
 
93
        # message, as well as interesting fields parsed out.
51
94
        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'])
 
95
        parsed_message = _parse_message(TEST_MESSAGE)[1]
 
96
        for key, value in parsed_message.iteritems():
 
97
            self.assertEqual(value, cmsg[key])