Skip to content

Commit

Permalink
Merge pull request #459 from python-jsonschema/enhance-testing
Browse files Browse the repository at this point in the history
Minor testing enhancements
  • Loading branch information
sirosen committed Jul 8, 2024
2 parents e31b55f + 08d04a7 commit ddc6341
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
25 changes: 25 additions & 0 deletions tests/acceptance/test_custom_validator_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,28 @@ def test_custom_validator_class_can_pass_when_valid(run_line, tmp_path):
],
)
assert result.exit_code == 0 # pass


@pytest.mark.parametrize(
"add_opts",
(
["--builtin-schema", "vendor.github-workflows"],
["--check-metaschema"],
),
)
def test_custom_validator_class_is_incompatible_with_schema_opts(
run_line, tmp_path, add_opts
):
doc = tmp_path / "instance.json"
doc.write_text("{}")
result = run_line(
[
"check-jsonschema",
"--validator-class",
"foo:MyValidator",
str(doc),
]
+ add_opts
)
assert result.exit_code == 2
assert "--validator-class can only be used with --schemafile" in result.stderr
7 changes: 7 additions & 0 deletions tests/unit/cli/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
from click.testing import CliRunner


@pytest.fixture
def runner() -> CliRunner:
return CliRunner(mix_stderr=False)
File renamed without changes.
51 changes: 51 additions & 0 deletions tests/unit/cli/test_callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import click
import pytest

from check_jsonschema.cli.warnings import deprecation_warning_callback


@click.command("foo")
@click.option(
"--bar",
is_flag=True,
callback=deprecation_warning_callback("--bar", is_flag=True),
)
@click.option(
"--baz",
callback=deprecation_warning_callback(
"--baz", append_message="Use --frob instead!"
),
)
def mycli(bar, baz):
print(bar)
if baz:
print(baz)


def test_deprecation_warning_callback_on_missing_opts(runner):
result = runner.invoke(mycli, [])
assert result.exit_code == 0
assert result.stdout == "False\n"


def test_deprecation_warning_callback_on_flag(runner):
with pytest.warns(
UserWarning,
match="'--bar' is deprecated and will be removed in a future release",
):
result = runner.invoke(mycli, ["--bar"], catch_exceptions=False)
assert result.exit_code == 0, result.stdout
assert result.stdout == "True\n"


def test_deprecation_warning_callback_added_message(runner):
with pytest.warns(
UserWarning,
match=(
"'--baz' is deprecated and will be removed in a future release. "
"Use --frob instead!"
),
):
result = runner.invoke(mycli, ["--baz", "ok"], catch_exceptions=False)
assert result.exit_code == 0, result.stdout
assert result.stdout == "False\nok\n"
6 changes: 0 additions & 6 deletions tests/unit/test_cli_parse.py → tests/unit/cli/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import click
import pytest
from click.testing import CliRunner

from check_jsonschema import main as cli_main
from check_jsonschema.cli.parse_result import ParseResult, SchemaLoadingMode
Expand Down Expand Up @@ -43,11 +42,6 @@ def get_ctx(*args):
yield m


@pytest.fixture
def runner() -> CliRunner:
return CliRunner(mix_stderr=False)


@pytest.mark.parametrize(
"schemafile,builtin_schema,check_metaschema,expect_mode",
[
Expand Down
1 change: 1 addition & 0 deletions tests/unit/formats/test_rfc3339.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_simple_positive_cases(datestr):
@pytest.mark.parametrize(
"datestr",
(
object(),
"2018-12-31T23:59:59",
"2018-12-31T23:59:59+00:00Z",
"2018-12-31 23:59:59",
Expand Down
1 change: 1 addition & 0 deletions tests/unit/formats/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_simple_positive_cases(timestr):
@pytest.mark.parametrize(
"timestr",
(
object(),
"12:34:56",
"23:59:60Z",
"23:59:59+24:00",
Expand Down

0 comments on commit ddc6341

Please sign in to comment.