From d0ee82ba82a52aeef4d122d097277a33f396a5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 5 Jan 2020 21:38:57 +0100 Subject: [PATCH] :white_check_mark: Add tests for commands: callbacks --- .../test_commands/test_callback/__init__.py | 0 .../test_callback/test_tutorial001.py | 61 +++++++++++++++++++ .../test_callback/test_tutorial002.py | 25 ++++++++ .../test_callback/test_tutorial003.py | 30 +++++++++ .../test_callback/test_tutorial004.py | 32 ++++++++++ .../test_one_or_multiple/__init__.py | 0 .../test_one_or_multiple/test_tutorial001.py | 31 ++++++++++ .../test_one_or_multiple/test_tutorial002.py | 33 ++++++++++ 8 files changed, 212 insertions(+) create mode 100644 tests/test_tutorial/test_commands/test_callback/__init__.py create mode 100644 tests/test_tutorial/test_commands/test_callback/test_tutorial001.py create mode 100644 tests/test_tutorial/test_commands/test_callback/test_tutorial002.py create mode 100644 tests/test_tutorial/test_commands/test_callback/test_tutorial003.py create mode 100644 tests/test_tutorial/test_commands/test_callback/test_tutorial004.py create mode 100644 tests/test_tutorial/test_commands/test_one_or_multiple/__init__.py create mode 100644 tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial001.py create mode 100644 tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial002.py diff --git a/tests/test_tutorial/test_commands/test_callback/__init__.py b/tests/test_tutorial/test_commands/test_callback/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_commands/test_callback/test_tutorial001.py b/tests/test_tutorial/test_commands/test_callback/test_tutorial001.py new file mode 100644 index 0000000..ef62c49 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_callback/test_tutorial001.py @@ -0,0 +1,61 @@ +import subprocess +from commands.callback import tutorial001 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "Manage users in the awesome CLI app." in result.output + assert "--verbose / --no-verbose" in result.output + + +def test_create(): + result = runner.invoke(app, ["create", "Camila"]) + assert result.exit_code == 0 + assert "Creating user: Camila" in result.output + + +def test_create_verbose(): + result = runner.invoke(app, ["--verbose", "create", "Camila"]) + assert result.exit_code == 0 + assert "Will write verbose output" in result.output + assert "About to create a user" in result.output + assert "Creating user: Camila" in result.output + assert "Just created a user" in result.output + + +def test_delete(): + result = runner.invoke(app, ["delete", "Camila"]) + assert result.exit_code == 0 + assert "Deleting user: Camila" in result.output + + +def test_delete_verbose(): + result = runner.invoke(app, ["--verbose", "delete", "Camila"]) + assert result.exit_code == 0 + assert "Will write verbose output" in result.output + assert "About to delete a user" in result.output + assert "Deleting user: Camila" in result.output + assert "Just deleted a user" in result.output + + +def test_wrong_verbose(): + result = runner.invoke(app, ["delete", "--verbose", "Camila"]) + assert result.exit_code != 0 + assert "Error: no such option: --verbose" 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_commands/test_callback/test_tutorial002.py b/tests/test_tutorial/test_commands/test_callback/test_tutorial002.py new file mode 100644 index 0000000..9df721f --- /dev/null +++ b/tests/test_tutorial/test_commands/test_callback/test_tutorial002.py @@ -0,0 +1,25 @@ +import subprocess +from commands.callback import tutorial002 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_app(): + result = runner.invoke(app, ["create", "Camila"]) + assert result.exit_code == 0 + assert "Running a command" in result.output + assert "Creating user: Camila" 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_commands/test_callback/test_tutorial003.py b/tests/test_tutorial/test_commands/test_callback/test_tutorial003.py new file mode 100644 index 0000000..7097e7c --- /dev/null +++ b/tests/test_tutorial/test_commands/test_callback/test_tutorial003.py @@ -0,0 +1,30 @@ +import subprocess +from commands.callback import tutorial003 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_app(): + result = runner.invoke(app, ["create", "Camila"]) + assert result.exit_code == 0 + assert "Override callback, running a command" in result.output + assert "Running a command" not in result.output + assert "Creating user: Camila" in result.output + + +def test_for_coverage(): + mod.callback() + + +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_commands/test_callback/test_tutorial004.py b/tests/test_tutorial/test_commands/test_callback/test_tutorial004.py new file mode 100644 index 0000000..33c4166 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_callback/test_tutorial004.py @@ -0,0 +1,32 @@ +import subprocess +from commands.callback import tutorial004 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "Manage users CLI app." in result.output + assert "Use it with the create command." in result.output + assert "A new user with the given NAME will be created." in result.output + + +def test_app(): + result = runner.invoke(app, ["create", "Camila"]) + assert result.exit_code == 0 + assert "Creating user: Camila" 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_commands/test_one_or_multiple/__init__.py b/tests/test_tutorial/test_commands/test_one_or_multiple/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial001.py b/tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial001.py new file mode 100644 index 0000000..582e16e --- /dev/null +++ b/tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial001.py @@ -0,0 +1,31 @@ +import subprocess +from commands.one_or_multiple import tutorial001 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "Commands:" in result.output + assert "create" in result.output + + +def test_command(): + result = runner.invoke(app, ["create"]) + assert result.exit_code == 0 + assert "Creating user: Hiro Hamada" 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_commands/test_one_or_multiple/test_tutorial002.py b/tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial002.py new file mode 100644 index 0000000..02b60c8 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_one_or_multiple/test_tutorial002.py @@ -0,0 +1,33 @@ +import subprocess +from commands.one_or_multiple import tutorial002 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "Creates a single user Hiro Hamada." in result.output + assert "In the next version it will create 5 users more." in result.output + assert "Commands:" in result.output + assert "create" in result.output + + +def test_command(): + result = runner.invoke(app, ["create"]) + assert result.exit_code == 0 + assert "Creating user: Hiro Hamada" 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