Exemplo n.º 1
0
    def test_fixed_nesting(self):
        """
        Fixed levels of nesting, no type specified
        """
        #
        #   Maximum of four levels
        #
        import nested_dict
        nd4 = nested_dict.nested_dict(4)
        # OK: Assign to "string"
        nd4[1][2][3][4] = "a"

        # Bad: Five levels is one too many
        try:
            nd4[1][2][3]["four"][5] = "b"
            self.assertTrue("Should have throw assertion before getting here")
        except KeyError:
            pass

        nd2 = nested_dict.nested_dict(2)
        # OK: Assign to "string"
        nd2[1][2] = "a"

        # Bad: Five levels is one too many
        try:
            nd2[1]["two"][3] = "b"
            self.assertTrue("Should have throw assertion before getting here")
        except KeyError:
            pass
Exemplo n.º 2
0
    def test_fixed_nesting(self):
        """Fixed levels of nesting, no type specified."""
        #
        #   Maximum of four levels
        #
        import nested_dict
        nd4 = nested_dict.nested_dict(4)
        # OK: Assign to "string"
        nd4[1][2][3][4] = "a"

        # Bad: Five levels is one too many
        try:
            nd4[1][2][3]["four"][5] = "b"
            self.assertTrue("Should have throw assertion before getting here")
        except KeyError:
            pass

        nd2 = nested_dict.nested_dict(2)
        # OK: Assign to "string"
        nd2[1][2] = "a"

        # Bad: Five levels is one too many
        try:
            nd2[1]["two"][3] = "b"
            self.assertTrue("Should have throw assertion before getting here")
        except KeyError:
            pass
Exemplo n.º 3
0
 def test_bad_init(self):
     """
     """
     #
     #   Maximum of four levels
     #
     import nested_dict
     try:
         nd3 = nested_dict.nested_dict(1, 2, 3)
         self.assertTrue("Should have throw assertion before getting here")
         # just so flake8 stops complaining!
         nd3[1][2][3] = "b"
     except Exception:
         pass
     #
     #   levels not int
     #
     import nested_dict
     try:
         nd2 = nested_dict.nested_dict("a", "b")
         self.assertTrue("Should have throw assertion before getting here")
         # just so flake8 stops complaining!
         nd2[1][2] = "b"
     except Exception:
         pass
Exemplo n.º 4
0
    def test_to_dict(self):
        """Test to_dict method."""
        import nested_dict
        a = nested_dict.nested_dict()
        a['1']['2']['3'] = 3
        a['A']['B'] = 15

        normal_dict = a.to_dict()
        self.assertEqual(normal_dict, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})

        b = nested_dict.nested_dict(normal_dict)
        self.assertEqual(b, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})
    def test_to_dict(self):
        """Test to_dict method."""
        import nested_dict
        a = nested_dict.nested_dict()
        a['1']['2']['3'] = 3
        a['A']['B'] = 15

        normal_dict = a.to_dict()
        self.assertEqual(normal_dict, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})

        b = nested_dict.nested_dict(normal_dict)
        self.assertEqual(b, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})
Exemplo n.º 6
0
    def test_to_dict(self):
        """
        to_dict()
        Converts the nested dictionary to a nested series of standard ``dict`` objects
        """
        import nested_dict
        a = nested_dict.nested_dict()
        a['1']['2']['3'] = 3
        a['A']['B'] = 15

        normal_dict = a.to_dict()
        self.assertEqual(normal_dict, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})

        b = nested_dict.nested_dict(normal_dict)
        self.assertEqual(b, {'1': {'2': {'3': 3}}, 'A': {'B': 15}})
 def test_iterkeys_flat(self):
     """Test iterkeys_flat method."""
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.iterkeys_flat()), [('1', '2', '3'), ('A', 'B')])
Exemplo n.º 8
0
 def test_itervalues_flat(self):
     """Test itervalues_flat method."""
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.itervalues_flat()), [3, 15])
 def test_itervalues_flat(self):
     """Test itervalues_flat method."""
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.itervalues_flat()), [3, 15])
Exemplo n.º 10
0
 def test_str(self):
     """Test __str__ method."""
     import nested_dict
     import json
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(json.loads(str(a)), {'1': {'2': {'3': 3}}, 'A': {'B': 15}})
Exemplo n.º 11
0
 def test_iterkeys_flat(self):
     """Test iterkeys_flat method."""
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.iterkeys_flat()), [('1', '2', '3'),
                                                  ('A', 'B')])
Exemplo n.º 12
0
 def test_iteritems_flat(self):
     """
     test iteritems_flat()
     """
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.iteritems_flat()), [(('1', '2', '3'), 3), (('A', 'B'), 15)])
Exemplo n.º 13
0
 def test_iterkeys_flat(self):
     """
     test iterkeys_flat
     iterate through values with nested keys flattened into a tuple
     """
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.iterkeys_flat()), [('1', '2', '3'), ('A', 'B')])
Exemplo n.º 14
0
 def test_str(self):
     """
     str()
     """
     import nested_dict
     import json
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(json.loads(str(a)), {u('1'): {u('2'): {u('3'): 3}}, u('A'): {u('B'): 15}})
Exemplo n.º 15
0
 def test_itervalues_flat(self):
     """
     itervalues_flat
     iterate through values as a single list, without considering the degree of nesting
     """
     import nested_dict
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(sorted(a.itervalues_flat()), [3, 15])
Exemplo n.º 16
0
 def test_bad_init(self):
     """Test with invalid constructor parameters."""
     #
     #   Maximum of four levels
     #
     import nested_dict
     try:
         nd3 = nested_dict.nested_dict(1, 2, 3)
         self.assertTrue("Should have throw assertion before getting here")
         # just so flake8 stops complaining!
         nd3[1][2][3] = "b"
     except Exception:
         pass
     #
     #   levels not int
     #
     import nested_dict
     try:
         nd2 = nested_dict.nested_dict("a", "b")
         self.assertTrue("Should have throw assertion before getting here")
         # just so flake8 stops complaining!
         nd2[1][2] = "b"
     except Exception:
         pass
Exemplo n.º 17
0
 def test_str(self):
     """Test __str__ method."""
     import nested_dict
     import json
     a = nested_dict.nested_dict()
     a['1']['2']['3'] = 3
     a['A']['B'] = 15
     self.assertEqual(json.loads(str(a)), {
         '1': {
             '2': {
                 '3': 3
             }
         },
         'A': {
             'B': 15
         }
     })
Exemplo n.º 18
0
    def test_list(self):
        """Test with nested type of `list`."""
        import nested_dict
        nd = nested_dict.nested_dict(2, list)
        nd['new jersey']['mercer county'].append('plumbers')
        nd['new jersey']['mercer county'].append('programmers')
        nd['new jersey']['middlesex county'].append('salesmen')
        nd['new jersey']['middlesex county'].append('staff')
        nd['new york']['queens county'].append('cricketers')
        all = sorted(tup for tup in nd.iteritems_flat())
        self.assertEqual(all, [
            (('new jersey', 'mercer county'), ['plumbers', 'programmers']),
            (('new jersey', 'middlesex county'), ['salesmen', 'staff']),
            (('new york', 'queens county'), ['cricketers']),
        ])
        all = sorted(tup for tup in nd.itervalues_flat())
        self.assertEqual(all, [['cricketers'], ['plumbers', 'programmers'],
                               ['salesmen', 'staff']])
        all = sorted(tup for tup in nd.values_flat())
        self.assertEqual(all, [['cricketers'], ['plumbers', 'programmers'],
                               ['salesmen', 'staff']])
        all = sorted(tup for tup in nd.iterkeys_flat())
        self.assertEqual(all, [('new jersey', 'mercer county'),
                               ('new jersey', 'middlesex county'),
                               ('new york', 'queens county')])
        all = sorted(tup for tup in nd.keys_flat())
        self.assertEqual(all, [('new jersey', 'mercer county'),
                               ('new jersey', 'middlesex county'),
                               ('new york', 'queens county')])

        self.assertEqual(
            nd, {
                "new jersey": {
                    "mercer county": ["plumbers", "programmers"],
                    "middlesex county": ["salesmen", "staff"]
                },
                "new york": {
                    "queens county": ["cricketers"]
                }
            })
Exemplo n.º 19
0
    def test_list(self):
        """
            test a range of nested_dict
        """
        import nested_dict
        nd = nested_dict.nested_dict(2, list)
        nd['new jersey']['mercer county'].append('plumbers')
        nd['new jersey']['mercer county'].append('programmers')
        nd['new jersey']['middlesex county'].append('salesmen')
        nd['new jersey']['middlesex county'].append('staff')
        nd['new york']['queens county'].append('cricketers')
        all = sorted(tup for tup in nd.iteritems_flat())
        self.assertEqual(all, [(('new jersey', 'mercer county'),    ['plumbers', 'programmers']),
                               (('new jersey', 'middlesex county'), ['salesmen', 'staff']),
                               (('new york', 'queens county'),      ['cricketers']),
                               ])
        all = sorted(tup for tup in nd.itervalues_flat())
        self.assertEqual(all, [['cricketers'],
                               ['plumbers', 'programmers'],
                               ['salesmen', 'staff']])
        all = sorted(tup for tup in nd.values_flat())
        self.assertEqual(all, [['cricketers'],
                               ['plumbers', 'programmers'],
                               ['salesmen', 'staff']])
        all = sorted(tup for tup in nd.iterkeys_flat())
        self.assertEqual(all, [('new jersey', 'mercer county'),
                               ('new jersey', 'middlesex county'),
                               ('new york', 'queens county')])
        all = sorted(tup for tup in nd.keys_flat())
        self.assertEqual(all, [('new jersey', 'mercer county'),
                               ('new jersey', 'middlesex county'),
                               ('new york', 'queens county')])

        self.assertEqual(nd, {"new jersey": {"mercer county": ["plumbers",
                                                               "programmers"],
                                             "middlesex county": ["salesmen",
                                                                  "staff"]},
                              "new york": {"queens county": ["cricketers"]}})
Exemplo n.º 20
0
    def test_update(self):
        """
        update()
        """
        import nested_dict

        #
        #   nested dictionary updates
        #
        d1 = nested_dict.nested_dict()
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d2[1][2][4] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: 4, 4: 5}}})

        #
        #   dictionary updates
        #
        d1 = nested_dict.nested_dict()
        d2 = {1: {2: {4: 5}}}
        d1[1][2][3] = 4
        d2[1][2][4] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: 4, 4: 5}}})

        #
        #   scalar overwrites
        #
        d1 = nested_dict.nested_dict()
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d2[1][2] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: 5}})

        #
        #   updates try to preserve the sort of nested_dict
        #
        d1 = nested_dict.nested_dict(3, int)
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d1[1][2][4] = 5
        d2[2][3][4][5] = 6
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: 4, 4: 5}}, 2: {3: {4: {5: 6}}}})
        # d1[2][3][4][5] = 6 but d1[2][3][4] should still be a default dict of int
        self.assertEqual(d1[2][3][5], 0)

        #
        #   updates try to preserve the sort of nested_dict
        #
        d1 = nested_dict.nested_dict(3, list)
        d2 = {2: {3: {4: {5: 6}}}}
        d1[1][2][3].append(4)
        d1[1][2][4].append(4)
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: [4], 4: [4]}}, 2: {3: {4: {5: 6}}}})
        # d1[2][3][4][5] = 6 but d1[2][3][5] should still be a default dict of list
        d1[2][3][5].append(4)
        self.assertEqual(d1.to_dict(), {1: {2: {3: [4], 4: [4]}}, 2: {3: {4: {5: 6}, 5: [4]}}})
Exemplo n.º 21
0
    def test_update(self):
        """Test update method."""
        import nested_dict

        #
        #   nested dictionary updates
        #
        d1 = nested_dict.nested_dict()
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d2[1][2][4] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: 4, 4: 5}}})

        #
        #   dictionary updates
        #
        d1 = nested_dict.nested_dict()
        d2 = {1: {2: {4: 5}}}
        d1[1][2][3] = 4
        d2[1][2][4] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: {3: 4, 4: 5}}})

        #
        #   scalar overwrites
        #
        d1 = nested_dict.nested_dict()
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d2[1][2] = 5
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {1: {2: 5}})

        #
        #   updates try to preserve the sort of nested_dict
        #
        d1 = nested_dict.nested_dict(3, int)
        d2 = nested_dict.nested_dict()
        d1[1][2][3] = 4
        d1[1][2][4] = 5
        d2[2][3][4][5] = 6
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {
            1: {
                2: {
                    3: 4,
                    4: 5
                }
            },
            2: {
                3: {
                    4: {
                        5: 6
                    }
                }
            }
        })
        # d1[2][3][4][5] = 6 but d1[2][3][4] should still be a default dict of int
        self.assertEqual(d1[2][3][5], 0)

        #
        #   updates try to preserve the sort of nested_dict
        #
        d1 = nested_dict.nested_dict(3, list)
        d2 = {2: {3: {4: {5: 6}}}}
        d1[1][2][3].append(4)
        d1[1][2][4].append(4)
        d1.update(d2)
        d1.to_dict()
        self.assertEqual(d1.to_dict(), {
            1: {
                2: {
                    3: [4],
                    4: [4]
                }
            },
            2: {
                3: {
                    4: {
                        5: 6
                    }
                }
            }
        })
        # d1[2][3][4][5] = 6 but d1[2][3][5] should still be a default dict of list
        d1[2][3][5].append(4)
        self.assertEqual(d1.to_dict(), {
            1: {
                2: {
                    3: [4],
                    4: [4]
                }
            },
            2: {
                3: {
                    4: {
                        5: 6
                    },
                    5: [4]
                }
            }
        })