|
| 1 | +"""Paketle gelen örnek verinin ilk açılışta "Simple Project" olarak yüklenmesi. |
| 2 | +
|
| 3 | +cli.py packaged çalıştırmada BIBEXPY_SAMPLES_DIR'i set eder; main.py startup'ı |
| 4 | +depo TAMAMEN boşken örnek projeyi BİR KEZ oluşturur (.sample_seeded işareti). |
| 5 | +Kullanıcı projeyi silerse yeniden oluşturulmaz; env yoksa hiç çalışmaz. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from fastapi.testclient import TestClient |
| 12 | + |
| 13 | + |
| 14 | +def _fresh_app(monkeypatch, storage: Path, samples: Path | None): |
| 15 | + monkeypatch.setenv("STORAGE_DIR", str(storage)) |
| 16 | + storage.mkdir(parents=True, exist_ok=True) |
| 17 | + if samples is None: |
| 18 | + monkeypatch.delenv("BIBEXPY_SAMPLES_DIR", raising=False) |
| 19 | + else: |
| 20 | + monkeypatch.setenv("BIBEXPY_SAMPLES_DIR", str(samples)) |
| 21 | + api_root = Path(__file__).resolve().parents[1] |
| 22 | + sys.path.insert(0, str(api_root)) |
| 23 | + # config/main'i temiz import et (env'i her testte yeniden okumak için) |
| 24 | + for mod in list(sys.modules): |
| 25 | + if mod.startswith(("main", "config", "routers", "services", "models")): |
| 26 | + sys.modules.pop(mod, None) |
| 27 | + from main import app # noqa: E402 |
| 28 | + |
| 29 | + return app |
| 30 | + |
| 31 | + |
| 32 | +def test_seed_creates_simple_project_once(monkeypatch, tmp_path): |
| 33 | + samples = tmp_path / "samples" |
| 34 | + samples.mkdir() |
| 35 | + (samples / "scopus.csv").write_text("a,b\n1,2\n", encoding="utf-8") |
| 36 | + (samples / "savedrecs.txt").write_text("FN Clarivate\nVR 1.0\n", encoding="utf-8") |
| 37 | + (samples / "notes.md").write_text("ignored", encoding="utf-8") # unknown tür → kopyalanmaz |
| 38 | + storage = tmp_path / "storage" |
| 39 | + |
| 40 | + # İlk açılış (boş depo) → Simple Project + yalnız tanınan 2 dosya |
| 41 | + app = _fresh_app(monkeypatch, storage, samples) |
| 42 | + with TestClient(app) as client: # context manager → startup event'leri çalışır |
| 43 | + projects = client.get("/api/projects").json() |
| 44 | + assert [p["name"] for p in projects] == ["Simple Project"] |
| 45 | + pid = projects[0]["id"] |
| 46 | + files = client.get(f"/api/projects/{pid}/files").json() |
| 47 | + assert {f["kind"] for f in files} == {"scopus_csv", "wos_txt"} |
| 48 | + assert len(files) == 2 |
| 49 | + |
| 50 | + # Kullanıcı örnek projeyi siler... |
| 51 | + app2 = _fresh_app(monkeypatch, storage, samples) |
| 52 | + with TestClient(app2) as client: |
| 53 | + pid = client.get("/api/projects").json()[0]["id"] |
| 54 | + assert client.delete(f"/api/projects/{pid}").status_code == 204 |
| 55 | + |
| 56 | + # ...sonraki açılışta YENİDEN OLUŞMAZ (.sample_seeded işareti) |
| 57 | + app3 = _fresh_app(monkeypatch, storage, samples) |
| 58 | + with TestClient(app3) as client: |
| 59 | + assert client.get("/api/projects").json() == [] |
| 60 | + |
| 61 | + |
| 62 | +def test_no_seed_without_env(monkeypatch, tmp_path): |
| 63 | + app = _fresh_app(monkeypatch, tmp_path / "storage", None) |
| 64 | + with TestClient(app) as client: |
| 65 | + assert client.get("/api/projects").json() == [] |
| 66 | + |
| 67 | + |
| 68 | +def test_no_seed_when_projects_exist(monkeypatch, tmp_path): |
| 69 | + samples = tmp_path / "samples" |
| 70 | + samples.mkdir() |
| 71 | + (samples / "scopus.csv").write_text("a,b\n1,2\n", encoding="utf-8") |
| 72 | + storage = tmp_path / "storage" |
| 73 | + |
| 74 | + # Önce örneksiz bir proje oluşturulmuş depo hazırla |
| 75 | + app = _fresh_app(monkeypatch, storage, None) |
| 76 | + with TestClient(app) as client: |
| 77 | + client.post("/api/projects", json={"name": "Mine"}) |
| 78 | + |
| 79 | + # Env set edilse bile dolu depoya örnek EKLENMEZ |
| 80 | + app2 = _fresh_app(monkeypatch, storage, samples) |
| 81 | + with TestClient(app2) as client: |
| 82 | + names = [p["name"] for p in client.get("/api/projects").json()] |
| 83 | + assert names == ["Mine"] |
0 commit comments