Debian Codegraph @

docs/testing/getting_started.rst

raw
1 .. warning::
2
3 This component is an *alpha*. Interfaces may change outside of the
4 normal semver policy.
5
6 Getting Started with testing
7 ============================
8
9 Dependencies
10 ------------
11
12 This toolchain requires the ``responses`` library.
13
14 ``globus_sdk.testing`` is tested to operate with the latest version of
15 ``responses``.
16
17 Recommended Fixtures
18 --------------------
19
20 Under pytest, this is the recommended fixture for setting up responses and
21 guaranteeing that requests are sent to the production hostnames:
22
23 .. code-block:: python
24
25 @pytest.fixture(autouse=True)
26 def mocked_responses(monkeypatch):
27 responses.start()
28 monkeypatch.setitem(os.environ, "GLOBUS_SDK_ENVIRONMENT", "production")
29 yield
30 responses.stop()
31 responses.reset()
32
33 Usage
34 -----
35
36 Activating Individual Responses
37 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38
39 Once ``responses`` has been activated, each response fixture can be loaded and
40 activated by name:
41
42 .. code-block:: python
43
44 from globus_sdk.testing import load_response
45
46 # load_response will add the response to `responses` and return it
47 load_response("auth.get_identities")
48 # "case" is used to have a single name map to multiple responses
49 data = load_response("auth.get_identities", case="multiple")
50
51 Responses can also be activated by passing an SDK client method, bound or
52 unbound, as in:
53
54 .. code-block:: python
55
56 import globus_sdk
57 from globus_sdk.testing import load_response
58
59 load_response(globus_sdk.AuthClient.get_identities)
60 load_response(globus_sdk.AuthClient.get_identities, case="unauthorized")
61
62 # or, with a bound method
63 ac = globus_sdk.AuthClient()
64 load_response(ac.get_identities, case="multiple")
65
66 Activating "Scenarios"
67 ~~~~~~~~~~~~~~~~~~~~~~
68
69 Some sets of fixtures may describe a scenario, and therefore it's desirable to
70 load all of them at once:
71
72 .. code-block:: python
73
74 from globus_sdk.testing import load_response_set
75
76 fixtures = load_response_set("scenario.foo")
77
78 Getting Responses and ResponseSets without Activating
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
81 If you want to fetch a ``ResponseSet`` or ``RegisteredResponse`` without
82 activating it, you can do this via the ``get_response_set`` method. Responses
83 must always be part of a response set, and the default name for an individual
84 response is ``"default"``.
85
86 .. code-block:: python
87
88 from globus_sdk import AuthClient
89 from globus_sdk.testing import get_response_set
90
91 # rset will not be activated
92 rset = get_response_set(AuthClient.get_identities)
93 # you can get an individual response from rset
94 get_ids = rset.get("default")
95 # you can manually activate a whole set
96 rset.activate_all()
97 # or just one response from it by name
98 rset.activate("default")
99
100 Note that activating a whole response set may or may not make sense. For
101 example, the response set for ``AuthClient.get_identities`` provides various
102 responses for the same API call.
103
104 Registering Response Sets
105 ~~~~~~~~~~~~~~~~~~~~~~~~~
106
107 You can register your own response sets dynamically, and then load them up with
108 the same ``load_response_set`` method. Note that custom response sets will
109 override the builtin response sets, if names match.
110
111 .. code-block:: python
112
113 from globus_sdk.testing import load_response_set, register_response_set
114 import uuid
115
116 # register a scenario under which Globus Auth get_identities and Globus
117 # Transfer operation_ls both return payloads of `{"foo": "bar"}`
118 # use an autogenerated endpoint ID and put it into the response metadata
119 # register_response_set takes dict data and converts it to fixtures
120 endpoint_id = str(uuid.uuid1())
121 register_response_set(
122 "foobar",
123 {
124 "get_identities": {
125 "service": "auth",
126 "path": "/v2/api/identities",
127 "json": {"foo": "bar"},
128 },
129 "operation_ls": {
130 "service": "transfer",
131 "path": f"/operation/endpoint/{endpoint_id}/ls",
132 "json": {"foo": "bar"},
133 },
134 },
135 metadata={
136 "endpoint_id": endpoint_id,
137 },
138 )
139
140 # activate the result, and get it as a ResponseSet
141 fixtures = load_response_set("foobar")
142 # you can then pull the epid from the metadata
143 epid = fixtures.metadata["endpoint_id"]
144 transfer_client.operation_ls(epid)
145
146 ``register_response_set`` can therefore be used to load fixture data early in
147 a tetstsuite run (e.g. as an autouse session-level fixture), for reference
148 later in the testsuite.
149
150 Loading Responses without Registering
151 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152
153 Because ``RegisteredResponse`` takes care of resolving ``"auth"`` to the Auth
154 URL, ``"transfer"`` to the Transfer URL, and so forth, you might want to use
155 ``globus_sdk.testing`` in lieu of ``responses`` even when registering single
156 responses for individual tests.
157
158 To support this mode of usage, ``load_response`` can take a
159 ``RegisteredResponse`` instance, and ``load_response_set`` can take a
160 ``ResponseSet`` instance.
161
162 Consider the following example of a parametrized test which uses
163 ``load_response(RegisteredResponse(...))`` as a replacement for
164 ``responses.add``:
165
166 .. code-block:: python
167
168 from globus_sdk.testing import load_response, RegisteredResponse
169 import pytest
170
171
172 @pytest.mark.parametrize("message", ["foo", "bar"])
173 def test_get_identities_sends_back_strange_message(message):
174 load_response(
175 RegisteredResponse(
176 service="auth",
177 path="/v2/api/identities",
178 json={"message": message},
179 )
180 )
181
182 ac = globus_sdk.AuthClient()
183 res = ac.get_identities(usernames="[email protected]")
184 assert res["message"] == message
185
186
187 In this mode of usage, the response set registry is skipped altogether. It is
188 not necessary to name or organize the response fixtures in a way that is usable
189 outside of the specific test.
190
191 Using non-default responses.RequestsMock objects
192 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193
194 By default, all methods in ``globus_sdk.testing`` which converse with
195 ``responses`` use the default mock. This is the behavior offered by
196 ``responses.add(...)`` and similar methods.
197
198 However, you can pass a custom ``RequestsMock`` if so desired to the following
199 methods:
200
201 * ``get_last_request``
202 * ``load_response_set``
203 * ``load_response``
204
205 as a keyword argument, ``requests_mock``.
206 e.g.
207
208
209 .. code-block:: python
210
211 from globus_sdk.testing import get_last_request
212 import responses
213
214 custom_mock = responses.RequestsMock(...)
215 ...
216
217 get_last_request(requests_mock=custom_mock)