Spaces:
Running on Zero
Running on Zero
File size: 4,555 Bytes
4ea3839 1a4837b 4ea3839 | 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | """Unit tests for the Unified Moldovan Master Dictionary Manager & Studio Endpoints."""
import pytest
import os
import sys
from fastapi.testclient import TestClient
# Add project roots to sys.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "moldovan-qwen"))
from engine.dictionary_manager import DictionaryManager
from studio_server import app
@pytest.fixture
def dict_manager():
return DictionaryManager()
@pytest.fixture
def client():
import os
headers = {}
if os.environ.get("FLOW_API_KEY"):
headers["Authorization"] = f"Bearer {os.environ['FLOW_API_KEY']}"
return TestClient(app, headers=headers)
def test_dictionary_manager_initialization(dict_manager):
entries = dict_manager.load_all_entries()
assert len(entries) >= 700, f"Expected at least 700 entries, got {len(entries)}"
# Check entry schema
sample = entries[0]
assert "word" in sample
assert "variants" in sample
assert "phonetic" in sample
assert "ro_meaning" in sample
assert "dialect_level" in sample
assert sample["phonetic"].startswith("[") and sample["phonetic"].endswith("]")
def test_dictionary_manager_filtering(dict_manager):
# Test search
res = dict_manager.get_entries(search="harbuz")
assert res["total"] >= 1
assert any(e["word"] == "harbuz" or "harbuz" in e["variants"] for e in res["terms"])
# Test dialect level filter
l4_res = dict_manager.get_entries(dialect_level=4, limit=200)
assert l4_res["total"] > 0
assert all(e["dialect_level"] == 4 for e in l4_res["terms"])
# Test letter filter
h_res = dict_manager.get_entries(letter="H", limit=200)
assert h_res["total"] > 0
assert all(e["word"].upper().startswith("H") for e in h_res["terms"])
def test_dictionary_stats(dict_manager):
stats = dict_manager.get_stats()
assert stats["total_terms"] >= 700
assert stats["phonetic_coverage_pct"] == 100.0
assert stats["with_russian"] > 0
assert stats["with_english"] > 0
assert "1" in stats["levels"]
assert "4" in stats["levels"]
def test_dictionary_crud_cycle(dict_manager):
test_entry = {
"id": "test_cuvant_unic_123",
"word": "ciotkocuvant",
"variants": ["ciotkocuvant", "cetkocuvant"],
"phonetic": "[ciot-ko-cu-VANT]",
"ro_meaning": "cuvant de test foarte bun",
"ru_meaning": "чёткое тестовое слово",
"en_meaning": "awesome test word",
"category": "Argou Urban Chișinău",
"dialect_level": 4
}
# Add
saved = dict_manager.add_entry(test_entry)
assert saved["word"] == "ciotkocuvant"
assert dict_manager.get_entry("test_cuvant_unic_123") is not None
# Update
test_entry["ro_meaning"] = "definitie actualizata"
updated = dict_manager.update_entry("test_cuvant_unic_123", test_entry)
assert updated["ro_meaning"] == "definitie actualizata"
# Delete
deleted = dict_manager.delete_entry("test_cuvant_unic_123")
assert deleted is True
assert dict_manager.get_entry("test_cuvant_unic_123") is None
def test_api_master_dictionary_endpoints(client):
# GET Master list
res = client.get("/api/dictionary/master?limit=10")
assert res.status_code == 200
data = res.json()
assert "terms" in data
assert "total" in data
assert len(data["terms"]) == 10
# GET Stats
stats_res = client.get("/api/dictionary/stats")
assert stats_res.status_code == 200
stats = stats_res.json()
assert stats["total_terms"] >= 700
assert stats["phonetic_coverage_pct"] == 100.0
# POST new entry via API
new_word = {
"word": "zavaliscoterm",
"variants": ["zavaliscoterm"],
"phonetic": "[za-va-lis-co-TERM]",
"ro_meaning": "termen de proba",
"ru_meaning": "пробный термин",
"en_meaning": "sample term",
"category": "Argou Urban Chișinău",
"dialect_level": 4
}
create_res = client.post("/api/dictionary/entry", json=new_word)
assert create_res.status_code == 200
created_data = create_res.json()
entry_id = created_data.get("id") or created_data.get("entry", {}).get("id") or "zavaliscoterm"
# GET created entry
get_res = client.get(f"/api/dictionary/entry/{entry_id}")
assert get_res.status_code == 200
# DELETE created entry
del_res = client.delete(f"/api/dictionary/entry/{entry_id}")
assert del_res.status_code == 200
|