| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import base64 |
| 12 | import json |
| 13 | import re |
| 14 | import sys |
| 15 | import time |
| 16 | import urllib.error |
| 17 | import urllib.request |
| 18 | |
| 19 | ENDPOINT = "http://hzk-surreal:8000/sql" |
| 20 | NS = "hezarfen" |
| 21 | DB = "zeka" |
| 22 | PER_QUERY_TIMEOUT = 240 |
| 23 | |
| 24 | |
| 25 | def sql(query: str): |
| 26 | req = urllib.request.Request(ENDPOINT, data=query.encode("utf-8")) |
| 27 | req.add_header("Accept", "application/json") |
| 28 | req.add_header("Content-Type", "text/plain; charset=utf-8") |
| 29 | req.add_header("surreal-ns", NS) |
| 30 | req.add_header("surreal-db", DB) |
| 31 | req.add_header("Authorization", "Basic " + base64.b64encode(b"root:root").decode()) |
| 32 | with urllib.request.urlopen(req, timeout=PER_QUERY_TIMEOUT) as resp: |
| 33 | return json.loads(resp.read().decode("utf-8")) |
| 34 | |
| 35 | |
| 36 | def main() -> int: |
| 37 | text = open("/seed/99_dogrula.surql", encoding="utf-8").read() |
| 38 | lets = [l.strip() for l in text.splitlines() if l.strip().startswith("LET ")] |
| 39 | prefix = "\n".join(lets) + "\n" |
| 40 | |
| 41 | blocks = re.split(r"\n(?=--\s*I\d+:)", text) |
| 42 | passed = violations = errors = slow = 0 |
| 43 | t_all = time.time() |
| 44 | |
| 45 | for block in blocks: |
| 46 | m = re.match(r"--\s*(I\d+):\s*(.+)", block) |
| 47 | if not m: |
| 48 | continue |
| 49 | tag, desc = m.group(1), m.group(2).strip() |
| 50 | body = "\n".join( |
| 51 | l for l in block.splitlines() if not l.strip().startswith("--") |
| 52 | ).strip() |
| 53 | if not body: |
| 54 | continue |
| 55 | |
| 56 | t0 = time.time() |
| 57 | try: |
| 58 | res = sql(prefix + body) |
| 59 | except urllib.error.HTTPError as e: |
| 60 | errors += 1 |
| 61 | print(f"{tag:<5} HATA {e.code} {e.read().decode('utf-8','replace')[:120]}", |
| 62 | flush=True) |
| 63 | continue |
| 64 | except Exception as e: |
| 65 | slow += 1 |
| 66 | print(f"{tag:<5} ZAMAN ASIMI ({type(e).__name__}) — {desc[:55]}", flush=True) |
| 67 | continue |
| 68 | dt = time.time() - t0 |
| 69 | |
| 70 | last = res[-1] if res else {} |
| 71 | if last.get("status") != "OK": |
| 72 | errors += 1 |
| 73 | print(f"{tag:<5} HATA {str(last.get('result'))[:120]}", flush=True) |
| 74 | continue |
| 75 | |
| 76 | out = last.get("result") |
| 77 | n = 0 |
| 78 | if isinstance(out, list) and out and isinstance(out[0], dict): |
| 79 | n = out[0].get("count", 0) or 0 |
| 80 | if n: |
| 81 | violations += 1 |
| 82 | print(f"{tag:<5} IHLAL {n:>8} {desc[:60]}", flush=True) |
| 83 | else: |
| 84 | passed += 1 |
| 85 | if dt > 5: |
| 86 | print(f"{tag:<5} ok ({dt:.0f}s)", flush=True) |
| 87 | |
| 88 | print(f"\ngecen: {passed} | IHLAL: {violations} | hata: {errors} | " |
| 89 | f"zaman asimi: {slow} | sure: {time.time() - t_all:.0f}s") |
| 90 | ok = violations == 0 and errors == 0 |
| 91 | print("SONUC: " + ("TEMIZ" if ok and slow == 0 else |
| 92 | "TEMIZ (bazi sorgular zaman asimina ugradi)" if ok else |
| 93 | "SORUN VAR")) |
| 94 | return 0 if ok else 1 |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | sys.exit(main()) |