diff --git a/tests/test_tutorial/test_parameter_types/__init__.py b/tests/test_tutorial/test_parameter_types/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_datetime/__init__.py b/tests/test_tutorial/test_parameter_types/test_datetime/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial001.py new file mode 100644 index 0000000..79cedf7 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial001.py @@ -0,0 +1,43 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.datetime 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 "[%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output + + +def test_main(): + result = runner.invoke(app, ["1956-01-31T10:00:00"]) + assert result.exit_code == 0 + assert "Interesting day to be born: 1956-01-31 10:00:00" in result.output + assert "Birth hour: 10" in result.output + + +def test_invalid(): + result = runner.invoke(app, ["july-19-1989"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "[%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]": invalid datetime format: july-19-1989. (choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)' + 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_datetime/test_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial002.py new file mode 100644 index 0000000..00831d3 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial002.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.datetime import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_main(): + result = runner.invoke(app, ["1969-10-29"]) + assert result.exit_code == 0 + assert "Launch will be at: 1969-10-29 00:00:00" in result.output + + +def test_usa_weird_date_format(): + result = runner.invoke(app, ["10/29/1969"]) + assert result.exit_code == 0 + assert "Launch will be at: 1969-10-29 00:00:00" 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_enum/__init__.py b/tests/test_tutorial/test_parameter_types/test_enum/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_enum/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_enum/test_tutorial001.py new file mode 100644 index 0000000..64a0b99 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_enum/test_tutorial001.py @@ -0,0 +1,42 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.enum 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 "--network [simple|conv|lstm]" in result.output + + +def test_main(): + result = runner.invoke(app, ["--network", "conv"]) + assert result.exit_code == 0 + assert "Training neural network of type: conv" in result.output + + +def test_invalid(): + result = runner.invoke(app, ["--network", "capsule"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--network": invalid choice: capsule. (choose from simple, conv, lstm)' + 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_enum/test_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_enum/test_tutorial002.py new file mode 100644 index 0000000..e72efea --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_enum/test_tutorial002.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.enum import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_upper(): + result = runner.invoke(app, ["--network", "CONV"]) + assert result.exit_code == 0 + assert "Training neural network of type: conv" in result.output + + +def test_mix(): + result = runner.invoke(app, ["--network", "LsTm"]) + assert result.exit_code == 0 + assert "Training neural network of type: lstm" 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_file/__init__.py b/tests/test_tutorial/test_parameter_types/test_file/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_file/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial001.py new file mode 100644 index 0000000..86368a7 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial001.py @@ -0,0 +1,33 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.file import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +config_file = Path("./config.txt") + + +def test_main(): + config_file.write_text("some settings\nsome more settings") + result = runner.invoke(app, ["--config", f"{config_file}"]) + config_file.unlink() + assert result.exit_code == 0 + assert "Config line: some settings" in result.output + assert "Config line: some more settings" 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_file/test_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial002.py new file mode 100644 index 0000000..17a9696 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial002.py @@ -0,0 +1,35 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.file import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +config_file = Path("./config.txt") + + +def test_main(): + if config_file.exists(): # pragma no cover + config_file.unlink() + result = runner.invoke(app, ["--config", f"{config_file}"]) + text = config_file.read_text() + config_file.unlink() + assert result.exit_code == 0 + assert "Config written" in result.output + assert "Some config written by the app" in text + + +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_file/test_tutorial003.py b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial003.py new file mode 100644 index 0000000..702e7e0 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial003.py @@ -0,0 +1,32 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.file import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +binary_file = Path("./config.txt") + + +def test_main(): + binary_file.write_bytes(b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o") + result = runner.invoke(app, ["--file", f"{binary_file}"]) + binary_file.unlink() + assert result.exit_code == 0 + assert "Processed bytes total:" 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_file/test_tutorial004.py b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial004.py new file mode 100644 index 0000000..3a704b6 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial004.py @@ -0,0 +1,36 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.file import tutorial004 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +binary_file = Path("./config.txt") + + +def test_main(): + if binary_file.exists(): # pragma no cover + binary_file.unlink() + result = runner.invoke(app, ["--file", f"{binary_file}"]) + text = binary_file.read_text() + binary_file.unlink() + assert result.exit_code == 0 + assert "Binary file written" in result.output + assert "some settings" in text + assert "la cigüeña trae al niño" in text + + +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_file/test_tutorial005.py b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial005.py new file mode 100644 index 0000000..f2541ed --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_file/test_tutorial005.py @@ -0,0 +1,38 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.file import tutorial005 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +config_file = Path("./config.txt") + + +def test_main(): + if config_file.exists(): # pragma no cover + config_file.unlink() + config_file.write_text("") + result = runner.invoke(app, ["--config", f"{config_file}"]) + result = runner.invoke(app, ["--config", f"{config_file}"]) + result = runner.invoke(app, ["--config", f"{config_file}"]) + text = config_file.read_text() + config_file.unlink() + assert result.exit_code == 0 + assert "Config line written" + assert "This is a single line\nThis is a single line\nThis is a single line" in text + + +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_index/__init__.py b/tests/test_tutorial/test_parameter_types/test_index/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_index/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_index/test_tutorial001.py new file mode 100644 index 0000000..98efe4b --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_index/test_tutorial001.py @@ -0,0 +1,47 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.index 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 "--age INTEGER" in result.output + assert "--height-meters FLOAT" in result.output + + +def test_params(): + result = runner.invoke( + app, ["Camila", "--age", "15", "--height-meters", "1.70", "--female"] + ) + assert result.exit_code == 0 + assert "NAME is Camila, of type: " in result.output + assert "--age is 15, of type: " in result.output + assert "--height-meters is 1.7, of type: " in result.output + assert "--female is True, of type: " in result.output + + +def test_invalid(): + result = runner.invoke(app, ["Camila", "--age", "15.3"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--age": 15.3 is not a valid integer' 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/__init__.py b/tests/test_tutorial/test_parameter_types/test_number/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_number/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial001.py new file mode 100644 index 0000000..cbf7e62 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial001.py @@ -0,0 +1,71 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.number 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 "--age INTEGER RANGE" in result.output + assert "--score FLOAT RANGE" in result.output + + +def test_params(): + result = runner.invoke(app, ["5", "--age", "20", "--score", "90"]) + assert result.exit_code == 0 + assert "ID is 5" in result.output + assert "--age is 20" in result.output + assert "--score is 90.0" in result.output + + +def test_invalid_id(): + result = runner.invoke(app, ["1002"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "ID": 1002 is not in the valid range of 0 to 1000.' + in result.output + ) + + +def test_invalid_age(): + result = runner.invoke(app, ["5", "--age", "15"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--age": 15 is smaller than the minimum valid value 18.' + in result.output + ) + + +def test_invalid_score(): + result = runner.invoke(app, ["5", "--age", "20", "--score", "100.5"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--score": 100.5 is bigger than the maximum valid value 100.' + in result.output + ) + + +def test_negative_score(): + result = runner.invoke(app, ["5", "--age", "20", "--score", "-5"]) + assert result.exit_code == 0 + assert "ID is 5" in result.output + assert "--age is 20" in result.output + assert "--score is -5.0" 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_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial002.py new file mode 100644 index 0000000..e8a9e42 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_number/test_tutorial002.py @@ -0,0 +1,38 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.number import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_invalid_id(): + result = runner.invoke(app, ["1002"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "ID": 1002 is not in the valid range of 0 to 1000.' + in result.output + ) + + +def test_clamped(): + result = runner.invoke(app, ["5", "--rank", "11", "--score", "-5"]) + assert result.exit_code == 0 + assert "ID is 5" in result.output + assert "--rank is 10" in result.output + assert "--score is 0" 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_path/__init__.py b/tests/test_tutorial/test_parameter_types/test_path/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_path/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_path/test_tutorial001.py new file mode 100644 index 0000000..88affcf --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_path/test_tutorial001.py @@ -0,0 +1,46 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.path import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +config_file = Path("./config.txt") + + +def test_not_exists(): + if config_file.exists(): # pragma no cover + config_file.unlink() + result = runner.invoke(app, ["--config", f"{config_file}"]) + assert result.exit_code == 0 + assert "The config doesn't exist" in result.output + + +def test_exists(): + config_file.write_text("some settings") + result = runner.invoke(app, ["--config", f"{config_file}"]) + config_file.unlink() + assert result.exit_code == 0 + assert "Config file contents: some settings" in result.output + + +def test_dir(): + result = runner.invoke(app, ["--config", "./"]) + assert result.exit_code == 0 + assert "Config is a directory, will use all its config files" 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_path/test_tutorial002.py b/tests/test_tutorial/test_parameter_types/test_path/test_tutorial002.py new file mode 100644 index 0000000..cbdf2b6 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_path/test_tutorial002.py @@ -0,0 +1,52 @@ +import subprocess +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from parameter_types.path import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + +config_file = Path("./config.txt") + + +def test_not_exists(): + if config_file.exists(): # pragma no cover + config_file.unlink() + result = runner.invoke(app, ["--config", f"{config_file}"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--config": File "config.txt" does not exist.' + in result.output + ) + + +def test_exists(): + config_file.write_text("some settings") + result = runner.invoke(app, ["--config", f"{config_file}"]) + config_file.unlink() + assert result.exit_code == 0 + assert "Config file contents: some settings" in result.output + + +def test_dir(): + result = runner.invoke(app, ["--config", "./"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "--config": File "./" is a directory.' + 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_uuid/__init__.py b/tests/test_tutorial/test_parameter_types/test_uuid/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_parameter_types/test_uuid/test_tutorial001.py b/tests/test_tutorial/test_parameter_types/test_uuid/test_tutorial001.py new file mode 100644 index 0000000..2c25963 --- /dev/null +++ b/tests/test_tutorial/test_parameter_types/test_uuid/test_tutorial001.py @@ -0,0 +1,37 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from parameter_types.uuid import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_main(): + result = runner.invoke(app, ["d48edaa6-871a-4082-a196-4daab372d4a1"]) + assert result.exit_code == 0 + assert "USER_ID is d48edaa6-871a-4082-a196-4daab372d4a1" in result.output + assert "UUID version is: 4" in result.output + + +def test_invalid_uuid(): + result = runner.invoke(app, ["7479706572-72756c6573"]) + assert result.exit_code != 0 + assert ( + 'Error: Invalid value for "USER_ID": 7479706572-72756c6573 is not a valid UUID value' + 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