Raise self-formatted exceptions in SQlite.

- A helper, marconi.tests.util.Expected, is added to replace
   testtools.ExpectedException, since the later one does not obey
   the exception hierarchy.

 - ClaimDoesNotExist is added, similar to MessageDoesNotExist.

Change-Id: I8f592184cbf7f3bdab95e9adef1306055097cb6f
This commit is contained in:
Zhihao Yuan
2013-04-02 23:41:27 -04:00
committed by Zhihao Yuan
parent c82fbd7d5b
commit 772474264d
6 changed files with 57 additions and 43 deletions

View File

@@ -37,3 +37,12 @@ class MessageDoesNotExist(DoesNotExist):
"queue %(queue)s of tenant %(tenant)s") %
dict(mid=mid, queue=queue, tenant=tenant))
super(MessageDoesNotExist, self).__init__(msg)
class ClaimDoesNotExist(DoesNotExist):
def __init__(self, cid, queue, tenant):
msg = (_("Claim %(cid)s does not exist in "
"queue %(queue)s of tenant %(tenant)s") %
dict(cid=cid, queue=queue, tenant=tenant))
super(ClaimDoesNotExist, self).__init__(msg)

View File

@@ -49,7 +49,7 @@ class Queue(base.QueueBase):
where tenant = ? and name = ?''', tenant, name)[0]
except _NoResult:
_queue_doesnotexist(name, tenant)
raise exceptions.QueueDoesNotExist(name, tenant)
def upsert(self, name, metadata, tenant):
with self.driver('immediate'):
@@ -78,7 +78,7 @@ class Queue(base.QueueBase):
where tenant = ? and name = ?''', tenant, name)
if qid is None:
_queue_doesnotexist(name, tenant)
raise exceptions.QueueDoesNotExist(name, tenant)
return {
'messages': messages,
@@ -125,7 +125,7 @@ class Message(base.MessageBase):
}
except (_NoResult, _BadID):
_msg_doesnotexist(message_id)
raise exceptions.MessageDoesNotExist(message_id, queue, tenant)
def list(self, queue, tenant, marker=None,
limit=10, echo=False, client_uuid=None):
@@ -275,7 +275,7 @@ class Claim(base.ClaimBase):
)
except (_NoResult, _BadID):
_claim_doesnotexist(claim_id)
raise exceptions.ClaimDoesNotExist(claim_id, queue, tenant)
def create(self, queue, metadata, tenant, limit=10):
with self.driver('immediate'):
@@ -340,10 +340,10 @@ class Claim(base.ClaimBase):
''', metadata['ttl'], _cid_decode(claim_id), tenant, queue)
if not self.driver.affected:
_claim_doesnotexist(claim_id)
raise exceptions.ClaimDoesNotExist(claim_id, queue, tenant)
except _BadID:
_claim_doesnotexist(claim_id)
raise exceptions.ClaimDoesNotExist(claim_id, queue, tenant)
def delete(self, queue, claim_id, tenant):
try:
@@ -369,27 +369,6 @@ class _BadID(Exception):
pass
def _queue_doesnotexist(name, tenant):
msg = (_("Queue %(name)s does not exist for tenant %(tenant)s")
% dict(name=name, tenant=tenant))
raise exceptions.DoesNotExist(msg)
def _msg_doesnotexist(id):
msg = (_("Message %(id)s does not exist")
% dict(id=id))
raise exceptions.DoesNotExist(msg)
def _claim_doesnotexist(id):
msg = (_("Claim %(id)s does not exist")
% dict(id=id))
raise exceptions.DoesNotExist(msg)
def _get_qid(driver, queue, tenant):
try:
return driver.get('''
@@ -397,7 +376,7 @@ def _get_qid(driver, queue, tenant):
where tenant = ? and name = ?''', tenant, queue)[0]
except _NoResult:
_queue_doesnotexist(queue, tenant)
raise exceptions.QueueDoesNotExist(queue, tenant)
# The utilities below make the database IDs opaque to the users

View File

@@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import testtools
from marconi.common import config
from marconi.tests import util as testing
@@ -38,5 +36,5 @@ class TestConfig(testing.TestBase):
def test_wrong_type(self):
ns = config.namespace('local')
with testtools.ExpectedException(config.cfg.Error):
with testing.expected(config.cfg.Error):
ns.from_options(opt={})

View File

@@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import testtools
from marconi.storage import exceptions
from marconi.storage import sqlite
from marconi.tests import util as testing
@@ -87,7 +85,7 @@ class TestSqlite(testing.TestBase):
# can not delete a message with a wrong claim
meta, msgs = self.claim_ctrl.create('fizbit', {'ttl': 10}, '480924')
with testtools.ExpectedException(exceptions.NotPermitted):
with testing.expected(exceptions.NotPermitted):
self.msg_ctrl.delete('fizbit', msgid, '480924', meta['id'])
self.msg_ctrl.get('fizbit', msgid, '480924')
@@ -101,7 +99,7 @@ class TestSqlite(testing.TestBase):
# delete a message under a claim
self.msg_ctrl.delete('fizbit', msgid, '480924', meta['id'])
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.msg_ctrl.get('fizbit', msgid, '480924')
meta, msgs = self.claim_ctrl.get('fizbit', meta['id'], '480924')
@@ -114,13 +112,13 @@ class TestSqlite(testing.TestBase):
# claim expires
self.claim_ctrl.update('fizbit', meta['id'], {'ttl': 0}, '480924')
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.claim_ctrl.get('fizbit', meta['id'], '480924')
# delete the claim
self.claim_ctrl.delete('fizbit', meta['id'], '480924')
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.claim_ctrl.update('fizbit', meta['id'], {'ttl': 40}, '480924')
def test_expired_messages(self):
@@ -131,19 +129,19 @@ class TestSqlite(testing.TestBase):
msgid = self.msg_ctrl.post('fizbit', doc, '480924',
client_uuid='unused')[0]
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.msg_ctrl.get('fizbit', msgid, '480924')
def test_nonexsitent(self):
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.msg_ctrl.post('nonexistent', [], '480924',
client_uuid='30387f00')
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
for _ in self.msg_ctrl.list('nonexistent', '480924'):
pass
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.queue_ctrl.stats('nonexistent', '480924')
#TODO(zyuan): move this to tests/storage/test_impl_sqlite.py
@@ -152,12 +150,12 @@ class TestSqlite(testing.TestBase):
# SQlite-specific tests. Since all IDs exposed in APIs are opaque,
# any ill-formed IDs should be regarded as non-existing ones.
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.msg_ctrl.get('nonexistent', 'illformed', '480924')
self.claim_ctrl.delete('nonexistent', 'illformed', '480924')
with testtools.ExpectedException(exceptions.DoesNotExist):
with testing.expected(exceptions.DoesNotExist):
self.claim_ctrl.update('nonexistent', 'illformed',
{'ttl': 40}, '480924')

View File

@@ -1,3 +1,4 @@
"""Test utilities"""
from marconi.tests.util.base import TestBase # NOQA
from marconi.tests.util.helpers import expected # NOQA

View File

@@ -0,0 +1,29 @@
# Copyright (c) 2013 Rackspace Hosting, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
@contextlib.contextmanager
def expected(*exc_type):
assert len(exc_type) > 0
try:
yield
except exc_type:
pass
else:
raise AssertionError(
'Not raised: %s' % ', '.join(e.__name__ for e in exc_type))