"""Observed coercion matrix, only four authored in-memory tables."""
import sqlite3,json,platform
def demo():
 c=sqlite3.connect(':memory:',isolation_level=None);values=[('digits','123'),('leading-zero','000123'),('whole-real',3.0),('fraction-real',3.5),('word','xyz'),('null',None)];matrix={}
 try:
  for strict in [False,True]:
   for typ in ['INTEGER','ANY']:
    table=('s'if strict else'n')+('_int'if typ=='INTEGER'else'_any');c.execute('CREATE TABLE '+table+'(label TEXT,value '+typ+')'+(' STRICT'if strict else''));out=[]
    for label,val in values:
     before=c.execute('SELECT label,typeof(value),quote(value) FROM '+table+' ORDER BY label').fetchall()
     try:
      c.execute('INSERT INTO '+table+' VALUES(?,?)',(label,val));t,q,v=c.execute('SELECT typeof(value),quote(value),value FROM '+table+' WHERE label=?',(label,)).fetchone();out.append({'input_label':label,'input_value':val,'accepted':True,'stored_type':t,'quoted':q,'stored_value':v})
     except sqlite3.IntegrityError as e:
      assert c.execute('SELECT label,typeof(value),quote(value) FROM '+table+' ORDER BY label').fetchall()==before
      out.append({'input_label':label,'input_value':val,'accepted':False,'error':str(e),'previous_rows_unchanged':True})
    matrix[table]=out
  assert matrix['n_any'][1]['stored_value']==123 and matrix['s_any'][1]['stored_value']=='000123'
  assert matrix['s_int'][0]['stored_type']=='integer'and matrix['s_int'][2]['stored_value']==3
  assert not matrix['s_int'][3]['accepted']and not matrix['s_int'][4]['accepted']and matrix['s_int'][5]['accepted']
  c.execute('CREATE TABLE required(value ANY NOT NULL) STRICT')
  try:c.execute('INSERT INTO required VALUES(NULL)')
  except sqlite3.IntegrityError as e:null_error=str(e)
  else:raise AssertionError('NULL accepted')
  return {'python':platform.python_version(),'sqlite':sqlite3.sqlite_version,'matrix':matrix,'separate_not_null_error':null_error,'existing_database_accessed':False,'all_assertions_passed':True}
 finally:c.close()
if __name__=='__main__':print(json.dumps(demo(),indent=2))
