Esempio n. 1
0
def main(filename: Path, is_write_file: bool, is_verbose: bool) -> int:
    try:
        expect = json.loads(filename.read_text())
        alphabetize_modules(expect[0])
        actual = [generate_combos(new_archive=expect[0]["new_archive"])]
        diff = Compare().check(expect, actual)

        if is_verbose:
            logger.info("=" * 80)
            logger.info("Program Output:")
            logger.info(json.dumps(actual, sort_keys=True, indent=2))

            logger.info("=" * 80)
            logger.info(f"Comparison with existing '{filename}':")
            logger.info(json.dumps(diff, sort_keys=True, indent=2))
            logger.info("=" * 80)

        if diff == NO_DIFF:
            logger.info(
                f"{filename} is up to date! No PR is necessary this time!")
            return 0  # no difference
        if is_write_file:
            logger.info(f"{filename} has changed; writing changes to file...")
            write_combinations_json(actual, filename)
            return 0  # File written successfully
        logger.warning(
            f"{filename} is out of date, but no changes were written")
        return 1  # difference reported

    except (ArchiveConnectionError, ArchiveDownloadError) as e:
        logger.error(format(e))
        return 1
Esempio n. 2
0
    def test_compare_deep_data(self):
        rules = load_json('compare/rules.json')
        actual = load_json('compare/actual.json')
        expected = load_json('compare/expected.json')

        diff = Compare(self.config, rules).check(expected, actual)
        self.assertEqual(diff, NO_DIFF)
Esempio n. 3
0
def main():
    f1 = sys.argv[1]
    f2 = sys.argv[2]
    with open(f1, 'r') as f:
        t1 = f.read()
        json1 = json.loads(t1)

    with open(f2, 'r') as f:
        t2 = f.read()
        json2 = json.loads(t2)

    diff = Compare().check(json1, json2)
    print(type(diff))
    # print(diff_json)
    with open('./json_diff.json', 'w') as f:
        f.write(json.dumps(diff))
Esempio n. 4
0
            'ensure_ascii': True,
            'indent': 4,
            'name': None,
            'skipkeys': True,
        },
    },
    'types': {
        'float': {
            'allow_round': 2,
        },
        'list': {
            'check_length': False,
        },
    },
}
json_compare = Compare(JSONDIFF_CONFIG)


@pytest.fixture(scope="session")
def wait_for_healthchecks():
    # os.system('docker run --network datahub_network spark-submit')
    yield


@pytest.mark.dependency()
def test_healthchecks(wait_for_healthchecks):
    # Call to wait_for_healthchecks fixture will do the actual functionality.
    pass


@pytest.mark.dependency(depends=["test_healthchecks"])
Esempio n. 5
0
 def setUp(self):
     self.config = load_json('compare/config.json')
     self.compare = Compare(self.config)
Esempio n. 6
0
class CompareTestCase(unittest.TestCase):
    def setUp(self):
        self.config = load_json('compare/config.json')
        self.compare = Compare(self.config)

    def test_compare_int(self):
        diff = self.compare.check(1, 1)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(1, 2)
        self.assertEqual(diff, ValuesNotEqual(1, 2).explain())

    def test_compare_str(self):
        diff = self.compare.check('str', 'str')
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check('str1', 'str2')
        self.assertEqual(diff, ValuesNotEqual('str1', 'str2').explain())

    def test_compare_float(self):
        diff = self.compare.check(1.2, 1.2)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(1.23456, 1.23)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(1.2, 1.3)
        self.assertEqual(diff, ValuesNotEqual(1.2, 1.3).explain())

    def test_compare_bool(self):
        diff = self.compare.check(True, True)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(True, False)
        self.assertEqual(diff, ValuesNotEqual(True, False).explain())

    def test_compare_dict_diff(self):
        e = {'int': 1, 'str': 'Hi', 'float': 1.23, 'bool': True}
        a = {'int': 2, 'str': 'Hi', 'float': 1}

        diff = self.compare.check(e, e)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(e, a)
        self.assertEqual(
            diff,
            {
                'int': ValuesNotEqual(1, 2).explain(),
                'float': TypesNotEqual(1.23, 1).explain(),
                'bool': KeyNotExist('bool', None).explain(),
            },
        )

    def test_compare_dict_diff_unexpected(self):
        e = {'int': 2, 'str': 'Hi', 'float': 1}
        a = {'int': 1, 'str': 'Hi', 'float': 1.23, 'bool': True}

        diff = self.compare.check(e, a)
        self.assertEqual(
            diff,
            {
                'int': ValuesNotEqual(2, 1).explain(),
                'float': TypesNotEqual(1, 1.23).explain(),
                'bool': UnexpectedKey(None, 'bool').explain()
            },
        )

    def test_list_compare(self):
        e = [1.23, 2, 'three', True]
        a = [1.23, 3, 'three', False, None]

        diff = self.compare.check(e, e)
        self.assertEqual(diff, NO_DIFF)

        diff = self.compare.check(e, a)
        self.assertEqual(
            diff,
            {
                '_length': LengthsNotEqual(len(e), len(a)).explain(),
                '_content': {
                    1: ValueNotFound(2, None).explain(),
                    3: ValueNotFound(True, None).explain(),
                },
            },
        )

    def test_prepare_method(self):
        e = [1, 2, 3, 4]
        p = self.compare.prepare(e)

        self.assertTrue(e == p)
        self.assertTrue(e is not p)

    def test_compare_deep_data(self):
        rules = load_json('compare/rules.json')
        actual = load_json('compare/actual.json')
        expected = load_json('compare/expected.json')

        diff = Compare(self.config, rules).check(expected, actual)
        self.assertEqual(diff, NO_DIFF)