Skip to content
Myr

:white_check_mark: Add tests for CLI parameter types

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent 073a724 · Browse files · 23 files+616 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

23 files+616 added−0 removed

  • tests:+616 added−0 removed

23 files+616 added−0 removed

added:tests/test_tutorial/test_parameter_types/test_datetime/__init__.py
added:tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial001.py+43 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.datetime import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "[%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output181920def test_main():21 result = runner.invoke(app, ["1956-01-31T10:00:00"])22 assert result.exit_code == 023 assert "Interesting day to be born: 1956-01-31 10:00:00" in result.output24 assert "Birth hour: 10" in result.output252627def test_invalid():28 result = runner.invoke(app, ["july-19-1989"])29 assert result.exit_code != 030 assert (31 '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)'32 in result.output33 )343536def test_script():37 result = subprocess.run(38 ["coverage", "run", mod.__file__, "--help"],39 stdout=subprocess.PIPE,40 stderr=subprocess.PIPE,41 encoding="utf-8",42 )43 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_datetime/test_tutorial002.py+33 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.datetime import tutorial002 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_main():15 result = runner.invoke(app, ["1969-10-29"])16 assert result.exit_code == 017 assert "Launch will be at: 1969-10-29 00:00:00" in result.output181920def test_usa_weird_date_format():21 result = runner.invoke(app, ["10/29/1969"])22 assert result.exit_code == 023 assert "Launch will be at: 1969-10-29 00:00:00" in result.output242526def test_script():27 result = subprocess.run(28 ["coverage", "run", mod.__file__, "--help"],29 stdout=subprocess.PIPE,30 stderr=subprocess.PIPE,31 encoding="utf-8",32 )33 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_enum/__init__.py
added:tests/test_tutorial/test_parameter_types/test_enum/test_tutorial001.py+42 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.enum import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "--network [simple|conv|lstm]" in result.output181920def test_main():21 result = runner.invoke(app, ["--network", "conv"])22 assert result.exit_code == 023 assert "Training neural network of type: conv" in result.output242526def test_invalid():27 result = runner.invoke(app, ["--network", "capsule"])28 assert result.exit_code != 029 assert (30 'Error: Invalid value for "--network": invalid choice: capsule. (choose from simple, conv, lstm)'31 in result.output32 )333435def test_script():36 result = subprocess.run(37 ["coverage", "run", mod.__file__, "--help"],38 stdout=subprocess.PIPE,39 stderr=subprocess.PIPE,40 encoding="utf-8",41 )42 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_enum/test_tutorial002.py+33 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.enum import tutorial002 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_upper():15 result = runner.invoke(app, ["--network", "CONV"])16 assert result.exit_code == 017 assert "Training neural network of type: conv" in result.output181920def test_mix():21 result = runner.invoke(app, ["--network", "LsTm"])22 assert result.exit_code == 023 assert "Training neural network of type: lstm" in result.output242526def test_script():27 result = subprocess.run(28 ["coverage", "run", mod.__file__, "--help"],29 stdout=subprocess.PIPE,30 stderr=subprocess.PIPE,31 encoding="utf-8",32 )33 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_file/__init__.py
added:tests/test_tutorial/test_parameter_types/test_file/test_tutorial001.py+33 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.file import tutorial001 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314config_file = Path("./config.txt")151617def test_main():18 config_file.write_text("some settings\nsome more settings")19 result = runner.invoke(app, ["--config", f"{config_file}"])20 config_file.unlink()21 assert result.exit_code == 022 assert "Config line: some settings" in result.output23 assert "Config line: some more settings" in result.output242526def test_script():27 result = subprocess.run(28 ["coverage", "run", mod.__file__, "--help"],29 stdout=subprocess.PIPE,30 stderr=subprocess.PIPE,31 encoding="utf-8",32 )33 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_file/test_tutorial002.py+35 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.file import tutorial002 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314config_file = Path("./config.txt")151617def test_main():18 if config_file.exists(): # pragma no cover19 config_file.unlink()20 result = runner.invoke(app, ["--config", f"{config_file}"])21 text = config_file.read_text()22 config_file.unlink()23 assert result.exit_code == 024 assert "Config written" in result.output25 assert "Some config written by the app" in text262728def test_script():29 result = subprocess.run(30 ["coverage", "run", mod.__file__, "--help"],31 stdout=subprocess.PIPE,32 stderr=subprocess.PIPE,33 encoding="utf-8",34 )35 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_file/test_tutorial003.py+32 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.file import tutorial003 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314binary_file = Path("./config.txt")151617def test_main():18 binary_file.write_bytes(b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o")19 result = runner.invoke(app, ["--file", f"{binary_file}"])20 binary_file.unlink()21 assert result.exit_code == 022 assert "Processed bytes total:" in result.output232425def test_script():26 result = subprocess.run(27 ["coverage", "run", mod.__file__, "--help"],28 stdout=subprocess.PIPE,29 stderr=subprocess.PIPE,30 encoding="utf-8",31 )32 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_file/test_tutorial004.py+36 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.file import tutorial004 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314binary_file = Path("./config.txt")151617def test_main():18 if binary_file.exists(): # pragma no cover19 binary_file.unlink()20 result = runner.invoke(app, ["--file", f"{binary_file}"])21 text = binary_file.read_text()22 binary_file.unlink()23 assert result.exit_code == 024 assert "Binary file written" in result.output25 assert "some settings" in text26 assert "la cigüeña trae al niño" in text272829def test_script():30 result = subprocess.run(31 ["coverage", "run", mod.__file__, "--help"],32 stdout=subprocess.PIPE,33 stderr=subprocess.PIPE,34 encoding="utf-8",35 )36 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_file/test_tutorial005.py+38 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.file import tutorial005 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314config_file = Path("./config.txt")151617def test_main():18 if config_file.exists(): # pragma no cover19 config_file.unlink()20 config_file.write_text("")21 result = runner.invoke(app, ["--config", f"{config_file}"])22 result = runner.invoke(app, ["--config", f"{config_file}"])23 result = runner.invoke(app, ["--config", f"{config_file}"])24 text = config_file.read_text()25 config_file.unlink()26 assert result.exit_code == 027 assert "Config line written"28 assert "This is a single line\nThis is a single line\nThis is a single line" in text293031def test_script():32 result = subprocess.run(33 ["coverage", "run", mod.__file__, "--help"],34 stdout=subprocess.PIPE,35 stderr=subprocess.PIPE,36 encoding="utf-8",37 )38 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_index/__init__.py
added:tests/test_tutorial/test_parameter_types/test_index/test_tutorial001.py+47 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.index import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "--age INTEGER" in result.output18 assert "--height-meters FLOAT" in result.output192021def test_params():22 result = runner.invoke(23 app, ["Camila", "--age", "15", "--height-meters", "1.70", "--female"]24 )25 assert result.exit_code == 026 assert "NAME is Camila, of type: <class 'str'>" in result.output27 assert "--age is 15, of type: <class 'int'>" in result.output28 assert "--height-meters is 1.7, of type: <class 'float'>" in result.output29 assert "--female is True, of type: <class 'bool'>" in result.output303132def test_invalid():33 result = runner.invoke(app, ["Camila", "--age", "15.3"])34 assert result.exit_code != 035 assert (36 'Error: Invalid value for "--age": 15.3 is not a valid integer' in result.output37 )383940def test_script():41 result = subprocess.run(42 ["coverage", "run", mod.__file__, "--help"],43 stdout=subprocess.PIPE,44 stderr=subprocess.PIPE,45 encoding="utf-8",46 )47 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_number/__init__.py
added:tests/test_tutorial/test_parameter_types/test_number/test_tutorial001.py+71 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.number import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "--age INTEGER RANGE" in result.output18 assert "--score FLOAT RANGE" in result.output192021def test_params():22 result = runner.invoke(app, ["5", "--age", "20", "--score", "90"])23 assert result.exit_code == 024 assert "ID is 5" in result.output25 assert "--age is 20" in result.output26 assert "--score is 90.0" in result.output272829def test_invalid_id():30 result = runner.invoke(app, ["1002"])31 assert result.exit_code != 032 assert (33 'Error: Invalid value for "ID": 1002 is not in the valid range of 0 to 1000.'34 in result.output35 )363738def test_invalid_age():39 result = runner.invoke(app, ["5", "--age", "15"])40 assert result.exit_code != 041 assert (42 'Error: Invalid value for "--age": 15 is smaller than the minimum valid value 18.'43 in result.output44 )454647def test_invalid_score():48 result = runner.invoke(app, ["5", "--age", "20", "--score", "100.5"])49 assert result.exit_code != 050 assert (51 'Error: Invalid value for "--score": 100.5 is bigger than the maximum valid value 100.'52 in result.output53 )545556def test_negative_score():57 result = runner.invoke(app, ["5", "--age", "20", "--score", "-5"])58 assert result.exit_code == 059 assert "ID is 5" in result.output60 assert "--age is 20" in result.output61 assert "--score is -5.0" in result.output626364def test_script():65 result = subprocess.run(66 ["coverage", "run", mod.__file__, "--help"],67 stdout=subprocess.PIPE,68 stderr=subprocess.PIPE,69 encoding="utf-8",70 )71 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_number/test_tutorial002.py+38 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.number import tutorial002 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_invalid_id():15 result = runner.invoke(app, ["1002"])16 assert result.exit_code != 017 assert (18 'Error: Invalid value for "ID": 1002 is not in the valid range of 0 to 1000.'19 in result.output20 )212223def test_clamped():24 result = runner.invoke(app, ["5", "--rank", "11", "--score", "-5"])25 assert result.exit_code == 026 assert "ID is 5" in result.output27 assert "--rank is 10" in result.output28 assert "--score is 0" in result.output293031def test_script():32 result = subprocess.run(33 ["coverage", "run", mod.__file__, "--help"],34 stdout=subprocess.PIPE,35 stderr=subprocess.PIPE,36 encoding="utf-8",37 )38 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_path/__init__.py
added:tests/test_tutorial/test_parameter_types/test_path/test_tutorial001.py+46 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.path import tutorial001 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314config_file = Path("./config.txt")151617def test_not_exists():18 if config_file.exists(): # pragma no cover19 config_file.unlink()20 result = runner.invoke(app, ["--config", f"{config_file}"])21 assert result.exit_code == 022 assert "The config doesn't exist" in result.output232425def test_exists():26 config_file.write_text("some settings")27 result = runner.invoke(app, ["--config", f"{config_file}"])28 config_file.unlink()29 assert result.exit_code == 030 assert "Config file contents: some settings" in result.output313233def test_dir():34 result = runner.invoke(app, ["--config", "./"])35 assert result.exit_code == 036 assert "Config is a directory, will use all its config files" in result.output373839def test_script():40 result = subprocess.run(41 ["coverage", "run", mod.__file__, "--help"],42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 encoding="utf-8",45 )46 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_path/test_tutorial002.py+52 added
1import subprocess2from pathlib import Path34import typer5from typer.testing import CliRunner67from parameter_types.path import tutorial002 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)1314config_file = Path("./config.txt")151617def test_not_exists():18 if config_file.exists(): # pragma no cover19 config_file.unlink()20 result = runner.invoke(app, ["--config", f"{config_file}"])21 assert result.exit_code != 022 assert (23 'Error: Invalid value for "--config": File "config.txt" does not exist.'24 in result.output25 )262728def test_exists():29 config_file.write_text("some settings")30 result = runner.invoke(app, ["--config", f"{config_file}"])31 config_file.unlink()32 assert result.exit_code == 033 assert "Config file contents: some settings" in result.output343536def test_dir():37 result = runner.invoke(app, ["--config", "./"])38 assert result.exit_code != 039 assert (40 'Error: Invalid value for "--config": File "./" is a directory.'41 in result.output42 )434445def test_script():46 result = subprocess.run(47 ["coverage", "run", mod.__file__, "--help"],48 stdout=subprocess.PIPE,49 stderr=subprocess.PIPE,50 encoding="utf-8",51 )52 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_uuid/__init__.py
added:tests/test_tutorial/test_parameter_types/test_uuid/test_tutorial001.py+37 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.uuid import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_main():15 result = runner.invoke(app, ["d48edaa6-871a-4082-a196-4daab372d4a1"])16 assert result.exit_code == 017 assert "USER_ID is d48edaa6-871a-4082-a196-4daab372d4a1" in result.output18 assert "UUID version is: 4" in result.output192021def test_invalid_uuid():22 result = runner.invoke(app, ["7479706572-72756c6573"])23 assert result.exit_code != 024 assert (25 'Error: Invalid value for "USER_ID": 7479706572-72756c6573 is not a valid UUID value'26 in result.output27 )282930def test_script():31 result = subprocess.run(32 ["coverage", "run", mod.__file__, "--help"],33 stdout=subprocess.PIPE,34 stderr=subprocess.PIPE,35 encoding="utf-8",36 )37 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/__init__.py