Added test cases for finding empty spool fields
This commit is contained in:
@@ -67,7 +67,7 @@ line-length = 120
|
|||||||
target-version = "py39"
|
target-version = "py39"
|
||||||
|
|
||||||
[tool.ruff.per-file-ignores]
|
[tool.ruff.per-file-ignores]
|
||||||
"tests*/*" = ["ANN201", "S101", "PLR2004"]
|
"tests*/*" = ["ANN201", "S101", "PLR2004", "D103"]
|
||||||
"migrations/versions/*" = ["N999"]
|
"migrations/versions/*" = ["N999"]
|
||||||
|
|
||||||
[tool.black]
|
[tool.black]
|
||||||
|
|||||||
@@ -67,6 +67,23 @@ def random_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()
|
||||||
|
def random_empty_vendor():
|
||||||
|
"""Return a random vendor with only required fields specified."""
|
||||||
|
# Add vendor
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/vendor",
|
||||||
|
json={"name": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
vendor = result.json()
|
||||||
|
yield vendor
|
||||||
|
|
||||||
|
# Delete vendor
|
||||||
|
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def random_filament(random_vendor: dict[str, Any]):
|
def random_filament(random_vendor: dict[str, Any]):
|
||||||
"""Return a random filament."""
|
"""Return a random filament."""
|
||||||
@@ -95,6 +112,47 @@ def random_filament(random_vendor: dict[str, Any]):
|
|||||||
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()
|
||||||
|
def random_empty_filament():
|
||||||
|
"""Return a random filament with only required fields specified."""
|
||||||
|
# Add filament
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
json={
|
||||||
|
"density": 1.25,
|
||||||
|
"diameter": 1.75,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
filament = 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]):
|
||||||
|
"""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()
|
||||||
|
|
||||||
|
filament = result.json()
|
||||||
|
yield filament
|
||||||
|
|
||||||
|
# Delete filament
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
def length_from_weight(*, weight: float, diameter: float, density: float) -> float:
|
def length_from_weight(*, weight: float, diameter: float, density: float) -> float:
|
||||||
"""Calculate the length of a piece of filament.
|
"""Calculate the length of a piece of filament.
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ import pytest
|
|||||||
URL = "http://spoolman:8000"
|
URL = "http://spoolman:8000"
|
||||||
|
|
||||||
|
|
||||||
|
def spool_lists_equal(a: Iterable[dict[str, Any]], b: Iterable[dict[str, Any]]) -> bool:
|
||||||
|
"""Compare two lists of spools where the order of the spools is not guaranteed."""
|
||||||
|
return sorted(a, key=lambda x: x["id"]) == sorted(b, key=lambda x: x["id"])
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Fixture:
|
class Fixture:
|
||||||
spools: list[dict[str, Any]]
|
spools: list[dict[str, Any]]
|
||||||
@@ -18,7 +23,11 @@ class Fixture:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def spools(random_filament: dict[str, Any]) -> Iterable[Fixture]:
|
def spools(
|
||||||
|
random_filament: dict[str, Any],
|
||||||
|
random_empty_filament: dict[str, Any],
|
||||||
|
random_empty_filament_empty_vendor: dict[str, Any],
|
||||||
|
) -> 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",
|
||||||
@@ -55,14 +64,34 @@ def spools(random_filament: dict[str, Any]) -> Iterable[Fixture]:
|
|||||||
result.raise_for_status()
|
result.raise_for_status()
|
||||||
spool_3 = result.json()
|
spool_3 = result.json()
|
||||||
|
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_empty_filament["id"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool_4 = result.json()
|
||||||
|
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_empty_filament_empty_vendor["id"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool_5 = result.json()
|
||||||
|
|
||||||
added_spools_by_id = {
|
added_spools_by_id = {
|
||||||
spool_1["id"]: spool_1,
|
spool_1["id"]: spool_1,
|
||||||
spool_2["id"]: spool_2,
|
spool_2["id"]: spool_2,
|
||||||
spool_3["id"]: spool_3,
|
spool_3["id"]: spool_3,
|
||||||
|
spool_4["id"]: spool_4,
|
||||||
|
spool_5["id"]: spool_5,
|
||||||
}
|
}
|
||||||
|
|
||||||
yield Fixture(
|
yield Fixture(
|
||||||
spools=[spool_1, spool_2, spool_3],
|
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,
|
||||||
)
|
)
|
||||||
@@ -70,6 +99,8 @@ def spools(random_filament: dict[str, Any]) -> Iterable[Fixture]:
|
|||||||
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()
|
||||||
httpx.delete(f"{URL}/api/v1/spool/{spool_2['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/spool/{spool_2['id']}").raise_for_status()
|
||||||
httpx.delete(f"{URL}/api/v1/spool/{spool_3['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/spool/{spool_3['id']}").raise_for_status()
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool_4['id']}").raise_for_status()
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool_5['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
def test_find_all_spools(spools: Fixture):
|
def test_find_all_spools(spools: Fixture):
|
||||||
@@ -80,10 +111,7 @@ def test_find_all_spools(spools: Fixture):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1], spools.spools[3], spools.spools[4]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
assert spool["archived"] is False
|
|
||||||
|
|
||||||
|
|
||||||
def test_find_all_spools_including_archived(spools: Fixture):
|
def test_find_all_spools_including_archived(spools: Fixture):
|
||||||
@@ -94,9 +122,16 @@ def test_find_all_spools_including_archived(spools: Fixture):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 3
|
assert spool_lists_equal(
|
||||||
for spool in spools_result:
|
spools_result,
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
(
|
||||||
|
spools.spools[0],
|
||||||
|
spools.spools[1],
|
||||||
|
spools.spools[2],
|
||||||
|
spools.spools[3],
|
||||||
|
spools.spools[4],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("field_name", ["filament_name", "filament.name"])
|
@pytest.mark.parametrize("field_name", ["filament_name", "filament.name"])
|
||||||
@@ -111,9 +146,20 @@ def test_find_spools_by_filament_name(spools: Fixture, field_name: str):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
|
def test_find_spools_by_empty_filament_name(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"filament.name": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("field_name", ["filament_id", "filament.id"])
|
@pytest.mark.parametrize("field_name", ["filament_id", "filament.id"])
|
||||||
@@ -128,9 +174,7 @@ def test_find_spools_by_filament_id(spools: Fixture, field_name: str):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("field_name", ["filament_material", "filament.material"])
|
@pytest.mark.parametrize("field_name", ["filament_material", "filament.material"])
|
||||||
@@ -145,9 +189,20 @@ def test_find_spools_by_filament_material(spools: Fixture, field_name: str):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
|
def test_find_spools_by_empty_filament_material(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"filament.material": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("field_name", ["vendor_name", "vendor.name"])
|
@pytest.mark.parametrize("field_name", ["vendor_name", "vendor.name"])
|
||||||
@@ -162,9 +217,20 @@ def test_find_spools_by_filament_vendor_name(spools: Fixture, field_name: str):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
|
def test_find_spools_by_empty_filament_vendor_name(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"vendor.name": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spools_result == [spools.spools[4]]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("field_name", ["vendor_id", "vendor.id"])
|
@pytest.mark.parametrize("field_name", ["vendor_id", "vendor.id"])
|
||||||
@@ -179,9 +245,20 @@ def test_find_spools_by_filament_vendor_id(spools: Fixture, field_name: str):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 2
|
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
|
||||||
for spool in spools_result:
|
|
||||||
assert spool == spools.spools_by_id[spool["id"]]
|
|
||||||
|
def test_find_spools_by_empty_filament_vendor_id(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"vendor.id": -1},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spools_result == [spools.spools[3]]
|
||||||
|
|
||||||
|
|
||||||
def test_find_spools_by_location(spools: Fixture):
|
def test_find_spools_by_location(spools: Fixture):
|
||||||
@@ -195,8 +272,20 @@ def test_find_spools_by_location(spools: Fixture):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 1
|
assert spools_result == [spools.spools[0]]
|
||||||
assert spools_result[0] == spools.spools[0]
|
|
||||||
|
|
||||||
|
def test_find_spools_by_empty_location(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"location": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
|
||||||
|
|
||||||
|
|
||||||
def test_find_spools_by_lot_nr(spools: Fixture):
|
def test_find_spools_by_lot_nr(spools: Fixture):
|
||||||
@@ -210,5 +299,17 @@ def test_find_spools_by_lot_nr(spools: Fixture):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spools_result = result.json()
|
spools_result = result.json()
|
||||||
assert len(spools_result) == 1
|
assert spools_result == [spools.spools[0]]
|
||||||
assert spools_result[0] == spools.spools[0]
|
|
||||||
|
|
||||||
|
def test_find_spools_by_empty_lot_nr(spools: Fixture):
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"lot_nr": ""},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools_result = result.json()
|
||||||
|
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
|
||||||
|
|||||||
Reference in New Issue
Block a user