Debian Codegraph @

tests/apps/test_burgery.py

raw symbols SCIP
1 from pathlib import Path
2 from textwrap import dedent
3 from typing import Annotated, Literal
4
5 import cyclopts
6 from cyclopts import App, Parameter, validators
7
8 config_file = Path(__file__).parent / "config.toml"
9
10 app = App(
11 name="burgery",
12 help="Welcome to Cyclopts Burgery!",
13 config=cyclopts.config.Toml(config_file),
14 result_action="return_value", # For testing, return actual values
15 )
16 app.command(create := App(name="create"))
17
18
19 @create.command
20 def burger(
21 variety: Literal["classic", "double"],
22 quantity: Annotated[int, Parameter(validator=validators.Number(gt=0))] = 1,
23 /,
24 *,
25 lettuce: Annotated[bool, Parameter(name="--iceberg", group="Toppings")] = True,
26 tomato: Annotated[bool, Parameter(group="Toppings")] = True,
27 onion: Annotated[bool, Parameter(group="Toppings")] = True,
28 mustard: Annotated[bool, Parameter(group="Condiments")] = True,
29 ketchup: Annotated[bool, Parameter(group="Condiments")] = True,
30 mayo: Annotated[bool, Parameter(group="Condiments")] = True,
31 custom: Annotated[list[str] | None, Parameter(group="Condiments")] = None,
32 ):
33 """Create a burger.
34
35 Parameters
36 ----------
37 variety: Literal["classic", "double"]
38 Type of burger to create
39 quantity: int
40 lettuce: bool
41 Add lettuce.
42 tomato: bool
43 Add tomato.
44 onion: bool
45 Add onion.
46 mustard: bool
47 Add mustard.
48 ketchup: bool
49 Add ketchup.
50 """
51 return locals()
52
53
54 def test_create_burger_help(console):
55 with console.capture() as capture:
56 app("create burger --help", console=console)
57 actual = capture.get()
58 expected = dedent(
59 """\
60 Usage: burgery create burger [OPTIONS] VARIETY [ARGS]
61
62 Create a burger.
63
64 ╭─ Arguments ────────────────────────────────────────────────────────╮
65 │ * VARIETY Type of burger to create [choices: classic, double] │
66 │ [required] │
67 │ QUANTITY [default: 1] │
68 ╰────────────────────────────────────────────────────────────────────╯
69 ╭─ Condiments ───────────────────────────────────────────────────────╮
70 │ --mustard --no-mustard Add mustard. [default: True] │
71 │ --ketchup --no-ketchup Add ketchup. [default: True] │
72 │ --mayo --no-mayo [default: True] │
73 │ --custom --empty-custom │
74 ╰────────────────────────────────────────────────────────────────────╯
75 ╭─ Toppings ─────────────────────────────────────────────────────────╮
76 │ --iceberg --no-iceberg Add lettuce. [default: True] │
77 │ --tomato --no-tomato Add tomato. [default: True] │
78 │ --onion --no-onion Add onion. [default: True] │
79 ╰────────────────────────────────────────────────────────────────────╯
80 """
81 )
82 assert actual == expected
83
84
85 def test_create_burger_1():
86 """Tests generic functionality.
87
88 Detailed:
89 * that config-file overrides (mayo, custom) work.
90 * typical boolean flags work.
91 """
92 actual = app("create burger classic --iceberg --no-onion --no-ketchup --custom sriracha --custom egg")
93 assert actual == {
94 "variety": "classic",
95 "quantity": 1,
96 "lettuce": True,
97 "tomato": True,
98 "onion": False,
99 "ketchup": False,
100 "mustard": True,
101 "mayo": False, # Set from config file.
102 "custom": ["sriracha", "egg"],
103 }
104
105
106 def test_create_burger_2():
107 """Tests that the list from the toml file correctly populates."""
108 actual = app("create burger classic")
109 assert actual == {
110 "variety": "classic",
111 "quantity": 1,
112 "lettuce": True,
113 "tomato": True,
114 "onion": True,
115 "ketchup": True,
116 "mustard": True,
117 "mayo": False, # Set from config file.
118 "custom": ["sweet-chili", "house-sauce"], # Set from config file.
119 }
120
121
122 def test_create_burger_3():
123 """Tests the --empty- config override."""
124 actual = app("create burger classic --empty-custom")
125 assert actual == {
126 "variety": "classic",
127 "quantity": 1,
128 "lettuce": True,
129 "tomato": True,
130 "onion": True,
131 "ketchup": True,
132 "mustard": True,
133 "mayo": False, # Set from config file.
134 "custom": [],
135 }
136
137
138 if __name__ == "__main__":
139 app()