Debian Codegraph @

seamicroclient/tests/fakes.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 """
15 A fake server that "responds" to API methods with pre-canned responses.
16
17 All of these responses come from the spec, so if for some reason the spec's
18 wrong the tests might raise AssertionError. I've indicated in comments the
19 places where actual behavior differs from the spec.
20 """
21
22 from seamicroclient import base
23
24
25 def assert_has_keys(dict, required=[], optional=[]):
26 keys = dict.keys()
27 for k in required:
28 try:
29 assert k in keys
30 except AssertionError:
31 extra_keys = set(keys).difference(set(required + optional))
32 raise AssertionError("found unexpected keys: %s" %
33 list(extra_keys))
34
35
36 class FakeClient(object):
37
38 def assert_called(self, method, url, body=None, pos=-1):
39 """
40 Assert than an API method was just called.
41 """
42 expected = (method, url)
43 called = self.client.callstack[pos][0:2]
44
45 assert self.client.callstack, \
46 "Expected %s %s but no calls were made." % expected
47
48 assert expected == called, 'Expected %s %s; got %s %s' % \
49 (expected + called)
50
51 if body is not None:
52 if self.client.callstack[pos][2] != body:
53 raise AssertionError('%r != %r' %
54 (self.client.callstack[pos][2], body))
55
56 def assert_called_anytime(self, method, url, body=None):
57 """
58 Assert than an API method was called anytime in the test.
59 """
60 expected = (method, url)
61
62 assert self.client.callstack, \
63 "Expected %s %s but no calls were made." % expected
64
65 found = False
66 for entry in self.client.callstack:
67 if expected == entry[0:2]:
68 found = True
69 break
70
71 assert found, 'Expected %s; got %s' % \
72 (expected, self.client.callstack)
73 if body is not None:
74 try:
75 assert entry[2] == body
76 except AssertionError:
77 print(entry[2])
78 print("!=")
79 print(body)
80 raise
81
82 self.client.callstack = []
83
84 def clear_callstack(self):
85 self.client.callstack = []
86
87 def authenticate(self):
88 pass
89
90
91 # Fake class that will be used as an extension
92 class FakeManager(base.Manager):
93 pass