✅ Add tests for CLI parameter types
This commit is contained in:
parent
073a72469c
commit
73f047ad39
23 changed files with 616 additions and 0 deletions
0
tests/test_tutorial/test_parameter_types/__init__.py
Normal file
0
tests/test_tutorial/test_parameter_types/__init__.py
Normal file
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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: <class 'str'>" in result.output
|
||||
assert "--age is 15, of type: <class 'int'>" in result.output
|
||||
assert "--height-meters is 1.7, of type: <class 'float'>" in result.output
|
||||
assert "--female is True, of type: <class 'bool'>" 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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in a new issue