Skip to content

Commit 8f1f76c

Browse files
committed
refactor(tests): move some definitions
1 parent 5d96acc commit 8f1f76c

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

tests/json_infra/conftest.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Pytest configuration for the json infra tests."""
22

3-
import json
43
import os
54
import shutil
65
import tarfile
@@ -9,7 +8,6 @@
98
from typing import (
109
Callable,
1110
Final,
12-
Generator,
1311
Optional,
1412
Self,
1513
Set,
@@ -22,12 +20,12 @@
2220
from _pytest.nodes import Item
2321
from filelock import FileLock
2422
from git.exc import GitCommandError, InvalidGitRepositoryError
25-
from pytest import Collector, File, Session, StashKey, fixture
23+
from pytest import Collector, Session, StashKey, fixture
2624
from requests_cache import CachedSession
2725
from requests_cache.backends.sqlite import SQLiteCache
2826

2927
from . import TEST_FIXTURES
30-
from .helpers import ALL_FIXTURE_TYPES
28+
from .helpers import FixturesFile
3129

3230
try:
3331
from xdist import get_xdist_worker_id
@@ -299,35 +297,3 @@ def pytest_collect_file(
299297
if file_path.suffix == ".json":
300298
return FixturesFile.from_parent(parent, path=file_path)
301299
return None
302-
303-
304-
class FixturesFile(File):
305-
"""Single JSON file containing fixtures."""
306-
307-
def collect(
308-
self: Self,
309-
) -> Generator[Item | Collector, None, None]:
310-
"""Collect test cases from a single JSON fixtures file."""
311-
with open(self.path, "r") as file:
312-
try:
313-
loaded_file = json.load(file)
314-
except Exception:
315-
return # Skip *.json files that are unreadable.
316-
if not isinstance(loaded_file, dict):
317-
return
318-
for key, test_dict in loaded_file.items():
319-
if not isinstance(test_dict, dict):
320-
continue
321-
for fixture_type in ALL_FIXTURE_TYPES:
322-
if not fixture_type.is_format(test_dict):
323-
continue
324-
name = key
325-
if "::" in name:
326-
name = name.split("::")[1]
327-
yield fixture_type.from_parent( # type: ignore
328-
parent=self,
329-
name=name,
330-
test_file=str(self.path),
331-
test_key=key,
332-
test_dict=test_dict,
333-
)
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
"""Helpers to load tests from JSON files."""
22

3-
from typing import List, Type
4-
5-
from .fixtures import Fixture
3+
from .fixtures import ALL_FIXTURE_TYPES, Fixture, FixturesFile
64
from .load_blockchain_tests import BlockchainTestFixture
75
from .load_state_tests import StateTestFixture
86

9-
ALL_FIXTURE_TYPES: List[Type[Fixture]] = [
10-
BlockchainTestFixture,
11-
StateTestFixture,
12-
]
7+
ALL_FIXTURE_TYPES.append(BlockchainTestFixture)
8+
ALL_FIXTURE_TYPES.append(StateTestFixture)
139

14-
__all__ = ["ALL_FIXTURE_TYPES", "Fixture"]
10+
__all__ = ["ALL_FIXTURE_TYPES", "Fixture", "FixturesFile"]

tests/json_infra/helpers/fixtures.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Base class for all fixture loaders."""
22

3+
import json
34
from abc import ABC, abstractmethod
4-
from typing import Any, Dict, Self
5+
from typing import Any, Dict, Generator, List, Self, Type
56

67
from _pytest.nodes import Node
8+
from pytest import Collector, File, Item
79

810

911
class Fixture(ABC):
@@ -47,3 +49,38 @@ def from_parent(
4749
def is_format(cls, test_dict: Dict[str, Any]) -> bool:
4850
"""Return true if the object can be parsed as the fixture type."""
4951
pass
52+
53+
54+
ALL_FIXTURE_TYPES: List[Type[Fixture]] = []
55+
56+
57+
class FixturesFile(File):
58+
"""Single JSON file containing fixtures."""
59+
60+
def collect(
61+
self: Self,
62+
) -> Generator[Item | Collector, None, None]:
63+
"""Collect test cases from a single JSON fixtures file."""
64+
with open(self.path, "r") as file:
65+
try:
66+
loaded_file = json.load(file)
67+
except Exception:
68+
return # Skip *.json files that are unreadable.
69+
if not isinstance(loaded_file, dict):
70+
return
71+
for key, test_dict in loaded_file.items():
72+
if not isinstance(test_dict, dict):
73+
continue
74+
for fixture_type in ALL_FIXTURE_TYPES:
75+
if not fixture_type.is_format(test_dict):
76+
continue
77+
name = key
78+
if "::" in name:
79+
name = name.split("::")[1]
80+
yield fixture_type.from_parent( # type: ignore
81+
parent=self,
82+
name=name,
83+
test_file=str(self.path),
84+
test_key=key,
85+
test_dict=test_dict,
86+
)

0 commit comments

Comments
 (0)