|
| 1 | +import io |
| 2 | +import os |
| 3 | +import json |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
| 7 | +os.environ.setdefault("TEST_MODE", "true") |
| 8 | + |
| 9 | +from backend.pdf_server import app # noqa: E402 |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture() |
| 13 | +def client(): |
| 14 | + app.config.update(TESTING=True) |
| 15 | + with app.test_client() as c: |
| 16 | + yield c |
| 17 | + |
| 18 | + |
| 19 | +def make_csv_bytes(rows=5): |
| 20 | + df = pd.DataFrame([ |
| 21 | + { |
| 22 | + "year": 2015, "make": "Kia", "model": "Sorento", "trim": "LX", "body": "SUV", |
| 23 | + "transmission": "automatic", "vin": "5xy", "state": "ca", "condition": 5.0, |
| 24 | + "odometer": 10000, "color": "white", "interior": "black", "seller": "dealer", |
| 25 | + "mmr": 20000, "sellingprice": 21000, "saledate": "2015-01-01" |
| 26 | + } for _ in range(rows) |
| 27 | + ]) |
| 28 | + bio = io.BytesIO() |
| 29 | + bio.write(df.to_csv(index=False).encode("utf-8")) |
| 30 | + bio.seek(0) |
| 31 | + return bio |
| 32 | + |
| 33 | + |
| 34 | +def test_upload_csv_basic(client): |
| 35 | + data = { |
| 36 | + "file": (make_csv_bytes(), "cars.csv") |
| 37 | + } |
| 38 | + resp = client.post("/api/upload?page=1&page_size=3", content_type="multipart/form-data", data=data) |
| 39 | + assert resp.status_code == 200 |
| 40 | + payload = resp.get_json() |
| 41 | + assert "table_data" in payload and len(payload["table_data"]) == 3 |
| 42 | + assert "basic_analysis" in payload |
| 43 | + |
| 44 | + |
| 45 | +def test_analyze_stub(client): |
| 46 | + payload = { |
| 47 | + "provider": "openai", |
| 48 | + "model": "gpt-4.1", |
| 49 | + "table_data": [ |
| 50 | + {"year": 2015, "make": "Kia", "sellingprice": 21000, "saledate": "2015-01-01"}, |
| 51 | + {"year": 2016, "make": "Kia", "sellingprice": 22000, "saledate": "2016-01-01"}, |
| 52 | + ] |
| 53 | + } |
| 54 | + resp = client.post("/api/analyze", data=json.dumps(payload), content_type="application/json") |
| 55 | + assert resp.status_code == 200 |
| 56 | + data = resp.get_json() |
| 57 | + assert "analysis" in data and isinstance(data["analysis"], str) |
| 58 | + |
| 59 | + |
| 60 | +def test_fill_missing_ai(client): |
| 61 | + table = [ |
| 62 | + {"make": "Kia", "model": "Sorento", "color": "black"}, |
| 63 | + {"make": "Kia", "model": "Sorento", "color": ""}, # пропуск |
| 64 | + ] |
| 65 | + payload = { |
| 66 | + "table_data": table, |
| 67 | + "columns": ["make", "model", "color"], |
| 68 | + "missing_info": {"color": 1} |
| 69 | + } |
| 70 | + resp = client.post("/api/fill-missing-ai", data=json.dumps(payload), content_type="application/json") |
| 71 | + assert resp.status_code == 200 |
| 72 | + data = resp.get_json() |
| 73 | + assert "recommendations" in data and "color" in data["recommendations"] |
| 74 | + assert isinstance(data["recommendations"]["color"], list) |
| 75 | + |
| 76 | + |
0 commit comments