Skip to content

Commit ccece32

Browse files
committed
tests: factor out common/utils.py
Consolidate test data loading code in common/utils.py Move `test/fixtures` to `test/data/fixtures` to simplify resource dependencies in BUCK. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 90757ad commit ccece32

21 files changed

+61
-51
lines changed

tests/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66

77
# pyre-strict
88

9-
import importlib.resources
109
import logging
1110

1211
logging.disable()
13-
14-
15-
def read_fixture(filepath: str) -> str:
16-
return (
17-
importlib.resources.files(__package__)
18-
.joinpath("fixtures")
19-
.joinpath(filepath)
20-
.read_text()
21-
)

tests/common/patchwork_mock.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
# pyre-unsafe
88

9-
import json
10-
import os
119
import re
1210

13-
from typing import Any, Dict, Final, List
11+
from typing import Any, Dict, Final
1412

1513
from aioresponses import aioresponses
1614

@@ -86,33 +84,6 @@ def get_dict_key(d: Dict[str, Any], idx: int = 0) -> str:
8684
return list(d)[idx]
8785

8886

89-
def load_test_data_file(file):
90-
with open(file, "r") as f:
91-
if file.endswith(".json"):
92-
return json.load(f)
93-
else:
94-
return f.read()
95-
96-
97-
def load_test_data(path) -> Dict[str, Any]:
98-
jsons_by_path = {}
99-
with open(os.path.join(path, "responses.json"), "r") as f:
100-
jsons_by_path = json.load(f)
101-
data = {}
102-
for url, value in jsons_by_path.items():
103-
match value:
104-
case str():
105-
filepath = os.path.join(path, value)
106-
data[url] = load_test_data_file(filepath)
107-
case list():
108-
lst = []
109-
for filename in value:
110-
filepath = os.path.join(path, filename)
111-
lst.append(load_test_data_file(filepath))
112-
data[url] = lst
113-
return data
114-
115-
11687
FOO_SERIES_FIRST = 2
11788
FOO_SERIES_LAST = 10
11889

tests/common/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import json
8+
import os
9+
from typing import Any, Dict
10+
11+
12+
def load_test_data_file(file):
13+
with open(file, "r") as f:
14+
if file.endswith(".json"):
15+
return json.load(f)
16+
else:
17+
return f.read()
18+
19+
20+
def load_test_data(path) -> Dict[str, Any]:
21+
jsons_by_path = {}
22+
with open(os.path.join(path, "responses.json"), "r") as f:
23+
jsons_by_path = json.load(f)
24+
data = {}
25+
for url, value in jsons_by_path.items():
26+
match value:
27+
case str():
28+
filepath = os.path.join(path, value)
29+
data[url] = load_test_data_file(filepath)
30+
case list():
31+
lst = []
32+
for filename in value:
33+
filepath = os.path.join(path, filename)
34+
lst.append(load_test_data_file(filepath))
35+
data[url] = lst
36+
return data
37+
38+
39+
def read_test_data_file(relative_path: str) -> str:
40+
path = os.path.join(os.path.dirname(__file__), "../data", relative_path)
41+
with open(path, "r") as f:
42+
return f.read()
43+
44+
45+
def read_fixture(filename: str) -> str:
46+
return read_test_data_file(f"fixtures/{filename}")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)