diff --git a/tests/test_tutorial/test_commands/__init__.py b/tests/test_tutorial/test_commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_commands/test_tutorial001.py b/tests/test_tutorial/test_commands/test_tutorial001.py new file mode 100644 index 0000000..33135f7 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial001.py @@ -0,0 +1,30 @@ +import subprocess +from commands import tutorial001 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_no_arg(): + result = runner.invoke(app) + assert result.exit_code != 0 + assert 'Error: Missing argument "NAME".' in result.output + + +def test_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello 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_tutorial002.py b/tests/test_tutorial/test_commands/test_tutorial002.py new file mode 100644 index 0000000..0a5fc31 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial002.py @@ -0,0 +1,39 @@ +import subprocess +from commands 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 "[OPTIONS] COMMAND [ARGS]..." in result.output + assert "Commands:" in result.output + assert "create" in result.output + assert "delete" in result.output + + +def test_create(): + result = runner.invoke(app, ["create"]) + assert result.exit_code == 0 + assert "Creating user: Hiro Hamada" in result.output + + +def test_delete(): + result = runner.invoke(app, ["delete"]) + assert result.exit_code == 0 + assert "Deleting 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_tutorial003.py b/tests/test_tutorial/test_commands/test_tutorial003.py new file mode 100644 index 0000000..3a5b379 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial003.py @@ -0,0 +1,42 @@ +import subprocess +from commands import tutorial003 as mod + +from typer.testing import CliRunner + +app = mod.app + +runner = CliRunner() + + +def test_help_create(): + result = runner.invoke(app, ["create", "--help"]) + assert result.exit_code == 0 + assert "create [OPTIONS] USERNAME" in result.output + + +def test_help_delete(): + result = runner.invoke(app, ["delete", "--help"]) + assert result.exit_code == 0 + assert "delete [OPTIONS] USERNAME" 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_delete(): + result = runner.invoke(app, ["delete", "Camila"]) + assert result.exit_code == 0 + assert "Deleting 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_tutorial004.py b/tests/test_tutorial/test_commands/test_tutorial004.py new file mode 100644 index 0000000..a2e26c7 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial004.py @@ -0,0 +1,75 @@ +import subprocess +from commands 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 "Commands:" in result.output + assert "create" in result.output + assert "delete" in result.output + assert "delete-all" in result.output + assert "init" 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_delete(): + result = runner.invoke(app, ["delete", "Camila"], input="y\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete the user? [y/N]:" in result.output + assert "Deleting user: Camila" in result.output + + +def test_no_delete(): + result = runner.invoke(app, ["delete", "Camila"], input="n\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete the user? [y/N]:" in result.output + assert "Operation cancelled" in result.output + + +def test_delete_all(): + result = runner.invoke(app, ["delete-all"], input="y\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete ALL users? [y/N]:" in result.output + assert "Deleting all users" in result.output + + +def test_no_delete_all(): + result = runner.invoke(app, ["delete-all"], input="n\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete ALL users? [y/N]:" in result.output + assert "Operation cancelled" in result.output + + +def test_delete_all_force(): + result = runner.invoke(app, ["delete-all", "--force"]) + assert result.exit_code == 0 + assert "Are you sure you want to delete ALL users? [y/N]:" not in result.output + assert "Deleting all users" in result.output + + +def test_init(): + result = runner.invoke(app, ["init"]) + assert result.exit_code == 0 + assert "Initializing user database" 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_tutorial005.py b/tests/test_tutorial/test_commands/test_tutorial005.py new file mode 100644 index 0000000..8771766 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial005.py @@ -0,0 +1,106 @@ +import subprocess +from commands import tutorial005 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 "Awesome CLI user manager." in result.output + assert "create" in result.output + assert "Create a new user with USERNAME." in result.output + assert "delete" in result.output + assert "Delete a user with USERNAME." in result.output + assert "delete-all" in result.output + assert "Delete ALL users in the database." in result.output + assert "init" in result.output + assert "Initialize the users database." in result.output + + +def test_help_create(): + result = runner.invoke(app, ["create", "--help"]) + assert result.exit_code == 0 + assert "create [OPTIONS] USERNAME" in result.output + assert "Create a new user with USERNAME." in result.output + + +def test_help_delete(): + result = runner.invoke(app, ["delete", "--help"]) + assert result.exit_code == 0 + assert "delete [OPTIONS] USERNAME" in result.output + assert "Delete a user with USERNAME." in result.output + assert "--force / --no-force" in result.output + assert "Force deletion without confirmation." in result.output + + +def test_help_delete_all(): + result = runner.invoke(app, ["delete-all", "--help"]) + assert result.exit_code == 0 + assert "delete-all [OPTIONS]" in result.output + assert "Delete ALL users in the database." in result.output + assert "If --force is not used, will ask for confirmation." in result.output + assert "[required]" in result.output + assert "--force / --no-force" in result.output + assert "Force deletion without confirmation." in result.output + + +def test_help_init(): + result = runner.invoke(app, ["init", "--help"]) + assert result.exit_code == 0 + assert "init [OPTIONS]" in result.output + assert "Initialize the users database." 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_delete(): + result = runner.invoke(app, ["delete", "Camila"], input="y\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete the user? [y/N]:" in result.output + assert "Deleting user: Camila" in result.output + + +def test_no_delete(): + result = runner.invoke(app, ["delete", "Camila"], input="n\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete the user? [y/N]:" in result.output + assert "Operation cancelled" in result.output + + +def test_delete_all(): + result = runner.invoke(app, ["delete-all",], input="y\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete ALL users? [y/N]:" in result.output + assert "Deleting all users" in result.output + + +def test_no_delete_all(): + result = runner.invoke(app, ["delete-all",], input="n\n") + assert result.exit_code == 0 + assert "Are you sure you want to delete ALL users? [y/N]:" in result.output + assert "Operation cancelled" in result.output + + +def test_init(): + result = runner.invoke(app, ["init",]) + assert result.exit_code == 0 + assert "Initializing user database" 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_tutorial006.py b/tests/test_tutorial/test_commands/test_tutorial006.py new file mode 100644 index 0000000..3630db1 --- /dev/null +++ b/tests/test_tutorial/test_commands/test_tutorial006.py @@ -0,0 +1,38 @@ +import subprocess +from commands import tutorial006 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 + assert "delete" 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_delete(): + result = runner.invoke(app, ["delete", "Camila"]) + assert result.exit_code == 0 + assert "Deleting 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