from pathlib import Path
import subprocess
import sys

here = Path(__file__).resolve().parent
runner = here.parent / 'common' / 'run_single.py'
# In a standalone copy, put run_single.py next to this verifier.
if not runner.exists():
    runner = here / 'run_single.py'
p = subprocess.run([sys.executable, str(runner), str(here/'transactions.sql')],
                   capture_output=True, text=True)
text = p.stdout.replace('backend> ', '')
assert p.returncode == 1, (p.returncode, p.stderr)
assert 'violates check constraint "accounts_balance_check"' in p.stderr
for expected in [
    'initial\n1|100\n2|100',
    'after-rollback\n1|100\n2|100',
    'after-commit\n1|70\n2|130',
    'after-savepoint-recovery\n1|70\n2|131',
]:
    assert expected in text, (expected, text)
assert '1|70\n2|131' in text
assert 'issued_inside_transaction' not in text  # SELECT is emitted through COPY
assert 'sequence-after-rollback\n2' in text
print('rollback=ok commit=ok savepoint=ok conditional-debit=ok expected-error=ok sequence-gap=ok')
