Skip to content

Commit dfbe21f

Browse files
committed
test: add unit tests for data pipeline and api
1 parent 3cd1885 commit dfbe21f

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

tests/test_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fastapi.testclient import TestClient
2+
from app.main import app
3+
4+
client = TestClient(app)
5+
6+
def test_read_health():
7+
# Since the model won't be loaded in a standard test environment,
8+
# we expect the model_loaded status to be false or for the startup to fail.
9+
# We can mock the engine if needed, but a simple health check is a good start.
10+
response = client.get("/health")
11+
assert response.status_code == 200
12+
assert "status" in response.json()
13+
assert response.json()["status"] == "ok"
14+
15+
def test_generate_fail_no_model():
16+
# Test that /generate returns 503 if engine is not initialized
17+
response = client.post(
18+
"/generate",
19+
json={"instruction": "How do I reset my password?"}
20+
)
21+
assert response.status_code == 503
22+
assert response.json()["detail"] == "Model not loaded"

tests/test_data.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
import os
3+
import json
4+
import shutil
5+
from src.data.preprocess import clean_data, format_prompt, load_raw_data
6+
7+
def test_clean_data():
8+
sample_data = [
9+
{"instruction": "test 1", "response": "resp 1"},
10+
{"instruction": "test 1", "response": "resp 1"}, # Duplicate
11+
{"instruction": "test 2", "response": "resp 2"}
12+
]
13+
cleaned = clean_data(sample_data)
14+
assert len(cleaned) == 2
15+
assert cleaned[0]["instruction"] == "test 1"
16+
assert cleaned[1]["instruction"] == "test 2"
17+
18+
def test_format_prompt():
19+
sample = {"instruction": "Hello", "response": "Hi there"}
20+
formatted = format_prompt(sample)
21+
assert "text" in formatted
22+
assert "<system>" in formatted["text"]
23+
assert "<instruction> Hello </instruction>" in formatted["text"]
24+
assert "<response> Hi there </response>" in formatted["text"]
25+
26+
def test_load_raw_data(tmp_path):
27+
d = tmp_path / "data"
28+
d.mkdir()
29+
p = d / "test.jsonl"
30+
content = {"instruction": "q1", "response": "a1"}
31+
p.write_text(json.dumps(content) + "\n")
32+
33+
loaded = load_raw_data(str(p))
34+
assert len(loaded) == 1
35+
assert loaded[0]["instruction"] == "q1"

0 commit comments

Comments
 (0)