Skip to content

Commit

Permalink
Modernized vendor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed Sep 8, 2023
1 parent 45f33e3 commit e7affa3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 53 deletions.
10 changes: 0 additions & 10 deletions tests_integration/tests/filament/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def filament_lists_equal(a: Iterable[dict[str, Any]], b: Iterable[dict[str, Any]
@dataclass
class Fixture:
filaments: list[dict[str, Any]]
filaments_by_id: dict[str, dict[str, Any]]


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -98,17 +97,8 @@ def filaments(random_vendor_mod: dict[str, Any], random_empty_vendor_mod: dict[s
result.raise_for_status()
filament_5 = result.json()

added_filaments_by_id = {
filament_1["id"]: filament_1,
filament_2["id"]: filament_2,
filament_3["id"]: filament_3,
filament_4["id"]: filament_4,
filament_5["id"]: filament_5,
}

yield Fixture(
filaments=[filament_1, filament_2, filament_3, filament_4, filament_5],
filaments_by_id=added_filaments_by_id,
)

httpx.delete(f"{URL}/api/v1/filament/{filament_1['id']}").raise_for_status()
Expand Down
10 changes: 0 additions & 10 deletions tests_integration/tests/spool/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def spool_lists_equal(a: Iterable[dict[str, Any]], b: Iterable[dict[str, Any]])
@dataclass
class Fixture:
spools: list[dict[str, Any]]
spools_by_id: dict[str, dict[str, Any]]
filament: dict[str, Any]


Expand Down Expand Up @@ -83,17 +82,8 @@ def spools(
result.raise_for_status()
spool_5 = result.json()

added_spools_by_id = {
spool_1["id"]: spool_1,
spool_2["id"]: spool_2,
spool_3["id"]: spool_3,
spool_4["id"]: spool_4,
spool_5["id"]: spool_5,
}

yield Fixture(
spools=[spool_1, spool_2, spool_3, spool_4, spool_5],
spools_by_id=added_spools_by_id,
filament=random_filament_mod,
)

Expand Down
69 changes: 36 additions & 33 deletions tests_integration/tests/vendor/test_find.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
"""Integration tests for the Vendor API endpoint."""

from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any

import httpx
import pytest

URL = "http://spoolman:8000"


def test_find_vendors():
"""Test finding vendors from the database."""
# Setup
name_1 = "John"
comment_1 = "abcdefghåäö"
def vendor_lists_equal(a: Iterable[dict[str, Any]], b: Iterable[dict[str, Any]]) -> bool:
"""Compare two lists of vendors where the order of the vendors is not guaranteed."""
return sorted(a, key=lambda x: x["id"]) == sorted(b, key=lambda x: x["id"])


@dataclass
class Fixture:
vendors: list[dict[str, Any]]


@pytest.fixture(scope="module")
def vendors() -> Iterable[Fixture]:
"""Add some vendors to the database."""
result = httpx.post(
f"{URL}/api/v1/vendor",
json={"name": name_1, "comment": comment_1},
json={"name": "John", "comment": "abcdefghåäö"},
)
result.raise_for_status()
vendor_1 = result.json()

name_2 = "Stan"
comment_2 = "gfdadfg"
result = httpx.post(
f"{URL}/api/v1/vendor",
json={"name": name_2, "comment": comment_2},
json={"name": "Stan", "comment": "gfdadfg"},
)
result.raise_for_status()
vendor_2 = result.json()

added_vendors_by_id = {
vendor_1["id"]: vendor_1,
vendor_2["id"]: vendor_2,
}
yield Fixture(
vendors=[vendor_1, vendor_2],
)

# Execute - Find all vendors
httpx.delete(f"{URL}/api/v1/vendor/{vendor_1['id']}").raise_for_status()
httpx.delete(f"{URL}/api/v1/vendor/{vendor_2['id']}").raise_for_status()


def test_find_all_vendors(vendors: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/vendor",
)
result.raise_for_status()

# Verify
vendors = result.json()
assert len(vendors) == 2
vendors_result = result.json()
assert vendor_lists_equal(vendors_result, vendors.vendors)

for vendor in vendors:
assert vendor == added_vendors_by_id[vendor["id"]]

# Execute - Find a specific vendor
def test_find_vendors_by_name(vendors: Fixture):
# Execute
result = httpx.get(
f"{URL}/api/v1/vendor",
params={"name": name_1},
params={"name": vendors.vendors[0]["name"]},
)
result.raise_for_status()

# Verify
vendors = result.json()
assert len(vendors) == 1

vendor = vendors[0]

assert vendor["name"] == name_1
assert vendor["comment"] == comment_1
assert vendor["id"] == vendor_1["id"]
assert vendor["registered"] == vendor_1["registered"]

# Clean up
httpx.delete(f"{URL}/api/v1/vendor/{vendor_1['id']}").raise_for_status()
httpx.delete(f"{URL}/api/v1/vendor/{vendor_2['id']}").raise_for_status()
vendors_result = result.json()
assert vendors_result == [vendors.vendors[0]]

0 comments on commit e7affa3

Please sign in to comment.