Skip to content

Commit

Permalink
test: refactor error paths to json_arrays_api
Browse files Browse the repository at this point in the history
  • Loading branch information
kod-kristoff committed Mar 22, 2024
1 parent 9f8d40b commit 3948895
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
32 changes: 32 additions & 0 deletions tests/test_json_arrays_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,35 @@ def test_sink_from_file(file_name: str, snapshot, array_of_dicts: list[dict]) ->
with files.BinaryFileRead(file_name).file as fp:
bytes_written = fp.read()
assert bytes_written == snapshot


def test_load_from_file_fails_without_file_name() -> None:
try:
for _ in json_arrays.load_from_file(None, use_stdin_as_default=False):
pass
except ValueError as exc:
assert str(exc) == "You must give a FILENAME or USE_STDIN_AS_DEFAULT=`True`"


def test_dump_to_file_fails_without_file_name() -> None:
try:
json_arrays.dump_to_file(
[], None, use_stdout_as_default=False, use_stderr_as_default=False
)
except ValueError as exc:
assert (
str(exc)
== "You must give a FILENAME or USE_STDOUT_AS_DEFAULT=`True` or USE_STDERR_AS_DEFAULT=`True`" # noqa: E501
)


def test_sink_from_file_fails_without_file_name() -> None:
try:
json_arrays.sink_from_file(
None, use_stdout_as_default=False, use_stderr_as_default=False
)
except ValueError as exc:
assert (
str(exc)
== "You must give a FILENAME or USE_STDOUT_AS_DEFAULT=`True` or USE_STDERR_AS_DEFAULT=`True`" # noqa: E501
)
52 changes: 12 additions & 40 deletions tests/test_json_streams_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
],
)
def test_dump_to_file_json(file_name, json_format, expected_iter):
with mock.patch("json_arrays.json_iter.dump_to_file") as json_iter_mock, mock.patch(
"json_arrays.jsonl_iter.dump_to_file"
) as jsonl_iter_mock:
with (
mock.patch("json_arrays.json_iter.dump_to_file") as json_iter_mock,
mock.patch("json_arrays.jsonl_iter.dump_to_file") as jsonl_iter_mock,
):
in_iter = None
json_arrays.dump_to_file(in_iter, file_name, json_format=json_format)
expected_call = [mock.call(in_iter, file_name, file_mode="wb")]
Expand Down Expand Up @@ -57,9 +58,10 @@ def test_dump_to_file_json(file_name, json_format, expected_iter):
],
)
def test_sink_from_file_json(file_name, json_format, expected_iter):
with mock.patch("json_arrays.json_iter.sink_from_file") as json_iter_mock, mock.patch(
"json_arrays.jsonl_iter.sink_from_file"
) as jsonl_iter_mock:
with (
mock.patch("json_arrays.json_iter.sink_from_file") as json_iter_mock,
mock.patch("json_arrays.jsonl_iter.sink_from_file") as jsonl_iter_mock,
):
json_arrays.sink_from_file(file_name, json_format=json_format)
expected_call = [mock.call(file_name, file_mode="wb")]
if expected_iter == "json_iter":
Expand Down Expand Up @@ -88,9 +90,10 @@ def test_sink_from_file_json(file_name, json_format, expected_iter):
],
)
def test_load_from_file_json(file_name, json_format, expected_iter):
with mock.patch("json_arrays.json_iter.load_from_file") as json_iter_mock, mock.patch(
"json_arrays.jsonl_iter.load_from_file"
) as jsonl_iter_mock:
with (
mock.patch("json_arrays.json_iter.load_from_file") as json_iter_mock,
mock.patch("json_arrays.jsonl_iter.load_from_file") as jsonl_iter_mock,
):
for _ in json_arrays.load_from_file(file_name, json_format=json_format):
pass
expected_call = mock.call(file_name, file_mode="rb")
Expand Down Expand Up @@ -141,34 +144,3 @@ def test_sink_int_memoryio(data, facit, json_format):
for d in data if isinstance(data, list) else [data]: # sourcery skip: no-loop-in-tests
sink.send(d)
assert out.getvalue() == facit


def test_load_from_file_fails_without_file_name() -> None:
try:
next(json_arrays.load_from_file(None, use_stdin_as_default=False))
except ValueError as exc:
assert str(exc) == "You must give a FILENAME or USE_STDIN_AS_DEFAULT=`True`"


def test_dump_to_file_fails_without_file_name() -> None:
try:
json_arrays.dump_to_file(
[], None, use_stdout_as_default=False, use_stderr_as_default=False
)
except ValueError as exc:
assert (
str(exc)
== "You must give a FILENAME or USE_STDOUT_AS_DEFAULT=`True` or USE_STDERR_AS_DEFAULT=`True`" # noqa: E501
)


def test_sink_from_file_fails_without_file_name() -> None:
try:
json_arrays.sink_from_file(
None, use_stdout_as_default=False, use_stderr_as_default=False
)
except ValueError as exc:
assert (
str(exc)
== "You must give a FILENAME or USE_STDOUT_AS_DEFAULT=`True` or USE_STDERR_AS_DEFAULT=`True`" # noqa: E501
)

0 comments on commit 3948895

Please sign in to comment.