Stuff
This commit is contained in:
0
src/tests/__init__.py
Normal file
0
src/tests/__init__.py
Normal file
42
src/tests/conftest.py
Normal file
42
src/tests/conftest.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlmodel import SQLModel
|
||||
from sqlalchemy.pool import StaticPool
|
||||
from src import app
|
||||
from src.db import get_db
|
||||
|
||||
TEST_DATABASE_URL = "sqlite:///:memory:"
|
||||
|
||||
engine = create_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool
|
||||
)
|
||||
|
||||
TestingSessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def setup_db():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
yield
|
||||
SQLModel.metadata.drop_all(engine)
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def db_session():
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client(db_session):
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
return TestClient(app)
|
||||
75
src/tests/test_maps.py
Normal file
75
src/tests/test_maps.py
Normal file
@@ -0,0 +1,75 @@
|
||||
def fake_auth_dependency():
|
||||
return lambda: True
|
||||
|
||||
auth_required = lambda *args, **kwargs: (lambda x: x)
|
||||
|
||||
def test_create_waypoint(client):
|
||||
payload = {"name": "TestPoint", "x": 10.5, "y": 20.5}
|
||||
response = client.post("/maps/waypoints", json=payload)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == payload["name"]
|
||||
assert data["x"] == payload["x"]
|
||||
assert data["y"] == payload["y"]
|
||||
assert "id" in data
|
||||
|
||||
def test_get_waypoints(client):
|
||||
response = client.get("/maps/waypoints")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) >= 1
|
||||
|
||||
def test_get_waypoint_by_id(client):
|
||||
payload = {"name": "Point1", "x": 1.0, "y": 2.0}
|
||||
create_resp = client.post("/maps/waypoints", json=payload)
|
||||
wp_id = create_resp.json()["id"]
|
||||
|
||||
response = client.get(f"/maps/waypoints/{wp_id}")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == wp_id
|
||||
assert data["name"] == payload["name"]
|
||||
|
||||
def test_get_waypoint_not_found(client):
|
||||
response = client.get("/maps/waypoints/9999")
|
||||
assert response.status_code == 404
|
||||
data = response.json()
|
||||
assert data["detail"] == "Waypoint not found"
|
||||
|
||||
def test_delete_waypoint(client):
|
||||
payload = {"name": "DeletePoint", "x": 5.0, "y": 5.0}
|
||||
create_resp = client.post("/maps/waypoints", json=payload)
|
||||
wp_id = create_resp.json()["id"]
|
||||
|
||||
del_resp = client.delete(f"/maps/waypoints/{wp_id}")
|
||||
assert del_resp.status_code == 204
|
||||
|
||||
get_resp = client.get(f"/maps/waypoints/{wp_id}")
|
||||
assert get_resp.status_code == 404
|
||||
assert get_resp.json()["detail"] == "Waypoint not found"
|
||||
|
||||
def test_delete_waypoint_not_found(client):
|
||||
response = client.delete("/maps/waypoints/9999")
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Waypoint not found"
|
||||
|
||||
def test_patch_waypoint(client):
|
||||
payload = {"name": "PatchPoint", "x": 0.0, "y": 0.0}
|
||||
create_resp = client.post("/maps/waypoints", json=payload)
|
||||
wp_id = create_resp.json()["id"]
|
||||
|
||||
patch_payload = {"name": "UpdatedPoint", "x": 1.1, "y": 2.2}
|
||||
patch_resp = client.patch(f"/maps/waypoints/{wp_id}", json=patch_payload)
|
||||
assert patch_resp.status_code == 200
|
||||
data = patch_resp.json()
|
||||
assert data["id"] == wp_id
|
||||
assert data["name"] == patch_payload["name"]
|
||||
assert data["x"] == patch_payload["x"]
|
||||
assert data["y"] == patch_payload["y"]
|
||||
|
||||
def test_patch_waypoint_not_found(client):
|
||||
patch_payload = {"name": "NonExistent", "x": 1.0, "y": 2.0}
|
||||
response = client.patch("/maps/waypoints/9999", json=patch_payload)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Waypoint not found"
|
||||
126
src/tests/test_resources.py
Normal file
126
src/tests/test_resources.py
Normal file
@@ -0,0 +1,126 @@
|
||||
def fake_auth_dependency():
|
||||
return lambda: True
|
||||
|
||||
auth_required = lambda *args, **kwargs: (lambda x: x)
|
||||
|
||||
def test_create_resource_type(client):
|
||||
payload = {"name": "Food"}
|
||||
response = client.post("/resources/types", json=payload)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == "Food"
|
||||
assert "id" in data
|
||||
|
||||
def test_list_resource_types(client):
|
||||
response = client.get("/resources/types")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) >= 1
|
||||
|
||||
def test_get_resource_type_by_id(client):
|
||||
payload = {"name": "Water"}
|
||||
create_resp = client.post("/resources/types", json=payload)
|
||||
type_id = create_resp.json()["id"]
|
||||
|
||||
response = client.get(f"/resources/types/{type_id}")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == type_id
|
||||
assert data["name"] == "Water"
|
||||
|
||||
def test_get_resource_type_not_found(client):
|
||||
response = client.get("/resources/types/9999")
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Resource type not found"
|
||||
|
||||
def test_delete_resource_type(client):
|
||||
payload = {"name": "Ammo"}
|
||||
create_resp = client.post("/resources/types", json=payload)
|
||||
type_id = create_resp.json()["id"]
|
||||
|
||||
del_resp = client.delete(f"/resources/types/{type_id}")
|
||||
assert del_resp.status_code == 204
|
||||
|
||||
get_resp = client.get(f"/resources/types/{type_id}")
|
||||
assert get_resp.status_code == 404
|
||||
assert get_resp.json()["detail"] == "Resource type not found"
|
||||
|
||||
def test_delete_resource_type_not_found(client):
|
||||
response = client.delete("/resources/types/9999")
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Resource type not found"
|
||||
|
||||
# -------- Resource Change Tests --------
|
||||
def test_create_resource_change(client):
|
||||
# First create a resource type
|
||||
type_resp = client.post("/resources/types", json={"name": "Medicine"})
|
||||
type_id = type_resp.json()["id"]
|
||||
|
||||
payload = {"change": 10.5}
|
||||
response = client.post(f"/resources/types/{type_id}/changes", json=payload)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["change"] == 10.5
|
||||
assert "timestamp" in data
|
||||
assert "id" in data
|
||||
|
||||
def test_create_resource_change_type_not_found(client):
|
||||
payload = {"change": -5}
|
||||
response = client.post("/resources/types/9999/changes", json=payload)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Resource type not found"
|
||||
|
||||
def test_list_resource_changes(client):
|
||||
# Create resource type with changes
|
||||
type_resp = client.post("/resources/types", json={"name": "Fuel"})
|
||||
type_id = type_resp.json()["id"]
|
||||
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": 20})
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": -5})
|
||||
|
||||
response = client.get(f"/resources/types/{type_id}/changes")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) == 2
|
||||
assert data[0]["change"] == 20
|
||||
assert data[1]["change"] == -5
|
||||
|
||||
def test_list_resource_changes_type_not_found(client):
|
||||
response = client.get("/resources/types/9999/changes")
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Resource type not found"
|
||||
|
||||
def test_balance_of_resource_type(client):
|
||||
# Create type
|
||||
type_resp = client.post("/resources/types", json={"name": "Supplies"})
|
||||
type_id = type_resp.json()["id"]
|
||||
|
||||
# Add changes
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": 50})
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": -20})
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": 5})
|
||||
|
||||
# Get by ID with balance
|
||||
resp = client.get(f"/resources/types/{type_id}")
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["balance"] == 35 # 50 - 20 + 5
|
||||
|
||||
def test_list_resource_types_with_balance(client):
|
||||
# Create type
|
||||
type_resp = client.post("/resources/types", json={"name": "Tools"})
|
||||
type_id = type_resp.json()["id"]
|
||||
|
||||
# Add changes
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": 10})
|
||||
client.post(f"/resources/types/{type_id}/changes", json={"change": 15})
|
||||
|
||||
# List all
|
||||
resp = client.get("/resources/types")
|
||||
print(resp.json())
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
balances = {rt["name"]: rt["balance"] for rt in data}
|
||||
assert balances["Tools"] == 25
|
||||
Reference in New Issue
Block a user