|
1 |
| -# Standard library imports |
2 |
| -import unittest |
3 |
| -import logging |
| 1 | +""" |
| 2 | +Tests for the jsonpath.py command line interface. |
| 3 | +""" |
| 4 | + |
4 | 5 | import io
|
5 |
| -import sys |
6 |
| -import os |
7 | 6 | import json
|
| 7 | +import os |
| 8 | +import sys |
8 | 9 |
|
9 | 10 | from jsonpath_ng.bin.jsonpath import main
|
10 | 11 |
|
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') |
53 | 12 |
|
| 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