|
| 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 | +from backend.pdf_server import app # noqa: E402 |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture() |
| 12 | +def client(): |
| 13 | + app.config.update(TESTING=True) |
| 14 | + with app.test_client() as c: |
| 15 | + yield c |
| 16 | + |
| 17 | + |
| 18 | +def make_large_csv_bytes(rows=2000): |
| 19 | + df = pd.DataFrame([ |
| 20 | + { |
| 21 | + "year": 2015 + (i % 5), |
| 22 | + "make": "Make" + str(i % 3), |
| 23 | + "model": "Model" + str(i % 7), |
| 24 | + "trim": "Base", |
| 25 | + "body": "Sedan", |
| 26 | + "transmission": "auto", |
| 27 | + "vin": f"VIN{i:06d}", |
| 28 | + "state": "ca", |
| 29 | + "condition": float((i % 10) + 1), |
| 30 | + "odometer": 1000 * i, |
| 31 | + "color": "black", |
| 32 | + "interior": "gray", |
| 33 | + "seller": "dealer", |
| 34 | + "mmr": 10000 + i, |
| 35 | + "sellingprice": 12000 + i, |
| 36 | + "saledate": "2015-01-01", |
| 37 | + } for i in range(rows) |
| 38 | + ]) |
| 39 | + bio = io.BytesIO(df.to_csv(index=False).encode("utf-8")) |
| 40 | + bio.seek(0) |
| 41 | + return bio |
| 42 | + |
| 43 | + |
| 44 | +def test_upload_large_csv_is_limited(client): |
| 45 | + # page_size=1000, должен вернуться ровно 1000 строк |
| 46 | + resp = client.post( |
| 47 | + "/api/upload?page=1&page_size=1000", |
| 48 | + content_type="multipart/form-data", |
| 49 | + data={"file": (make_large_csv_bytes(5000), "big.csv")}, |
| 50 | + ) |
| 51 | + assert resp.status_code == 200 |
| 52 | + data = resp.get_json() |
| 53 | + assert len(data["table_data"]) == 1000 |
| 54 | + assert data["total_rows"] >= 1000 |
| 55 | + |
| 56 | + |
| 57 | +def test_upload_invalid_extension_rejected(client): |
| 58 | + bad_file = io.BytesIO(b"not a real data") |
| 59 | + resp = client.post( |
| 60 | + "/api/upload", |
| 61 | + content_type="multipart/form-data", |
| 62 | + data={"file": (bad_file, "data.txt")}, |
| 63 | + ) |
| 64 | + assert resp.status_code == 400 |
| 65 | + j = resp.get_json() |
| 66 | + assert "error" in j |
| 67 | + |
| 68 | + |
| 69 | +def test_analyze_missing_fields_400(client): |
| 70 | + resp = client.post("/api/analyze", data=json.dumps({"provider": "openai"}), content_type="application/json") |
| 71 | + assert resp.status_code == 400 |
| 72 | + |
| 73 | + |
0 commit comments