| 1 |
import json |
| 2 |
from dataclasses import dataclass, field |
| 3 |
from typing import Annotated |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from cyclopts import CycloptsError, Parameter |
| 8 |
|
| 9 |
|
| 10 |
@dataclass |
| 11 |
class User: |
| 12 |
id: int |
| 13 |
name: str = "John Doe" |
| 14 |
tastes: dict[str, int] = field(default_factory=dict) |
| 15 |
|
| 16 |
|
| 17 |
def test_bind_dataclass_from_env_json(app, assert_parse_args, monkeypatch): |
| 18 |
@app.command |
| 19 |
def foo(some_number: int, user: Annotated[User, Parameter(env_var="USER")]): |
| 20 |
pass |
| 21 |
|
| 22 |
external_data = { |
| 23 |
"id": 123, |
| 24 |
|
| 25 |
"tastes": { |
| 26 |
"wine": 9, |
| 27 |
"cheese": 7, |
| 28 |
"cabbage": 1, |
| 29 |
}, |
| 30 |
} |
| 31 |
monkeypatch.setenv("USER", json.dumps(external_data)) |
| 32 |
assert_parse_args( |
| 33 |
foo, |
| 34 |
"foo 100", |
| 35 |
100, |
| 36 |
User(**external_data), |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize( |
| 41 |
"cmd_str", |
| 42 |
[ |
| 43 |
"""--origin='{"x": 1, "y": 2}'""", |
| 44 |
"""--origin '{"x": 1, "y": 2}'""", |
| 45 |
"""--origin='{"x": 1, "y": 2, "label": null}'""", |
| 46 |
], |
| 47 |
) |
| 48 |
def test_bind_dataclass_from_cli_json(app, assert_parse_args, cmd_str): |
| 49 |
@dataclass |
| 50 |
class Coordinate: |
| 51 |
x: int |
| 52 |
y: int |
| 53 |
label: str | None = None |
| 54 |
|
| 55 |
@app.default |
| 56 |
def main(origin: Coordinate): |
| 57 |
pass |
| 58 |
|
| 59 |
assert_parse_args(main, cmd_str, Coordinate(1, 2)) |
| 60 |
|
| 61 |
|
| 62 |
def test_nested_dataclass_from_env_json(app, assert_parse_args, monkeypatch): |
| 63 |
"""Test parsing nested dataclasses from environment variable JSON.""" |
| 64 |
|
| 65 |
@dataclass |
| 66 |
class Address: |
| 67 |
street: str |
| 68 |
city: str |
| 69 |
zipcode: str | None = None |
| 70 |
|
| 71 |
@dataclass |
| 72 |
class Person: |
| 73 |
name: str |
| 74 |
age: int |
| 75 |
address: Address |
| 76 |
hobbies: list[str] = field(default_factory=list) |
| 77 |
|
| 78 |
@app.default |
| 79 |
def main(person: Annotated[Person, Parameter(env_var="PERSON")]): |
| 80 |
pass |
| 81 |
|
| 82 |
person_data = { |
| 83 |
"name": "John Doe", |
| 84 |
"age": 30, |
| 85 |
"address": {"street": "123 Main St", "city": "Springfield", "zipcode": "12345"}, |
| 86 |
"hobbies": ["reading", "gaming", "cooking"], |
| 87 |
} |
| 88 |
monkeypatch.setenv("PERSON", json.dumps(person_data)) |
| 89 |
|
| 90 |
expected = Person( |
| 91 |
name="John Doe", |
| 92 |
age=30, |
| 93 |
address=Address(street="123 Main St", city="Springfield", zipcode="12345"), |
| 94 |
hobbies=["reading", "gaming", "cooking"], |
| 95 |
) |
| 96 |
assert_parse_args(main, "", expected) |
| 97 |
|
| 98 |
|
| 99 |
def test_list_of_simple_dataclass_from_env(app, assert_parse_args, monkeypatch): |
| 100 |
"""Test parsing list of simple dataclasses from environment variable.""" |
| 101 |
|
| 102 |
@dataclass |
| 103 |
class Product: |
| 104 |
name: str |
| 105 |
price: float |
| 106 |
in_stock: bool = True |
| 107 |
|
| 108 |
@app.default |
| 109 |
def main(products: Annotated[list[Product], Parameter(env_var="PRODUCTS")]): |
| 110 |
pass |
| 111 |
|
| 112 |
products_data = [ |
| 113 |
{"name": "Widget", "price": 19.99, "in_stock": True}, |
| 114 |
{"name": "Gadget", "price": 29.99, "in_stock": False}, |
| 115 |
{"name": "Doohickey", "price": 39.99}, |
| 116 |
] |
| 117 |
monkeypatch.setenv("PRODUCTS", json.dumps(products_data)) |
| 118 |
|
| 119 |
expected = [ |
| 120 |
Product(name="Widget", price=19.99, in_stock=True), |
| 121 |
Product(name="Gadget", price=29.99, in_stock=False), |
| 122 |
Product(name="Doohickey", price=39.99, in_stock=True), |
| 123 |
] |
| 124 |
assert_parse_args(main, "", expected) |
| 125 |
|
| 126 |
|
| 127 |
def test_complex_nested_type_validation_env_var(app, monkeypatch): |
| 128 |
"""Test that type validation works for complex nested structures from env vars.""" |
| 129 |
|
| 130 |
@dataclass |
| 131 |
class Config: |
| 132 |
name: str |
| 133 |
value: int |
| 134 |
enabled: bool = True |
| 135 |
|
| 136 |
@app.default |
| 137 |
def main(config: Annotated[Config, Parameter(env_var="CONFIG")]): |
| 138 |
pass |
| 139 |
|
| 140 |
|
| 141 |
invalid_data = {"name": "test", "value": "not_an_int", "enabled": True} |
| 142 |
monkeypatch.setenv("CONFIG", json.dumps(invalid_data)) |
| 143 |
|
| 144 |
with pytest.raises(CycloptsError): |
| 145 |
app([], exit_on_error=False) |