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
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
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
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): """ 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')])
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_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}})
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)])
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')])
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}})
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])
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
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 } })
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"] } })
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"]}})
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]}}})
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] } } })