| 1 |
import pytest |
| 2 |
|
| 3 |
|
| 4 |
@pytest.mark.parametrize( |
| 5 |
"type_", |
| 6 |
[ |
| 7 |
dict[str, str], |
| 8 |
dict, |
| 9 |
dict, |
| 10 |
], |
| 11 |
) |
| 12 |
def test_bind_dict_str_to_str(app, assert_parse_args, type_): |
| 13 |
@app.command |
| 14 |
def foo(d: type_): |
| 15 |
pass |
| 16 |
|
| 17 |
assert_parse_args(foo, "foo --d.key_1='val1' --d.key-2='val2'", d={"key_1": "val1", "key-2": "val2"}) |
| 18 |
|
| 19 |
|
| 20 |
def test_bind_dict_str_to_int_typing(app, assert_parse_args): |
| 21 |
@app.command |
| 22 |
def foo(d: dict[str, int]): |
| 23 |
pass |
| 24 |
|
| 25 |
assert_parse_args(foo, "foo --d.key1=7 --d.key2=42", d={"key1": 7, "key2": 42}) |
| 26 |
|
| 27 |
|
| 28 |
def test_bind_dict_str_to_int_builtin(app, assert_parse_args): |
| 29 |
@app.command |
| 30 |
def foo(d: dict[str, int]): |
| 31 |
pass |
| 32 |
|
| 33 |
assert_parse_args(foo, "foo --d.key1=7 --d.key2=42", d={"key1": 7, "key2": 42}) |
| 34 |
|
| 35 |
|
| 36 |
def test_bind_dict_with_mixed_keys_and_regular_params(app, assert_parse_args): |
| 37 |
"""Test that dict parameters with keys work alongside regular parameters.""" |
| 38 |
|
| 39 |
@app.command |
| 40 |
def foo(name: str, config: dict[str, int], count: int): |
| 41 |
pass |
| 42 |
|
| 43 |
assert_parse_args( |
| 44 |
foo, |
| 45 |
"foo --name=test --config.key1=7 --config.key2=42 --count=3", |
| 46 |
name="test", |
| 47 |
config={"key1": 7, "key2": 42}, |
| 48 |
count=3, |
| 49 |
) |