2013-08-06 13:36:45 -03:00
|
|
|
# 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.
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
"""
|
|
|
|
A fake server that "responds" to API methods with pre-canned responses.
|
|
|
|
|
|
|
|
All of these responses come from the spec, so if for some reason the spec's
|
|
|
|
wrong the tests might raise AssertionError. I've indicated in comments the
|
|
|
|
places where actual behavior differs from the spec.
|
|
|
|
"""
|
|
|
|
|
2013-06-09 21:24:10 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
def assert_has_keys(dict, required=[], optional=[]):
|
2013-06-19 20:46:46 -05:00
|
|
|
keys = list(dict.keys())
|
2012-05-21 16:32:35 -04:00
|
|
|
for k in required:
|
|
|
|
try:
|
|
|
|
assert k in keys
|
|
|
|
except AssertionError:
|
|
|
|
extra_keys = set(keys).difference(set(required + optional))
|
|
|
|
raise AssertionError("found unexpected keys: %s" %
|
2012-06-01 18:55:40 -06:00
|
|
|
list(extra_keys))
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
class FakeClient(object):
|
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
def assert_called(self, method, url, body=None, pos=-1, **kwargs):
|
2012-05-21 16:32:35 -04:00
|
|
|
"""
|
|
|
|
Assert than an API method was just called.
|
|
|
|
"""
|
|
|
|
expected = (method, url)
|
|
|
|
called = self.client.callstack[pos][0:2]
|
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
assert self.client.callstack, ("Expected %s %s but no calls "
|
|
|
|
"were made." % expected)
|
2012-05-21 16:32:35 -04:00
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
assert expected == called, 'Expected %s %s; got %s %s' % (
|
|
|
|
expected + called)
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
if body is not None:
|
|
|
|
assert self.client.callstack[pos][2] == body
|
|
|
|
|
|
|
|
def assert_called_anytime(self, method, url, body=None):
|
|
|
|
"""
|
|
|
|
Assert than an API method was called anytime in the test.
|
|
|
|
"""
|
|
|
|
expected = (method, url)
|
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
assert self.client.callstack, ("Expected %s %s but no calls "
|
|
|
|
"were made." % expected)
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
found = False
|
|
|
|
for entry in self.client.callstack:
|
|
|
|
if expected == entry[0:2]:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
assert found, 'Expected %s %s; got %s' % (
|
2013-08-12 09:05:44 -04:00
|
|
|
expected + (self.client.callstack, ))
|
2012-06-01 18:55:40 -06:00
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
if body is not None:
|
|
|
|
try:
|
|
|
|
assert entry[2] == body
|
|
|
|
except AssertionError:
|
2013-06-09 21:24:10 -05:00
|
|
|
print(entry[2])
|
|
|
|
print("!=")
|
|
|
|
print(body)
|
2012-05-21 16:32:35 -04:00
|
|
|
raise
|
|
|
|
|
|
|
|
def clear_callstack(self):
|
|
|
|
self.client.callstack = []
|
|
|
|
|
|
|
|
def authenticate(self):
|
|
|
|
pass
|