|
| 1 | +import re |
1 | 2 | from typing import Any, List |
2 | 3 |
|
3 | 4 | from end_to_end_tests.functional_tests.helpers import ( |
4 | 5 | with_generated_code_import, |
5 | 6 | with_generated_client_fixture, |
6 | 7 | ) |
| 8 | +from end_to_end_tests.generated_client import GeneratedClientContext |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class DocstringParser: |
@@ -36,18 +38,57 @@ def get_section(self, header_line: str) -> List[str]: |
36 | 38 | required: ["reqStr", "undescribedProp"] |
37 | 39 | """) |
38 | 40 | @with_generated_code_import(".models.MyModel") |
39 | | -class TestSchemaDocstrings: |
| 41 | +class TestSchemaDocstringsDefaultBehavior: |
40 | 42 | def test_model_description(self, MyModel): |
41 | 43 | assert DocstringParser(MyModel).lines[0] == "I like this type." |
42 | 44 |
|
43 | | - def test_model_properties(self, MyModel): |
| 45 | + def test_model_properties_in_model_description(self, MyModel): |
44 | 46 | assert set(DocstringParser(MyModel).get_section("Attributes:")) == { |
45 | 47 | "req_str (str): This is necessary.", |
46 | 48 | "opt_str (Union[Unset, str]): This isn't necessary.", |
47 | 49 | "undescribed_prop (str):", |
48 | 50 | } |
49 | 51 |
|
50 | 52 |
|
| 53 | +@with_generated_client_fixture( |
| 54 | +""" |
| 55 | +components: |
| 56 | + schemas: |
| 57 | + MyModel: |
| 58 | + description: I like this type. |
| 59 | + type: object |
| 60 | + properties: |
| 61 | + prop1: |
| 62 | + type: string |
| 63 | + description: This attribute has a description |
| 64 | + prop2: |
| 65 | + type: string # no description for this one |
| 66 | + required: ["prop1", "prop2"] |
| 67 | +""", |
| 68 | +config="docstrings_on_attributes: true", |
| 69 | +) |
| 70 | +@with_generated_code_import(".models.MyModel") |
| 71 | +class TestSchemaWithDocstringsOnAttributesOption: |
| 72 | + def test_model_description_is_entire_docstring(self, MyModel): |
| 73 | + assert MyModel.__doc__.strip() == "I like this type." |
| 74 | + |
| 75 | + def test_attrs_have_docstrings(self, generated_client: GeneratedClientContext): |
| 76 | + # A docstring that appears after an attribute is *not* stored in __doc__ anywhere |
| 77 | + # by the interpreter, so we can't inspect it that way-- but it's still valid for it |
| 78 | + # to appear there, and it will be recognized by documentation tools. So we'll assert |
| 79 | + # that these strings appear in the source code. The code should look like this: |
| 80 | + # class MyModel: |
| 81 | + # """I like this type." |
| 82 | + # prop1: str |
| 83 | + # """This attribute has a description" |
| 84 | + # prop2: str |
| 85 | + # |
| 86 | + source_file_path = generated_client.output_path / generated_client.base_module / "models" / "my_model.py" |
| 87 | + content = source_file_path.read_text() |
| 88 | + assert re.search('\n *prop1: *str\n *""" *This attribute has a description *"""\n', content) |
| 89 | + assert re.search('\n *prop2: *str\n *[^"]', content) |
| 90 | + |
| 91 | + |
51 | 92 | @with_generated_client_fixture( |
52 | 93 | """ |
53 | 94 | tags: |
|
0 commit comments