~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Curtis Hovey
  • Date: 2012-03-16 20:25:49 UTC
  • Revision ID: curtis.hovey@canonical.com-20120316202549-2dqmxc2f8d9dr7k9
Raise an error when the archive already exists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
from testtools import ExpectedException
19
19
 
20
 
from grackle.client import (
21
 
    GrackleClient,
 
20
from grackle.client import GrackleClient
 
21
from grackle.error import (
 
22
    ArchiveIdExists,
22
23
    UnparsableDateRange,
23
24
    UnsupportedDisplayType,
24
25
    UnsupportedOrder,
133
134
        self.logger = logging.getLogger('http')
134
135
        BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
135
136
 
 
137
    def do_PUT(self):
 
138
        """Create an archive or message on PUT."""
 
139
        scheme, netloc, path, params, query_string, fragments = (
 
140
            urlparse(self.path))
 
141
        parts = path.split('/')
 
142
        if parts[1] != 'archive':
 
143
            # This is an unknonwn operation?
 
144
            return
 
145
        if len(parts) == 3:
 
146
            # This expected path is /archive/archive_id.
 
147
            try:
 
148
                self.server.store.put_archive(parts[2])
 
149
                self.send_response(httplib.CREATED)
 
150
                self.end_headers()
 
151
                self.wfile.close()
 
152
            except Exception, error:
 
153
                self.send_response(
 
154
                    httplib.BAD_REQUEST, error.__doc__)
 
155
        if len(parts) == 4:
 
156
            # This expected path is /archive/archive_id/message_id.
 
157
            try:
 
158
                message = self.rfile.read(int(self.headers['content-length']))
 
159
                self.server.store.put_message(parts[2], parts[3], message)
 
160
                self.send_response(httplib.CREATED)
 
161
                self.end_headers()
 
162
                self.wfile.close()
 
163
            except:
 
164
                self.send_error(httplib.BAD_REQUEST)
 
165
 
136
166
    def do_POST(self):
137
 
        """Create a message on POST."""
 
167
        """Change a message on POST."""
138
168
        scheme, netloc, path, params, query_string, fragments = (
139
169
            urlparse(self.path))
140
170
        parts = path.split('/')
141
171
        if parts[1] != 'archive':
142
172
            # This is an unknonwn operation?
143
173
            return
144
 
        if 'content-length' in self.headers:
145
 
            operation = 'put_message'
146
 
        else:
147
 
            operation = 'hide_message'
148
 
        if operation == 'put_message':
149
 
            message = self.rfile.read(int(self.headers['content-length']))
150
 
            try:
151
 
                # This expected path is /archive/archive_id/message_id.
152
 
                self.server.store.put_message(parts[2], parts[3], message)
153
 
                self.send_response(httplib.CREATED)
154
 
                self.end_headers()
155
 
                self.wfile.close()
156
 
            except:
157
 
                self.send_error(httplib.BAD_REQUEST)
158
 
        elif operation == 'hide_message':
 
174
        if len(parts) == 4:
 
175
            # This expected path is /archive/archive_id/message_id.
159
176
            try:
160
177
                # This expected path is /archive/archive_id/message_id.
161
178
                response = self.server.store.hide_message(
190
207
        self.logger.info(message)
191
208
 
192
209
 
 
210
class TestPutArchive(TestCase):
 
211
 
 
212
    def test_put_archive(self):
 
213
        client = GrackleClient('localhost', 8410)
 
214
        message_archives = {}
 
215
        with ForkedFakeService.from_client(client, message_archives):
 
216
            client.put_archive('arch1')
 
217
            response = client.get_messages('arch1')
 
218
        self.assertEqual(0, len(response['messages']))
 
219
 
 
220
    def test_put_archive_existing_archive(self):
 
221
        client = GrackleClient('localhost', 8411)
 
222
        message_archives = {'arch1': []}
 
223
        with ForkedFakeService.from_client(client, message_archives):
 
224
            with ExpectedException(ArchiveIdExists, ''):
 
225
                client.put_archive('arch1')
 
226
 
 
227
 
193
228
class TestPutMessage(TestCase):
194
229
 
195
230
    def test_put_message(self):