Skip to content

Commit a069857

Browse files
kurtmckeemichaelmior
authored andcommitted
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
1 parent 7ef56a4 commit a069857

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed

tests/bin/__init__.py

Whitespace-only changes.

tests/bin/test_jsonpath.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,37 @@
1-
# Standard library imports
2-
import unittest
3-
import logging
1+
"""
2+
Tests for the jsonpath.py command line interface.
3+
"""
4+
45
import io
5-
import sys
6-
import os
76
import json
7+
import os
8+
import sys
89

910
from jsonpath_ng.bin.jsonpath import main
1011

11-
class TestJsonPathScript(unittest.TestCase):
12-
"""
13-
Tests for the jsonpath.py command line interface.
14-
"""
15-
16-
@classmethod
17-
def setup_class(cls):
18-
logging.basicConfig()
19-
20-
def setUp(self):
21-
self.input = io.StringIO()
22-
self.output = io.StringIO()
23-
self.saved_stdout = sys.stdout
24-
self.saved_stdin = sys.stdin
25-
sys.stdout = self.output
26-
sys.stdin = self.input
27-
28-
def tearDown(self):
29-
self.output.close()
30-
self.input.close()
31-
sys.stdout = self.saved_stdout
32-
sys.stdin = self.saved_stdin
33-
34-
def test_stdin_mode(self):
35-
# 'format' is a benign Python 2/3 way of ensuring it is a text type rather than binary
36-
self.input.write('{0}'.format(json.dumps({
37-
'foo': {
38-
'baz': 1,
39-
'bizzle': {
40-
'baz': 2
41-
}
42-
}
43-
})))
44-
self.input.seek(0)
45-
main('jsonpath.py', 'foo..baz')
46-
self.assertEqual(self.output.getvalue(), '1\n2\n')
47-
48-
def test_filename_mode(self):
49-
test1 = os.path.join(os.path.dirname(__file__), 'test1.json')
50-
test2 = os.path.join(os.path.dirname(__file__), 'test2.json')
51-
main('jsonpath.py', 'foo..baz', test1, test2)
52-
self.assertEqual(self.output.getvalue(), '1\n2\n3\n4\n')
5312

13+
def test_stdin_mode(monkeypatch, capsys):
14+
stdin_text = json.dumps(
15+
{
16+
"foo": {
17+
"baz": 1,
18+
"bizzle": {"baz": 2},
19+
},
20+
}
21+
)
22+
monkeypatch.setattr(sys, "stdin", io.StringIO(stdin_text))
23+
24+
main("jsonpath.py", "foo..baz")
25+
26+
stdout, _ = capsys.readouterr()
27+
assert stdout == "1\n2\n"
28+
29+
30+
def test_filename_mode(capsys):
31+
test1 = os.path.join(os.path.dirname(__file__), "test1.json")
32+
test2 = os.path.join(os.path.dirname(__file__), "test2.json")
33+
34+
main("jsonpath.py", "foo..baz", test1, test2)
35+
36+
stdout, _ = capsys.readouterr()
37+
assert stdout == "1\n2\n3\n4\n"

0 commit comments

Comments
 (0)