From be75c9c3a36ae6c3912a98e45b1ca9bc69689483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 5 Jan 2020 21:53:57 +0100 Subject: [PATCH] :white_check_mark: Add tests for extra param types, bool, number --- .../test_bool/__init__.py | 0 .../test_bool/test_tutorial001.py | 46 +++++++++++++++ .../test_bool/test_tutorial002.py | 52 +++++++++++++++++ .../test_bool/test_tutorial003.py | 39 +++++++++++++ .../test_bool/test_tutorial004.py | 45 +++++++++++++++ .../test_number/test_tutorial003.py | 57 +++++++++++++++++++ 6 files changed, 239 insertions(+) create mode 100644 tests/test_tutorial/test_parameter_types/test_bool/__init__.py create mode 100644 tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py create mode 100644 tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py create mode 100644 tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py create mode 100644 tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py create mode 100644 tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py diff --git a/tests/test_tutorial/test_parameter_types/test_bool/__init__.py b/tests/test_tutorial/test_parameter_types/test_bool/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py new file mode 100644 index 0000000..975adf2 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py @@ -0,0 +1,46 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.bool import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--force" in result.output + assert "--no-force" not in result.output + + +def test_no_force(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Not forcing" in result.output + + +def test_force(): + result = runner.invoke(app, ["--force"]) + assert result.exit_code == 0 + assert "Forcing operation" in result.output + + +def test_invalid_no_force(): + result = runner.invoke(app, ["--no-force"]) + assert result.exit_code != 0 + assert "Error: no such option: --no-force" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py new file mode 100644 index 0000000..e5c32e9 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py @@ -0,0 +1,52 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.bool import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--accept / --reject" in result.output + assert "--no-accept" not in result.output + + +def test_main(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "I don't know what you want yet" in result.output + + +def test_accept(): + result = runner.invoke(app, ["--accept"]) + assert result.exit_code == 0 + assert "Accepting!" in result.output + + +def test_reject(): + result = runner.invoke(app, ["--reject"]) + assert result.exit_code == 0 + assert "Rejecting!" in result.output + + +def test_invalid_no_accept(): + result = runner.invoke(app, ["--no-accept"]) + assert result.exit_code != 0 + assert "Error: no such option: --no-accept" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py new file mode 100644 index 0000000..b24d1d7 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py @@ -0,0 +1,39 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.bool import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "-f, --force / -F, --no-force" in result.output + + +def test_force(): + result = runner.invoke(app, ["-f"]) + assert result.exit_code == 0 + assert "Forcing operation" in result.output + + +def test_no_force(): + result = runner.invoke(app, ["-F"]) + assert result.exit_code == 0 + assert "Not forcing" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py new file mode 100644 index 0000000..3306074 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py @@ -0,0 +1,45 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.bool import tutorial004 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "/ -d, --demo" in result.output + + +def test_main(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Running in production" in result.output + + +def test_demo(): + result = runner.invoke(app, ["--demo"]) + assert result.exit_code == 0 + assert "Running demo" in result.output + + +def test_short_demo(): + result = runner.invoke(app, ["-d"]) + assert result.exit_code == 0 + assert "Running demo" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py new file mode 100644 index 0000000..25946f7 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py @@ -0,0 +1,57 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.number import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_main(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Verbose level is 0" in result.output + + +def test_verbose_1(): + result = runner.invoke(app, ["--verbose"]) + assert result.exit_code == 0 + assert "Verbose level is 1" in result.output + + +def test_verbose_3(): + result = runner.invoke(app, ["--verbose", "--verbose", "--verbose"]) + assert result.exit_code == 0 + assert "Verbose level is 3" in result.output + + +def test_verbose_short_1(): + result = runner.invoke(app, ["-v"]) + assert result.exit_code == 0 + assert "Verbose level is 1" in result.output + + +def test_verbose_short_3(): + result = runner.invoke(app, ["-v", "-v", "-v"]) + assert result.exit_code == 0 + assert "Verbose level is 3" in result.output + + +def test_verbose_short_3_condensed(): + result = runner.invoke(app, ["-vvv"]) + assert result.exit_code == 0 + assert "Verbose level is 3" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout