Skip to content

Commit

Permalink
Fixed a bug inside create_path that prevented the creation of simple …
Browse files Browse the repository at this point in the history
…objects inside lists. Added unittest for this case.
  • Loading branch information
erewok committed Sep 15, 2014
1 parent b8e31c5 commit 84a2f3f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
v0.4.1 -- Fixed bug inside create_path method that prevented adding simple items to list (only added new objects to list)

v0.4.0 -- Added `force` kwarg to set_nested_value and `create_path` method to PelicanJson object. Also added the `toolbox` module to the application with a handful of functions which may be used to get somewhat the same behavior as a PelicanJson object.

v0.3.2 -- Updated documentation and docstrings.
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,4 @@ Finally, there is also a `find_and_replace` method which searches for a particul
'SOME NEW URL'
```

This can, of course, be dangerous, so use with caution.

# TO DO

* Separate all methods out of `PelicanJson` class for use on free-floating Python dictionaries that come from netsed JSON objects.

![](http://i.imgur.com/f6bG1XN.jpg)
This can, of course, be dangerous, so use with caution.
2 changes: 1 addition & 1 deletion pelecanus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"""
from .pelicanjson import PelicanJson

__version__ = '0.4.0'
__version__ = '0.4.1'
6 changes: 5 additions & 1 deletion pelecanus/pelicanjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ def test_path(path):
if not isinstance(index, int):
errmsg = "Check path. List index must be integer: {}."
raise IndexError(errmsg.format(index))
new_object = PelicanJson(new_json_from_path(rest, newvalue))
if rest:
new_object = new_json_from_path(rest, newvalue)
new_object = PelicanJson(new_object)
else:
new_object = newvalue
edited_list = backfill_append(edit_object, index, new_object)
self.set_nested_value(keys_present, edited_list)
elif isinstance(edit_object, PelicanJson):
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/datadoc.json

Large diffs are not rendered by default.

58 changes: 46 additions & 12 deletions test/test_pelicanjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
current_dir = os.path.abspath(os.path.dirname(__file__))
fixture_dir = os.path.join(current_dir, 'fixtures')
# Actual datasets
data = os.path.join(fixture_dir, 'test_data.json')
data = os.path.join(fixture_dir, 'datadoc.json')
book = os.path.join(fixture_dir, 'book.json')
ricketts = os.path.join(fixture_dir, 'ricketts.json')
monterrey = os.path.join(fixture_dir, 'monterrey.json')
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_create_path_in_dict(self):
self.assertEqual(test_rickettsi.get_nested_value(path),
"NEWVALUE")

def test_create_path_in_list(self):
def test_create_path_new_object_inside_list(self):
test_rickettsi = PelicanJson(self.ricketts)
paths = [['query', 'pages', '1422396', 'images', 7, 'title'],
['query', 'normalized', 10, 'NEW']]
Expand All @@ -298,6 +298,21 @@ def test_create_path_in_list(self):
'normalized'])),
11)

def test_create_path_add_item_to_list(self):
test_item = PelicanJson(self.item)
paths = [['attributes', 'tags', 2],
['attributes', 'tags', 5]]
check_for_none = paths[1][:-1]
check_for_none.append(4)
test_item.create_path(paths[0], "New value inside list")
self.assertEqual(test_item.get_nested_value(paths[0]),
"New value inside list")
test_item.create_path(paths[1], "New value inside list with None")
self.assertEqual(test_item.get_nested_value(paths[1]),
"New value inside list with None")
self.assertEqual(test_item.get_nested_value(check_for_none),
None)

def test_create_path_raise_badpath(self):
test_rickettsi = PelicanJson(self.ricketts)
bad_path = [[4, 'query', 'normalized', 0, 'from'],
Expand Down Expand Up @@ -338,9 +353,11 @@ def test_searchvalue(self):
self.assertEqual(list(test_monty.search_value('2014-08-25')),
[['results', 1, 'maxdate'],
['results', 3, 'maxdate']])
npr_api_tag = [['attributes', 'tags', 0],
['items', 0, 'attributes', 'tags', 0]]
pelican_item = PelicanJson(self.item)
self.assertEqual(next(pelican_item.search_value('Cove')),
['attributes', 'tags', 2])
for path in pelican_item.search_value('npr_api'):
self.assertIn(path, npr_api_tag)

def test_pluck(self):
answer = PelicanJson({'to': 'Pelecanus occidentalis',
Expand All @@ -353,13 +370,13 @@ def test_pluck(self):
next(test_pelican.pluck('from',
'Pelecanus_occidentalis')))
pelican_item = PelicanJson(self.item)
self.assertEqual(pelican_item,
next(pelican_item.pluck('version', '1.0')))
self.assertEqual(pelican_item['attributes'],
next(pelican_item.pluck('byline', 'Emily Reddy')))

def test_get_nested_value(self):
pelican_item = PelicanJson(self.item)
answer = pelican_item.get_nested_value(['attributes', 'tags', 2])
self.assertEqual(answer, 'Cove')
answer = pelican_item.get_nested_value(['attributes', 'tags', 0])
self.assertEqual(answer, 'npr_api')
test_monty = PelicanJson(self.monterrey)
answers = [('gov.noaa.ncdc:C00822', ['results', 7, 'uid']),
('gov.noaa.ncdc:C00040', ['results', 0, 'uid'])]
Expand Down Expand Up @@ -438,11 +455,12 @@ def test_set_nested_value_raises_error(self):
for path in index_error_paths:
test_rickettsi.set_nested_value(path, "Shouldn't Work")

def test_set_nested_value_force(self):
# Attempt to set paths that previously raised:
# IndexError, KeyError, TypeError
# Attempt to set paths that previously raised: IndexError, KeyError,
# TypeError
def test_set_nested_value_force_key_error(self):
test_rickettsi = PelicanJson(self.ricketts)
success_msg = "Should now work"
# KeyErrors overidden
key_error_paths = [['unknownKey', 'unknownKey2'],
['query-continue', 'unknownKey']]
for path in key_error_paths:
Expand All @@ -452,15 +470,21 @@ def test_set_nested_value_force(self):
self.assertEqual(test_rickettsi.get_nested_value(path),
success_msg)

def test_set_nested_value_force_type_error(self):
test_rickettsi = PelicanJson(self.ricketts)
success_msg = "Should now work"
# TypeErrors overridden: path created
type_error_path = ['query-continue', 'extlinks',
'eloffset', 'newdict-with-key']
test_rickettsi.set_nested_value(type_error_path,
success_msg,
force=True)
self.assertEqual(test_rickettsi.get_nested_value(path),
self.assertEqual(test_rickettsi.get_nested_value(type_error_path),
success_msg)

def test_set_nested_value_force_previous_index_error(self):
test_rickettsi = PelicanJson(self.ricketts)
success_msg = "Should now work"
# IndexErrors overridden: paths created
index_error_paths = [['query', 'normalized', 1, 'from'],
['query', 'normalized', 1, 'to']]
Expand All @@ -469,6 +493,16 @@ def test_set_nested_value_force(self):
self.assertEqual(test_rickettsi.get_nested_value(path),
success_msg)

def test_set_nested_value_force_add_to_list(self):
path = ['attributes', 'tags', 4]
test_pelican = PelicanJson(self.item)
test_pelican.set_nested_value(path, 'New Tag', force=True)
new_tag = test_pelican.get_nested_value(path)
self.assertEqual(new_tag, 'New Tag')
none_placeholder = ['attributes', 'tags', 3]
self.assertEqual(test_pelican.get_nested_value(none_placeholder),
None)

def test_find_and_replace(self):
test_pelican = PelicanJson(self.pelecanus_occidentalis)
test_pelican.find_and_replace('Pelecanus occidentalis',
Expand Down

0 comments on commit 84a2f3f

Please sign in to comment.