Skip to content

Commit

Permalink
Simplify a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Feb 7, 2024
1 parent b96cf77 commit 3ed293d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 54 deletions.
55 changes: 55 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@
from .units.fixtures import * # noqa
from .recipes.fixtures import * # noqa
from nest.core.clients import BaseHTTPClient
from collections.abc import Mapping


################
# Spec helpers #
################


@pytest.fixture
def spec():
def _spec(request_spec, default_spec):
default_spec = default_spec.copy()

if not request_spec or not isinstance(request_spec, dict):
return default_spec

def update_spec(
original: dict[str, Any], new: dict[str, Any]
) -> dict[str, Any]:
for key, value in new.items():
if isinstance(value, Mapping):
original[key] = update_spec(original.get(key, {}), value)
else:
original[key] = value
return original

return update_spec(default_spec, request_spec)

return _spec


@pytest.fixture
def create_instances(request: pytest.FixtureRequest, spec):
def _create_instances(create_callback, default_spec, marker_name):
instances = {}

for marker in request.node.iter_markers(marker_name):
assert not marker.args, "Only kwargs is accepted with this fixture"

for slug in marker.kwargs:
request_spec = marker.kwargs.get(slug, {})
instances[slug] = create_callback(spec(request_spec, default_spec))

return instances

return _create_instances


@pytest.fixture
def create_instance(request: pytest.FixtureRequest, spec):
def _create_instance(create_callback, default_spec, marker_name):
request_spec = request.node.get_closest_marker(marker_name).kwargs
return create_callback(spec(request_spec, default_spec))

return _create_instance


################
Expand Down
70 changes: 19 additions & 51 deletions tests/products/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from decimal import Decimal
from typing import Any, Callable, TypedDict
from collections.abc import Mapping
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile

Expand Down Expand Up @@ -48,37 +47,6 @@ def default_product_spec() -> ProductSpec:
)


@pytest.fixture
def spec():
def _spec(request_spec, default_spec):
default_spec = default_spec.copy()

if not request_spec or not isinstance(request_spec, dict):
return default_spec

def update_spec(
original: dict[str, Any], new: dict[str, Any]
) -> dict[str, Any]:
for key, value in new.items():
if isinstance(value, Mapping):
original[key] = update_spec(original.get(key, {}), value)
else:
original[key] = value
return original

return update_spec(default_spec, request_spec)

return _spec


@pytest.fixture
def product_spec(
request: pytest.FixtureRequest, spec, default_product_spec
) -> dict[str, Any]:
request_spec = request.node.get_closest_marker("product").kwargs
return spec(request_spec, default_product_spec)


@pytest.fixture
def create_product_from_spec(db: Any, get_unit: Callable[[str], Unit]):
def _create_product(spec: dict[str, Any]):
Expand All @@ -91,31 +59,31 @@ def _create_product(spec: dict[str, Any]):


@pytest.fixture
def product(create_product_from_spec, product_spec):
return create_product_from_spec(product_spec)
def product(create_instance, create_product_from_spec, default_product_spec):
return create_instance(
create_callback=create_product_from_spec,
default_spec=default_product_spec,
marker_name="product",
)


@pytest.fixture
def oda_product(create_product_from_spec, default_product_spec):
def oda_product(create_instance, create_product_from_spec, default_product_spec):
modified_spec = default_product_spec.copy()
modified_spec["oda_id"] = next_oda_id()
modified_spec["oda_url"] = "https://example.com/"
return create_product_from_spec(modified_spec)


@pytest.fixture
def products(
request: pytest.FixtureRequest, spec, default_product_spec, create_product_from_spec
):
products = {}

for marker in request.node.iter_markers("products"):
assert not marker.args, "Only kwargs is accepted with this fixture"
return create_instance(
create_callback=create_product_from_spec,
default_spec=modified_spec,
marker_name="oda_product",
)

for slug in marker.kwargs:
request_spec = marker.kwargs.get(slug, {})
products[slug] = create_product_from_spec(
spec(request_spec, default_product_spec)
)

return products
@pytest.fixture
def products(create_instances, default_product_spec, create_product_from_spec):
return create_instances(
create_callback=create_product_from_spec,
default_spec=default_product_spec,
marker_name="products",
)
5 changes: 2 additions & 3 deletions tests/products/test_core_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
product2={"name": "Product 2"},
product3={"name": "Product 3"},
)
@pytest.mark.product
@pytest.mark.oda_product
def test_selector_get_products(
product: Product,
oda_product: Product,
products: dict[str, Product],
mocker: MagicMock,
django_assert_num_queries: Any,
) -> None:
oda_product = product
log_entries_mock_return = {
oda_product.id: [],
**{p.id: [] for p in products.values()},
Expand Down

0 comments on commit 3ed293d

Please sign in to comment.