-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional unit tests combining enums with list/tuple
- Loading branch information
Showing
6 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
docs_src/multiple_values/options_with_multiple_values/tutorial002.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
25 changes: 25 additions & 0 deletions
25
docs_src/multiple_values/options_with_multiple_values/tutorial003.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
49 changes: 49 additions & 0 deletions
49
.../test_tutorial/test_multiple_values/test_options_with_multiple_values/test_tutorial002.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
49 changes: 49 additions & 0 deletions
49
.../test_tutorial/test_multiple_values/test_options_with_multiple_values/test_tutorial003.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
47 changes: 47 additions & 0 deletions
47
tests/test_tutorial/test_parameter_types/test_enum/test_tutorial006.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |