コード例 #1
0
    def test_supports_leaf_to_be_dictionary(self):
        my_dict = ndict(depth=1, leaf_type=dict)
        my_dict[1][1] = 'a'
        my_dict[1][2] = 'b'

        self.assertEqual(my_dict[1][1], 'a')
        self.assertEqual(my_dict[1][2], 'b')
コード例 #2
0
    def test_supports_leaf_to_be_dictionary(self):
        my_dict = ndict(depth=1, leaf_type=dict)
        my_dict[1][1] = 'a'
        my_dict[1][2] = 'b'

        self.assertEqual(my_dict[1][1], 'a')
        self.assertEqual(my_dict[1][2], 'b')
コード例 #3
0
    def test_can_be_iterated_on_leaf_level(self):
        my_dict = ndict()
        my_dict['a'] = 1
        my_dict['b', 'c'] = 2
        my_dict['b', 'd'] = 3
        my_dict['e', 'f', 'g'] = 4
        my_dict['h', 'i', 'j'] = 5
        my_dict['h', 'k', 'l'] = 6

        self.assertEqual(list(my_dict.items()), [(('a', ), 1), (('b', 'c'), 2),
                                                 (('b', 'd'), 3),
                                                 (('e', 'f', 'g'), 4),
                                                 (('h', 'i', 'j'), 5),
                                                 (('h', 'k', 'l'), 6)])
コード例 #4
0
    def test_can_be_iterated_on_leaf_level(self):
        my_dict = ndict()
        my_dict['a'] = 1
        my_dict['b', 'c'] = 2
        my_dict['b', 'd'] = 3
        my_dict['e', 'f', 'g'] = 4
        my_dict['h', 'i', 'j'] = 5
        my_dict['h', 'k', 'l'] = 6

        self.assertEqual(list(my_dict.items()), [(('a',), 1),
                                                 (('b', 'c'), 2),
                                                 (('b', 'd'), 3),
                                                 (('e', 'f', 'g'), 4),
                                                 (('h', 'i', 'j'), 5),
                                                 (('h', 'k', 'l'), 6)])
コード例 #5
0
    def test_can_be_set_item_using_list_of_keys(self):
        my_dict = ndict()
        my_dict['f', 'g', 'h'] = 42

        self.assertEqual(my_dict['f']['g']['h'], 42)
        self.assertEqual(my_dict['f', 'g', 'h'], 42)
コード例 #6
0
    def test_can_be_set_item_using_normal_dictionary_access(self):
        my_dict = ndict()
        my_dict['a']['b']['d'] = 5

        self.assertEqual(my_dict['a']['b']['d'], 5)
        self.assertEqual(my_dict['a', 'b', 'd'], 5)
コード例 #7
0
class TestNestedDict(unittest.TestCase):
    def test_can_be_set_item_using_normal_dictionary_access(self):
        my_dict = ndict()
        my_dict['a']['b']['d'] = 5

        self.assertEqual(my_dict['a']['b']['d'], 5)
        self.assertEqual(my_dict['a', 'b', 'd'], 5)

    def test_can_be_set_item_using_list_of_keys(self):
        my_dict = ndict()
        my_dict['f', 'g', 'h'] = 42

        self.assertEqual(my_dict['f']['g']['h'], 42)
        self.assertEqual(my_dict['f', 'g', 'h'], 42)

    def test_can_be_iterated_on_leaf_level(self):
        my_dict = ndict()
        my_dict['a'] = 1
        my_dict['b', 'c'] = 2
        my_dict['b', 'd'] = 3
        my_dict['e', 'f', 'g'] = 4
        my_dict['h', 'i', 'j'] = 5
        my_dict['h', 'k', 'l'] = 6

        self.assertEqual(list(my_dict.items()), [(('a', ), 1), (('b', 'c'), 2),
                                                 (('b', 'd'), 3),
                                                 (('e', 'f', 'g'), 4),
                                                 (('h', 'i', 'j'), 5),
                                                 (('h', 'k', 'l'), 6)])

    def test_does_not_support_values_and_dictionaries_for_same_key(self):
        my_dict = ndict()
        my_dict['a'] = 1

        with self.assertRaisesRegex(Exception, "Not possible to assign a key"):
            my_dict['a', 'b'] = 2

    def test_can_be_created_using_list_of_keys(self):
        my_dict = ndict([(('a', 'b', 'c'), 1), (('d', 'e', 'f'), 2)])

        self.assertEqual(my_dict['a']['b']['c'], 1)
        self.assertEqual(my_dict['d', 'e', 'f'], 2)

    def test_can_be_one_level_deep(self):
        my_dict = ndict(depth=1, leaf_type=list)
        my_dict[1].append('value')
        my_dict[1].append(True)

        self.assertEqual(my_dict[1], ['value', True])

    def test_supports_leaf_to_be_dictionary(self):
        my_dict = ndict(depth=1, leaf_type=dict)
        my_dict[1][1] = 'a'
        my_dict[1][2] = 'b'

        self.assertEqual(my_dict[1][1], 'a')
        self.assertEqual(my_dict[1][2], 'b')

    def test_can_contain_multiple_levels(self):
        my_dict = ndict(depth=3, leaf_type=int)
        my_dict[1, 2, 3] += 4
        my_dict[1, 2, 3] += 12

        self.assertEqual(my_dict[1, 2, 3], 16)

    @examples(
        ('a', ndict(), "NestedDict([(('a',), NestedDict())])"),
        ((('a', 'b'), ndict(), "NestedDict([(('a', 'b'), NestedDict())])")),
        ((('a', 'b'), True, "NestedDict([(('a', 'b'), True)])")))
    def test_repr_with_levels(self, key, value, representation):
        my_dict = ndict()
        my_dict[key] = value
        self.assertEqual(repr(my_dict), representation)

    @examples(((1, 2, 3), True, [(1, 2, 3)]), ((2, 3, 4), None, [(2, 3, 4)]),
              ((3, 4, 5), {}, [(3, 4, 5)]), ((4, 5, 6), [], [(4, 5, 6)]),
              ((5, 6, 7), ndict(), [(5, 6, 7)]))
    def test_shows_keys_properly_for_empty_values(self, key, value, keys):
        my_dict = ndict()
        my_dict[key] = value

        self.assertEqual(list(my_dict.keys()), keys)
コード例 #8
0
    def test_can_be_set_item_using_normal_dictionary_access(self):
        my_dict = ndict()
        my_dict['a']['b']['d'] = 5

        self.assertEqual(my_dict['a']['b']['d'], 5)
        self.assertEqual(my_dict['a', 'b', 'd'], 5)
コード例 #9
0
    def test_can_contain_multiple_levels(self):
        my_dict = ndict(depth=3, leaf_type=int)
        my_dict[1, 2, 3] += 4
        my_dict[1, 2, 3] += 12

        self.assertEqual(my_dict[1, 2, 3], 16)
コード例 #10
0
    def test_can_be_one_level_deep(self):
        my_dict = ndict(depth=1, leaf_type=list)
        my_dict[1].append('value')
        my_dict[1].append(True)

        self.assertEqual(my_dict[1], ['value', True])
コード例 #11
0
    def test_can_be_one_level_deep(self):
        my_dict = ndict(depth=1, leaf_type=list)
        my_dict[1].append('value')
        my_dict[1].append(True)

        self.assertEqual(my_dict[1], ['value', True])
コード例 #12
0
    def test_can_be_created_using_list_of_keys(self):
        my_dict = ndict([(('a', 'b', 'c'), 1),
                         (('d', 'e', 'f'), 2)])

        self.assertEqual(my_dict['a']['b']['c'], 1)
        self.assertEqual(my_dict['d', 'e', 'f'], 2)
コード例 #13
0
    def test_does_not_support_values_and_dictionaries_for_same_key(self):
        my_dict = ndict()
        my_dict['a'] = 1

        with self.assertRaisesRegex(Exception, "Not possible to assign a key"):
            my_dict['a', 'b'] = 2
コード例 #14
0
    def test_can_be_set_item_using_list_of_keys(self):
        my_dict = ndict()
        my_dict['f', 'g', 'h'] = 42

        self.assertEqual(my_dict['f']['g']['h'], 42)
        self.assertEqual(my_dict['f', 'g', 'h'], 42)
コード例 #15
0
    def test_does_not_support_values_and_dictionaries_for_same_key(self):
        my_dict = ndict()
        my_dict['a'] = 1

        with self.assertRaisesRegex(Exception, "Not possible to assign a key"):
            my_dict['a', 'b'] = 2
コード例 #16
0
    def test_can_be_created_using_list_of_keys(self):
        my_dict = ndict([(('a', 'b', 'c'), 1), (('d', 'e', 'f'), 2)])

        self.assertEqual(my_dict['a']['b']['c'], 1)
        self.assertEqual(my_dict['d', 'e', 'f'], 2)
コード例 #17
0
    def test_can_contain_multiple_levels(self):
        my_dict = ndict(depth=3, leaf_type=int)
        my_dict[1, 2, 3] += 4
        my_dict[1, 2, 3] += 12

        self.assertEqual(my_dict[1, 2, 3], 16)
コード例 #18
0
 def test_repr_with_levels(self, key, value, representation):
     my_dict = ndict()
     my_dict[key] = value
     self.assertEqual(repr(my_dict), representation)
コード例 #19
0
    def test_shows_keys_properly_for_empty_values(self, key, value, keys):
        my_dict = ndict()
        my_dict[key] = value

        self.assertEqual(list(my_dict.keys()), keys)
コード例 #20
0
 def test_repr_with_levels(self, key, value, representation):
     my_dict = ndict()
     my_dict[key] = value
     self.assertEqual(repr(my_dict), representation)
コード例 #21
0
    def test_shows_keys_properly_for_empty_values(self, key, value, keys):
        my_dict = ndict()
        my_dict[key] = value

        self.assertEqual(list(my_dict.keys()), keys)