File size: 2,133 Bytes
c1fe190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""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