File size: 1,411 Bytes
64a1c56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.testclient import TestClient

from app import rag
from app.main import app

client = TestClient(app)  # no `with`: skips model warm-up


def upload(name, text):
    return client.post("/documents", files={"file": (name, text.encode(), "text/plain")})


def test_upload_list_ask_delete(monkeypatch):
    r = upload("notes.txt", "First page about apples.\fSecond page about oranges and juice.")
    assert r.status_code == 200 and r.json()["duplicate"] is False

    docs = client.get("/documents").json()
    assert docs[0]["status"] == "ready" and docs[0]["pages"] == 2

    assert upload("notes.txt", "First page about apples.\fSecond page about oranges and juice.").json()["duplicate"]

    monkeypatch.setattr(rag.llm, "chat", lambda *a, **k: "Oranges and juice [1]")
    r = client.post("/ask", json={"question": "what is on page 2?", "history": []})
    body = r.json()
    assert r.status_code == 200 and body["mode"] == "page_lookup" and body["sources"][0]["page"] == 2

    assert client.delete(f"/documents/{docs[0]['doc_id']}").status_code == 200
    assert client.get("/documents").json() == []


def test_ask_without_documents_gives_409():
    r = client.post("/ask", json={"question": "hello?"})
    assert r.status_code == 409


def test_bad_file_type():
    r = client.post("/documents", files={"file": ("x.exe", b"abc", "application/octet-stream")})
    assert r.status_code == 400