"""Rezervasyon API testleri — geçici DATA_DIR ile izole çalışır.""" import importlib import os import pytest from fastapi import FastAPI from fastapi.testclient import TestClient @pytest.fixture() def client(tmp_path, monkeypatch): monkeypatch.setenv("DATA_DIR", str(tmp_path)) monkeypatch.setenv("RESTAURANT_TOTAL_TABLES", "2") from app.routes import reservations importlib.reload(reservations) app = FastAPI() app.include_router(reservations.router) return TestClient(app) PAYLOAD = { "name": "Arda Yılmaz", "phone": "05321112233", "date": "2030-08-12", "time": "20:00", "guests": 4, "note": "Pencere kenarı", } def test_create_assigns_table_and_lists(client): r = client.post("/api/reservations", json=PAYLOAD) assert r.status_code == 201 body = r.json() assert body["table"] == 1 assert body["status"] == "pending" listed = client.get("/api/reservations", params={"date": PAYLOAD["date"]}).json() assert len(listed) == 1 def test_table_map_and_capacity(client): client.post("/api/reservations", json=PAYLOAD) client.post("/api/reservations", json={**PAYLOAD, "name": "İkinci Misafir"}) tables = client.get( "/api/reservations/tables", params={"date": PAYLOAD["date"], "time": PAYLOAD["time"]}, ).json() assert tables["total"] == 2 assert all(t["occupied"] for t in tables["tables"]) full = client.post("/api/reservations", json={**PAYLOAD, "name": "Üçüncü"}) assert full.status_code == 409 def test_cancel_frees_table(client): created = client.post("/api/reservations", json=PAYLOAD).json() r = client.patch(f"/api/reservations/{created['id']}", json={"status": "cancelled"}) assert r.status_code == 200 tables = client.get( "/api/reservations/tables", params={"date": PAYLOAD["date"], "time": PAYLOAD["time"]}, ).json() assert sum(t["occupied"] for t in tables["tables"]) == 0 def test_past_date_rejected(client): r = client.post("/api/reservations", json={**PAYLOAD, "date": "2020-01-01"}) assert r.status_code == 422