import copy
import importlib.util
from pathlib import Path
import unittest

HERE = Path(__file__).resolve().parent
spec = importlib.util.spec_from_file_location('cl_compare', HERE / 'compare.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)


class CompareContractTests(unittest.TestCase):
    def setUp(self):
        self.row = module.strict.load_json(HERE / 'red-result-B.json')

    def test_original_mysql_seconds_format(self):
        self.assertEqual(module.normalized(self.row)[1], [])

    def test_millisecond_timestamps_not_hidden(self):
        bad = copy.deepcopy(self.row)
        bad['samples']['create']['body']['data']['create_time'] += '.123456'
        self.assertTrue(module.normalized(bad)[1])

    def test_stale_update_time_not_hidden(self):
        bad = copy.deepcopy(self.row)
        bad['samples']['update']['body']['data']['update_time'] = bad['samples']['create']['body']['data']['update_time']
        self.assertTrue(module.normalized(bad)[1])

    def test_changed_id_type_not_hidden(self):
        bad = copy.deepcopy(self.row)
        bad['samples']['detail']['body']['data']['id'] = str(bad['owned_ids']['limit'])
        self.assertTrue(module.normalized(bad)[1])

    def test_scenario_order_and_type_remain_strict(self):
        a, _ = module.normalized(self.row)
        bad = copy.deepcopy(self.row)
        bad['samples']['detail']['body']['data']['scenarios'].reverse()
        b, _ = module.normalized(bad)
        self.assertTrue(module.strict.compare(a, b))
        bad['samples']['detail']['body']['data']['monthly_limit'] = '13'
        b, _ = module.normalized(bad)
        self.assertTrue(module.strict.compare(a, b))

    def test_unexpected_key_not_hidden(self):
        a, _ = module.normalized(self.row)
        bad = copy.deepcopy(self.row)
        bad['samples']['detail']['body']['data']['extra'] = None
        b, _ = module.normalized(bad)
        self.assertTrue(module.strict.compare(a, b))


if __name__ == '__main__':
    unittest.main()
