Skip to content

Commit b263eec

Browse files
committed
Update types and readability of test utils
Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent 7585cc8 commit b263eec

5 files changed

Lines changed: 51 additions & 39 deletions

File tree

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include COPYING COPYING.LESSER README.md
22
include tox.ini
33
recursive-include requirements *
4-
recursive-include docs *.py *.rst user/*rst Makefile make.bat
4+
recursive-include docs *.py *.rst user/*rst Makefile make.bat
5+
recursive-include tests/fixtures *.json

tests/fixtures/account.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"url": "https://api.transip.nl/v6/ssh-keys",
111111
"status": 201,
112112
"content_type": "application/json",
113-
"match_json": {
113+
"match_json_params": {
114114
"sshKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf2pxWX/yhUBDyk2LPhvRtI0LnVO8PyR5Zt6AHrnhtLGqK+8YG9EMlWbCCWrASR+Q1hFQG example",
115115
"description": "Jim key"
116116
}
@@ -120,7 +120,7 @@
120120
"url": "https://api.transip.nl/v6/ssh-keys/123",
121121
"status": 204,
122122
"content_type": "application/json",
123-
"match_json": {
123+
"match_json_params": {
124124
"description": "Jim key"
125125
}
126126
},

tests/fixtures/colocations.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"url": "https://api.transip.nl/v6/colocations/example2/ip-addresses",
6969
"status": 201,
7070
"content_type": "application/json",
71-
"match_json": {
71+
"match_json_params": {
7272
"ipAddress": "2a01:7c8:3:1337::6",
7373
"reverseDns": "example.com"
7474
}
@@ -78,7 +78,7 @@
7878
"url": "https://api.transip.nl/v6/colocations/example2/ip-addresses/37.97.254.6",
7979
"status": 204,
8080
"content_type": "application/json",
81-
"match_json": {
81+
"match_json_params": {
8282
"ipAddress": {
8383
"address": "37.97.254.6",
8484
"subnetMask": "255.255.255.0",
@@ -102,7 +102,7 @@
102102
"url": "https://api.transip.nl/v6/colocations/example2/remote-hands",
103103
"status": 201,
104104
"content_type": "application/json",
105-
"match_json": {
105+
"match_json_params": {
106106
"remoteHands": {
107107
"coloName": "example2",
108108
"contactName": "Herman Kaakdorst",

tests/fixtures/domains.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"url": "https://api.transip.nl/v6/domains",
5353
"status": 201,
5454
"content_type": "application/json",
55-
"match_json": {
55+
"match_json_params": {
5656
"domainName": "example.com",
5757
"contacts": [
5858
{
@@ -94,7 +94,7 @@
9494
"url": "https://api.transip.nl/v6/domains",
9595
"status": 201,
9696
"content_type": "application/json",
97-
"match_json": {
97+
"match_json_params": {
9898
"domainName": "example.com",
9999
"authCode": "CYPMaVH+9MRjXGBc3InzHs7vNSUBPOjwpZm3GO+iDLHnFLtiP7sOKqW5JD1WeUpevZM6q1qS5YH9dGSp",
100100
"contacts": [
@@ -137,7 +137,7 @@
137137
"url": "https://api.transip.nl/v6/domains/example.com",
138138
"status": 204,
139139
"content_type": "application/json",
140-
"match_json": {
140+
"match_json_params": {
141141
"domain": {
142142
"name": "example.com",
143143
"authCode": "kJqfuOXNOYQKqh/jO4bYSn54YDqgAt1ksCe+ZG4Ud4nfpzw8qBsfR2JqAj7Ce12SxKcGD09v+yXd6lrm",
@@ -160,7 +160,7 @@
160160
"url": "https://api.transip.nl/v6/domains/example.com",
161161
"status": 204,
162162
"content_type": "application/json",
163-
"match_json": {
163+
"match_json_params": {
164164
"endTime": "end"
165165
}
166166
},
@@ -186,7 +186,7 @@
186186
"url": "https://api.transip.nl/v6/domains/example.com/branding",
187187
"status": 204,
188188
"content_type": "application/json",
189-
"match_json": {
189+
"match_json_params": {
190190
"branding": {
191191
"companyName": "Example B.V.",
192192
"supportEmail": "admin@example.com",
@@ -229,7 +229,7 @@
229229
"url": "https://api.transip.nl/v6/domains/example.com/contacts",
230230
"status": 204,
231231
"content_type": "application/json",
232-
"match_json": {
232+
"match_json_params": {
233233
"contacts": [
234234
{
235235
"type": "registrant",
@@ -271,7 +271,7 @@
271271
"url": "https://api.transip.nl/v6/domains/example.com/dns",
272272
"status": 201,
273273
"content_type": "application/json",
274-
"match_json": {
274+
"match_json_params": {
275275
"dnsEntry": {
276276
"name": "www",
277277
"expire": 86400,
@@ -300,7 +300,7 @@
300300
"url": "https://api.transip.nl/v6/domains/example.com/nameservers",
301301
"status": 204,
302302
"content_type": "application/json",
303-
"match_json": {
303+
"match_json_params": {
304304
"nameservers": [
305305
{
306306
"hostname": "ns0.transip.nl",

tests/utils.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,28 @@
1717
# You should have received a copy of the GNU Lesser General Public License
1818
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
1919

20-
from typing import Any
20+
from typing import Any, List, Dict
2121
import json
2222
import os
2323
import responses # type: ignore
2424

2525

26-
def load_fixture(path) -> Any:
27-
"""Load a JSON fixture from the fixtures directory."""
28-
fixtures: str = os.path.join(
29-
os.path.dirname(os.path.realpath(__file__)),
30-
"fixtures"
31-
)
32-
with open(os.path.join(fixtures, path)) as fixture:
33-
return json.load(fixture)
34-
35-
3626
def load_responses_fixture(path) -> None:
3727
"""Load a JSON fixture containing all the API response examples."""
3828

39-
def get_responses_method(method: str) -> str:
40-
"""
29+
def _load_json_fixtures(path: str) -> List[Dict[str, Any]]:
30+
"""Load JSON fixtures from file."""
31+
cwd: str = os.path.dirname(os.path.realpath(__file__))
32+
fixtures: str = os.path.join(os.path.join(cwd, 'fixtures'), path)
33+
with open(fixtures) as fixture:
34+
return json.load(fixture)
35+
36+
def _get_responses_method(method: str) -> str:
37+
"""Returns the responses method based upon the supplied method.
38+
39+
Args:
40+
method (str): The response method.
41+
4142
Raises:
4243
ValueError: if the specified method is invalid.
4344
"""
@@ -54,17 +55,27 @@ def get_responses_method(method: str) -> str:
5455
return responses.PATCH
5556
raise ValueError(f"Unable to find method '{method}' in responses")
5657

57-
fixture = load_fixture(path)
58-
for response in fixture:
59-
match = []
60-
if response.get("match_json"):
61-
match.append(responses.json_params_matcher(response["match_json"]))
58+
fixtures: List[Dict[str, Any]] = _load_json_fixtures(path)
59+
for fixture in fixtures:
60+
# Add the matchers for the request parameters or JSON body
61+
matchers: List[Any] = []
62+
if fixture.get('match_json_params'):
63+
matchers.append(
64+
responses.json_params_matcher(fixture['match_json_params'])
65+
)
66+
if fixture.get('match_urlencoded_params'):
67+
matchers.append(
68+
responses.urlencoded_params_matcher(
69+
fixture['match_urlencoded_params']
70+
)
71+
)
6272

73+
# Register the mocked response
6374
responses.add(
64-
get_responses_method(response["method"]),
65-
url=response["url"],
66-
json=response.get("json"),
67-
status=response["status"],
68-
content_type=response.get("content_type", "application/json"),
69-
match=match
70-
)
75+
_get_responses_method(fixture["method"]),
76+
url=fixture["url"],
77+
json=fixture.get("json"),
78+
status=fixture["status"],
79+
content_type=fixture.get("content_type", "application/json"),
80+
match=matchers
81+
)

0 commit comments

Comments
 (0)