Updated integration tests to not require exact match

This matches how we want clients to behave, they should ignore any extra fields that are sent in the response.
This commit is contained in:
Donkie
2024-01-16 21:21:49 +01:00
parent 7972f4d401
commit f8cc9f7eda
8 changed files with 183 additions and 151 deletions

View File

@@ -6,7 +6,7 @@ from typing import Any
import httpx
import pytest
from ..conftest import length_from_weight
from ..conftest import assert_dicts_compatible, length_from_weight
URL = "http://spoolman:8000"
@@ -50,22 +50,25 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
)
spool = result.json()
assert spool == {
"id": spool["id"],
"registered": spool["registered"],
"first_used": "2023-01-02T11:00:00Z",
"last_used": "2023-01-02T11:00:00Z",
"filament": random_filament,
"remaining_weight": pytest.approx(remaining_weight),
"used_weight": pytest.approx(used_weight),
"remaining_length": pytest.approx(remaining_length),
"used_length": pytest.approx(used_length),
"location": location,
"lot_nr": lot_nr,
"comment": comment,
"archived": archived,
"price": price,
}
assert_dicts_compatible(
spool,
{
"id": spool["id"],
"registered": spool["registered"],
"first_used": "2023-01-02T11:00:00Z",
"last_used": "2023-01-02T11:00:00Z",
"filament": random_filament,
"remaining_weight": pytest.approx(remaining_weight),
"used_weight": pytest.approx(used_weight),
"remaining_length": pytest.approx(remaining_length),
"used_length": pytest.approx(used_length),
"location": location,
"lot_nr": lot_nr,
"comment": comment,
"archived": archived,
"price": price,
},
)
# Verify that registered happened almost now (within 1 minute)
diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(spool["registered"])).total_seconds())
@@ -114,21 +117,24 @@ def test_add_spool_used_weight(random_filament: dict[str, Any]):
)
spool = result.json()
assert spool == {
"id": spool["id"],
"registered": spool["registered"],
"first_used": first_used,
"last_used": last_used,
"filament": random_filament,
"remaining_weight": pytest.approx(remaining_weight),
"used_weight": pytest.approx(used_weight),
"remaining_length": pytest.approx(remaining_length),
"used_length": pytest.approx(used_length),
"location": location,
"lot_nr": lot_nr,
"comment": comment,
"archived": archived,
}
assert_dicts_compatible(
spool,
{
"id": spool["id"],
"registered": spool["registered"],
"first_used": first_used,
"last_used": last_used,
"filament": random_filament,
"remaining_weight": pytest.approx(remaining_weight),
"used_weight": pytest.approx(used_weight),
"remaining_length": pytest.approx(remaining_length),
"used_length": pytest.approx(used_length),
"location": location,
"lot_nr": lot_nr,
"comment": comment,
"archived": archived,
},
)
# Clean up
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
@@ -161,16 +167,19 @@ def test_add_spool_required(random_filament: dict[str, Any]):
)
spool = result.json()
assert spool == {
"id": spool["id"],
"registered": spool["registered"],
"filament": random_filament,
"used_weight": pytest.approx(used_weight),
"remaining_weight": pytest.approx(remaining_weight),
"used_length": pytest.approx(used_length),
"remaining_length": pytest.approx(remaining_length),
"archived": False,
}
assert_dicts_compatible(
spool,
{
"id": spool["id"],
"registered": spool["registered"],
"filament": random_filament,
"used_weight": pytest.approx(used_weight),
"remaining_weight": pytest.approx(remaining_weight),
"used_length": pytest.approx(used_length),
"remaining_length": pytest.approx(remaining_length),
"archived": False,
},
)
# Clean up
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()

View File

@@ -7,14 +7,11 @@ from typing import Any
import httpx
import pytest
from ..conftest import assert_lists_compatible
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
class Fixture:
spools: list[dict[str, Any]]
@@ -101,7 +98,10 @@ def test_find_all_spools(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1], spools.spools[3], spools.spools[4]))
assert_lists_compatible(
spools_result,
(spools.spools[0], spools.spools[1], spools.spools[3], spools.spools[4]),
)
def test_find_all_spools_including_archived(spools: Fixture):
@@ -111,7 +111,7 @@ def test_find_all_spools_including_archived(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(
assert_lists_compatible(
spools_result,
(
spools.spools[0],
@@ -272,7 +272,7 @@ def test_find_spools_by_filament_name(spools: Fixture, field_name: str):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1]))
def test_find_spools_by_empty_filament_name(spools: Fixture):
@@ -285,7 +285,7 @@ def test_find_spools_by_empty_filament_name(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
assert_lists_compatible(spools_result, (spools.spools[3], spools.spools[4]))
@pytest.mark.parametrize("field_name", ["filament_id", "filament.id"])
@@ -299,7 +299,7 @@ def test_find_spools_by_filament_id(spools: Fixture, field_name: str):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1]))
def test_find_spools_by_multiple_filament_ids(spools: Fixture):
@@ -315,7 +315,7 @@ def test_find_spools_by_multiple_filament_ids(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1], spools.spools[3]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1], spools.spools[3]))
@pytest.mark.parametrize("field_name", ["filament_material", "filament.material"])
@@ -329,7 +329,7 @@ def test_find_spools_by_filament_material(spools: Fixture, field_name: str):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1]))
def test_find_spools_by_empty_filament_material(spools: Fixture):
@@ -342,7 +342,7 @@ def test_find_spools_by_empty_filament_material(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
assert_lists_compatible(spools_result, (spools.spools[3], spools.spools[4]))
@pytest.mark.parametrize("field_name", ["vendor_name", "filament.vendor.name"])
@@ -356,7 +356,7 @@ def test_find_spools_by_filament_vendor_name(spools: Fixture, field_name: str):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1]))
def test_find_spools_by_empty_filament_vendor_name(spools: Fixture):
@@ -383,7 +383,7 @@ def test_find_spools_by_filament_vendor_id(spools: Fixture, field_name: str):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[1]))
def test_find_spools_by_multiple_vendor_ids(spools: Fixture):
@@ -403,7 +403,10 @@ def test_find_spools_by_multiple_vendor_ids(spools: Fixture):
# Verify
spools_result = result.json()
assert len(spools_result) == 4
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[1], spools.spools[2], spools.spools[4]))
assert_lists_compatible(
spools_result,
(spools.spools[0], spools.spools[1], spools.spools[2], spools.spools[4]),
)
def test_find_spools_by_empty_filament_vendor_id(spools: Fixture):
@@ -442,7 +445,7 @@ def test_find_spools_by_empty_location(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
assert_lists_compatible(spools_result, (spools.spools[3], spools.spools[4]))
def test_find_spools_by_empty_and_filled_location(spools: Fixture):
@@ -455,7 +458,7 @@ def test_find_spools_by_empty_and_filled_location(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[0], spools.spools[3], spools.spools[4]))
assert_lists_compatible(spools_result, (spools.spools[0], spools.spools[3], spools.spools[4]))
def test_find_spools_by_lot_nr(spools: Fixture):
@@ -481,4 +484,4 @@ def test_find_spools_by_empty_lot_nr(spools: Fixture):
# Verify
spools_result = result.json()
assert spool_lists_equal(spools_result, (spools.spools[3], spools.spools[4]))
assert_lists_compatible(spools_result, (spools.spools[3], spools.spools[4]))