import copy,json,unittest
from pathlib import Path
from sql_audit import audit
class NativeSqlAudit(unittest.TestCase):
 def test_original_failures_and_final_capture(self):
  p=Path(__file__).resolve().parent/'private-evidence'
  old=[json.loads(x) for x in (p/'C'/'pdo-native.jsonl').read_text().splitlines() if x.strip()]
  self.assertEqual(audit(old)['prepare_failures'],14)
  events=[json.loads(x) for x in (p/'C-final'/'pdo-native-final.jsonl').read_text().splitlines() if x.strip()]
  self.assertEqual(audit(events,True)['prepare_failures'],0)
  for op in range(6):
   m=copy.deepcopy(events);stmt=next(x for x in m if x['layer']=='actual PDOStatement prepare/bindValue/execute' and x['operation']=='metadata_CALL')
   if op==0:stmt['bindValue']['1']['value']='ZHCT_APP'
   elif op==1:stmt['bindValue']['2']['pdo_type']=1
   elif op==2:stmt['queryString']='CALL OTHER(?, ?)'
   elif op==3:stmt['success']=False
   elif op==4:
    i=next(i for i,x in enumerate(m) if x['layer']=='actual PDO.prepare');m.pop(i)
   else:
    stmt=next(x for x in m if x['layer']=='actual PDOStatement prepare/bindValue/execute' and x['bindValue'] and x['operation']=='SELECT');next(iter(stmt['bindValue'].values()))['value']='wrong exact bind'
   with self.assertRaises(AssertionError):audit(m,True)
if __name__=='__main__':unittest.main()
