Text-to-SQL / tests /test_pilot_duzeltmeleri.py
SevdanurGenc's picture
Upload 466 files
a72d1cf verified
Raw History Blame Contribute Delete
4.51 kB
"""Stres testinde (pilot/) bulunan hatalar için regresyon testleri."""
import sqlite3
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from t2s.extract import ResolvedTable, extract_html, resolve # noqa: E402
from t2s.pipeline import merge_continuations # noqa: E402
from t2s.schema import TOTAL_RE, build_table, column_name, connect, write_table # noqa: E402
from t2s.trnum import fold # noqa: E402
def _rt(caption, page, body, src="a.pdf", hdr=(["Ad"], ["Değer"])):
return ResolvedTable(caption, [list(h) for h in hdr], body, src, page, "test")
def test_parantezli_yil_korunur():
assert column_name(["Nüfus (2025)"], 0) == "nufus_2025"
assert column_name(["Ücret (TL)"], 0) == "ucret"
def test_il_toplami_toplam_satiri():
assert TOTAL_RE.search(fold("İl Toplamı"))
assert TOTAL_RE.search(fold("Genel Toplam"))
assert not TOTAL_RE.search(fold("Toplamı bilinmeyen kalem a"))
def test_sayfalara_bolunmus_tablo_birlesir_uc_sayfa():
parts = [_rt("Tablo 6: Mahalleler", 1, [["A", "1"]]), _rt("", 2, [["B", "2"]]), _rt("", 3, [["C", "3"]]),
_rt("Tablo 7: Başka", 4, [["D", "4"]])]
out = merge_continuations(parts)
assert len(out) == 2 and [r[0] for r in out[0].body] == ["A", "B", "C"]
def test_farkli_baslikli_ardisik_tablo_birlesmez():
out = merge_continuations([_rt("Bilanço", 1, [["A", "1"]]), _rt("Kar veya Zarar Tablosu", 2, [["B", "2"]])])
assert len(out) == 2
def test_adsiz_tablolar_birbirinin_uzerine_yazilmaz(tmp_path):
con = connect(tmp_path / "t.db")
n1 = write_table(con, build_table(_rt("", 1, [["A", "1"]], src="x.pdf")))
n2 = write_table(con, build_table(_rt("", 3, [["B", "2"]], src="y.pdf")))
assert n1 != n2 and "sutun" not in (n1, n2)
assert {t for (t,) in con.execute("SELECT tablo FROM _tablolar")} == {n1, n2}
def test_ayni_adli_iki_tablo_ayni_sayfada_korunur(tmp_path):
con = connect(tmp_path / "t.db")
a = write_table(con, build_table(_rt("Tablo 1: Satışlar", 1, [["A", "1"]])))
b = write_table(con, build_table(_rt("Tablo 2: Satışlar", 1, [["B", "2"]])))
assert a != b
# aynı tablo yeniden aktarılırsa kendi yerine yazılır, kopya oluşmaz
b2 = write_table(con, build_table(_rt("Tablo 2: Satışlar", 1, [["B", "3"]])))
assert b2 == b and con.execute("SELECT COUNT(*) FROM _tablolar").fetchone()[0] == 2
def test_tfoot_veri_sayilmaz():
html = ("<table><caption>Tablo 1: X</caption><thead><tr><th>İl</th><th>Değer</th></tr></thead>"
"<tbody><tr><td>A</td><td>1</td></tr></tbody><tfoot><tr><td colspan='2'>Kaynak: Y</td></tr></tfoot></table>")
rt = resolve(extract_html(html)[0])
assert [r[0] for r in rt.body] == ["A"]
def test_donem_satiri_baslik_bolum_satiri_govde():
from t2s.extract import Cell, RawTable
g = [["", "Dipnot", "Cari Dönem", "Önceki Dönem"], ["", "", "31.12.2025", "31.12.2024"],
["VARLIKLAR", "", "", ""], ["Nakit", "5", "1.000", "900"], ["Stoklar", "9", "2.000", "1.500"]]
cells = [Cell(r, c, v) for r, row in enumerate(g) for c, v in enumerate(row)]
rt = resolve(RawTable(cells, len(g), 4, caption="Bilanço", source="f.pdf", page=1, extractor="test"))
assert rt.n_header_rows == 2 and rt.body[0][0] == "VARLIKLAR"
bt = build_table(rt)
names = [c.name for c in bt.columns]
assert any("2025" in n for n in names)
assert next(c for c in bt.columns if c.original.startswith("Dipnot")).semantic == "kod"
def test_replace_islevi_serbest_yazma_komutu_reddedilir():
from t2s.errors import static_checks
con = sqlite3.connect(":memory:")
assert not static_checks("SELECT CAST(REPLACE(REPLACE('x', '.', ''), ',', '.') AS REAL)", con)
for bad in ("REPLACE INTO t VALUES (1)", "WITH x AS (SELECT 1) DELETE FROM t", "SELECT 1; DROP TABLE t"):
assert [e.tip for e in static_checks(bad, con)] == ["SOZDIZIMI"]
def test_toplam_uyarisi_yalnizca_toplam_satiri_sonuca_girebiliyorsa(tmp_path):
from t2s.errors import static_checks
con = connect(tmp_path / "t.db")
rt = _rt("Tablo 1: İller", 1, [["Ankara", "Marmara", "10"], ["İzmir", "Ege", "20"], ["Toplam", "", "30"]],
hdr=(["İl"], ["Bölge"], ["Değer"]))
name = write_table(con, build_table(rt))
assert [e.tip for e in static_checks(f'SELECT SUM(deger) FROM "{name}"', con)] == ["SAYISAL_AKIL"]
assert static_checks(f"SELECT SUM(deger) FROM \"{name}\" WHERE bolge = 'Ege'", con) == []