Skip to content

Commit

Permalink
fix base case
Browse files Browse the repository at this point in the history
  • Loading branch information
amirziai committed Feb 20, 2021
1 parent f71aed9 commit 1b78379
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
3 changes: 3 additions & 0 deletions flatten_json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def flatten(
if root_keys_to_ignore is None:
root_keys_to_ignore = set()

if len(nested_dict) == 0:
return {}

# This global dictionary stores the flattened keys and values and is
# ultimately returned
flattened_dict = dict()
Expand Down
54 changes: 30 additions & 24 deletions test_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ def test_numbers_consecutive(self):
actual = check_if_numbers_are_consecutive(list_)
self.assertFalse(actual)

def test_empty(self):
d = {}
expected = d
actual = flatten(d)
self.assertEqual(expected, actual)

def test_no_flatten(self):
dic = {'a': '1', 'b': '2', 'c': 3}
expected = dic
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_one_flatten(self):
dic = {'a': '1',
Expand All @@ -51,7 +57,7 @@ def test_one_flatten(self):
}
expected = {'a': '1', 'b': '2', 'c_c1': '3', 'c_c2': '4'}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_one_flatten_utf8(self):
dic = {'a': '1',
Expand All @@ -60,14 +66,14 @@ def test_one_flatten_utf8(self):
}
expected = {'a': '1', u'ñ': u'áéö', 'c_c1': '3', 'c_c2': '4'}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_one_flatten_utf8_dif(self):
a = {u'eñe': 1}
info = dict(info=a)
expected = {u'info_{}'.format(u'eñe'): 1}
actual = flatten(info)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_custom_separator(self):
dic = {'a': '1',
Expand All @@ -76,7 +82,7 @@ def test_custom_separator(self):
}
expected = {'a': '1', 'b': '2', 'c*c1': '3', 'c*c2': '4'}
actual = flatten(dic, '*')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_list(self):
dic = {
Expand All @@ -85,7 +91,7 @@ def test_list(self):
}
expected = {'a': 1, 'b_0_c_0': 2, 'b_0_c_1': 3}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_list_and_dict(self):
dic = {
Expand All @@ -96,7 +102,7 @@ def test_list_and_dict(self):
expected = {'a': 1, 'b': 2, 'c_0_d_0': 2, 'c_0_d_1': 3, 'c_0_d_2': 4,
'c_0_e_0_f': 1, 'c_0_e_0_g': 2}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_empty_list_and_dict(self):
dic = {
Expand All @@ -110,7 +116,7 @@ def test_empty_list_and_dict(self):
'e_0_f': [], 'e_0_g_0_h': {}, 'e_0_g_0_i': [],
'e_0_g_0_j': '', 'e_0_g_0_k': None}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_tuple(self):
dic = {
Expand All @@ -119,7 +125,7 @@ def test_tuple(self):
}
expected = {'a': 1, 'b_0_c_0': 2, 'b_0_c_1': 3}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_empty_tuple(self):
dic = {
Expand All @@ -128,7 +134,7 @@ def test_empty_tuple(self):
}
expected = {'a': 1, 'b_0_c': ()}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_blog_example(self):
dic = {
Expand All @@ -140,7 +146,7 @@ def test_blog_example(self):
'c_0_d_2': 4, 'c_0_e_0_f': 1,
'c_0_e_0_g': 2}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_no_list(self):
dic = {
Expand All @@ -155,7 +161,7 @@ def test_unflatten_no_list(self):
'c': {'a': {'b': 5}}
}
actual = unflatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_with_list(self):
"""Dictionary with lists"""
Expand All @@ -174,17 +180,17 @@ def test_unflatten_with_list(self):
'c': {'a': 'a', 'b': [1, 2, 3]}
}
actual = unflatten_list(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

dic = {'a': 1, 'b_0': 5}
expected = {'a': 1, 'b': [5]}
actual = unflatten_list(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

dic = {'a': 1, 'b:0': 5}
expected = {'a': 1, 'b': [5]}
actual = unflatten_list(dic, ':')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_with_list_custom_separator(self):
"""Complex dictionary with lists"""
Expand Down Expand Up @@ -219,7 +225,7 @@ def test_unflatten_with_list_custom_separator(self):
}
}
actual = unflatten_list(dic, ':')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_with_list_nested(self):
dic = {"a": [[{"b": 1}], [{"d": 1}]]}
Expand Down Expand Up @@ -264,7 +270,7 @@ def test_unflatten_with_df_issue40(self):
}
}
actual = unflatten(dic, '.')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_with_key_loss_issue51(self):
"""https://github.com/amirziai/flatten/issues/51"""
Expand All @@ -280,7 +286,7 @@ def test_unflatten_with_key_loss_issue51(self):
'a_c': {'d': 3, 'e': 4}
}
actual = unflatten(dic, '.')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_flatten_preserve_lists_issue43_nested(self):
"""https://github.com/amirziai/flatten/issues/43"""
Expand Down Expand Up @@ -2162,7 +2168,7 @@ def test_flatten_preserve_lists_issue43(self):
'c_c': 100}]

actual = flatten_preserve_lists(dic, max_list_index=50, max_depth=10)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_unflatten_with_list_deep(self):
dic = {'a': [
Expand All @@ -2184,7 +2190,7 @@ def test_flatten_ignore_keys(self):
'a_a_2': 3
}
actual = flatten(dic, root_keys_to_ignore={'b', 'c'})
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_command_line(self):
input_stream = StringIO(u'{"a": {"b": 1}}')
Expand All @@ -2204,7 +2210,7 @@ def test_replace_separators_none(self):
'a_with_separator_b_2': 3
}
actual = flatten(dic)
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_replace_separators_remove(self):
dic = {
Expand All @@ -2216,7 +2222,7 @@ def test_replace_separators_remove(self):
'awithseparator_b_2': 3
}
actual = flatten(dic, replace_separators='')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_replace_separators_something(self):
dic = {
Expand All @@ -2228,7 +2234,7 @@ def test_replace_separators_something(self):
'a.with.separator_b_2': 3
}
actual = flatten(dic, replace_separators='.')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)

def test_replace_separators_nested(self):
dic = {
Expand All @@ -2240,7 +2246,7 @@ def test_replace_separators_nested(self):
'awithseparator_bwithseparator_2': 3
}
actual = flatten(dic, replace_separators='')
self.assertEqual(actual, expected)
self.assertEqual(expected, actual)


if __name__ == '__main__':
Expand Down

0 comments on commit 1b78379

Please sign in to comment.