Merge "Don't use 'assert' keyword in unit tests"
This commit is contained in:
commit
fe2c8963c1
|
@ -29,21 +29,26 @@ class FakeClient(object):
|
|||
expected = (method, url)
|
||||
called = self.client.callstack[pos][0:2]
|
||||
|
||||
assert self.client.callstack, ("Expected %s %s "
|
||||
"but no calls were made." % expected)
|
||||
if not self.client.callstack:
|
||||
raise AssertionError("Expected %s %s "
|
||||
"but no calls were made." % expected)
|
||||
|
||||
assert expected == called, 'Expected %s %s; got %s %s' % (
|
||||
expected + called)
|
||||
if expected != called:
|
||||
raise AssertionError('Expected %s %s; got %s %s' %
|
||||
(expected + called))
|
||||
|
||||
if body is not None:
|
||||
assert self.client.callstack[pos][2] == body
|
||||
if self.client.callstack[pos][2] != body:
|
||||
raise AssertionError('%s != %s',
|
||||
(self.client.callstack[pos][2], body))
|
||||
|
||||
def assert_called_anytime(self, method, url, body=None):
|
||||
"""Assert that an API method was called anytime in the test."""
|
||||
expected = (method, url)
|
||||
|
||||
assert self.client.callstack, ("Expected %s %s but no calls "
|
||||
"were made." % expected)
|
||||
if not self.client.callstack:
|
||||
raise AssertionError("Expected %s %s but no calls "
|
||||
"were made." % expected)
|
||||
|
||||
found = False
|
||||
for entry in self.client.callstack:
|
||||
|
@ -51,16 +56,12 @@ class FakeClient(object):
|
|||
found = True
|
||||
break
|
||||
|
||||
assert found, 'Expected %s %s; got %s' % (expected,
|
||||
self.client.callstack)
|
||||
if not found:
|
||||
raise AssertionError('Expected %s %s; got %s' %
|
||||
(expected, self.client.callstack))
|
||||
if body is not None:
|
||||
try:
|
||||
assert entry[2] == body
|
||||
except AssertionError:
|
||||
print(entry[2])
|
||||
print("!=")
|
||||
print(body)
|
||||
raise
|
||||
if entry[2] != body:
|
||||
raise AssertionError("%s != %s" % (entry[2], body))
|
||||
|
||||
self.client.callstack = []
|
||||
|
||||
|
|
|
@ -52,9 +52,11 @@ class FakeSessionClient(base_client.SessionClient):
|
|||
def request(self, url, method, **kwargs):
|
||||
# Check that certain things are called correctly
|
||||
if method in ['GET', 'DELETE']:
|
||||
assert 'body' not in kwargs
|
||||
if 'body' in kwargs:
|
||||
raise AssertionError('Request body in %s' % method)
|
||||
elif method == 'PUT':
|
||||
assert 'body' in kwargs
|
||||
if 'body' not in kwargs:
|
||||
raise AssertionError('No request body in %s' % method)
|
||||
|
||||
# Call the method
|
||||
args = urlparse.parse_qsl(urlparse.urlparse(url)[4])
|
||||
|
@ -244,21 +246,28 @@ class FakeSessionClient(base_client.SessionClient):
|
|||
def post_servers_1234_action(self, body, **kw):
|
||||
_body = None
|
||||
resp = 202
|
||||
assert len(body.keys()) == 1
|
||||
if len(body.keys()) != 1:
|
||||
raise AssertionError('No keys in request body')
|
||||
action = next(iter(body))
|
||||
keys = list(body[action].keys()) if body[action] is not None else None
|
||||
if action == 'reboot':
|
||||
assert list(body[action].keys()) == ['type']
|
||||
assert body[action]['type'] in ['HARD', 'SOFT']
|
||||
if keys != ['type']:
|
||||
raise AssertionError('Unexpection action keys for %s: %s',
|
||||
action, keys)
|
||||
if body[action]['type'] not in ['HARD', 'SOFT']:
|
||||
raise AssertionError('Unexpected reboot type %s',
|
||||
body[action]['type'])
|
||||
elif action == 'rebuild':
|
||||
keys = list(body[action].keys())
|
||||
if 'adminPass' in keys:
|
||||
keys.remove('adminPass')
|
||||
assert keys == ['imageRef']
|
||||
if keys != ['imageRef']:
|
||||
raise AssertionError('Unexpection action keys for %s: %s',
|
||||
action, keys)
|
||||
_body = self.get_servers_1234()[1]
|
||||
elif action == 'resize':
|
||||
assert list(body[action].keys()) == ['flavorRef']
|
||||
elif action == 'confirmResize':
|
||||
assert body[action] is None
|
||||
if body[action] is not None:
|
||||
raise AssertionError('Unexpected data for confirmResize: %s',
|
||||
body[action])
|
||||
# This one method returns a different response code
|
||||
return (204, None)
|
||||
elif action in ['revertResize',
|
||||
|
@ -266,45 +275,53 @@ class FakeSessionClient(base_client.SessionClient):
|
|||
'rescue', 'unrescue',
|
||||
'suspend', 'resume',
|
||||
'lock', 'unlock',
|
||||
]:
|
||||
assert body[action] is None
|
||||
elif action == 'addFixedIp':
|
||||
assert list(body[action].keys()) == ['networkId']
|
||||
elif action in ['removeFixedIp',
|
||||
'addFloatingIp',
|
||||
'removeFloatingIp',
|
||||
]:
|
||||
assert list(body[action].keys()) == ['address']
|
||||
elif action == 'createImage':
|
||||
assert set(body[action].keys()) == set(['name', 'metadata'])
|
||||
resp = {"status": 202,
|
||||
"location": "http://blah/images/456"}
|
||||
elif action == 'changePassword':
|
||||
assert list(body[action].keys()) == ['adminPass']
|
||||
elif action == 'os-getConsoleOutput':
|
||||
assert list(body[action].keys()) == ['length']
|
||||
return (202, {'output': 'foo'})
|
||||
elif action == 'os-getVNCConsole':
|
||||
assert list(body[action].keys()) == ['type']
|
||||
elif action == 'os-migrateLive':
|
||||
assert set(body[action].keys()) == set(['host',
|
||||
'block_migration',
|
||||
'disk_over_commit'])
|
||||
elif action == 'forceDelete':
|
||||
assert body is not None
|
||||
'forceDelete']:
|
||||
if body[action] is not None:
|
||||
raise AssertionError('Unexpected data for %s: %s',
|
||||
action, body[action])
|
||||
else:
|
||||
raise AssertionError("Unexpected server action: %s" % action)
|
||||
expected_keys = {
|
||||
'resize': {'flavorRef'},
|
||||
'addFixedIp': {'networkId'},
|
||||
'removeFixedIp': {'address'},
|
||||
'addFloatingIp': {'address'},
|
||||
'removeFloatingp': {'address'},
|
||||
'createImage': {'name', 'metadata'},
|
||||
'changePassword': {'adminPass'},
|
||||
'os-getConsoleOutput': {'length'},
|
||||
'os-getVNCConsole': {'type'},
|
||||
'os-migrateLive': {'host', 'block_migration',
|
||||
'disk_over_commit'},
|
||||
}
|
||||
|
||||
if action in expected_keys:
|
||||
if set(keys) != set(expected_keys[action]):
|
||||
raise AssertionError('Unexpection action keys for %s: %s',
|
||||
action, keys)
|
||||
else:
|
||||
raise AssertionError("Unexpected server action: %s" % action)
|
||||
|
||||
if action == 'createImage':
|
||||
resp = {"status": 202,
|
||||
"location": "http://blah/images/456"}
|
||||
if action == 'os-getConsoleOutput':
|
||||
return (202, {'output': 'foo'})
|
||||
|
||||
return (resp, _body)
|
||||
|
||||
def post_servers_5678_action(self, body, **kw):
|
||||
_body = None
|
||||
resp = 202
|
||||
assert len(body.keys()) == 1
|
||||
if len(body.keys()) != 1:
|
||||
raise AssertionError("No action in body")
|
||||
action = next(iter(body))
|
||||
if action in ['addFloatingIp',
|
||||
'removeFloatingIp',
|
||||
]:
|
||||
assert list(body[action].keys()) == ['address']
|
||||
keys = list(body[action].keys())
|
||||
if keys != ['address']:
|
||||
raise AssertionError('Unexpection action keys for %s: %s',
|
||||
action, keys)
|
||||
|
||||
return (resp, _body)
|
||||
|
||||
|
|
|
@ -79,7 +79,8 @@ class FakeApp(object):
|
|||
def __call__(self, env, start_response):
|
||||
"""Assert that expected environment is present when finally called."""
|
||||
for k, v in self.expected_env.items():
|
||||
assert env[k] == v, '%s != %s' % (env[k], v)
|
||||
if env[k] != v:
|
||||
raise AssertionError('%s != %s' % (env[k], v))
|
||||
resp = webob.Response()
|
||||
resp.body = six.b('SUCCESS')
|
||||
return resp(env, start_response)
|
||||
|
|
|
@ -44,7 +44,8 @@ class FakeApp(object):
|
|||
def __call__(self, env, start_response):
|
||||
"""Assert that expected environment is present when finally called."""
|
||||
for k, v in self.expected_env.items():
|
||||
assert env[k] == v, '%s != %s' % (env[k], v)
|
||||
if env[k] != v:
|
||||
raise AssertionError('%s != %s' % (env[k], v))
|
||||
resp = webob.Response()
|
||||
resp.body = six.b('SUCCESS')
|
||||
return resp(env, start_response)
|
||||
|
|
Loading…
Reference in New Issue