Full API integration test coverage
This commit is contained in:
@@ -1,12 +1,15 @@
|
|||||||
"""Test fixtures for integration tests."""
|
"""Test fixtures for integration tests."""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
TIMEOUT = 10
|
TIMEOUT = 10
|
||||||
|
|
||||||
|
URL = "http://spoolman:8000"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session", autouse=True)
|
@pytest.fixture(scope="session", autouse=True)
|
||||||
def _wait_for_server(): # noqa: ANN202
|
def _wait_for_server(): # noqa: ANN202
|
||||||
@@ -21,3 +24,48 @@ def _wait_for_server(): # noqa: ANN202
|
|||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def random_vendor():
|
||||||
|
"""Return a random vendor."""
|
||||||
|
# Add vendor
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/vendor",
|
||||||
|
json={"name": "John"},
|
||||||
|
)
|
||||||
|
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()
|
||||||
|
def random_filament(random_vendor: dict[str, Any]):
|
||||||
|
"""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()
|
||||||
|
|
||||||
|
filament = result.json()
|
||||||
|
yield filament
|
||||||
|
|
||||||
|
# Delete filament
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|||||||
363
tests_integration/tests/test_filament.py
Normal file
363
tests_integration/tests/test_filament.py
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
"""Integration tests for the Filament API endpoint."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
URL = "http://spoolman:8000"
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_filament(random_vendor: dict[str, Any]):
|
||||||
|
"""Test adding a filament to the database."""
|
||||||
|
# Execute
|
||||||
|
name = "Filament X"
|
||||||
|
material = "PLA"
|
||||||
|
price = 100
|
||||||
|
density = 1.25
|
||||||
|
diameter = 1.75
|
||||||
|
weight = 1000
|
||||||
|
spool_weight = 250
|
||||||
|
article_number = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
json={
|
||||||
|
"name": name,
|
||||||
|
"vendor_id": random_vendor["id"],
|
||||||
|
"material": material,
|
||||||
|
"price": price,
|
||||||
|
"density": density,
|
||||||
|
"diameter": diameter,
|
||||||
|
"weight": weight,
|
||||||
|
"spool_weight": spool_weight,
|
||||||
|
"article_number": article_number,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filament = result.json()
|
||||||
|
assert filament["name"] == name
|
||||||
|
assert filament["vendor"] == random_vendor
|
||||||
|
assert filament["material"] == material
|
||||||
|
assert filament["price"] == price
|
||||||
|
assert filament["density"] == density
|
||||||
|
assert filament["diameter"] == diameter
|
||||||
|
assert filament["weight"] == weight
|
||||||
|
assert filament["spool_weight"] == spool_weight
|
||||||
|
assert filament["article_number"] == article_number
|
||||||
|
assert filament["comment"] == comment
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_filament(random_vendor: dict[str, Any]):
|
||||||
|
"""Test getting a filament from the database."""
|
||||||
|
# Setup
|
||||||
|
name = "Filament X"
|
||||||
|
material = "PLA"
|
||||||
|
price = 100
|
||||||
|
density = 1.25
|
||||||
|
diameter = 1.75
|
||||||
|
weight = 1000
|
||||||
|
spool_weight = 250
|
||||||
|
article_number = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
json={
|
||||||
|
"name": name,
|
||||||
|
"vendor_id": random_vendor["id"],
|
||||||
|
"material": material,
|
||||||
|
"price": price,
|
||||||
|
"density": density,
|
||||||
|
"diameter": diameter,
|
||||||
|
"weight": weight,
|
||||||
|
"spool_weight": spool_weight,
|
||||||
|
"article_number": article_number,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
added_filament = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament/{added_filament['id']}",
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filament = result.json()
|
||||||
|
assert filament["name"] == name
|
||||||
|
assert filament["vendor"] == random_vendor
|
||||||
|
assert filament["material"] == material
|
||||||
|
assert filament["price"] == price
|
||||||
|
assert filament["density"] == density
|
||||||
|
assert filament["diameter"] == diameter
|
||||||
|
assert filament["weight"] == weight
|
||||||
|
assert filament["spool_weight"] == spool_weight
|
||||||
|
assert filament["article_number"] == article_number
|
||||||
|
assert filament["comment"] == comment
|
||||||
|
assert filament["id"] == added_filament["id"]
|
||||||
|
assert filament["registered"] == added_filament["registered"]
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_filament_not_found():
|
||||||
|
"""Test getting a filament that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament/123456789",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_filaments(random_vendor: dict[str, Any]):
|
||||||
|
"""Test finding filaments from the database."""
|
||||||
|
# Setup
|
||||||
|
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_1 = result.json()
|
||||||
|
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
json={
|
||||||
|
"name": "Filament Y",
|
||||||
|
"vendor_id": random_vendor["id"],
|
||||||
|
"material": "ABS",
|
||||||
|
"price": 200,
|
||||||
|
"density": 1.25,
|
||||||
|
"diameter": 1.75,
|
||||||
|
"weight": 1000,
|
||||||
|
"spool_weight": 250,
|
||||||
|
"article_number": "987654321",
|
||||||
|
"comment": "abcdefghåäö",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
filament_2 = result.json()
|
||||||
|
|
||||||
|
added_filaments_by_id = {
|
||||||
|
filament_1["id"]: filament_1,
|
||||||
|
filament_2["id"]: filament_2,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute - find all filaments
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 2
|
||||||
|
for filament in filaments:
|
||||||
|
assert filament == added_filaments_by_id[filament["id"]]
|
||||||
|
|
||||||
|
# Execute - find filaments by name
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
params={"name": "Filament X"},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 1
|
||||||
|
assert filaments[0] == added_filaments_by_id[filament_1["id"]]
|
||||||
|
|
||||||
|
# Execute - find filaments by material
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
params={"material": "abs"},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 1
|
||||||
|
assert filaments[0] == added_filaments_by_id[filament_2["id"]]
|
||||||
|
|
||||||
|
# Execute - find filaments by vendor id
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
params={"vendor_id": random_vendor["id"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 2
|
||||||
|
for filament in filaments:
|
||||||
|
assert filament == added_filaments_by_id[filament["id"]]
|
||||||
|
|
||||||
|
# Execute - find filaments by vendor name
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
params={"vendor_name": random_vendor["name"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 2
|
||||||
|
for filament in filaments:
|
||||||
|
assert filament == added_filaments_by_id[filament["id"]]
|
||||||
|
|
||||||
|
# Execute - find filaments by article number
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament",
|
||||||
|
params={"article_number": "321"},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filaments = result.json()
|
||||||
|
assert len(filaments) == 1
|
||||||
|
assert filaments[0] == added_filaments_by_id[filament_2["id"]]
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament_1['id']}").raise_for_status()
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament_2['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_filament(random_vendor: dict[str, Any]):
|
||||||
|
"""Test deleting a filament from the database."""
|
||||||
|
# Setup
|
||||||
|
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()
|
||||||
|
added_filament = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
httpx.delete(
|
||||||
|
f"{URL}/api/v1/filament/{added_filament['id']}",
|
||||||
|
).raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/filament/{added_filament['id']}",
|
||||||
|
)
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_filament_not_found():
|
||||||
|
"""Test deleting a filament that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.delete(f"{URL}/api/v1/filament/123456789")
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_filament(random_vendor: dict[str, Any]):
|
||||||
|
"""Test updating a filament in the database."""
|
||||||
|
# Setup
|
||||||
|
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()
|
||||||
|
added_filament = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
new_name = "Filament Y"
|
||||||
|
new_material = "ABS"
|
||||||
|
new_price = 200
|
||||||
|
new_density = 4.2
|
||||||
|
new_diameter = 0.12
|
||||||
|
new_weight = 5431
|
||||||
|
new_spool_weight = 123
|
||||||
|
new_article_number = "987654321"
|
||||||
|
new_comment = "test"
|
||||||
|
result = httpx.patch(
|
||||||
|
f"{URL}/api/v1/filament/{added_filament['id']}",
|
||||||
|
json={
|
||||||
|
"name": new_name,
|
||||||
|
"vendor_id": random_vendor["id"],
|
||||||
|
"material": new_material,
|
||||||
|
"price": new_price,
|
||||||
|
"density": new_density,
|
||||||
|
"diameter": new_diameter,
|
||||||
|
"weight": new_weight,
|
||||||
|
"spool_weight": new_spool_weight,
|
||||||
|
"article_number": new_article_number,
|
||||||
|
"comment": new_comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
filament = result.json()
|
||||||
|
assert filament["name"] == new_name
|
||||||
|
assert filament["vendor"] == random_vendor
|
||||||
|
assert filament["material"] == new_material
|
||||||
|
assert filament["price"] == new_price
|
||||||
|
assert filament["density"] == new_density
|
||||||
|
assert filament["diameter"] == new_diameter
|
||||||
|
assert filament["weight"] == new_weight
|
||||||
|
assert filament["spool_weight"] == new_spool_weight
|
||||||
|
assert filament["article_number"] == new_article_number
|
||||||
|
assert filament["comment"] == new_comment
|
||||||
|
assert filament["id"] == added_filament["id"]
|
||||||
|
assert filament["registered"] == added_filament["registered"]
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_filament_not_found():
|
||||||
|
"""Test updating a filament that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.patch(
|
||||||
|
f"{URL}/api/v1/filament/123456789",
|
||||||
|
json={"name": "Filament Y"},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
495
tests_integration/tests/test_spool.py
Normal file
495
tests_integration/tests/test_spool.py
Normal file
@@ -0,0 +1,495 @@
|
|||||||
|
"""Integration tests for the Spool API endpoint."""
|
||||||
|
|
||||||
|
import math
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
URL = "http://spoolman:8000"
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test adding a spool to the database."""
|
||||||
|
# Execute
|
||||||
|
first_used = "2023-01-01T00:00:00"
|
||||||
|
last_used = "2023-01-02T00:00:00"
|
||||||
|
remaining_weight = 750
|
||||||
|
location = "The Pantry"
|
||||||
|
lot_nr = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"first_used": first_used,
|
||||||
|
"last_used": last_used,
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": remaining_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
assert spool["first_used"] == first_used
|
||||||
|
assert spool["last_used"] == last_used
|
||||||
|
assert spool["filament"] == random_filament
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(remaining_weight)
|
||||||
|
assert spool["used_weight"] == pytest.approx(random_filament["weight"] - remaining_weight)
|
||||||
|
assert spool["location"] == location
|
||||||
|
assert spool["lot_nr"] == lot_nr
|
||||||
|
assert spool["comment"] == comment
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_spool_used_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test adding a spool to the database."""
|
||||||
|
# Execute
|
||||||
|
first_used = "2023-01-01T00:00:00"
|
||||||
|
last_used = "2023-01-02T00:00:00"
|
||||||
|
used_weight = 250
|
||||||
|
location = "The Pantry"
|
||||||
|
lot_nr = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"first_used": first_used,
|
||||||
|
"last_used": last_used,
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"used_weight": used_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
assert spool["first_used"] == first_used
|
||||||
|
assert spool["last_used"] == last_used
|
||||||
|
assert spool["filament"] == random_filament
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(random_filament["weight"] - used_weight)
|
||||||
|
assert spool["used_weight"] == pytest.approx(used_weight)
|
||||||
|
assert spool["location"] == location
|
||||||
|
assert spool["lot_nr"] == lot_nr
|
||||||
|
assert spool["comment"] == comment
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_spool_both_used_and_remaining_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test adding a spool to the database."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": 750,
|
||||||
|
"used_weight": 250,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert result.status_code == 400 # Cannot set both used and remaining weight
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_spool(random_filament: dict[str, Any]):
|
||||||
|
"""Test getting a spool from the database."""
|
||||||
|
# Setup
|
||||||
|
first_used = "2023-01-01T00:00:00"
|
||||||
|
last_used = "2023-01-02T00:00:00"
|
||||||
|
remaining_weight = 750
|
||||||
|
location = "The Pantry"
|
||||||
|
lot_nr = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"first_used": first_used,
|
||||||
|
"last_used": last_used,
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": remaining_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(f"{URL}/api/v1/spool/{spool['id']}")
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.json() == spool
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_spool_not_found():
|
||||||
|
"""Test getting a spool that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(f"{URL}/api/v1/spool/123456789")
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_spools(random_filament: dict[str, Any]): # noqa: PLR0915
|
||||||
|
"""Test finding spools in the database."""
|
||||||
|
# Setup
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": 1000,
|
||||||
|
"location": "The Pantry",
|
||||||
|
"lot_nr": "123456789",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool_1 = result.json()
|
||||||
|
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": 1000,
|
||||||
|
"location": "Living Room",
|
||||||
|
"lot_nr": "987654321",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool_2 = result.json()
|
||||||
|
|
||||||
|
added_spools_by_id = {
|
||||||
|
spool_1["id"]: spool_1,
|
||||||
|
spool_2["id"]: spool_2,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute - find all spools
|
||||||
|
result = httpx.get(f"{URL}/api/v1/spool")
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by filament name
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"filament_name": random_filament["name"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by filament id
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"filament_id": random_filament["id"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by filament material
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"filament_material": random_filament["material"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by filament vendor name
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"vendor_name": random_filament["vendor"]["name"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by filament vendor id
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"vendor_id": random_filament["vendor"]["id"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 2
|
||||||
|
for spool in spools:
|
||||||
|
assert spool == added_spools_by_id[spool["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by location
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"location": "The Pantry"},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 1
|
||||||
|
assert spools[0] == added_spools_by_id[spool_1["id"]]
|
||||||
|
|
||||||
|
# Execute - find spools by lot nr
|
||||||
|
result = httpx.get(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
params={"lot_nr": "123456789"},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spools = result.json()
|
||||||
|
assert len(spools) == 1
|
||||||
|
assert spools[0] == added_spools_by_id[spool_1["id"]]
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_spool(random_filament: dict[str, Any]):
|
||||||
|
"""Test deleting a spool from the database."""
|
||||||
|
# Setup
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": 1000,
|
||||||
|
"location": "The Pantry",
|
||||||
|
"lot_nr": "123456789",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
result = httpx.get(f"{URL}/api/v1/spool/{spool['id']}")
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_spool_not_found():
|
||||||
|
"""Test deleting a spool that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.delete(f"{URL}/api/v1/spool/123456789")
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_spool(random_filament: dict[str, Any]):
|
||||||
|
"""Test updating a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": 1000,
|
||||||
|
"location": "The Pantry",
|
||||||
|
"lot_nr": "123456789",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
first_used = "2023-01-01T00:00:00"
|
||||||
|
last_used = "2023-01-02T00:00:00"
|
||||||
|
remaining_weight = 750
|
||||||
|
location = "Living Room"
|
||||||
|
lot_nr = "987654321"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
result = httpx.patch(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}",
|
||||||
|
json={
|
||||||
|
"first_used": first_used,
|
||||||
|
"last_used": last_used,
|
||||||
|
"remaining_weight": remaining_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
assert spool["first_used"] == first_used
|
||||||
|
assert spool["last_used"] == last_used
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(remaining_weight)
|
||||||
|
assert spool["used_weight"] == pytest.approx(random_filament["weight"] - remaining_weight)
|
||||||
|
assert spool["location"] == location
|
||||||
|
assert spool["lot_nr"] == lot_nr
|
||||||
|
assert spool["comment"] == comment
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_spool_both_used_and_remaining_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test updating a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={"filament_id": random_filament["id"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
result = httpx.patch(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}",
|
||||||
|
json={
|
||||||
|
"remaining_weight": 750,
|
||||||
|
"used_weight": 250,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert result.status_code == 400 # Cannot update both used and remaining weight
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_spool_not_found(random_filament: dict[str, Any]):
|
||||||
|
"""Test updating a spool that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.patch(
|
||||||
|
f"{URL}/api/v1/spool/123456789",
|
||||||
|
json={"filament_id": random_filament["id"]},
|
||||||
|
)
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_spool_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test using a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
start_weight = 1000
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": start_weight,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
used_weight = 0.05
|
||||||
|
result = httpx.put(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}/use",
|
||||||
|
json={
|
||||||
|
"use_weight": used_weight,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(start_weight - used_weight)
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_spool_length(random_filament: dict[str, Any]):
|
||||||
|
"""Test using a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
start_weight = 1000
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": start_weight,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
used_length = 10 # mm
|
||||||
|
result = httpx.put(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}/use",
|
||||||
|
json={
|
||||||
|
"use_length": used_length,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
used_weight = (
|
||||||
|
random_filament["density"] * (used_length * 1e-1) * math.pi * ((random_filament["diameter"] * 1e-1 / 2) ** 2)
|
||||||
|
)
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(start_weight - used_weight)
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_spool_weight_and_length(random_filament: dict[str, Any]):
|
||||||
|
"""Test using a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={"filament_id": random_filament["id"]},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
result = httpx.put(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}/use",
|
||||||
|
json={
|
||||||
|
"use_weight": 0.05,
|
||||||
|
"use_length": 10,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert result.status_code == 400 # Cannot use both weight and length
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_spool_not_found():
|
||||||
|
"""Test using a spool that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.put(
|
||||||
|
f"{URL}/api/v1/spool/123456789/use",
|
||||||
|
json={"use_weight": 0.05},
|
||||||
|
)
|
||||||
|
assert result.status_code == 404
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Integration tests for the API."""
|
"""Integration tests for the Vendor API endpoint."""
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
@@ -56,6 +56,15 @@ def test_get_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()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_vendor_not_found():
|
||||||
|
"""Test getting a vendor that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.get(f"{URL}/api/v1/vendor/123456789")
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
def test_find_vendors():
|
def test_find_vendors():
|
||||||
"""Test finding vendors from the database."""
|
"""Test finding vendors from the database."""
|
||||||
# Setup
|
# Setup
|
||||||
@@ -93,11 +102,7 @@ def test_find_vendors():
|
|||||||
assert len(vendors) == 2
|
assert len(vendors) == 2
|
||||||
|
|
||||||
for vendor in vendors:
|
for vendor in vendors:
|
||||||
added_vendor = added_vendors_by_id[vendor["id"]]
|
assert vendor == added_vendors_by_id[vendor["id"]]
|
||||||
assert vendor["name"] == added_vendor["name"]
|
|
||||||
assert vendor["comment"] == added_vendor["comment"]
|
|
||||||
assert vendor["id"] == added_vendor["id"]
|
|
||||||
assert vendor["registered"] == added_vendor["registered"]
|
|
||||||
|
|
||||||
# Execute - Find a specific vendor
|
# Execute - Find a specific vendor
|
||||||
result = httpx.get(
|
result = httpx.get(
|
||||||
@@ -146,6 +151,15 @@ def test_delete_vendor():
|
|||||||
assert result.status_code == 404
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_vendor_not_found():
|
||||||
|
"""Test deleting a vendor that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.delete(f"{URL}/api/v1/vendor/123456789")
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
def test_update_vendor():
|
def test_update_vendor():
|
||||||
"""Test update a vendor in the database."""
|
"""Test update a vendor in the database."""
|
||||||
# Setup
|
# Setup
|
||||||
@@ -176,3 +190,12 @@ def test_update_vendor():
|
|||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_vendor_not_found():
|
||||||
|
"""Test updating a vendor that does not exist."""
|
||||||
|
# Execute
|
||||||
|
result = httpx.patch(f"{URL}/api/v1/vendor/123456789", json={"name": "John"})
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
assert result.status_code == 404
|
||||||
Reference in New Issue
Block a user