-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41080ad
commit eed4a8b
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, print_function, division | ||
|
||
from tempfile import NamedTemporaryFile | ||
|
||
from petl import fromjson | ||
from petl.test.helpers import ieq | ||
|
||
|
||
def test_fromjson_1(): | ||
f = NamedTemporaryFile(delete=False, mode='w') | ||
data = '{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}\n' \ | ||
'{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}\n' \ | ||
'{"name": "May", "wins": []}\n' \ | ||
'{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}' | ||
|
||
f.write(data) | ||
f.close() | ||
|
||
actual = fromjson(f.name, header=['name', 'wins'], lines=True) | ||
|
||
expect = (('name', 'wins'), | ||
('Gilbert', [["straight", "7♣"], ["one pair", "10♥"]]), | ||
('Alexa', [["two pair", "4♠"], ["two pair", "9♠"]]), | ||
('May', []), | ||
('Deloise', [["three of a kind", "5♣"]])) | ||
|
||
ieq(expect, actual) | ||
ieq(expect, actual) # verify can iterate twice | ||
|
||
|
||
def test_fromjson_2(): | ||
f = NamedTemporaryFile(delete=False, mode='w') | ||
data = '{"foo": "bar1", "baz": 1}\n' \ | ||
'{"foo": "bar2", "baz": 2}\n' \ | ||
'{"foo": "bar3", "baz": 3}\n' \ | ||
'{"foo": "bar4", "baz": 4}\n' | ||
|
||
f.write(data) | ||
f.close() | ||
|
||
actual = fromjson(f.name, header=['foo', 'baz'], lines=True) | ||
|
||
expect = (('foo', 'baz'), | ||
('bar1', 1), | ||
('bar2', 2), | ||
('bar3', 3), | ||
('bar4', 4)) | ||
|
||
ieq(expect, actual) | ||
ieq(expect, actual) # verify can iterate twice |