|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | + |
| 5 | + |
| 6 | +def _section(content: str, start: str, end: str | None = None) -> str: |
| 7 | + section = content.split(start, 1)[1] |
| 8 | + if end is not None: |
| 9 | + section = section.split(end, 1)[0] |
| 10 | + return section |
| 11 | + |
| 12 | + |
| 13 | +def test_model_template_renders_lazy_imports_in_stable_order(env) -> None: |
| 14 | + template = env.get_template("model.py.jinja") |
| 15 | + |
| 16 | + model = SimpleNamespace( |
| 17 | + is_multipart_body=True, |
| 18 | + relative_imports=set(), |
| 19 | + lazy_imports={"from ..models.z import Z", "from ..models.a import A"}, |
| 20 | + additional_properties=False, |
| 21 | + class_info=SimpleNamespace(name="MyModel", module_name="my_model"), |
| 22 | + title="", |
| 23 | + description="", |
| 24 | + example="", |
| 25 | + required_properties=[], |
| 26 | + optional_properties=[], |
| 27 | + ) |
| 28 | + config = SimpleNamespace(docstrings_on_attributes=False) |
| 29 | + |
| 30 | + content = template.render(model=model, config=config) |
| 31 | + |
| 32 | + sections = [ |
| 33 | + _section(content, "if TYPE_CHECKING:", "T = TypeVar"), |
| 34 | + _section(content, "def to_dict(self)", "def to_multipart(self)"), |
| 35 | + _section(content, "def to_multipart(self)", "@classmethod"), |
| 36 | + _section(content, "def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:", "return my_model"), |
| 37 | + ] |
| 38 | + for section in sections: |
| 39 | + assert section.index("from ..models.a import A") < section.index("from ..models.z import Z") |
0 commit comments