forked from kennknowles/python-jsonpath-rw
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate
tests/bin/test_jsonpath.py
to use pytest
Also, the empty `__init__.py` in the same directory has been deleted; pytest doesn't rely on test suites being importable modules. Also, these tools were run against the test file: * pyupgrade --py37-plus * black * isort
- Loading branch information
1 parent
7ef56a4
commit a069857
Showing
2 changed files
with
31 additions
and
47 deletions.
There are no files selected for viewing
Empty file.
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 |
---|---|---|
@@ -1,53 +1,37 @@ | ||
# Standard library imports | ||
import unittest | ||
import logging | ||
""" | ||
Tests for the jsonpath.py command line interface. | ||
""" | ||
|
||
import io | ||
import sys | ||
import os | ||
import json | ||
import os | ||
import sys | ||
|
||
from jsonpath_ng.bin.jsonpath import main | ||
|
||
class TestJsonPathScript(unittest.TestCase): | ||
""" | ||
Tests for the jsonpath.py command line interface. | ||
""" | ||
|
||
@classmethod | ||
def setup_class(cls): | ||
logging.basicConfig() | ||
|
||
def setUp(self): | ||
self.input = io.StringIO() | ||
self.output = io.StringIO() | ||
self.saved_stdout = sys.stdout | ||
self.saved_stdin = sys.stdin | ||
sys.stdout = self.output | ||
sys.stdin = self.input | ||
|
||
def tearDown(self): | ||
self.output.close() | ||
self.input.close() | ||
sys.stdout = self.saved_stdout | ||
sys.stdin = self.saved_stdin | ||
|
||
def test_stdin_mode(self): | ||
# 'format' is a benign Python 2/3 way of ensuring it is a text type rather than binary | ||
self.input.write('{0}'.format(json.dumps({ | ||
'foo': { | ||
'baz': 1, | ||
'bizzle': { | ||
'baz': 2 | ||
} | ||
} | ||
}))) | ||
self.input.seek(0) | ||
main('jsonpath.py', 'foo..baz') | ||
self.assertEqual(self.output.getvalue(), '1\n2\n') | ||
|
||
def test_filename_mode(self): | ||
test1 = os.path.join(os.path.dirname(__file__), 'test1.json') | ||
test2 = os.path.join(os.path.dirname(__file__), 'test2.json') | ||
main('jsonpath.py', 'foo..baz', test1, test2) | ||
self.assertEqual(self.output.getvalue(), '1\n2\n3\n4\n') | ||
|
||
def test_stdin_mode(monkeypatch, capsys): | ||
stdin_text = json.dumps( | ||
{ | ||
"foo": { | ||
"baz": 1, | ||
"bizzle": {"baz": 2}, | ||
}, | ||
} | ||
) | ||
monkeypatch.setattr(sys, "stdin", io.StringIO(stdin_text)) | ||
|
||
main("jsonpath.py", "foo..baz") | ||
|
||
stdout, _ = capsys.readouterr() | ||
assert stdout == "1\n2\n" | ||
|
||
|
||
def test_filename_mode(capsys): | ||
test1 = os.path.join(os.path.dirname(__file__), "test1.json") | ||
test2 = os.path.join(os.path.dirname(__file__), "test2.json") | ||
|
||
main("jsonpath.py", "foo..baz", test1, test2) | ||
|
||
stdout, _ = capsys.readouterr() | ||
assert stdout == "1\n2\n3\n4\n" |