# -*- 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))
c=sqlite3.connect(':memory:'); c.executescript('CREATE TABLE channels(id INTEGER PRIMARY KEY,tenant,name,deleted_at); CREATE UNIQUE INDEX active_name ON channels(tenant,name) WHERE deleted_at IS NULL;')
c.execute("INSERT INTO channels VALUES(1,'A','blue',NULL)")
def rejected(sql):
    try: c.execute(sql); return False
    except sqlite3.IntegrityError: return True
duplicate=rejected("INSERT INTO channels VALUES(2,'A','blue',NULL)")
c.execute('UPDATE channels SET deleted_at=100 WHERE id=1'); c.execute("INSERT INTO channels VALUES(2,'A','blue',NULL)"); c.execute("INSERT INTO channels VALUES(3,'B','blue',NULL)")
restore=rejected('UPDATE channels SET deleted_at=NULL WHERE id=1')
assert duplicate and restore and c.execute('SELECT count(*) FROM channels').fetchone()[0]==3
emit(active_duplicate_rejected=duplicate,restore_conflict=restore,rows=c.execute('SELECT * FROM channels ORDER BY id').fetchall())
