Created
September 3, 2019 13:01
-
-
Save diefans/aa4eb4a0247331ba6d087edd5a88de9b to your computer and use it in GitHub Desktop.
aioresponses auto mock resource fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
@pytest.fixture | |
def domain_fixtures_path(): | |
import pathlib | |
fixtures_path = pathlib.Path(__file__).parent / "fixtures" | |
return fixtures_path | |
@pytest.fixture | |
def domain_fixture(domain_fixtures_path): | |
def payload(resource_type, resource_id): | |
with (domain_fixtures_path / resource_type / resource_id).with_suffix( | |
".json" | |
).open() as f: | |
data = json.load(f) | |
return data | |
return payload | |
@pytest.fixture | |
def domain_fixture_all(domain_fixtures_path, domain_fixture): | |
# get all resource types | |
resource_types = { | |
resource_path.name: {p.stem: p for p in resource_path.glob("*.json")} | |
for resource_path in domain_fixtures_path.iterdir() | |
if resource_path.is_dir() | |
} | |
def mockit(aioresponses_mock): | |
for resource_type, resources in resource_types.items(): | |
for resource_id in resources: | |
aioresponses_mock.get( | |
f"http://localhost:8088/api/v1/{resource_type}/{resource_id}", | |
payload=domain_fixture(resource_type, resource_id), | |
repeat=True, | |
) | |
return mockit | |
@pytest.fixture | |
def mock_domain(domain_fixture_all): | |
import aioresponses | |
with aioresponses.aioresponses( | |
passthrough=[ | |
"http://localhost:8000/", | |
"http://dynamodb:8000/", | |
"http://localhost:1080/", | |
] | |
) as m: | |
domain_fixture_all(m) | |
yield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment