149 lines
4.7 KiB
Python
149 lines
4.7 KiB
Python
import pytest
|
|
|
|
from app.crud import augmented_part as augmented_part_crud
|
|
from app.models.augmented_part import AugmentedPart
|
|
from app.models.imported_part import ImportedPart
|
|
from app.models.manufacturer import Manufacturer
|
|
from app.models.part_colour import PartColour
|
|
from app.models.part_type import PartType
|
|
|
|
|
|
async def _web_login(client, email, password="secret123"):
|
|
resp = await client.post("/login", data={"email": email, "password": password})
|
|
assert resp.status_code == 303
|
|
|
|
|
|
async def _seed_augmented_parts(db):
|
|
part_t = PartType(name="Scope")
|
|
db.add(part_t)
|
|
await db.flush()
|
|
parts = [
|
|
AugmentedPart(
|
|
description=desc,
|
|
sku=sku,
|
|
colour_id=1,
|
|
manufacturer_id=1,
|
|
part_type_id=part_t.id,
|
|
)
|
|
for desc, sku in [
|
|
("Alpha scope", "SKU-C"),
|
|
("Bravo scope", "SKU-A"),
|
|
("Charlie scope", "SKU-B"),
|
|
]
|
|
]
|
|
db.add_all(parts)
|
|
await db.commit()
|
|
|
|
|
|
async def _seed_imported_parts(db):
|
|
db.add_all(
|
|
[
|
|
ImportedPart(InventoryID="INV-2", Description="Charlie widget"),
|
|
ImportedPart(InventoryID="INV-1", Description="Alpha widget"),
|
|
ImportedPart(InventoryID="INV-3", Description="Bravo widget"),
|
|
]
|
|
)
|
|
await db.commit()
|
|
|
|
|
|
def _order_of(body: str, needles: list[str]) -> list[str]:
|
|
positions = {n: body.find(n) for n in needles}
|
|
return sorted(positions, key=lambda n: positions[n])
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_augmented_parts_page_sorts_by_description_desc(
|
|
client, seeded, make_user, db_sessionmaker
|
|
):
|
|
async with db_sessionmaker() as db:
|
|
await _seed_augmented_parts(db)
|
|
await make_user("reader@example.com", role_ids=[seeded["roles"]["user"].id])
|
|
await _web_login(client, "reader@example.com")
|
|
|
|
resp = await client.get("/admin/augmented-parts?sort=description&dir=desc")
|
|
assert resp.status_code == 200
|
|
body = resp.text
|
|
assert _order_of(body, ["Charlie scope", "Bravo scope", "Alpha scope"]) == [
|
|
"Charlie scope",
|
|
"Bravo scope",
|
|
"Alpha scope",
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_augmented_parts_filter_sorts_by_sku_asc(
|
|
client, seeded, make_user, db_sessionmaker
|
|
):
|
|
async with db_sessionmaker() as db:
|
|
await _seed_augmented_parts(db)
|
|
await make_user("reader2@example.com", role_ids=[seeded["roles"]["user"].id])
|
|
await _web_login(client, "reader2@example.com")
|
|
|
|
resp = await client.get("/admin/augmented-parts/filter?sort=sku&dir=asc")
|
|
assert resp.status_code == 200
|
|
body = resp.text
|
|
# SKU-A (Bravo), SKU-B (Charlie), SKU-C (Alpha)
|
|
assert _order_of(body, ["Bravo scope", "Charlie scope", "Alpha scope"]) == [
|
|
"Bravo scope",
|
|
"Charlie scope",
|
|
"Alpha scope",
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_imported_parts_page_sorts_by_description_desc(
|
|
client, seeded, make_user, db_sessionmaker
|
|
):
|
|
async with db_sessionmaker() as db:
|
|
await _seed_imported_parts(db)
|
|
await make_user("reader3@example.com", role_ids=[seeded["roles"]["user"].id])
|
|
await _web_login(client, "reader3@example.com")
|
|
|
|
resp = await client.get("/admin/imported-parts?sort=Description&dir=desc")
|
|
assert resp.status_code == 200
|
|
body = resp.text
|
|
assert _order_of(body, ["Charlie widget", "Bravo widget", "Alpha widget"]) == [
|
|
"Charlie widget",
|
|
"Bravo widget",
|
|
"Alpha widget",
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_filtered_sorts_by_joined_colour_name(db_sessionmaker):
|
|
async with db_sessionmaker() as db:
|
|
red = PartColour(name="Red")
|
|
blue = PartColour(name="Blue")
|
|
manufacturer = Manufacturer(name="Acme", url="https://acme.example")
|
|
part_t = PartType(name="Scope")
|
|
db.add_all([red, blue, manufacturer, part_t])
|
|
await db.flush()
|
|
|
|
db.add_all(
|
|
[
|
|
AugmentedPart(
|
|
description="Red one",
|
|
colour_id=red.id,
|
|
manufacturer_id=manufacturer.id,
|
|
part_type_id=part_t.id,
|
|
),
|
|
AugmentedPart(
|
|
description="Blue one",
|
|
colour_id=blue.id,
|
|
manufacturer_id=manufacturer.id,
|
|
part_type_id=part_t.id,
|
|
),
|
|
]
|
|
)
|
|
await db.commit()
|
|
|
|
parts = await augmented_part_crud.list_filtered(
|
|
db, sort="colour", direction="asc"
|
|
)
|
|
assert [p.description for p in parts] == ["Blue one", "Red one"]
|
|
|
|
parts_desc = await augmented_part_crud.list_filtered(
|
|
db, sort="colour", direction="desc"
|
|
)
|
|
assert [p.description for p in parts_desc] == ["Red one", "Blue one"]
|