Commit cc0a5d4b authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

astakos: Adapt tests to 2.6-style unittest

Define assertGreater, assertIn, assertRaises in snf_django.utils.testing
parent de5e5177
......@@ -36,7 +36,7 @@ import copy
import datetime
import functools
from snf_django.utils.testing import with_settings, override_settings
from snf_django.utils.testing import with_settings, override_settings, assertIn
from django.test import TestCase, Client
from django.core import mail
......@@ -1436,7 +1436,7 @@ class QuotaAPITest(TestCase):
self.assertEqual(r.status_code, 200)
body = json.loads(r.content)
for name in resource_names:
self.assertIn(name, body)
assertIn(name, body)
# get quota
r = client.get(u('quotas'), follow=True)
......@@ -1447,9 +1447,9 @@ class QuotaAPITest(TestCase):
self.assertEqual(r.status_code, 200)
body = json.loads(r.content)
system_quota = body['system']
self.assertIn('system', body)
assertIn('system', body)
for name in resource_names:
self.assertIn(name, system_quota)
assertIn(name, system_quota)
r = client.get(u('service_quotas'), follow=True)
self.assertEqual(r.status_code, 401)
......@@ -1458,7 +1458,7 @@ class QuotaAPITest(TestCase):
r = client.get(u('service_quotas'), follow=True, **s1_headers)
self.assertEqual(r.status_code, 200)
body = json.loads(r.content)
self.assertIn(user.uuid, body)
assertIn(user.uuid, body)
r = client.get(u('commissions'), follow=True, **s1_headers)
self.assertEqual(r.status_code, 200)
......@@ -1539,7 +1539,7 @@ class QuotaAPITest(TestCase):
self.assertEqual(r.status_code, 200)
body = json.loads(r.content)
self.assertEqual(body['serial'], serial)
self.assertIn('issue_time', body)
assertIn('issue_time', body)
self.assertEqual(body['provisions'], commission_request['provisions'])
self.assertEqual(body['name'], commission_request['name'])
......
......@@ -33,6 +33,7 @@
from django.test import TestCase
from snf_django.utils.testing import assertGreater, assertIn, assertRaises
import astakos.quotaholder_app.callpoint as qh
from astakos.quotaholder_app.exception import (
InvalidDataError,
......@@ -77,7 +78,7 @@ class QuotaholderTest(TestCase):
s1 = self.issue_commission([((holder, source, resource1), limit1/2),
((holder, source, resource2), limit2)],
name="initial")
self.assertGreater(s1, 0)
assertGreater(s1, 0)
r = qh.get_commission(self.client, s1)
provisions = [
......@@ -95,14 +96,14 @@ class QuotaholderTest(TestCase):
self.assertEqual(r['serial'], s1)
ps = r['provisions']
for p in ps:
self.assertIn(p, provisions)
assertIn(p, provisions)
with self.assertRaises(NoCommissionError):
with assertRaises(NoCommissionError):
qh.get_commission(self.client, s1+1)
# commission exceptions
with self.assertRaises(NoCapacityError) as cm:
with assertRaises(NoCapacityError) as cm:
self.issue_commission([((holder, source, resource1), 1),
((holder, source, resource2), 1)])
......@@ -115,7 +116,7 @@ class QuotaholderTest(TestCase):
self.assertEqual(e.data['usage'], limit2)
self.assertEqual(e.data['limit'], limit2)
with self.assertRaises(NoQuantityError) as cm:
with assertRaises(NoQuantityError) as cm:
self.issue_commission([((holder, source, resource1), -1)])
e = cm.exception
......@@ -127,7 +128,7 @@ class QuotaholderTest(TestCase):
self.assertEqual(e.data['usage'], 0)
self.assertEqual(e.data['limit'], 0)
with self.assertRaises(NoHoldingError) as cm:
with assertRaises(NoHoldingError) as cm:
self.issue_commission([((holder, source, resource1), 1),
(('nonex', source, resource1), 1)])
......@@ -138,7 +139,7 @@ class QuotaholderTest(TestCase):
self.assertEqual(provision['resource'], resource1)
self.assertEqual(provision['quantity'], 1)
with self.assertRaises(DuplicateError) as cm:
with assertRaises(DuplicateError) as cm:
self.issue_commission([((holder, source, resource1), 1),
((holder, source, resource1), 2)])
......@@ -149,7 +150,7 @@ class QuotaholderTest(TestCase):
self.assertEqual(provision['resource'], resource1)
self.assertEqual(provision['quantity'], 2)
with self.assertRaises(InvalidDataError):
with assertRaises(InvalidDataError):
self.issue_commission([((holder, source, resource1), 1),
((holder, source, resource1), "nan")])
......@@ -217,7 +218,7 @@ class QuotaholderTest(TestCase):
}
self.assertEqual(r, quotas)
with self.assertRaises(NoCapacityError):
with assertRaises(NoCapacityError):
self.issue_commission(
[((holder, source, resource1), limit1/2+1)])
......
......@@ -186,3 +186,44 @@ class BaseAPITest(TestCase):
def assertItemNotFound(self, response):
self.assertFault(response, 404, 'itemNotFound')
# Imitate unittest assertions new in Python 2.7
class _AssertRaisesContext(object):
"""
A context manager used to implement TestCase.assertRaises* methods.
Adapted from unittest2.
"""
def __init__(self, expected):
self.expected = expected
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
if exc_type is None:
try:
exc_name = self.expected.__name__
except AttributeError:
exc_name = str(self.expected)
raise AssertionError(
"%s not raised" % (exc_name,))
if not issubclass(exc_type, self.expected):
# let unexpected exceptions pass through
return False
self.exception = exc_value # store for later retrieval
return True
def assertRaises(excClass):
return _AssertRaisesContext(excClass)
def assertGreater(x, y):
assert x > y
def assertIn(x, y):
assert x in y
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment