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

View File

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

View File

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