# -*- coding: utf-8 -*-
"""只操作本程序创建的临时对象；全部数据为合成样本。"""
import os, sys, json, sqlite3, tempfile, pathlib, subprocess, time, socket, stat
from pathlib import Path
def emit(**kw):
    print(json.dumps(kw, ensure_ascii=False, sort_keys=True))
with tempfile.TemporaryDirectory(prefix='ywebs-next132-b2-wal-') as d:
    p=str(Path(d)/'demo.db'); w=sqlite3.connect(p); r=sqlite3.connect(p)
    w.execute('PRAGMA journal_mode=WAL'); w.execute('PRAGMA wal_autocheckpoint=0'); w.execute('CREATE TABLE t(n)'); w.execute('INSERT INTO t VALUES(0)'); w.commit()
    r.execute('BEGIN'); r.execute('SELECT * FROM t').fetchall()
    for n in range(10): w.execute('INSERT INTO t VALUES(?)',(n,)); w.commit()
    before=w.execute('PRAGMA wal_checkpoint(PASSIVE)').fetchone(); size_before=Path(p+'-wal').stat().st_size
    r.rollback(); after=w.execute('PRAGMA wal_checkpoint(TRUNCATE)').fetchone(); size_after=Path(p+'-wal').stat().st_size
    assert before[1]>before[2] and after==(0,0,0) and size_after==0
    emit(held_reader_checkpoint=before,after_reader_checkpoint=after,wal_before=size_before,wal_after=size_after,rows=w.execute('SELECT count(*) FROM t').fetchone()[0])
    r.close(); w.close()
