"""Two independent in-memory parent/child fixtures; no existing database writes."""
import sqlite3,json,platform
def branch(mode):
 c=sqlite3.connect(':memory:',isolation_level=None)
 try:
  c.execute('PRAGMA foreign_keys=ON');assert c.execute('PRAGMA foreign_keys').fetchone()[0]==1
  c.execute('CREATE TABLE parent(id INTEGER PRIMARY KEY,name TEXT NOT NULL)');c.execute('CREATE TABLE child(id INTEGER PRIMARY KEY,parent_id INTEGER NOT NULL REFERENCES parent(id) ON DELETE CASCADE,note TEXT)')
  c.execute("INSERT INTO parent VALUES(1,'old')");c.execute("INSERT INTO child VALUES(10,1,'history')")
  before={'parent':c.execute('SELECT * FROM parent').fetchall(),'child':c.execute('SELECT * FROM child').fetchall()}
  if mode=='replace':sql="INSERT OR REPLACE INTO parent VALUES(1,'new')"
  else:sql="INSERT INTO parent VALUES(1,'new') ON CONFLICT(id) DO UPDATE SET name=excluded.name"
  c.execute(sql);after={'parent':c.execute('SELECT * FROM parent').fetchall(),'child':c.execute('SELECT * FROM child').fetchall()}
  assert before=={'parent':[(1,'old')],'child':[(10,1,'history')]}and after['parent']==[(1,'new')]
  assert after['child']==([]if mode=='replace'else[(10,1,'history')])
  assert c.execute('PRAGMA foreign_key_check').fetchall()==[]
  return {'sql':sql,'foreign_keys':1,'before':before,'after':after,'foreign_key_check':[]}
 finally:c.close()
def demo():return {'python':platform.python_version(),'sqlite':sqlite3.sqlite_version,'replace':branch('replace'),'upsert':branch('upsert'),'triggers_or_existing_data_executed':False,'all_assertions_passed':True}
if __name__=='__main__':print(json.dumps(demo(),indent=2))
