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

« back to all changes in this revision

Viewing changes to grackle/tests/test_model.py

  • Committer: Aaron Bentley
  • Date: 2012-01-10 11:16:56 UTC
  • Revision ID: aaron@canonical.com-20120110111656-9daoyj766z8r184a
Add test framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
19
 
import datetime
20
 
from email.utils import formatdate
21
 
import os
22
 
from tempfile import _RandomNameSequence
23
 
import time
24
 
import unittest
25
 
 
26
 
from dateutil.tz import (
27
 
    tzoffset,
28
 
    tzutc,
29
 
    )
30
 
from pycassa.util import convert_uuid_to_time
31
 
 
32
 
from grackle.model import (
33
 
    CassandraConnection,
34
 
    _parse_message,
35
 
    )
36
 
 
37
 
 
38
 
TEMPLATE_MESSAGE = """\
39
 
From: sysadmin@example.com
40
 
To: developer@example.com
41
 
Subject: Everything is broken
42
 
Date: {date}
43
 
Message-Id: {id}
44
 
 
45
 
Help, everything has just broken.
46
 
"""
47
 
 
48
 
TEST_MESSAGE = TEMPLATE_MESSAGE.format(
49
 
    date='Sat, 1 Jan 2000 11:02:34 +1100',
50
 
    id='<aaaaaaaaaaaaa@example.com>')
51
 
 
52
 
 
53
 
class TestParseMessage(unittest.TestCase):
54
 
 
55
 
    def test_works(self):
56
 
        # _parse_message extracts interesting fields. It also parses the
57
 
        # date and returns it separately.
58
 
        date, msg = _parse_message(TEST_MESSAGE)
59
 
        self.assertEqual('sysadmin@example.com', msg['from'])
60
 
        self.assertEqual('developer@example.com', msg['to'])
61
 
        self.assertEqual('Everything is broken', msg['subject'])
62
 
        self.assertEqual('2000-01-01T11:02:34+11:00', msg['date'])
63
 
        self.assertEqual('<aaaaaaaaaaaaa@example.com>', msg['message-id'])
64
 
        self.assertEqual(
65
 
            datetime.datetime(
66
 
                2000, 1, 1, 11, 2, 34, tzinfo=tzoffset('', 39600)),
67
 
            date)
68
 
 
69
 
 
70
 
class TestAddMessage(unittest.TestCase):
71
 
 
72
 
    def test_add_message(self):
73
 
        c = CassandraConnection(
74
 
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
75
 
        archive = next(_RandomNameSequence())
76
 
 
77
 
        # Write the message out to Cassandra, and read it back in.
78
 
        key = c.add_message(archive, TEST_MESSAGE)
79
 
        cmsg = c.messages.get(key)
80
 
 
81
 
        # The archive should contain a single message, a reference to
82
 
        # our new key.
83
 
        archive_messages = c.archive_messages.get(archive).items()
84
 
        self.assertEqual(1, len(archive_messages))
85
 
        self.assertEqual(key, archive_messages[0][1])
86
 
 
87
 
        # The key in archive_message is a TimeUUID for the Date field in
88
 
        # the message.
89
 
        utctime = time.mktime(datetime.datetime(
90
 
            2000, 1, 1, 0, 2, 34, tzinfo=tzutc()).timetuple())
91
 
        self.assertEqual(
92
 
            utctime,
93
 
            convert_uuid_to_time(archive_messages[0][0]))
94
 
 
95
 
        # The stored message contains the full original text of the
96
 
        # message, as well as interesting fields parsed out.
97
 
        self.assertEqual(TEST_MESSAGE, cmsg['content'])
98
 
        parsed_message = _parse_message(TEST_MESSAGE)[1]
99
 
        for key, value in parsed_message.iteritems():
100
 
            self.assertEqual(value, cmsg[key])
101
 
 
102
 
 
103
 
class TestGetMessages(unittest.TestCase):
104
 
 
105
 
    def assertMessages(self, expected_ids, messages):
106
 
        expected_msgids = [
107
 
            '<message%d@example.com>' % id for id in expected_ids]
108
 
        actual_msgids = [msg['message-id'] for msg in messages]
109
 
        self.assertEqual(expected_msgids, actual_msgids)
110
 
 
111
 
    def makeMessages(self, conn, archive, count):
112
 
        return [
113
 
            conn.add_message(
114
 
                archive,
115
 
                TEMPLATE_MESSAGE.format(
116
 
                    date=formatdate(i * 100),
117
 
                    id='<message%d@example.com>' % i))
118
 
            for i in range(count)]
119
 
 
120
 
    def makeArchive(self):
121
 
        conn = CassandraConnection(
122
 
            os.environ['GRACKLE_TEST_KEYSPACE'], ['localhost:9160'])
123
 
        archive = next(_RandomNameSequence())
124
 
        return conn, archive
125
 
 
126
 
    def test_single_message(self):
127
 
        conn, archive = self.makeArchive()
128
 
        self.makeMessages(conn, archive, 1)
129
 
        self.assertMessages(
130
 
            [0], conn.get_messages(archive, 'date', 1, '')[1])
131
 
 
132
 
    def test_limit(self):
133
 
        conn, archive = self.makeArchive()
134
 
        self.makeMessages(conn, archive, 4)
135
 
        self.assertMessages(
136
 
            [0, 1], conn.get_messages(archive, 'date', 2, '')[1])
137
 
 
138
 
    def test_order(self):
139
 
        conn, archive = self.makeArchive()
140
 
        self.makeMessages(conn, archive, 4)
141
 
        self.assertMessages(
142
 
            [3, 2], conn.get_messages(archive, '-date', 2, '')[1])
143
 
 
144
 
    def test_batching_forward(self):
145
 
        conn, archive = self.makeArchive()
146
 
        self.makeMessages(conn, archive, 5)
147
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
148
 
        self.assertMessages([0, 1], messages)
149
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
150
 
        self.assertMessages([2, 3], messages)
151
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
152
 
        self.assertMessages([4], messages)
153
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
154
 
        self.assertIs(None, prev)
155
 
        self.assertMessages([], messages)
156
 
        self.assertIs(None, next)
157
 
 
158
 
    def test_batching_backward(self):
159
 
        conn, archive = self.makeArchive()
160
 
        self.makeMessages(conn, archive, 5)
161
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, '')
162
 
        self.assertMessages([0, 1], messages)
163
 
        prev, messages, next = conn.get_messages(archive, 'date', 2, next)
164
 
        self.assertMessages([2, 3], messages)
165
 
        prev, messages, next = conn.get_messages(
166
 
            archive, 'date', 2, prev, backward=True)
167
 
        self.assertMessages([0, 1], messages)
168
 
        prev, messages, next = conn.get_messages(
169
 
            archive, 'date', 2, prev, backward=True)
170
 
        self.assertIs(None, prev)
171
 
        self.assertMessages([], messages)
172
 
        self.assertIs(None, next)