Files
inventory/backend/tests/test_objects.py
jze9 d6a7ac0a68 Replace box picker with free-text location in audit scan
"Where found" during inventory audit is now a plain text field
(e.g. "каб. 305") stored on the object instead of assigning it to a
box, since audit walks often use room/office labels that don't map
to the box/shelf hierarchy used elsewhere. Box assignment via the
regular object form is unaffected.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-22 15:41:30 +05:00

84 lines
3.3 KiB
Python

import uuid
from httpx import AsyncClient
async def test_create_read_update_delete_object(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects",
headers=auth_headers,
json={"inventory_number": inventory_number, "name": "Test drill", "description": "for tests"},
)
assert create_res.status_code == 201
object_id = create_res.json()["id"]
public_res = await client.get(f"/api/v1/objects/{object_id}")
assert public_res.status_code == 200
assert public_res.json()["inventory_number"] == inventory_number
assert public_res.json()["photos"] == []
update_res = await client.put(
f"/api/v1/objects/{object_id}", headers=auth_headers, json={"name": "Renamed drill"}
)
assert update_res.status_code == 200
assert update_res.json()["name"] == "Renamed drill"
delete_res = await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)
assert delete_res.status_code == 204
missing_res = await client.get(f"/api/v1/objects/{object_id}")
assert missing_res.status_code == 404
async def test_duplicate_inventory_number_conflicts(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
payload = {"inventory_number": inventory_number, "name": "First"}
first = await client.post("/api/v1/objects", headers=auth_headers, json=payload)
assert first.status_code == 201
dup = await client.post(
"/api/v1/objects", headers=auth_headers, json={**payload, "name": "Second"}
)
assert dup.status_code == 409
await client.delete(f"/api/v1/objects/{first.json()['id']}", headers=auth_headers)
async def test_scan_marks_object_found_with_location(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects", headers=auth_headers, json={"inventory_number": inventory_number, "name": "Scan test"}
)
object_id = create_res.json()["id"]
assert create_res.json()["last_seen_at"] is None
assert create_res.json()["last_seen_location"] is None
scan_res = await client.post(
f"/api/v1/objects/{object_id}/scan", headers=auth_headers, json={"location": "каб. 305"}
)
assert scan_res.status_code == 200
assert scan_res.json()["last_seen_location"] == "каб. 305"
assert scan_res.json()["last_seen_at"] is not None
# Scanning only records where it was found; it does not touch the box assignment.
assert scan_res.json()["box_id"] is None
await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)
async def test_object_qrcode(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects", headers=auth_headers, json={"inventory_number": inventory_number, "name": "QR test"}
)
object_id = create_res.json()["id"]
qr_res = await client.get(f"/api/v1/objects/{object_id}/qrcode.png")
assert qr_res.status_code == 200
assert qr_res.headers["content-type"] == "image/png"
assert qr_res.content[:8] == b"\x89PNG\r\n\x1a\n"
await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)