Added module-level fixtures in tests

This commit is contained in:
Donkie
2023-09-08 21:00:47 +02:00
parent e436aa4975
commit 13994c486f
3 changed files with 134 additions and 61 deletions

View File

@@ -3,6 +3,7 @@
import math import math
import os import os
import time import time
from contextlib import contextmanager
from enum import StrEnum from enum import StrEnum
from typing import Any from typing import Any
@@ -50,8 +51,8 @@ def _wait_for_server(): # noqa: ANN202
break break
@pytest.fixture() @contextmanager
def random_vendor(): def random_vendor_impl():
"""Return a random vendor.""" """Return a random vendor."""
# Add vendor # Add vendor
result = httpx.post( result = httpx.post(
@@ -60,15 +61,15 @@ def random_vendor():
) )
result.raise_for_status() result.raise_for_status()
vendor = result.json() vendor: dict[str, Any] = result.json()
yield vendor yield vendor
# Delete vendor # Delete vendor
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status() httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
@pytest.fixture() @contextmanager
def random_empty_vendor(): def random_empty_vendor_impl():
"""Return a random vendor with only required fields specified.""" """Return a random vendor with only required fields specified."""
# Add vendor # Add vendor
result = httpx.post( result = httpx.post(
@@ -77,43 +78,44 @@ def random_empty_vendor():
) )
result.raise_for_status() result.raise_for_status()
vendor = result.json() vendor: dict[str, Any] = result.json()
yield vendor yield vendor
# Delete vendor # Delete vendor
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status() httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
@pytest.fixture() @contextmanager
def random_filament(random_vendor: dict[str, Any]): def random_filament_impl():
"""Return a random filament.""" """Return a random filament."""
# Add filament with random_vendor_impl() as random_vendor:
result = httpx.post( # Add filament
f"{URL}/api/v1/filament", result = httpx.post(
json={ f"{URL}/api/v1/filament",
"name": "Filament X", json={
"vendor_id": random_vendor["id"], "name": "Filament X",
"material": "PLA", "vendor_id": random_vendor["id"],
"price": 100, "material": "PLA",
"density": 1.25, "price": 100,
"diameter": 1.75, "density": 1.25,
"weight": 1000, "diameter": 1.75,
"spool_weight": 250, "weight": 1000,
"article_number": "123456789", "spool_weight": 250,
"comment": "abcdefghåäö", "article_number": "123456789",
}, "comment": "abcdefghåäö",
) },
result.raise_for_status() )
result.raise_for_status()
filament = result.json() filament: dict[str, Any] = result.json()
yield filament yield filament
# Delete filament # Delete filament
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status() httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
@pytest.fixture() @contextmanager
def random_empty_filament(): def random_empty_filament_impl():
"""Return a random filament with only required fields specified.""" """Return a random filament with only required fields specified."""
# Add filament # Add filament
result = httpx.post( result = httpx.post(
@@ -125,32 +127,103 @@ def random_empty_filament():
) )
result.raise_for_status() result.raise_for_status()
filament = result.json() filament: dict[str, Any] = result.json()
yield filament yield filament
# Delete filament # Delete filament
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status() httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
@contextmanager
def random_empty_filament_empty_vendor_impl():
"""Return a random filament with only required fields specified and a vendor with only required fields specified."""
with random_empty_vendor_impl() as random_empty_vendor:
# Add filament
result = httpx.post(
f"{URL}/api/v1/filament",
json={
"vendor_id": random_empty_vendor["id"],
"density": 1.25,
"diameter": 1.75,
},
)
result.raise_for_status()
filament: dict[str, Any] = result.json()
yield filament
# Delete filament
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
@pytest.fixture() @pytest.fixture()
def random_empty_filament_empty_vendor(random_empty_vendor: dict[str, Any]): def random_vendor():
"""Return a random vendor."""
with random_vendor_impl() as random_vendor:
yield random_vendor
@pytest.fixture()
def random_empty_vendor():
"""Return a random vendor with only required fields specified."""
with random_empty_vendor_impl() as random_empty_vendor:
yield random_empty_vendor
@pytest.fixture()
def random_filament():
"""Return a random filament."""
with random_filament_impl() as random_filament:
yield random_filament
@pytest.fixture()
def random_empty_filament():
"""Return a random filament with only required fields specified."""
with random_empty_filament_impl() as random_empty_filament:
yield random_empty_filament
@pytest.fixture()
def random_empty_filament_empty_vendor():
"""Return a random filament with only required fields specified and a vendor with only required fields specified.""" """Return a random filament with only required fields specified and a vendor with only required fields specified."""
# Add filament with random_empty_filament_empty_vendor_impl() as random_empty_filament_empty_vendor:
result = httpx.post( yield random_empty_filament_empty_vendor
f"{URL}/api/v1/filament",
json={
"vendor_id": random_empty_vendor["id"],
"density": 1.25,
"diameter": 1.75,
},
)
result.raise_for_status()
filament = result.json()
yield filament
# Delete filament @pytest.fixture(scope="module")
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status() def random_vendor_mod():
"""Return a random vendor."""
with random_vendor_impl() as random_vendor:
yield random_vendor
@pytest.fixture(scope="module")
def random_empty_vendor_mod():
"""Return a random vendor with only required fields specified."""
with random_empty_vendor_impl() as random_empty_vendor:
yield random_empty_vendor
@pytest.fixture(scope="module")
def random_filament_mod():
"""Return a random filament."""
with random_filament_impl() as random_filament:
yield random_filament
@pytest.fixture(scope="module")
def random_empty_filament_mod():
"""Return a random filament with only required fields specified."""
with random_empty_filament_impl() as random_empty_filament:
yield random_empty_filament
@pytest.fixture(scope="module")
def random_empty_filament_empty_vendor_mod():
"""Return a random filament with only required fields specified and a vendor with only required fields specified."""
with random_empty_filament_empty_vendor_impl() as random_empty_filament_empty_vendor:
yield random_empty_filament_empty_vendor
def length_from_weight(*, weight: float, diameter: float, density: float) -> float: def length_from_weight(*, weight: float, diameter: float, density: float) -> float:

View File

@@ -16,14 +16,14 @@ class Fixture:
filaments_by_id: dict[str, dict[str, Any]] filaments_by_id: dict[str, dict[str, Any]]
@pytest.fixture() @pytest.fixture(scope="module")
def filaments(random_vendor: dict[str, Any]) -> Iterable[Fixture]: def filaments(random_vendor_mod: dict[str, Any]) -> Iterable[Fixture]:
"""Add some filaments to the database.""" """Add some filaments to the database."""
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/filament", f"{URL}/api/v1/filament",
json={ json={
"name": "Filament X", "name": "Filament X",
"vendor_id": random_vendor["id"], "vendor_id": random_vendor_mod["id"],
"material": "PLA", "material": "PLA",
"price": 100, "price": 100,
"density": 1.25, "density": 1.25,
@@ -41,7 +41,7 @@ def filaments(random_vendor: dict[str, Any]) -> Iterable[Fixture]:
f"{URL}/api/v1/filament", f"{URL}/api/v1/filament",
json={ json={
"name": "Filament Y", "name": "Filament Y",
"vendor_id": random_vendor["id"], "vendor_id": random_vendor_mod["id"],
"material": "ABS", "material": "ABS",
"price": 200, "price": 200,
"density": 1.25, "density": 1.25,

View File

@@ -22,17 +22,17 @@ class Fixture:
filament: dict[str, Any] filament: dict[str, Any]
@pytest.fixture() @pytest.fixture(scope="module")
def spools( def spools(
random_filament: dict[str, Any], random_filament_mod: dict[str, Any],
random_empty_filament: dict[str, Any], random_empty_filament_mod: dict[str, Any],
random_empty_filament_empty_vendor: dict[str, Any], random_empty_filament_empty_vendor_mod: dict[str, Any],
) -> Iterable[Fixture]: ) -> Iterable[Fixture]:
"""Add some spools to the database.""" """Add some spools to the database."""
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/spool", f"{URL}/api/v1/spool",
json={ json={
"filament_id": random_filament["id"], "filament_id": random_filament_mod["id"],
"remaining_weight": 1000, "remaining_weight": 1000,
"location": "The Pantry", "location": "The Pantry",
"lot_nr": "123456789", "lot_nr": "123456789",
@@ -44,7 +44,7 @@ def spools(
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/spool", f"{URL}/api/v1/spool",
json={ json={
"filament_id": random_filament["id"], "filament_id": random_filament_mod["id"],
"remaining_weight": 1000, "remaining_weight": 1000,
"location": "Living Room", "location": "Living Room",
"lot_nr": "987654321", "lot_nr": "987654321",
@@ -56,7 +56,7 @@ def spools(
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/spool", f"{URL}/api/v1/spool",
json={ json={
"filament_id": random_filament["id"], "filament_id": random_filament_mod["id"],
"remaining_weight": 1000, "remaining_weight": 1000,
"archived": True, "archived": True,
}, },
@@ -67,7 +67,7 @@ def spools(
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/spool", f"{URL}/api/v1/spool",
json={ json={
"filament_id": random_empty_filament["id"], "filament_id": random_empty_filament_mod["id"],
}, },
) )
result.raise_for_status() result.raise_for_status()
@@ -76,7 +76,7 @@ def spools(
result = httpx.post( result = httpx.post(
f"{URL}/api/v1/spool", f"{URL}/api/v1/spool",
json={ json={
"filament_id": random_empty_filament_empty_vendor["id"], "filament_id": random_empty_filament_empty_vendor_mod["id"],
}, },
) )
result.raise_for_status() result.raise_for_status()
@@ -93,7 +93,7 @@ def spools(
yield Fixture( yield Fixture(
spools=[spool_1, spool_2, spool_3, spool_4, spool_5], spools=[spool_1, spool_2, spool_3, spool_4, spool_5],
spools_by_id=added_spools_by_id, spools_by_id=added_spools_by_id,
filament=random_filament, filament=random_filament_mod,
) )
httpx.delete(f"{URL}/api/v1/spool/{spool_1['id']}").raise_for_status() httpx.delete(f"{URL}/api/v1/spool/{spool_1['id']}").raise_for_status()