Skip to content

Commit

Permalink
Migrate tests/bin/test_jsonpath.py to use pytest
Browse files Browse the repository at this point in the history
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
kurtmckee authored and michaelmior committed Oct 11, 2023
1 parent 7ef56a4 commit a069857
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
Empty file removed tests/bin/__init__.py
Empty file.
78 changes: 31 additions & 47 deletions tests/bin/test_jsonpath.py
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"

0 comments on commit a069857

Please sign in to comment.