1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from cm_api import api_client
18 from cm_api.resource import Resource
19
20 try:
21 import json
22 except ImportError:
23 import simplejson as json
26 """
27 Allows code to control the behavior of a resource's "invoke" method for
28 unit testing.
29 """
30
32 self._next_expect = None
33 self.test = test
34 self.version = version
35
36 @property
39
40 - def invoke(self, method, relpath=None, params=None, data=None, headers=None):
41 """
42 Checks the expected input data and returns the appropriate data to the caller.
43 """
44 exp_method, exp_path, exp_params, exp_data, exp_headers, retdata = self._next_expect
45 self._next_expect = None
46
47 if exp_method is not None:
48 self.test.assertEquals(exp_method, method)
49 if exp_path is not None:
50 self.test.assertEquals(exp_path, relpath)
51 if exp_params is not None:
52 self.test.assertEquals(exp_params, params)
53 if exp_data is not None:
54 self.test.assertEquals(exp_data, data)
55 if exp_headers is not None:
56 self.test.assertEquals(exp_headers, headers)
57 return retdata
58
59 - def expect(self, method, reqpath, params=None, data=None, headers=None,
60 retdata=None):
61 """
62 Sets the data to expect in the next call to invoke().
63
64 @param method: method to expect, or None for any.
65 @param reqpath: request path, or None for any.
66 @param params: query parameters, or None for any.
67 @param data: request body, or None for any.
68 @param headers: request headers, or None for any.
69 @param retdata: data to return from the invoke call.
70 """
71 self._next_expect = (method, reqpath, params, data, headers, retdata)
72
74 """
75 Deserializes raw JSON data into an instance of cls.
76
77 The data is deserialized, serialized again using the class's to_json_dict()
78 implementation, and deserialized again, to make sure both from_json_dict()
79 and to_json_dict() are working.
80 """
81 instance = cls.from_json_dict(json.loads(raw_data), None)
82 return cls.from_json_dict(instance.to_json_dict(preserve_ro=True), None)
83