#!/usr/bin/env python3
import copy,json,unittest
from pathlib import Path
from verify import verify
ROOT=Path(__file__).resolve().parent
DATA={f:json.loads((ROOT/f).read_text()) for f in ['old-red.json','iterator-green.json','recovery-green.json','framework-source-gate-red.json','framework-old-red.json','framework-green.json']}
class Gates(unittest.TestCase):
    def test_real_evidence(self):self.assertEqual(verify(copy.deepcopy(DATA))['status'],'pass')
    def reject(self,mutation):
        x=copy.deepcopy(DATA);mutation(x)
        with self.assertRaises((AssertionError,KeyError,TypeError)):verify(x)
MUTATIONS={
    'missing_runtime':lambda x:x['iterator-green.json']['runs'].pop(),
    'lost_red':lambda x:x['old-red.json']['runs'][0]['report'].update(status='pass'),
    'wrong_red_stage':lambda x:x['old-red.json']['runs'][0]['report'].update(stage='runtime'),
    'wrong_assertions':lambda x:x['iterator-green.json']['runs'][1]['report'].update(assertions=0),
    'stderr':lambda x:x['iterator-green.json']['runs'][0].update(stderr_bytes=1),
    'new_driver':lambda x:x['iterator-green.json']['runs'][0]['report']['identity'].update(extra_decimal_defined=True),
    'unsafe_core':lambda x:x['iterator-green.json']['runs'][0]['report']['identity'].update(core_soft=1),
    'component_drift':lambda x:x['iterator-green.json']['runs'][0]['report']['identity']['mapped'].update({'/bad/module.so':'0'*64}),
    'wrong_schema':lambda x:x['iterator-green.json']['runs'][0]['report']['target'].update(schema_matches=False),
    'dml':lambda x:x['iterator-green.json']['runs'][0]['report'].update(dml=1),
    'ddl':lambda x:x['recovery-green.json']['runs'][0]['report'].update(ddl=1),
    'precision_gate_closed':lambda x:x['iterator-green.json']['runs'][0]['report'].update(global_precision_gate_closed=True),
    'wide_float':lambda x:x['recovery-green.json']['runs'][0]['report']['iterator']['rows'][0].update(v=9007199254740993.01),
    'helper_leak':lambda x:x['recovery-green.json']['runs'][0]['report']['iterator']['rows'][0].update(dm_shadow_0='9007199254740993.01'),
    'false_ORM_claim':lambda x:x['recovery-green.json']['runs'][0]['report'].update(real_orm_getter_tested=True),
    'cache_not_hit':lambda x:x['framework-green.json']['runs'][0]['report'].update(actual_file_cache_cold_hot_verified=False),
    'extra_prepare_on_hot':lambda x:x['framework-green.json']['runs'][0]['report'].update(actual_framework_prepares=31),
    'cache_not_cleared':lambda x:x['framework-green.json']['runs'][0]['report'].update(cache_files_remaining=1),
    'false_builder_claim':lambda x:x['framework-green.json']['runs'][0]['report'].update(builder_auto_projection_tested=True),
}
for name,mutation in MUTATIONS.items():
    setattr(Gates,'test_reject_'+name,lambda self,mutation=mutation:self.reject(mutation))
if __name__=='__main__':unittest.main()
