Skip to content

Commit

Permalink
Add additional unit tests combining enums with list/tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
svlandeg committed Sep 11, 2024
1 parent 8312bb8 commit b5648ed
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from enum import Enum
from typing import Tuple

import typer


class Food(str, Enum):
f1 = "Eggs"
f2 = "Bacon"
f3 = "Cheese"


def main(user: Tuple[str, int, bool, Food] = typer.Option((None, None, None, Food.f1))):
username, coins, is_wizard, food = user
if not username:
print("No user provided")
raise typer.Abort()
print(f"The username {username} has {coins} coins")
if is_wizard:
print("And this user is a wizard!")
print(f"And they love eating {food.value}")


if __name__ == "__main__":
typer.run(main)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from enum import Enum
from typing import Tuple

import typer


class Food(str, Enum):
f1 = "Eggs"
f2 = "Bacon"
f3 = "Cheese"


def main(user: Tuple[str, int, bool, Food] = typer.Option((None, None, None, "f1"), enum_by_name=True)):
username, coins, is_wizard, food = user
if not username:
print("No user provided")
raise typer.Abort()
print(f"The username {username} has {coins} coins")
if is_wizard:
print("And this user is a wizard!")
print(f"And they love eating {food.value}")


if __name__ == "__main__":
typer.run(main)
18 changes: 18 additions & 0 deletions docs_src/parameter_types/enum/tutorial006.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from enum import Enum
from typing import List

import typer


class Food(str, Enum):
f1 = "Eggs"
f2 = "Bacon"
f3 = "Cheese"


def main(groceries: List[Food] = typer.Option(["f1", "f3"], enum_by_name=True)):
print(f"Buying groceries: {', '.join([f.value for f in groceries])}")


if __name__ == "__main__":
typer.run(main)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import subprocess
import sys

import typer
from typer.testing import CliRunner

from docs_src.multiple_values.options_with_multiple_values import tutorial002 as mod

runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_main():
result = runner.invoke(app)
assert result.exit_code != 0
assert "No user provided" in result.output
assert "Aborted" in result.output


def test_user_1():
result = runner.invoke(app, ["--user", "Camila", "50", "yes", "Eggs"])
assert result.exit_code == 0
assert "The username Camila has 50 coins" in result.output
assert "And this user is a wizard!" in result.output
assert "And they love eating Eggs" in result.output


def test_user_2():
result = runner.invoke(app, ["--user", "Morty", "3", "no", "Bacon"])
assert result.exit_code == 0
assert "The username Morty has 3 coins" in result.output
assert "And this user is a wizard!" not in result.output
assert "And they love eating Bacon" in result.output


def test_invalid_user():
result = runner.invoke(app, ["--user", "Camila", "50"])
assert result.exit_code != 0
assert "Option '--user' requires 4 arguments" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import subprocess
import sys

import typer
from typer.testing import CliRunner

from docs_src.multiple_values.options_with_multiple_values import tutorial003 as mod

runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_main():
result = runner.invoke(app)
assert result.exit_code != 0
assert "No user provided" in result.output
assert "Aborted" in result.output


def test_user_1():
result = runner.invoke(app, ["--user", "Camila", "50", "yes", "f1"])
assert result.exit_code == 0
assert "The username Camila has 50 coins" in result.output
assert "And this user is a wizard!" in result.output
assert "And they love eating Eggs" in result.output


def test_user_2():
result = runner.invoke(app, ["--user", "Morty", "3", "no", "f2"])
assert result.exit_code == 0
assert "The username Morty has 3 coins" in result.output
assert "And this user is a wizard!" not in result.output
assert "And they love eating Bacon" in result.output


def test_invalid_user():
result = runner.invoke(app, ["--user", "Camila", "50"])
assert result.exit_code != 0
assert "Option '--user' requires 4 arguments" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import subprocess
import sys

import typer
from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial006 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 "--groceries" in result.output
assert "[f1|f2|f3]" in result.output
assert "default: f1, f3" in result.output


def test_call_no_arg():
result = runner.invoke(app)
assert result.exit_code == 0
assert "Buying groceries: Eggs, Cheese" in result.output


def test_call_single_arg():
result = runner.invoke(app, ["--groceries", "f2"])
assert result.exit_code == 0
assert "Buying groceries: Bacon" in result.output


def test_call_multiple_arg():
result = runner.invoke(app, ["--groceries", "f1", "--groceries", "f2"])
assert result.exit_code == 0
assert "Buying groceries: Eggs, Bacon" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout

0 comments on commit b5648ed

Please sign in to comment.