Пример #1
0
    def test_overwritte_defaults1(self):

        pattern = {
            'a': 1,
            'b': {
                'bb1': 2,
            },
            'c': {
                'cc1': {
                    'ccc1': 3,
                    'ccc2': {
                        'cccc1': 4,
                    },
                },
            },
        }

        opts = {"missing_keys": Idict.OPT.ALLOW}

        dict6 = Idict(pattern, opts)

        try:
            # structure change is allowed
            dict6['c']['cc1'] = 1
        except ValueNotAllowedException:
            self.assertTrue(False)
        else:
            '''
            dict6.__repr__()
            {'a': 1, 'b': {'bb1': 1}, 'c': {'cc1': 1}}
            '''
            self.assertEqual(
                dict6.hash_code(),
                "eydhJzogMSwgJ2InOiB7J2JiMSc6IDJ9LCAnYyc6IHsnY2MxJzogMX19")
class TestValidMandatoryValues(unittest.TestCase):
    dict1 = Idict(
        {
            'a': None,
            'b': {
                'bb1': None,
                'bb2':...
            },
            'c': {
                'cc1': {
                    'ccc1': None,
                    'ccc2': {
                        'cccc1':...,
                        'c': None,
                        'cc1': None
                    },
                    'ccc3':...
                },
            },
        }, {"missing_keys": Idict.OPT.THROW})

    def test_validate(self):
        try:
            TestValidMandatoryValues.dict1.validate()
        except MandatoryKeyValueException as ex:
            correct_key = str(ex).find("[b][bb2]")
            res = correct_key != -1
            self.assertTrue(res)

        try:
            TestValidMandatoryValues.dict1['b']['bb2'] = "something"
            TestValidMandatoryValues.dict1.validate()
        except MandatoryKeyValueException as ex:
            correct_key = str(ex).find("[c][cc1][ccc2][cccc1]")
            res = correct_key != -1
            self.assertTrue(res)

        try:
            TestValidMandatoryValues.dict1['c']['cc1']['ccc2'][
                'cccc1'] = "something 2"
            TestValidMandatoryValues.dict1.validate()
        except MandatoryKeyValueException as ex:
            correct_key = str(ex).find("[c][cc1][ccc3]")
            res = correct_key != -1
            self.assertTrue(res)

        try:
            TestValidMandatoryValues.dict1['c']['cc1']['ccc3'] = "something 3"
            TestValidMandatoryValues.dict1.validate()
        except MandatoryKeyValueException:
            self.assertTrue(False)
        else:
            self.assertTrue(True)
    def test_overwritte_defaults1(self):

        pattern = {
            'a': 1,
            'b': {
                'bb1': 2,
            },
            'c': {
                'cc1': {
                    'ccc1': 3,
                    'ccc2': {
                        'cccc1': 4,
                    },
                },
            },
        }

        opts = {"missing_keys": Idict.OPT.IGNORE}

        dict6 = Idict(pattern, opts)

        try:
            # structure change is allowed
            dict6['c']['cc1'] = 1
        except ValueNotAllowedException:
            self.assertTrue(False)
        else:
            '''
            dict6.__repr__()
            {'a': 1, 'b': {'bb1': 2}, 'c': {'cc1': {'ccc1': 3, 'ccc2': {'cccc1': 4}}}}
            '''
            expected_hash = "eydhJzogMSwgJ2InOiB7J2J" \
                            "iMSc6IDJ9LCAnYyc6IHsnY2MxJ" \
                            "zogeydjY2MxJzogMywgJ2NjYzInOiB" \
                            "7J2NjY2MxJzogNH19fX0="
            self.assertEqual(dict6.hash_code(), expected_hash)
Пример #4
0
    def test_overwritte_defaults1(self):

        pattern = {
            'a': 1,
            'b': {
                'bb1': 2,
            },
            'c': {
                'cc1': {
                    'ccc1': 3,
                    'ccc2': {
                        'cccc1': 4,
                    },
                },
            },
        }

        opts = {"missing_keys": Idict.OPT.THROW}

        dict6 = Idict(pattern, opts)

        res = False
        try:
            # this is notmal value set (overwritte defaule value)
            dict6['b']['bb1'] = 1
        except ValueNotAllowedException as ex:
            correct_key = str(ex).find("bb1")
            res = correct_key != -1
        else:
            res = False
        finally:
            self.assertFalse(res)

        res = False
        try:
            # illegal structure change interface defines dict
            # under ['c']['cc1'] but we are trying to set integer here
            dict6['c']['cc1'] = 1
        except ValueNotAllowedException as ex:
            correct_key = str(ex).find("<cc1>")
            res = correct_key != -1
        else:
            res = False
        finally:
            self.assertTrue(res)
class TestAdditionalKeysAllow(unittest.TestCase):
    dict1 = Idict({
        'a': None,
        'b': {
            'bb1': None,
            'bb2': None
        },
        'c': {
            'cc1': {
                'ccc1': None,
                'ccc2': {
                    'cccc1': None,
                    'c': None,
                    'cc1': None
                },
                'ccc3': ...
            },
        },
    }, {
        "missing_keys": Idict.OPT.ALLOW
    })

    def test_additional_keys(self):
        # non existing first element
        try:
            TestAdditionalKeysAllow.dict1['non_existing'] = 1
            TestAdditionalKeysAllow.dict1['non_existing2']['non_existing3']['non_existing4'] = 2
            TestAdditionalKeysAllow.dict1['bither_non_existing'] = 4
            TestAdditionalKeysAllow.dict1['c']['cc1']['ccc1'] = 5

        except Exception as ex:
            print(ex)
            self.assertTrue(False)
        finally:
            obj_hash = "eydhJzogTm9uZSwgJ2InOiB7J2JiMSc6IE5vbmUsICdiY" \
                   "jInOiBOb25lfSwgJ2MnOiB7J2NjMSc6IHsnY2NjMSc6IDUs" \
                   "ICdjY2MyJzogeydjY2NjMSc6IE5vbmUsICdjJzogTm9uZSwgJ" \
                   "2NjMSc6IE5vbmV9LCAnY2NjMyc6IEVsbGlwc2lzfX0sICdub25" \
                   "fZXhpc3RpbmcnOiAxLCAnbm9uX2V4aXN0aW5nMic6IHsnbm9uX" \
                   "2V4aXN0aW5nMyc6IHsnbm9uX2V4aXN0aW5nNCc6IDJ9fSwgJ2" \
                   "JpdGhlcl9ub25fZXhpc3RpbmcnOiA0fQ=="
            self.assertEqual(TestAdditionalKeysAllow.dict1.hash_code(), obj_hash)
class TestAdditionalKeysIgnore(unittest.TestCase):
    dict1 = Idict(
        {
            'a': None,
            'b': {
                'bb1': None,
                'bb2': None
            },
            'c': {
                'cc1': {
                    'ccc1': None,
                    'ccc2': {
                        'cccc1': None,
                        'c': None,
                        'cc1': None
                    },
                    'ccc3':...
                },
            },
        }, {"missing_keys": Idict.OPT.IGNORE})

    def test_additional_keys(self):
        # non existing first element
        try:
            TestAdditionalKeysIgnore.dict1['non_existing'] = 1
            TestAdditionalKeysIgnore.dict1['non_existing2']['non_existing3'][
                'non_existing4'] = 2
            TestAdditionalKeysIgnore.dict1['bither_non_existing'] = 4
            TestAdditionalKeysIgnore.dict1['c']['cc1']['ccc1'] = 5

        except Exception as ex:
            self.assertTrue(False)
        finally:
            obj_hash = "eydhJzogTm9uZSwgJ2InOiB7J2JiMSc6IE5vb" \
                       "mUsICdiYjInOiBOb25lfSwgJ2MnOiB7J2NjMSc6" \
                       "IHsnY2NjMSc6IDUsICdjY2MyJzogeydjY2NjMSc6" \
                       "IE5vbmUsICdjJzogTm9uZSwgJ2NjMSc6IE5vbmV9LC" \
                       "AnY2NjMyc6IEVsbGlwc2lzfX19"
            self.assertEqual(TestAdditionalKeysIgnore.dict1.hash_code(),
                             obj_hash)
class TestOverwritteDefault(unittest.TestCase):
    dict2 = Idict(
        {
            'a': 'default value <a>',
            'g': 'default value <g>',
            'b': {
                'bb1': 'default value <b><bb1>',
                'bb2': 'default value <b><bb2>',
            },
            'c': {
                'cc1': {
                    'ccc1': 'default value <c><cc1><ccc1>',
                    'ccc2': {
                        'cccc1': 'default value <c><cc1><ccc2><cccc1>',
                        'c': 'default value <c><cc1><ccc2><c>',
                        'cc1': 'default value <c><cc1><ccc2><cc1>',
                    },
                },
            },
        }, {"missing_keys": Idict.OPT.THROW})

    pattern2 = {
        'a': 1,
        'b': {
            'bb1': 2,
        },
        'c': {
            'cc1': {
                'ccc1': 3,
                'ccc2': {
                    'cccc1': 4,
                },
            },
        },
    }

    opts = {"missing_keys": Idict.OPT.THROW, "ellipsis_as_mandatory": True}

    dict3 = Idict(pattern2, opts)

    dict4 = Idict(pattern2, opts)

    dict5 = Idict(pattern2, opts)

    def test_overwritte_defaults1(self):
        TestOverwritteDefault.dict2['a'] = "overwritten value <a>"
        TestOverwritteDefault.dict2['b']['bb1'] = "overwritten value <b><bb1>"
        TestOverwritteDefault.dict2['c']['cc1'][
            'ccc1'] = "overwritten value <c><cc1><ccc1>"
        TestOverwritteDefault.dict2['c']['cc1']['ccc2'][
            'cccc1'] = "default value <c><cc1><ccc2><cccc1>"

        self.assertEqual(TestOverwritteDefault.dict2['a'],
                         'overwritten value <a>')

        self.assertEqual(TestOverwritteDefault.dict2['g'], 'default value <g>')

        self.assertEqual(TestOverwritteDefault.dict2['b']['bb1'],
                         'overwritten value <b><bb1>')

        self.assertEqual(TestOverwritteDefault.dict2['c']['cc1']['ccc1'],
                         'overwritten value <c><cc1><ccc1>')

        self.assertEqual(TestOverwritteDefault.dict2['c']['cc1']['ccc2']['c'],
                         'default value <c><cc1><ccc2><c>')

        self.assertEqual(
            TestOverwritteDefault.dict2['c']['cc1']['ccc2']['cc1'],
            'default value <c><cc1><ccc2><cc1>')

        self.assertEqual(
            TestOverwritteDefault.dict2['c']['cc1']['ccc2']['cccc1'],
            'default value <c><cc1><ccc2><cccc1>')

        self.assertEqual(TestOverwritteDefault.dict2['c']['cc1']['ccc2']['c'],
                         'default value <c><cc1><ccc2><c>')

        self.assertEqual(
            TestOverwritteDefault.dict2['c']['cc1']['ccc2']['cc1'],
            'default value <c><cc1><ccc2><cc1>')

    def test_overwritte_defaults2(self):
        TestOverwritteDefault.dict3['a'] = 11
        TestOverwritteDefault.dict3['b']['bb1'] = 22
        TestOverwritteDefault.dict3['c']['cc1']['ccc1'] = 33
        TestOverwritteDefault.dict3['c']['cc1']['ccc2']['cccc1'] = 44

        TestOverwritteDefault.dict4['a'] = 11
        TestOverwritteDefault.dict4['c']['cc1']['ccc1'] = 33

        self.assertEqual(TestOverwritteDefault.dict3['a'], 11)
        self.assertEqual(TestOverwritteDefault.dict3['b']['bb1'], 22)
        self.assertEqual(TestOverwritteDefault.dict3['c']['cc1']['ccc1'], 33)
        self.assertEqual(
            TestOverwritteDefault.dict3['c']['cc1']['ccc2']['cccc1'], 44)
        self.assertEqual(TestOverwritteDefault.dict4['a'], 11)
        self.assertEqual(TestOverwritteDefault.dict4['b']['bb1'], 2)
        self.assertEqual(TestOverwritteDefault.dict4['c']['cc1']['ccc1'], 33)
        self.assertEqual(
            TestOverwritteDefault.dict4['c']['cc1']['ccc2']['cccc1'], 4)
        self.assertEqual(TestOverwritteDefault.dict5['a'], 1)
        self.assertEqual(TestOverwritteDefault.dict5['b']['bb1'], 2)
        self.assertEqual(TestOverwritteDefault.dict5['c']['cc1']['ccc1'], 3)
        self.assertEqual(
            TestOverwritteDefault.dict5['c']['cc1']['ccc2']['cccc1'], 4)
Пример #8
0
class TestAdditionalKeysThrow(unittest.TestCase):
    dict1 = Idict(
        {
            'a': None,
            'b': {
                'bb1': None,
                'bb2': None
            },
            'c': {
                'cc1': {
                    'ccc1': None,
                    'ccc2': {
                        'cccc1': None,
                        'c': None,
                        'cc1': None
                    },
                    'ccc3':...
                },
            },
        }, {"missing_keys": Idict.OPT.THROW})

    def test_non_existing_first_element(self):
        # non existing first element
        res = False
        try:
            TestAdditionalKeysThrow.dict1['non_existing'] = 1
        except KeyNotAllowedException as ex:
            print(ex)
            correct_key = str(ex).find("<non_existing>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_non_existing_first_element_and_deeper_element(self):

        # non existing first element and deeper element
        res = False
        try:
            TestAdditionalKeysThrow.dict1['non_existing2']['non_existing3'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing2>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_non_existing_first_element_and_deeper_element2(self):
        # non existing first element and deeper element
        res = False
        try:
            TestAdditionalKeysThrow.dict1['non_existing5']['non_existing3'][
                'none_existing4'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing5>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_existing_key_and_existing_key_deeper_and_as_the_last_element_first(
            self):
        # existing key and existing key deeper and as the last element
        res = False

        try:
            TestAdditionalKeysThrow.dict1['c']['non_existing8'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing8>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_existing_key_and_existing_key_deeper_and_as_the_last_element_sec(
            self):
        # existing key and existing key deeper and as the last element
        res = False

        try:
            TestAdditionalKeysThrow.dict1['c']['cc1']['non_existing'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_existing_key_and_existing_key_deeper_and_as_the_last_element_third(
            self):
        # existing key and existing key deeper and as the last element
        res = False

        try:
            TestAdditionalKeysThrow.dict1['c']['another_non_existing8'][
                'ccc1'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<another_non_existing8>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_existing_key_at_the_beginning_and_multiple_non_existing_keys_later(
            self):
        # existing key at the beginning and multiple non existing keys later
        res = False

        try:
            TestAdditionalKeysThrow.dict1['c']['non_existing10'][
                'non_existing11']['non_existing12']['non_existing13'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing10>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_mix_of_non_existing_and_existing_keys(self):
        # mix of non existing and existing keys
        res = False

        try:
            res = False
            TestAdditionalKeysThrow.dict1['c']['non_existing11']['c1']['ccc1'][
                'non_existing13'] = 1
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing11>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)

    def test_non_existing_key_in_the_middle_of_existing_keys(self):
        # non existing key in the middle of existing keys
        res = False
        try:
            TestAdditionalKeysThrow.dict1['c']['cc1']['non_existing15'][
                'cccc1'] = 5
        except KeyNotAllowedException as ex:
            correct_key = str(ex).find("<non_existing15>")
            res = correct_key != -1
        finally:
            self.assertTrue(res)