-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathfixtures.py
More file actions
51 lines (39 loc) · 1.47 KB
/
Copy pathfixtures.py
File metadata and controls
51 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import re
from pathlib import Path
FIXTURES_DIR = Path(__file__).parent.parent / "fixtures"
# This regex is useful for finding individual underscore characters,
# which is necessary to allow us to use underscores in URL paths.
PATH_REPLACEMENT_REGEX = re.compile(r"(?<!_)_(?!_)")
class TestFixtures:
def __init__(self):
"""
Creates and loads test fixtures
"""
self._load_fixtures()
def get_fixture(self, url):
"""
Returns the test fixture data loaded at the given URL
"""
return self.fixtures[url]
def _load_fixtures(self):
"""
Handles loading JSON files and parsing them into responses. Also splits
returned lists into individual models that may be returned on their own.
"""
self.fixtures = {}
for json_file in FIXTURES_DIR.iterdir():
if json_file.suffix != ".json":
continue
with open(json_file) as f:
raw = f.read()
data = json.loads(raw)
fixture_url = PATH_REPLACEMENT_REGEX.sub(
"/", json_file.name
).replace("__", "_")[:-5]
self.fixtures[fixture_url] = data
if "results" in data:
# this is a paginated response
for obj in data["data"]:
if "id" in obj: # tags, obj-buckets don't have ids
self.fixtures[fixture_url + "/" + str(obj["id"])] = obj