diff --git a/.changeset/live_tests.md b/.changeset/live_tests.md new file mode 100644 index 000000000..90ba90ffc --- /dev/null +++ b/.changeset/live_tests.md @@ -0,0 +1,14 @@ +--- +default: minor +--- + +# Functional tests + +Automated tests have been extended to include a new category of "functional" tests, in [`end_to_end_tests/functional_tests`](./end_to_end_tests/functional_tests). These are of two kinds: + +1. Happy-path tests that run the generator from an inline API document and then actually import and execute the generated code. +2. Warning/error condition tests that run the generator from an inline API document that contains something invalid, and make assertions about the generator's output. + +These provide more efficient and granular test coverage than the "golden record"-based end-to-end tests. Also, the low-level unit tests in `tests`, which are dependent on internal implementation details, can now in many cases be replaced by functional tests. + +This does not affect any runtime functionality of openapi-python-client. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 194f26dcc..e3d9c68ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,26 +50,40 @@ All changes must be tested, I recommend writing the test first, then writing the If you think that some of the added code is not testable (or testing it would add little value), mention that in your PR and we can discuss it. -1. If you're adding support for a new OpenAPI feature or covering a new edge case, add an [end-to-end test](#end-to-end-tests) -2. If you're modifying the way an existing feature works, make sure an existing test generates the _old_ code in `end_to_end_tests/golden-record`. You'll use this to check for the new code once your changes are complete. -3. If you're improving an error or adding a new error, add a [unit test](#unit-tests) +1. If you're adding support for a new OpenAPI feature or covering a new edge case, add [functional tests](#functional-tests), and optionally an [end-to-end snapshot test](#end-to-end-snapshot-tests). +2. If you're modifying the way an existing feature works, make sure functional tests cover this case. Existing end-to-end snapshot tests might also be affected if you have changed what generated model/endpoint code looks like. +3. If you're improving error handling or adding a new error, add [functional tests](#functional-tests). +4. For tests of low-level pieces of code that are fairly self-contained, and not tightly coupled to other internal implementation details, you can use regular [unit tests](#unit-tests). -#### End-to-end tests +#### End-to-end snapshot tests -This project aims to have all "happy paths" (types of code which _can_ be generated) covered by end to end tests (snapshot tests). In order to check code changes against the previous set of snapshots (called a "golden record" here), you can run `pdm e2e`. To regenerate the snapshots, run `pdm regen`. +This project aims to have all "happy paths" (types of code which _can_ be generated) covered by end-to-end tests. There are two types of these: snapshot tests, and functional tests. -There are 4 types of snapshots generated right now, you may have to update only some or all of these depending on the changes you're making. Within the `end_to_end_tets` directory: +Snapshot tests verify that the generated code is identical to a previously-committed set of snapshots (called a "golden record" here). They are basically regression tests to catch any unintended changes in the generator output. + +In order to check code changes against the previous set of snapshots (called a "golden record" here), you can run `pdm e2e`. To regenerate the snapshots, run `pdm regen`. + +There are 4 types of snapshots generated right now, you may have to update only some or all of these depending on the changes you're making. Within the `end_to_end_tests` directory: 1. `baseline_openapi_3.0.json` creates `golden-record` for testing OpenAPI 3.0 features 2. `baseline_openapi_3.1.yaml` is checked against `golden-record` for testing OpenAPI 3.1 features (and ensuring consistency with 3.0) 3. `test_custom_templates` are used with `baseline_openapi_3.0.json` to generate `custom-templates-golden-record` for testing custom templates 4. `3.1_specific.openapi.yaml` is used to generate `test-3-1-golden-record` and test 3.1-specific features (things which do not have a 3.0 equivalent) +#### Functional tests + +These are black-box tests that verify the runtime behavior of generated code, as well as the generator's validation behavior. They are also end-to-end tests, since they run the generator as a shell command. + +This can sometimes identify issues with error handling, validation logic, module imports, etc., that might be harder to diagnose via the snapshot tests, especially during development of a new feature. For instance, they can verify that JSON data is correctly decoded into model class attributes, or that the generator will emit an appropriate warning or error for an invalid spec. + +See [`end_to_end_tests/functional_tests`](./end_to_end_tests/functional_tests). + #### Unit tests -> **NOTE**: Several older-style unit tests using mocks exist in this project. These should be phased out rather than updated, as the tests are brittle and difficult to maintain. Only error cases should be tests with unit tests going forward. +These include: -In some cases, we need to test things which cannot be generated—like validating that errors are caught and handled correctly. These should be tested via unit tests in the `tests` directory, using the `pytest` framework. +* Regular unit tests of basic pieces of fairly self-contained low-level functionality, such as helper functions. These are implemented in the `tests` directory, using the `pytest` framework. +* Older-style unit tests of low-level functions like `property_from_data` that have complex behavior. These are brittle and difficult to maintain, and should not be used going forward. Instead, they should be migrated to functional tests. ### Creating a Pull Request diff --git a/end_to_end_tests/__init__.py b/end_to_end_tests/__init__.py index 1bf33f63f..3793e0395 100644 --- a/end_to_end_tests/__init__.py +++ b/end_to_end_tests/__init__.py @@ -1 +1,5 @@ """ Generate a complete client and verify that it is correct """ +import pytest + +pytest.register_assert_rewrite("end_to_end_tests.end_to_end_test_helpers") +pytest.register_assert_rewrite("end_to_end_tests.functional_tests.helpers") diff --git a/end_to_end_tests/functional_tests/README.md b/end_to_end_tests/functional_tests/README.md new file mode 100644 index 000000000..1008527c5 --- /dev/null +++ b/end_to_end_tests/functional_tests/README.md @@ -0,0 +1,75 @@ +## The `functional_tests` module + +These are end-to-end tests which run the client generator against many small API documents that are specific to various test cases. + +Rather than testing low-level implementation details (like the unit tests in `tests`), or making assertions about the exact content of the generated code (like the "golden record"-based end-to-end tests), these treat both the generator and the generated code as black boxes and make assertions about their behavior. + +The tests are in two submodules: + +# `generated_code_execution` + +These tests use valid API specs, and after running the generator, they _import and execute_ pieces of the generated code to verify that it actually works at runtime. + +Each test class follows this pattern: + +- Use the decorator `@with_generated_client_fixture`, providing an inline API spec (JSON or YAML) that contains whatever schemas/paths/etc. are relevant to this test class. + - The spec can omit the `openapi:`, `info:`, and `paths:`, blocks, unless those are relevant to the test. + - The decorator creates a temporary file for the inline spec and a temporary directory for the generated code, and runs the client generator. + - It creates a `GeneratedClientContext` object (defined in `end_to_end_test_helpers.py`) to keep track of things like the location of the generated code and the output of the generator command. + - This object is injected into the test class as a fixture called `generated_client`, although most tests will not need to reference the fixture directly. + - `sys.path` is temporarily changed, for the scope of this test class, to allow imports from the generated code. +- Use the decorator `@with_generated_code_imports` or `@with_generated_code_import` to make classes or functions from the generated code available to the tests. + - `@with_generated_code_imports(".models.MyModel1", ".models.MyModel2)` would execute `from [package name].models import MyModel1, MyModel2` and inject the imported classes into the test class as fixtures called `MyModel1` and `MyModel2`. + - `@with_generated_code_import(".api.my_operation.sync", alias="endpoint_method")` would execute `from [package name].api.my_operation import sync`, but the fixture would be named `endpoint_method`. + - After the test class finishes, these imports are discarded. + +Example: + +```python +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + type: object + properties: + stringProp: {"type": "string"} +""") +@with_generated_code_import(".models.MyModel") +class TestSimpleJsonObject: + def test_encoding(self, MyModel): + instance = MyModel(string_prop="abc") + assert instance.to_dict() == {"stringProp": "abc"} +``` + +# `generator_failure_cases` + +These run the generator with an invalid API spec and make assertions about the warning/error output. Some of these invalid conditions are expected to only produce warnings about the affected schemas, while others are expected to produce fatal errors that terminate the generator. + +For warning conditions, each test class uses `@with_generated_client_fixture` as above, then uses `assert_bad_schema` to parse the output and check for a specific warning message for a specific schema name. + +```python +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + # some kind of invalid schema +""") +class TestBadSchema: + def test_encoding(self, generated_client): + assert_bad_schema(generated_client, "MyModel", "some expected warning text") +``` + +Or, for fatal error conditions: + +- Call `inline_spec_should_fail`, providing an inline API spec (JSON or YAML). + +```python +class TestBadSpec: + def test_some_spec_error(self): + result = inline_spec_should_fail(""" +# some kind of invalid spec +""") + assert "some expected error text" in result.output +``` diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_arrays.py b/end_to_end_tests/functional_tests/generated_code_execution/test_arrays.py new file mode 100644 index 000000000..443d764c5 --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_arrays.py @@ -0,0 +1,150 @@ +from typing import Any, ForwardRef, Union + +from end_to_end_tests.functional_tests.helpers import ( + assert_model_decode_encode, + assert_model_property_type_hint, + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + SimpleObject: + type: object + properties: + name: {"type": "string"} + ModelWithArrayOfAny: + properties: + arrayProp: + type: array + items: {} + ModelWithArrayOfInts: + properties: + arrayProp: + type: array + items: {"type": "integer"} + ModelWithArrayOfObjects: + properties: + arrayProp: + type: array + items: {"$ref": "#/components/schemas/SimpleObject"} +""") +@with_generated_code_imports( + ".models.ModelWithArrayOfAny", + ".models.ModelWithArrayOfInts", + ".models.ModelWithArrayOfObjects", + ".models.SimpleObject", + ".types.Unset", +) +class TestArraySchemas: + def test_array_of_any(self, ModelWithArrayOfAny): + assert_model_decode_encode( + ModelWithArrayOfAny, + {"arrayProp": ["a", 1]}, + ModelWithArrayOfAny(array_prop=["a", 1]), + ) + + def test_array_of_int(self, ModelWithArrayOfInts): + assert_model_decode_encode( + ModelWithArrayOfInts, + {"arrayProp": [1, 2]}, + ModelWithArrayOfInts(array_prop=[1, 2]), + ) + # Note, currently arrays of simple types are not validated, so the following assertion would fail: + # with pytest.raises(TypeError): + # ModelWithArrayOfInt.from_dict({"arrayProp": [1, "a"]}) + + def test_array_of_object(self, ModelWithArrayOfObjects, SimpleObject): + assert_model_decode_encode( + ModelWithArrayOfObjects, + {"arrayProp": [{"name": "a"}, {"name": "b"}]}, + ModelWithArrayOfObjects(array_prop=[SimpleObject(name="a"), SimpleObject(name="b")]), + ) + + def test_type_hints(self, ModelWithArrayOfAny, ModelWithArrayOfInts, ModelWithArrayOfObjects, Unset): + assert_model_property_type_hint(ModelWithArrayOfAny, "array_prop", Union[list[Any], Unset]) + assert_model_property_type_hint(ModelWithArrayOfInts, "array_prop", Union[list[int], Unset]) + assert_model_property_type_hint(ModelWithArrayOfObjects, "array_prop", Union[list["SimpleObject"], Unset]) + + +@with_generated_client_fixture( +""" +components: + schemas: + SimpleObject: + type: object + properties: + name: {"type": "string"} + ModelWithSinglePrefixItem: + type: object + properties: + arrayProp: + type: array + prefixItems: + - type: string + ModelWithPrefixItems: + type: object + properties: + arrayProp: + type: array + prefixItems: + - $ref: "#/components/schemas/SimpleObject" + - type: string + ModelWithMixedItems: + type: object + properties: + arrayProp: + type: array + prefixItems: + - $ref: "#/components/schemas/SimpleObject" + items: + type: string +""") +@with_generated_code_imports( + ".models.ModelWithSinglePrefixItem", + ".models.ModelWithPrefixItems", + ".models.ModelWithMixedItems", + ".models.SimpleObject", + ".types.Unset", +) +class TestArraysWithPrefixItems: + def test_single_prefix_item(self, ModelWithSinglePrefixItem): + assert_model_decode_encode( + ModelWithSinglePrefixItem, + {"arrayProp": ["a"]}, + ModelWithSinglePrefixItem(array_prop=["a"]), + ) + + def test_prefix_items(self, ModelWithPrefixItems, SimpleObject): + assert_model_decode_encode( + ModelWithPrefixItems, + {"arrayProp": [{"name": "a"}, "b"]}, + ModelWithPrefixItems(array_prop=[SimpleObject(name="a"), "b"]), + ) + + def test_prefix_items_and_regular_items(self, ModelWithMixedItems, SimpleObject): + assert_model_decode_encode( + ModelWithMixedItems, + {"arrayProp": [{"name": "a"}, "b"]}, + ModelWithMixedItems(array_prop=[SimpleObject(name="a"), "b"]), + ) + + def test_type_hints(self, ModelWithSinglePrefixItem, ModelWithPrefixItems, ModelWithMixedItems, Unset): + assert_model_property_type_hint(ModelWithSinglePrefixItem, "array_prop", Union[list[str], Unset]) + assert_model_property_type_hint( + ModelWithPrefixItems, + "array_prop", + Union[list[Union[ForwardRef("SimpleObject"), str]], Unset], + ) + assert_model_property_type_hint( + ModelWithMixedItems, + "array_prop", + Union[list[Union[ForwardRef("SimpleObject"), str]], Unset], + ) + # Note, this test is asserting the current behavior which, due to limitations of the implementation + # (see: https://github.com/openapi-generators/openapi-python-client/pull/1130), is not really doing + # tuple type validation-- the ordering of prefixItems is ignored, and instead all of the types are + # simply treated as a union. diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py b/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py new file mode 100644 index 000000000..5f8affb25 --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py @@ -0,0 +1,114 @@ +import datetime +import uuid + +from end_to_end_tests.functional_tests.helpers import ( + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + type: object + properties: + booleanProp: {"type": "boolean", "default": true} + stringProp: {"type": "string", "default": "a"} + numberProp: {"type": "number", "default": 1.5} + intProp: {"type": "integer", "default": 2} + dateProp: {"type": "string", "format": "date", "default": "2024-01-02"} + dateTimeProp: {"type": "string", "format": "date-time", "default": "2024-01-02T03:04:05Z"} + uuidProp: {"type": "string", "format": "uuid", "default": "07EF8B4D-AA09-4FFA-898D-C710796AFF41"} + anyPropWithString: {"default": "b"} + anyPropWithInt: {"default": 3} + booleanWithStringTrue1: {"type": "boolean", "default": "True"} + booleanWithStringTrue2: {"type": "boolean", "default": "true"} + booleanWithStringFalse1: {"type": "boolean", "default": "False"} + booleanWithStringFalse2: {"type": "boolean", "default": "false"} + intWithStringValue: {"type": "integer", "default": "4"} + numberWithIntValue: {"type": "number", "default": 5} + numberWithStringValue: {"type": "number", "default": "5.5"} + stringWithNumberValue: {"type": "string", "default": 6} + stringConst: {"type": "string", "const": "always", "default": "always"} + unionWithValidDefaultForType1: + anyOf: [{"type": "boolean"}, {"type": "integer"}] + default: true + unionWithValidDefaultForType2: + anyOf: [{"type": "boolean"}, {"type": "integer"}] + default: 3 +""") +@with_generated_code_imports(".models.MyModel") +class TestSimpleDefaults: + # Note, the null/None type is not covered here due to a known bug: + # https://github.com/openapi-generators/openapi-python-client/issues/1162 + def test_defaults_in_initializer(self, MyModel): + instance = MyModel() + assert instance == MyModel( + boolean_prop=True, + string_prop="a", + number_prop=1.5, + int_prop=2, + date_prop=datetime.date(2024, 1, 2), + date_time_prop=datetime.datetime(2024, 1, 2, 3, 4, 5, tzinfo=datetime.timezone.utc), + uuid_prop=uuid.UUID("07EF8B4D-AA09-4FFA-898D-C710796AFF41"), + any_prop_with_string="b", + any_prop_with_int=3, + boolean_with_string_true_1=True, + boolean_with_string_true_2=True, + boolean_with_string_false_1=False, + boolean_with_string_false_2=False, + int_with_string_value=4, + number_with_int_value=5, + number_with_string_value=5.5, + string_with_number_value="6", + string_const="always", + union_with_valid_default_for_type_1=True, + union_with_valid_default_for_type_2=3, + ) + + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "b"] + MyModel: + type: object + properties: + enumProp: + allOf: + - $ref: "#/components/schemas/MyEnum" + default: "a" + +""") +@with_generated_code_imports(".models.MyEnum", ".models.MyModel") +class TestEnumDefaults: + def test_enum_default(self, MyEnum, MyModel): + assert MyModel().enum_prop == MyEnum.A + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "A"] + MyModel: + properties: + enumProp: + allOf: + - $ref: "#/components/schemas/MyEnum" + default: A +""", + config="literal_enums: true", +) +@with_generated_code_imports(".models.MyModel") +class TestLiteralEnumDefaults: + def test_default_value(self, MyModel): + assert MyModel().enum_prop == "A" diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_docstrings.py b/end_to_end_tests/functional_tests/generated_code_execution/test_docstrings.py new file mode 100644 index 000000000..d2d560780 --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_docstrings.py @@ -0,0 +1,163 @@ +from typing import Any + +from end_to_end_tests.functional_tests.helpers import ( + with_generated_code_import, + with_generated_client_fixture, +) + + +class DocstringParser: + lines: list[str] + + def __init__(self, item: Any): + self.lines = [line.lstrip() for line in item.__doc__.split("\n")] + + def get_section(self, header_line: str) -> list[str]: + lines = self.lines[self.lines.index(header_line)+1:] + return lines[0:lines.index("")] + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + description: I like this type. + type: object + properties: + reqStr: + type: string + description: This is necessary. + optStr: + type: string + description: This isn't necessary. + undescribedProp: + type: string + required: ["reqStr", "undescribedProp"] +""") +@with_generated_code_import(".models.MyModel") +class TestSchemaDocstrings: + def test_model_description(self, MyModel): + assert DocstringParser(MyModel).lines[0] == "I like this type." + + def test_model_properties(self, MyModel): + assert set(DocstringParser(MyModel).get_section("Attributes:")) == { + "req_str (str): This is necessary.", + "opt_str (Union[Unset, str]): This isn't necessary.", + "undescribed_prop (str):", + } + + +@with_generated_client_fixture( +""" +tags: + - name: service1 +paths: + "/simple": + get: + operationId: getSimpleThing + description: Get a simple thing. + responses: + "200": + description: Success! + content: + application/json: + schema: + $ref: "#/components/schemas/GoodResponse" + tags: + - service1 + post: + operationId: postSimpleThing + description: Post a simple thing. + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Thing" + responses: + "200": + description: Success! + content: + application/json: + schema: + $ref: "#/components/schemas/GoodResponse" + "400": + description: Failure!! + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + tags: + - service1 + "/simple/{id}/{index}": + get: + operationId: getAttributeByIndex + description: Get a simple thing's attribute. + parameters: + - name: id + in: path + required: true + schema: + type: string + description: Which one. + - name: index + in: path + required: true + schema: + type: integer + - name: fries + in: query + required: false + schema: + type: boolean + description: Do you want fries with that? + responses: + "200": + description: Success! + content: + application/json: + schema: + $ref: "#/components/schemas/GoodResponse" + tags: + - service1 + +components: + schemas: + GoodResponse: + type: object + ErrorResponse: + type: object + Thing: + type: object + description: The thing. +""") +@with_generated_code_import(".api.service1.get_simple_thing.sync", alias="get_simple_thing_sync") +@with_generated_code_import(".api.service1.post_simple_thing.sync", alias="post_simple_thing_sync") +@with_generated_code_import(".api.service1.get_attribute_by_index.sync", alias="get_attribute_by_index_sync") +class TestEndpointDocstrings: + def test_description(self, get_simple_thing_sync): + assert DocstringParser(get_simple_thing_sync).lines[0] == "Get a simple thing." + + def test_response_single_type(self, get_simple_thing_sync): + assert DocstringParser(get_simple_thing_sync).get_section("Returns:") == [ + "GoodResponse", + ] + + def test_response_union_type(self, post_simple_thing_sync): + returns_line = DocstringParser(post_simple_thing_sync).get_section("Returns:")[0] + assert returns_line in ( + "Union[GoodResponse, ErrorResponse]", + "Union[ErrorResponse, GoodResponse]", + ) + + def test_request_body(self, post_simple_thing_sync): + assert DocstringParser(post_simple_thing_sync).get_section("Args:") == [ + "body (Thing): The thing." + ] + + def test_params(self, get_attribute_by_index_sync): + assert DocstringParser(get_attribute_by_index_sync).get_section("Args:") == [ + "id (str): Which one.", + "index (int):", + "fries (Union[Unset, bool]): Do you want fries with that?", + ] diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py b/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py new file mode 100644 index 000000000..89dbef7dc --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py @@ -0,0 +1,337 @@ +from typing import Literal, Union +import pytest + +from end_to_end_tests.functional_tests.helpers import ( + assert_model_decode_encode, + assert_model_property_type_hint, + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "B", "a23", "123", "1bc", "a Thing WIth spaces", ""] + MyModel: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + inlineEnumProp: + type: string + enum: ["a", "b"] + MyModelWithRequired: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + required: ["enumProp"] +""") +@with_generated_code_imports( + ".models.MyEnum", + ".models.MyModel", + ".models.MyModelInlineEnumProp", + ".models.MyModelWithRequired", + ".types.Unset", +) +class TestStringEnumClass: + @pytest.mark.parametrize( + "expected_name,expected_value", + [ + ("A", "a"), + ("B", "B"), + ("A23", "a23"), + ("VALUE_3", "123"), + ("VALUE_4", "1bc"), + ("A_THING_WITH_SPACES", "a Thing WIth spaces"), + ("VALUE_6", ""), + ], + ) + def test_enum_values(self, MyEnum, expected_name, expected_value): + assert getattr(MyEnum, expected_name) == MyEnum(expected_value) + + def test_enum_prop_in_object(self, MyEnum, MyModel, MyModelInlineEnumProp): + assert_model_decode_encode(MyModel, {"enumProp": "B"}, MyModel(enum_prop=MyEnum.B)) + assert_model_decode_encode( + MyModel, + {"inlineEnumProp": "a"}, + MyModel(inline_enum_prop=MyModelInlineEnumProp.A), + ) + + def test_type_hints(self, MyModel, MyModelWithRequired, MyEnum, Unset): + optional_type = Union[Unset, MyEnum] + assert_model_property_type_hint(MyModel,"enum_prop", optional_type) + assert_model_property_type_hint(MyModelWithRequired, "enum_prop", MyEnum) + + def test_invalid_values(self, MyModel): + with pytest.raises(ValueError): + MyModel.from_dict({"enumProp": "c"}) + with pytest.raises(ValueError): + MyModel.from_dict({"enumProp": "A"}) + with pytest.raises(ValueError): + MyModel.from_dict({"enumProp": 2}) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: integer + enum: [2, 3, -4] + MyModel: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + inlineEnumProp: + type: string + enum: [2, 3] + MyModelWithRequired: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + required: ["enumProp"] +""") +@with_generated_code_imports( + ".models.MyEnum", + ".models.MyModel", + ".models.MyModelInlineEnumProp", + ".models.MyModelWithRequired", + ".types.Unset", +) +class TestIntEnumClass: + @pytest.mark.parametrize( + "expected_name,expected_value", + [ + ("VALUE_2", 2), + ("VALUE_3", 3), + ("VALUE_NEGATIVE_4", -4), + ], + ) + def test_enum_values(self, MyEnum, expected_name, expected_value): + assert getattr(MyEnum, expected_name) == MyEnum(expected_value) + + def test_enum_prop_in_object(self, MyEnum, MyModel, MyModelInlineEnumProp): + assert_model_decode_encode(MyModel, {"enumProp": 2}, MyModel(enum_prop=MyEnum.VALUE_2)) + assert_model_decode_encode( + MyModel, + {"inlineEnumProp": 2}, + MyModel(inline_enum_prop=MyModelInlineEnumProp.VALUE_2), + ) + + def test_type_hints(self, MyModel, MyModelWithRequired, MyEnum, Unset): + optional_type = Union[Unset, MyEnum] + assert_model_property_type_hint(MyModel,"enum_prop", optional_type) + assert_model_property_type_hint(MyModelWithRequired, "enum_prop", MyEnum) + + def test_invalid_values(self, MyModel): + with pytest.raises(ValueError): + MyModel.from_dict({"enumProp": 5}) + with pytest.raises(ValueError): + MyModel.from_dict({"enumProp": "a"}) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "b"] + MyEnumIncludingNull: + type: ["string", "null"] + enum: ["a", "b", null] + MyNullOnlyEnum: + enum: [null] + MyModel: + properties: + nullableEnumProp: + oneOf: + - {"$ref": "#/components/schemas/MyEnum"} + - type: "null" + enumIncludingNullProp: {"$ref": "#/components/schemas/MyEnumIncludingNull"} + nullOnlyEnumProp: {"$ref": "#/components/schemas/MyNullOnlyEnum"} +""") +@with_generated_code_imports( + ".models.MyEnum", + ".models.MyEnumIncludingNullType1", # see comment in test_nullable_enum_prop + ".models.MyModel", + ".types.Unset", +) +class TestNullableEnums: + def test_nullable_enum_prop(self, MyModel, MyEnum, MyEnumIncludingNullType1): + # Note, MyEnumIncludingNullType1 should be named just MyEnumIncludingNull - + # known bug: https://github.com/openapi-generators/openapi-python-client/issues/1120 + assert_model_decode_encode(MyModel, {"nullableEnumProp": "b"}, MyModel(nullable_enum_prop=MyEnum.B)) + assert_model_decode_encode(MyModel, {"nullableEnumProp": None}, MyModel(nullable_enum_prop=None)) + assert_model_decode_encode( + MyModel, + {"enumIncludingNullProp": "a"}, + MyModel(enum_including_null_prop=MyEnumIncludingNullType1.A), + ) + assert_model_decode_encode( MyModel, {"enumIncludingNullProp": None}, MyModel(enum_including_null_prop=None)) + assert_model_decode_encode(MyModel, {"nullOnlyEnumProp": None}, MyModel(null_only_enum_prop=None)) + + def test_type_hints(self, MyModel, MyEnum, Unset): + expected_type = Union[MyEnum, None, Unset] + assert_model_property_type_hint(MyModel, "nullable_enum_prop", expected_type) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + properties: + mustBeErnest: + const: Ernest + mustBeThirty: + const: 30 +""", +) +@with_generated_code_imports(".models.MyModel") +class TestConst: + def test_valid_string(self, MyModel): + assert_model_decode_encode( + MyModel, + {"mustBeErnest": "Ernest"}, + MyModel(must_be_ernest="Ernest"), + ) + + def test_valid_int(self, MyModel): + assert_model_decode_encode( + MyModel, + {"mustBeThirty": 30}, + MyModel(must_be_thirty=30), + ) + + def test_invalid_string(self, MyModel): + with pytest.raises(ValueError): + MyModel.from_dict({"mustBeErnest": "Jack"}) + + def test_invalid_int(self, MyModel): + with pytest.raises(ValueError): + MyModel.from_dict({"mustBeThirty": 29}) + + +# The following tests of literal enums use basically the same specs as the tests above, but +# the "literal_enums" option is enabled in the test configuration. + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "A", "b"] + MyModel: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + inlineEnumProp: + type: string + enum: ["a", "b"] + MyModelWithRequired: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + required: ["enumProp"] +""", + config="literal_enums: true", +) +@with_generated_code_imports( + ".models.MyModel", + ".models.MyModelWithRequired", + ".types.Unset", +) +class TestStringLiteralEnum: + def test_enum_prop(self, MyModel): + assert_model_decode_encode(MyModel, {"enumProp": "a"}, MyModel(enum_prop="a")) + assert_model_decode_encode(MyModel, {"enumProp": "A"}, MyModel(enum_prop="A")) + assert_model_decode_encode(MyModel, {"inlineEnumProp": "a"}, MyModel(inline_enum_prop="a")) + + def test_type_hints(self, MyModel, MyModelWithRequired, Unset): + literal_type = Literal["a", "A", "b"] + optional_type = Union[Unset, literal_type] + assert_model_property_type_hint(MyModel, "enum_prop", optional_type) + assert_model_property_type_hint(MyModelWithRequired, "enum_prop", literal_type) + + def test_invalid_values(self, MyModel): + with pytest.raises(TypeError): + MyModel.from_dict({"enumProp": "c"}) + with pytest.raises(TypeError): + MyModel.from_dict({"enumProp": 2}) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: integer + enum: [2, 3, -4] + MyModel: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + inlineEnumProp: + type: string + enum: [2, 3] + MyModelWithRequired: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + required: ["enumProp"] +""", + config="literal_enums: true", +) +@with_generated_code_imports( + ".models.MyModel", + ".models.MyModelWithRequired", + ".types.Unset", +) +class TestIntLiteralEnum: + def test_enum_prop(self, MyModel): + assert_model_decode_encode(MyModel, {"enumProp": 2}, MyModel(enum_prop=2)) + assert_model_decode_encode(MyModel, {"enumProp": -4}, MyModel(enum_prop=-4)) + assert_model_decode_encode(MyModel, {"inlineEnumProp": 2}, MyModel(inline_enum_prop=2)) + + def test_type_hints(self, MyModel, MyModelWithRequired, Unset): + literal_type = Literal[2, 3, -4] + optional_type = Union[Unset, literal_type] + assert_model_property_type_hint(MyModel, "enum_prop", optional_type) + assert_model_property_type_hint(MyModelWithRequired, "enum_prop", literal_type) + + def test_invalid_values(self, MyModel): + with pytest.raises(TypeError): + MyModel.from_dict({"enumProp": 4}) + with pytest.raises(TypeError): + MyModel.from_dict({"enumProp": "a"}) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyEnum: + type: string + enum: ["a", "A"] + MyEnumIncludingNull: + type: ["string", "null"] + enum: ["a", "b", null] + MyNullOnlyEnum: + enum: [null] + MyModel: + properties: + enumProp: {"$ref": "#/components/schemas/MyEnum"} + nullableEnumProp: + oneOf: + - {"$ref": "#/components/schemas/MyEnum"} + - type: "null" + enumIncludingNullProp: {"$ref": "#/components/schemas/MyEnumIncludingNull"} + nullOnlyEnumProp: {"$ref": "#/components/schemas/MyNullOnlyEnum"} +""", + config="literal_enums: true", +) +@with_generated_code_imports(".models.MyModel") +class TestNullableLiteralEnum: + def test_nullable_enum_prop(self, MyModel): + assert_model_decode_encode(MyModel, {"nullableEnumProp": "B"}, MyModel(nullable_enum_prop="B")) + assert_model_decode_encode(MyModel, {"nullableEnumProp": None}, MyModel(nullable_enum_prop=None)) + assert_model_decode_encode(MyModel, {"enumIncludingNullProp": "a"}, MyModel(enum_including_null_prop="a")) + assert_model_decode_encode(MyModel, {"enumIncludingNullProp": None}, MyModel(enum_including_null_prop=None)) + assert_model_decode_encode(MyModel, {"nullOnlyEnumProp": None}, MyModel(null_only_enum_prop=None)) diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_properties.py b/end_to_end_tests/functional_tests/generated_code_execution/test_properties.py new file mode 100644 index 000000000..e1cfce9a5 --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_properties.py @@ -0,0 +1,186 @@ +import datetime +from typing import Any, ForwardRef, Union +import uuid +import pytest + +from end_to_end_tests.functional_tests.helpers import ( + assert_model_decode_encode, + assert_model_property_type_hint, + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + type: object + properties: + req1: {"type": "string"} + req2: {"type": "string"} + opt: {"type": "string"} + required: ["req1", "req2"] + DerivedModel: + allOf: + - $ref: "#/components/schemas/MyModel" + - type: object + properties: + req3: {"type": "string"} + required: ["req3"] +""") +@with_generated_code_imports( + ".models.MyModel", + ".models.DerivedModel", + ".types.Unset", +) +class TestRequiredAndOptionalProperties: + def test_required_ok(self, MyModel, DerivedModel): + assert_model_decode_encode( + MyModel, + {"req1": "a", "req2": "b"}, + MyModel(req1="a", req2="b"), + ) + assert_model_decode_encode( + DerivedModel, + {"req1": "a", "req2": "b", "req3": "c"}, + DerivedModel(req1="a", req2="b", req3="c"), + ) + + def test_required_and_optional(self, MyModel, DerivedModel): + assert_model_decode_encode( + MyModel, + {"req1": "a", "req2": "b", "opt": "c"}, + MyModel(req1="a", req2="b", opt="c"), + ) + assert_model_decode_encode( + DerivedModel, + {"req1": "a", "req2": "b", "req3": "c", "opt": "d"}, + DerivedModel(req1="a", req2="b", req3="c", opt="d"), + ) + + def test_required_missing(self, MyModel, DerivedModel): + with pytest.raises(KeyError): + MyModel.from_dict({"req1": "a"}) + with pytest.raises(KeyError): + MyModel.from_dict({"req2": "b"}) + with pytest.raises(KeyError): + DerivedModel.from_dict({"req1": "a", "req2": "b"}) + + def test_type_hints(self, MyModel, Unset): + assert_model_property_type_hint(MyModel, "req1", str) + assert_model_property_type_hint(MyModel, "opt", Union[str, Unset]) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + type: object + properties: + booleanProp: {"type": "boolean"} + stringProp: {"type": "string"} + numberProp: {"type": "number"} + intProp: {"type": "integer"} + anyObjectProp: {"$ref": "#/components/schemas/AnyObject"} + nullProp: {"type": "null"} + anyProp: {} + AnyObject: + type: object +""") +@with_generated_code_imports( + ".models.MyModel", + ".models.AnyObject", + ".types.Unset", +) +class TestBasicModelProperties: + def test_decode_encode(self, MyModel, AnyObject): + json_data = { + "booleanProp": True, + "stringProp": "a", + "numberProp": 1.5, + "intProp": 2, + "anyObjectProp": {"d": 3}, + "nullProp": None, + "anyProp": "e" + } + expected_any_object = AnyObject() + expected_any_object.additional_properties = {"d": 3} + assert_model_decode_encode( + MyModel, + json_data, + MyModel( + boolean_prop=True, + string_prop="a", + number_prop=1.5, + int_prop=2, + any_object_prop = expected_any_object, + null_prop=None, + any_prop="e", + ) + ) + + @pytest.mark.parametrize( + "bad_data", + ["a", True, 2, None], + ) + def test_decode_error_not_object(self, bad_data, MyModel): + with pytest.raises(Exception): + # Exception is overly broad, but unfortunately in the current implementation, the error + # being raised is AttributeError (because it tries to call bad_data.copy()) which isn't + # very meaningful + MyModel.from_dict(bad_data) + + def test_type_hints(self, MyModel, Unset): + assert_model_property_type_hint(MyModel, "boolean_prop", Union[bool, Unset]) + assert_model_property_type_hint(MyModel, "string_prop", Union[str, Unset]) + assert_model_property_type_hint(MyModel, "number_prop", Union[float, Unset]) + assert_model_property_type_hint(MyModel, "int_prop", Union[int, Unset]) + assert_model_property_type_hint(MyModel, "any_object_prop", Union[ForwardRef("AnyObject"), Unset]) + assert_model_property_type_hint(MyModel, "null_prop", Union[None, Unset]) + assert_model_property_type_hint(MyModel, "any_prop", Union[Any, Unset]) + + +@with_generated_client_fixture( +""" +components: + schemas: + MyModel: + type: object + properties: + dateProp: {"type": "string", "format": "date"} + dateTimeProp: {"type": "string", "format": "date-time"} + uuidProp: {"type": "string", "format": "uuid"} + unknownFormatProp: {"type": "string", "format": "weird"} +""") +@with_generated_code_imports( + ".models.MyModel", + ".types.Unset", +) +class TestSpecialStringFormats: + def test_date(self, MyModel): + date_value = datetime.date.today() + json_data = {"dateProp": date_value.isoformat()} + assert_model_decode_encode(MyModel, json_data, MyModel(date_prop=date_value)) + + def test_date_time(self, MyModel): + date_time_value = datetime.datetime.now(datetime.timezone.utc) + json_data = {"dateTimeProp": date_time_value.isoformat()} + assert_model_decode_encode(MyModel, json_data, MyModel(date_time_prop=date_time_value)) + + def test_uuid(self, MyModel): + uuid_value = uuid.uuid1() + json_data = {"uuidProp": str(uuid_value)} + assert_model_decode_encode(MyModel, json_data, MyModel(uuid_prop=uuid_value)) + + def test_unknown_format(self, MyModel): + json_data = {"unknownFormatProp": "whatever"} + assert_model_decode_encode(MyModel, json_data, MyModel(unknown_format_prop="whatever")) + + def test_type_hints(self, MyModel, Unset): + assert_model_property_type_hint(MyModel, "date_prop", Union[datetime.date, Unset]) + assert_model_property_type_hint(MyModel, "date_time_prop", Union[datetime.datetime, Unset]) + assert_model_property_type_hint(MyModel, "uuid_prop", Union[uuid.UUID, Unset]) + assert_model_property_type_hint(MyModel, "unknown_format_prop", Union[str, Unset]) diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_unions.py b/end_to_end_tests/functional_tests/generated_code_execution/test_unions.py new file mode 100644 index 000000000..9a9b49e4c --- /dev/null +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_unions.py @@ -0,0 +1,150 @@ +from typing import ForwardRef, Union + +from end_to_end_tests.functional_tests.helpers import ( + assert_model_decode_encode, + assert_model_property_type_hint, + with_generated_client_fixture, + with_generated_code_imports, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + StringOrInt: + type: ["string", "integer"] + MyModel: + type: object + properties: + stringOrIntProp: + type: ["string", "integer"] +""" +) +@with_generated_code_imports( + ".models.MyModel", + ".types.Unset" +) +class TestSimpleTypeList: + def test_decode_encode(self, MyModel): + assert_model_decode_encode(MyModel, {"stringOrIntProp": "a"}, MyModel(string_or_int_prop="a")) + assert_model_decode_encode(MyModel, {"stringOrIntProp": 1}, MyModel(string_or_int_prop=1)) + + def test_type_hints(self, MyModel, Unset): + assert_model_property_type_hint(MyModel, "string_or_int_prop", Union[str, int, Unset]) + + +@with_generated_client_fixture( +""" +components: + schemas: + ThingA: + type: object + properties: + propA: { type: "string" } + required: ["propA"] + ThingB: + type: object + properties: + propB: { type: "string" } + required: ["propB"] + ThingAOrB: + oneOf: + - $ref: "#/components/schemas/ThingA" + - $ref: "#/components/schemas/ThingB" + ModelWithUnion: + type: object + properties: + thing: {"$ref": "#/components/schemas/ThingAOrB"} + thingOrString: + oneOf: + - $ref: "#/components/schemas/ThingA" + - type: string + ModelWithRequiredUnion: + type: object + properties: + thing: {"$ref": "#/components/schemas/ThingAOrB"} + required: ["thing"] + ModelWithNestedUnion: + type: object + properties: + thingOrValue: + oneOf: + - "$ref": "#/components/schemas/ThingAOrB" + - oneOf: + - type: string + - type: number + ModelWithUnionOfOne: + type: object + properties: + thing: + oneOf: + - $ref: "#/components/schemas/ThingA" + requiredThing: + oneOf: + - $ref: "#/components/schemas/ThingA" + required: ["requiredThing"] +""") +@with_generated_code_imports( + ".models.ThingA", + ".models.ThingB", + ".models.ModelWithUnion", + ".models.ModelWithRequiredUnion", + ".models.ModelWithNestedUnion", + ".models.ModelWithUnionOfOne", + ".types.Unset" +) +class TestOneOf: + def test_disambiguate_objects_via_required_properties(self, ThingA, ThingB, ModelWithUnion): + assert_model_decode_encode( + ModelWithUnion, + {"thing": {"propA": "x"}}, + ModelWithUnion(thing=ThingA(prop_a="x")), + ) + assert_model_decode_encode( + ModelWithUnion, + {"thing": {"propB": "x"}}, + ModelWithUnion(thing=ThingB(prop_b="x")), + ) + + def test_disambiguate_object_and_non_object(self, ThingA, ModelWithUnion): + assert_model_decode_encode( + ModelWithUnion, + {"thingOrString": {"propA": "x"}}, + ModelWithUnion(thing_or_string=ThingA(prop_a="x")), + ) + assert_model_decode_encode( + ModelWithUnion, + {"thingOrString": "x"}, + ModelWithUnion(thing_or_string="x"), + ) + + def test_disambiguate_nested_union(self, ThingA, ThingB, ModelWithNestedUnion): + assert_model_decode_encode( + ModelWithNestedUnion, + {"thingOrValue": {"propA": "x"}}, + ModelWithNestedUnion(thing_or_value=ThingA(prop_a="x")), + ) + assert_model_decode_encode( + ModelWithNestedUnion, + {"thingOrValue": 3}, + ModelWithNestedUnion(thing_or_value=3), + ) + + def test_type_hints(self, ModelWithUnion, ModelWithRequiredUnion, ModelWithUnionOfOne, ThingA, Unset): + assert_model_property_type_hint( + ModelWithUnion, + "thing", + Union[ForwardRef("ThingA"), ForwardRef("ThingB"), Unset], + ) + assert_model_property_type_hint( + ModelWithRequiredUnion, + "thing", + Union[ForwardRef("ThingA"), ForwardRef("ThingB")], + ) + assert_model_property_type_hint( + ModelWithUnionOfOne, "thing", Union[ForwardRef("ThingA"), Unset] + ) + assert_model_property_type_hint( + ModelWithUnionOfOne, "required_thing", "ThingA" + ) diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_arrays.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_arrays.py new file mode 100644 index 000000000..e4ef0cffd --- /dev/null +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_arrays.py @@ -0,0 +1,23 @@ +import pytest + +from end_to_end_tests.functional_tests.helpers import assert_bad_schema, with_generated_client_fixture + + +@with_generated_client_fixture( +""" +components: + schemas: + ArrayWithNoItems: + type: array + ArrayWithInvalidItemsRef: + type: array + items: + $ref: "#/components/schemas/DoesntExist" +""" +) +class TestArrayInvalidSchemas: + def test_no_items(self, generated_client): + assert_bad_schema(generated_client, "ArrayWithNoItems", "must have items or prefixItems defined") + + def test_invalid_items_ref(self, generated_client): + assert_bad_schema(generated_client, "ArrayWithInvalidItemsRef", "invalid data in items of array") diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py new file mode 100644 index 000000000..93f5e11d4 --- /dev/null +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py @@ -0,0 +1,88 @@ +import pytest + +from end_to_end_tests.functional_tests.helpers import assert_bad_schema, with_generated_client_fixture + + +@with_generated_client_fixture( +""" +components: + schemas: + WithBadBoolean: + properties: + badBoolean: {"type": "boolean", "default": "not a boolean"} + WithBadIntAsString: + properties: + badInt: {"type": "integer", "default": "not an int"} + WithBadIntAsOther: + properties: + badInt: {"type": "integer", "default": true} + WithBadFloatAsString: + properties: + badInt: {"type": "number", "default": "not a number"} + WithBadFloatAsOther: + properties: + badInt: {"type": "number", "default": true} + WithBadDateAsString: + properties: + badDate: {"type": "string", "format": "date", "default": "xxx"} + WithBadDateAsOther: + properties: + badDate: {"type": "string", "format": "date", "default": 3} + WithBadDateTimeAsString: + properties: + badDate: {"type": "string", "format": "date-time", "default": "xxx"} + WithBadDateTimeAsOther: + properties: + badDate: {"type": "string", "format": "date-time", "default": 3} + WithBadUuidAsString: + properties: + badUuid: {"type": "string", "format": "uuid", "default": "xxx"} + WithBadUuidAsOther: + properties: + badUuid: {"type": "string", "format": "uuid", "default": 3} + WithBadEnum: + properties: + badEnum: {"type": "string", "enum": ["a", "b"], "default": "x"} + GoodEnum: + type: string + enum: ["a", "b"] + OverriddenEnumWithBadDefault: + properties: + badEnum: + allOf: + - $ref: "#/components/schemas/GoodEnum" + default: "x" + UnionWithNoValidDefault: + properties: + badBoolOrInt: + anyOf: + - type: boolean + - type: integer + default: "xxx" +""" +) +class TestInvalidDefaultValues: + # Note, the null/None type, and binary strings (files), are not covered here due to a known bug: + # https://github.com/openapi-generators/openapi-python-client/issues/1162 + + @pytest.mark.parametrize( + ("model_name", "message"), + [ + ("WithBadBoolean", "Invalid boolean value"), + ("WithBadIntAsString", "Invalid int value"), + ("WithBadIntAsOther", "Invalid int value"), + ("WithBadFloatAsString", "Invalid float value"), + ("WithBadFloatAsOther", "Cannot convert True to a float"), + ("WithBadDateAsString", "Invalid date"), + ("WithBadDateAsOther", "Cannot convert 3 to a date"), + ("WithBadDateTimeAsString", "Invalid datetime"), + ("WithBadDateTimeAsOther", "Cannot convert 3 to a datetime"), + ("WithBadUuidAsString", "Invalid UUID value"), + ("WithBadUuidAsOther", "Invalid UUID value"), + ("WithBadEnum", "Value x is not valid for enum"), + ("OverriddenEnumWithBadDefault", "Value x is not valid for enum"), + ("UnionWithNoValidDefault", "Invalid int value"), + ] + ) + def test_bad_default_warning(self, model_name, message, generated_client): + assert_bad_schema(generated_client, model_name, message) diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py new file mode 100644 index 000000000..7f1586f29 --- /dev/null +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py @@ -0,0 +1,128 @@ +from end_to_end_tests.functional_tests.helpers import ( + assert_bad_schema, + inline_spec_should_fail, + with_generated_client_fixture, +) + + +@with_generated_client_fixture( +""" +components: + schemas: + WithBadDefaultValue: + enum: ["A"] + default: "B" + WithBadDefaultType: + enum: ["A"] + default: 123 + WithMixedTypes: + enum: ["A", 1] + WithUnsupportedType: + enum: [1.4, 1.5] + DefaultNotMatchingConst: + const: "aaa" + default: "bbb" + WithConflictingInlineNames: + type: object + properties: + "12": + enum: ["a", "b"] + WithConflictingInlineNames1: + type: object + properties: + "2": + enum: ["c", "d"] +""" +) +class TestEnumAndConstInvalidSchemas: + def test_enum_bad_default_value(self, generated_client): + assert_bad_schema(generated_client, "WithBadDefaultValue", "Value B is not valid") + + def test_enum_bad_default_type(self, generated_client): + assert_bad_schema(generated_client, "WithBadDefaultType", "Cannot convert 123 to enum") + + def test_enum_mixed_types(self, generated_client): + assert_bad_schema(generated_client, "WithMixedTypes", "Enum values must all be the same type") + + def test_enum_unsupported_type(self, generated_client): + assert_bad_schema(generated_client, "WithUnsupportedType", "Unsupported enum type") + + def test_const_default_not_matching(self, generated_client): + assert_bad_schema(generated_client, "DefaultNotMatchingConst", "Invalid value for const") + + def test_conflicting_inline_class_names(self, generated_client): + assert "Found conflicting enums named WithConflictingInlineNames12 with incompatible values" in generated_client.generator_result.output + + def test_enum_duplicate_values(self): + # This one currently causes a full generator failure rather than a warning + result = inline_spec_should_fail( +""" +components: + schemas: + WithDuplicateValues: + enum: ["x", "x"] +""" + ) + assert "Duplicate key X in enum" in str(result.exception) + + +@with_generated_client_fixture( +""" +components: + schemas: + WithBadDefaultValue: + enum: ["A"] + default: "B" + WithBadDefaultType: + enum: ["A"] + default: 123 + WithMixedTypes: + enum: ["A", 1] + WithUnsupportedType: + enum: [1.4, 1.5] + DefaultNotMatchingConst: + const: "aaa" + default: "bbb" + WithConflictingInlineNames: + type: object + properties: + "12": + enum: ["a", "b"] + WithConflictingInlineNames1: + type: object + properties: + "2": + enum: ["c", "d"] +""", + config="literal_enums: true", +) +class TestLiteralEnumInvalidSchemas: + def test_literal_enum_bad_default_value(self, generated_client): + assert_bad_schema(generated_client, "WithBadDefaultValue", "Value B is not valid") + + def test_literal_enum_bad_default_type(self, generated_client): + assert_bad_schema(generated_client, "WithBadDefaultType", "Cannot convert 123 to enum") + + def test_literal_enum_mixed_types(self, generated_client): + assert_bad_schema(generated_client, "WithMixedTypes", "Enum values must all be the same type") + + def test_literal_enum_unsupported_type(self, generated_client): + assert_bad_schema(generated_client, "WithUnsupportedType", "Unsupported enum type") + + def test_const_default_not_matching(self, generated_client): + assert_bad_schema(generated_client, "DefaultNotMatchingConst", "Invalid value for const") + + def test_conflicting_inline_literal_enum_names(self, generated_client): + assert "Found conflicting enums named WithConflictingInlineNames12 with incompatible values" in generated_client.generator_result.output + + def test_literal_enum_duplicate_values(self): + # This one currently causes a full generator failure rather than a warning + result = inline_spec_should_fail( +""" +components: + schemas: + WithDuplicateValues: + enum: ["x", "x"] +""" + ) + assert "Duplicate key X in enum" in str(result.exception) diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_spec_format.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_spec_format.py new file mode 100644 index 000000000..2b0dfdda9 --- /dev/null +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_spec_format.py @@ -0,0 +1,86 @@ +import pytest +from end_to_end_tests.functional_tests.helpers import ( + inline_spec_should_fail, +) + + +class TestInvalidSpecFormats: + @pytest.mark.parametrize( + ("filename_suffix", "content", "expected_error"), + ( + (".yaml", "not a valid openapi document", "Failed to parse OpenAPI document"), + (".json", "Invalid JSON", "Invalid JSON"), + (".yaml", "{", "Invalid YAML"), + ), + ids=("invalid_openapi", "invalid_json", "invalid_yaml"), + ) + def test_unparseable_file(self, filename_suffix, content, expected_error): + result = inline_spec_should_fail(content, filename_suffix=filename_suffix, add_missing_sections=False) + assert expected_error in result.output + + def test_missing_openapi_version(self): + result = inline_spec_should_fail( +""" +info: + title: My API + version: "1.0" +paths: {} +""", + add_missing_sections=False, + ) + for text in ["Failed to parse OpenAPI document", "1 validation error", "openapi"]: + assert text in result.output + + def test_missing_title(self): + result = inline_spec_should_fail( +""" +info: + version: "1.0" +openapi: "3.1.0" +paths: {} +""", + add_missing_sections=False, + ) + for text in ["Failed to parse OpenAPI document", "1 validation error", "title"]: + assert text in result.output + + def test_missing_version(self): + result = inline_spec_should_fail( +""" +info: + title: My API +openapi: "3.1.0" +paths: {} +""", + add_missing_sections=False, + ) + for text in ["Failed to parse OpenAPI document", "1 validation error", "version"]: + assert text in result.output + + def test_missing_paths(self): + result = inline_spec_should_fail( +""" +info: + title: My API + version: "1.0" +openapi: "3.1.0" +""", + add_missing_sections=False, + ) + for text in ["Failed to parse OpenAPI document", "1 validation error", "paths"]: + assert text in result.output + + def test_swagger_unsupported(self): + result = inline_spec_should_fail( +""" +swagger: "2.0" +info: + title: My API + version: "1.0" +openapi: "3.1" +paths: {} +components: {} +""", + add_missing_sections=False, + ) + assert "You may be trying to use a Swagger document; this is not supported by this project." in result.output diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_unions.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_unions.py new file mode 100644 index 000000000..75621a094 --- /dev/null +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_unions.py @@ -0,0 +1,28 @@ +from end_to_end_tests.functional_tests.helpers import assert_bad_schema, with_generated_client_fixture + + +@with_generated_client_fixture( +""" +components: + schemas: + UnionWithInvalidReference: + anyOf: + - $ref: "#/components/schemas/DoesntExist" + UnionWithInvalidDefault: + type: ["number", "integer"] + default: aaa + UnionWithMalformedVariant: + anyOf: + - type: string + - type: array # invalid because no items +""" +) +class TestUnionInvalidSchemas: + def test_invalid_reference(self, generated_client): + assert_bad_schema(generated_client, "UnionWithInvalidReference", "Could not find reference") + + def test_invalid_default(self, generated_client): + assert_bad_schema(generated_client, "UnionWithInvalidDefault", "Invalid int value: aaa") + + def test_invalid_property(self, generated_client): + assert_bad_schema(generated_client, "UnionWithMalformedVariant", "Invalid property in union") diff --git a/end_to_end_tests/functional_tests/helpers.py b/end_to_end_tests/functional_tests/helpers.py new file mode 100644 index 000000000..cb63da11b --- /dev/null +++ b/end_to_end_tests/functional_tests/helpers.py @@ -0,0 +1,135 @@ +from typing import Any, Dict +import re +from typing import Optional + +from click.testing import Result +import pytest + +from end_to_end_tests.generated_client import generate_client_from_inline_spec, GeneratedClientContext + + +def with_generated_client_fixture( + openapi_spec: str, + name: str="generated_client", + config: str="", + extra_args: list[str] = [], +): + """Decorator to apply to a test class to create a fixture inside it called 'generated_client'. + + The fixture value will be a GeneratedClientContext created by calling + generate_client_from_inline_spec(). + """ + def _decorator(cls): + def generated_client(self): + with generate_client_from_inline_spec(openapi_spec, extra_args=extra_args, config=config) as g: + print(g.generator_result.stdout) # so we'll see the output if a test failed + yield g + + setattr(cls, name, pytest.fixture(scope="class")(generated_client)) + return cls + + return _decorator + + +def with_generated_code_import(import_path: str, alias: Optional[str] = None): + """Decorator to apply to a test class to create a fixture from a generated code import. + + The 'generated_client' fixture must also be present. + + If import_path is "a.b.c", then the fixture's value is equal to "from a.b import c", and + its name is "c" unless you specify a different name with the alias parameter. + """ + parts = import_path.split(".") + module_name = ".".join(parts[0:-1]) + import_name = parts[-1] + + def _decorator(cls): + nonlocal alias + + def _func(self, generated_client): + return generated_client.import_symbol(module_name, import_name) + + alias = alias or import_name + _func.__name__ = alias + setattr(cls, alias, pytest.fixture(scope="class")(_func)) + return cls + + return _decorator + + +def with_generated_code_imports(*import_paths: str): + def _decorator(cls): + decorated = cls + for import_path in import_paths: + decorated = with_generated_code_import(import_path)(decorated) + return decorated + + return _decorator + + +def assert_model_decode_encode(model_class: Any, json_data: dict, expected_instance: Any) -> None: + instance = model_class.from_dict(json_data) + assert instance == expected_instance + assert instance.to_dict() == json_data + + +def assert_model_property_type_hint(model_class: Any, name: str, expected_type_hint: Any) -> None: + assert model_class.__annotations__[name] == expected_type_hint + + +def inline_spec_should_fail( + openapi_spec: str, + extra_args: list[str] = [], + config: str = "", + filename_suffix: str = "", + add_missing_sections = True, +) -> Result: + """Asserts that the generator could not process the spec. + + Returns the command result, which could include stdout data or an exception. + """ + with generate_client_from_inline_spec( + openapi_spec, + extra_args, + config, + filename_suffix=filename_suffix, + add_missing_sections=add_missing_sections, + raise_on_error=False, + ) as generated_client: + assert generated_client.generator_result.exit_code != 0 + return generated_client.generator_result + + +def assert_bad_schema( + generated_client: GeneratedClientContext, + schema_name: str, + expected_message_str: str, +) -> None: + warnings = _GeneratorWarningsParser(generated_client) + assert schema_name in warnings.by_schema, f"Did not find warning for schema {schema_name} in output: {warnings.output}" + assert expected_message_str in warnings.by_schema[schema_name] + + +class _GeneratorWarningsParser: + output: str + by_schema: Dict[str, str] + + def __init__(self, generated_client: GeneratedClientContext) -> None: + """Runs the generator, asserts that it printed warnings, and parses the warnings.""" + + assert generated_client.generator_result.exit_code == 0 + output = generated_client.generator_result.stdout + assert "Warning(s) encountered while generating" in output + self.by_schema = {} + self.output = output + bad_schema_regex = "Unable to (parse|process) schema /components/schemas/(\\w*)" + last_name = "" + while True: + if not (match := re.search(bad_schema_regex, output)): + break + if last_name: + self.by_schema[last_name] = output[0:match.start()] + output = output[match.end():] + last_name = match.group(2) + if last_name: + self.by_schema[last_name] = output diff --git a/end_to_end_tests/generated_client.py b/end_to_end_tests/generated_client.py new file mode 100644 index 000000000..d7cb16fc7 --- /dev/null +++ b/end_to_end_tests/generated_client.py @@ -0,0 +1,156 @@ +import importlib +import os +import re +import shutil +from pathlib import Path +import sys +import tempfile +from typing import Any, Optional + +from attrs import define +import pytest +from click.testing import Result +from typer.testing import CliRunner + +from openapi_python_client.cli import app + + +@define +class GeneratedClientContext: + """A context manager with helpers for tests that run against generated client code. + + On entering this context, sys.path is changed to include the root directory of the + generated code, so its modules can be imported. On exit, the original sys.path is + restored, and any modules that were loaded within the context are removed. + """ + + output_path: Path + generator_result: Result + base_module: str + monkeypatch: pytest.MonkeyPatch + old_modules: Optional[set[str]] = None + + def __enter__(self) -> "GeneratedClientContext": + self.monkeypatch.syspath_prepend(self.output_path) + self.old_modules = set(sys.modules.keys()) + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.monkeypatch.undo() + for module_name in set(sys.modules.keys()) - self.old_modules: + del sys.modules[module_name] + shutil.rmtree(self.output_path, ignore_errors=True) + + def import_module(self, module_path: str) -> Any: + """Attempt to import a module from the generated code.""" + return importlib.import_module(f"{self.base_module}{module_path}") + + def import_symbol(self, module_path: str, name: str) -> Any: + module = self.import_module(module_path) + try: + return getattr(module, name) + except AttributeError: + existing = ", ".join(name for name in dir(module) if not name.startswith("_")) + assert False, ( + f"Couldn't find import \"{name}\" in \"{self.base_module}{module_path}\".\n" + f"Available imports in that module are: {existing}\n" + f"Output from generator was: {self.generator_result.stdout}" + ) + +def _run_command( + command: str, + extra_args: Optional[list[str]] = None, + openapi_document: Optional[str] = None, + url: Optional[str] = None, + config_path: Optional[Path] = None, + raise_on_error: bool = True, +) -> Result: + """Generate a client from an OpenAPI document and return the result of the command.""" + runner = CliRunner() + if openapi_document is not None: + openapi_path = Path(__file__).parent / openapi_document + source_arg = f"--path={openapi_path}" + else: + source_arg = f"--url={url}" + config_path = config_path or (Path(__file__).parent / "config.yml") + args = [command, f"--config={config_path}", source_arg] + if extra_args: + args.extend(extra_args) + result = runner.invoke(app, args) + if result.exit_code != 0 and raise_on_error: + message = f"{result.stdout}\n{result.exception}" if result.exception else result.stdout + raise Exception(message) + return result + + +def generate_client( + openapi_document: str, + extra_args: list[str] = [], + output_path: str = "my-test-api-client", + base_module: str = "my_test_api_client", + specify_output_path_explicitly: bool = True, + overwrite: bool = True, + raise_on_error: bool = True, +) -> GeneratedClientContext: + """Run the generator and return a GeneratedClientContext for accessing the generated code.""" + full_output_path = Path.cwd() / output_path + if not overwrite: + shutil.rmtree(full_output_path, ignore_errors=True) + args = extra_args + if specify_output_path_explicitly: + args = [*args, "--output-path", str(full_output_path)] + if overwrite: + args = [*args, "--overwrite"] + generator_result = _run_command("generate", args, openapi_document, raise_on_error=raise_on_error) + return GeneratedClientContext( + full_output_path, + generator_result, + base_module, + pytest.MonkeyPatch(), + ) + + +def generate_client_from_inline_spec( + openapi_spec: str, + extra_args: list[str] = [], + config: str = "", + filename_suffix: Optional[str] = None, + base_module: str = "testapi_client", + add_missing_sections = True, + raise_on_error: bool = True, +) -> GeneratedClientContext: + """Run the generator on a temporary file created with the specified contents. + + You can also optionally tell it to create a temporary config file. + """ + if add_missing_sections: + if not re.search("^openapi:", openapi_spec, re.MULTILINE): + openapi_spec += "\nopenapi: '3.1.0'\n" + if not re.search("^info:", openapi_spec, re.MULTILINE): + openapi_spec += "\ninfo: {'title': 'testapi', 'description': 'my test api', 'version': '0.0.1'}\n" + if not re.search("^paths:", openapi_spec, re.MULTILINE): + openapi_spec += "\npaths: {}\n" + + output_path = tempfile.mkdtemp() + file = tempfile.NamedTemporaryFile(suffix=filename_suffix, delete=False) + file.write(openapi_spec.encode('utf-8')) + file.close() + + if config: + config_file = tempfile.NamedTemporaryFile(delete=False) + config_file.write(config.encode('utf-8')) + config_file.close() + extra_args = [*extra_args, "--config", config_file.name] + + generated_client = generate_client( + file.name, + extra_args, + output_path, + base_module, + raise_on_error=raise_on_error, + ) + os.unlink(file.name) + if config: + os.unlink(config_file.name) + + return generated_client diff --git a/end_to_end_tests/test_end_to_end.py b/end_to_end_tests/test_end_to_end.py index 2452c3acd..943bb1c6f 100644 --- a/end_to_end_tests/test_end_to_end.py +++ b/end_to_end_tests/test_end_to_end.py @@ -7,6 +7,9 @@ from click.testing import Result from typer.testing import CliRunner +from end_to_end_tests.generated_client import ( + _run_command, generate_client, generate_client_from_inline_spec, +) from openapi_python_client.cli import app @@ -83,51 +86,26 @@ def run_e2e_test( golden_record_path: str = "golden-record", output_path: str = "my-test-api-client", expected_missing: Optional[set[str]] = None, + specify_output_path_explicitly: bool = True, ) -> Result: - output_path = Path.cwd() / output_path - shutil.rmtree(output_path, ignore_errors=True) - result = generate(extra_args, openapi_document) - gr_path = Path(__file__).parent / golden_record_path - - expected_differences = expected_differences or {} - # Use absolute paths for expected differences for easier comparisons - expected_differences = { - output_path.joinpath(key): value for key, value in expected_differences.items() - } - _compare_directories( - gr_path, output_path, expected_differences=expected_differences, expected_missing=expected_missing - ) - - import mypy.api - - out, err, status = mypy.api.run([str(output_path), "--strict"]) - assert status == 0, f"Type checking client failed: {out}" - - shutil.rmtree(output_path) - return result - + with generate_client(openapi_document, extra_args, output_path, specify_output_path_explicitly=specify_output_path_explicitly) as g: + gr_path = Path(__file__).parent / golden_record_path + + expected_differences = expected_differences or {} + # Use absolute paths for expected differences for easier comparisons + expected_differences = { + g.output_path.joinpath(key): value for key, value in expected_differences.items() + } + _compare_directories( + gr_path, g.output_path, expected_differences=expected_differences, expected_missing=expected_missing + ) -def generate(extra_args: Optional[list[str]], openapi_document: str) -> Result: - """Generate a client from an OpenAPI document and return the path to the generated code""" - _run_command("generate", extra_args, openapi_document) + import mypy.api + out, err, status = mypy.api.run([str(g.output_path), "--strict"]) + assert status == 0, f"Type checking client failed: {out}" -def _run_command(command: str, extra_args: Optional[list[str]] = None, openapi_document: Optional[str] = None, url: Optional[str] = None, config_path: Optional[Path] = None) -> Result: - """Generate a client from an OpenAPI document and return the path to the generated code""" - runner = CliRunner() - if openapi_document is not None: - openapi_path = Path(__file__).parent / openapi_document - source_arg = f"--path={openapi_path}" - else: - source_arg = f"--url={url}" - config_path = config_path or (Path(__file__).parent / "config.yml") - args = [command, f"--config={config_path}", source_arg] - if extra_args: - args.extend(extra_args) - result = runner.invoke(app, args) - if result.exit_code != 0: - raise Exception(result.stdout) - return result + return g.generator_result def test_baseline_end_to_end_3_0(): @@ -168,18 +146,17 @@ def test_literal_enums_end_to_end(): ) ) def test_meta(meta: str, generated_file: Optional[str], expected_file: Optional[str]): - output_path = Path.cwd() / "test-3-1-features-client" - shutil.rmtree(output_path, ignore_errors=True) - generate([f"--meta={meta}"], "3.1_specific.openapi.yaml") - - if generated_file and expected_file: - assert (output_path / generated_file).exists() - assert ( - (output_path / generated_file).read_text() == - (Path(__file__).parent / "metadata_snapshots" / expected_file).read_text() - ) - - shutil.rmtree(output_path) + with generate_client( + "3.1_specific.openapi.yaml", + extra_args=[f"--meta={meta}"], + output_path="test-3-1-features-client", + ) as g: + if generated_file and expected_file: + assert (g.output_path / generated_file).exists() + assert ( + (g.output_path / generated_file).read_text() == + (Path(__file__).parent / "metadata_snapshots" / expected_file).read_text() + ) def test_none_meta(): @@ -189,6 +166,7 @@ def test_none_meta(): golden_record_path="test-3-1-golden-record/test_3_1_features_client", output_path="test_3_1_features_client", expected_missing={"py.typed"}, + specify_output_path_explicitly=False, ) @@ -238,55 +216,41 @@ def test_bad_url(): @pytest.mark.parametrize("document", ERROR_DOCUMENTS, ids=[path.stem for path in ERROR_DOCUMENTS]) def test_documents_with_errors(snapshot, document): - runner = CliRunner() - output_path = Path.cwd() / "test-documents-with-errors" - shutil.rmtree(output_path, ignore_errors=True) - result = runner.invoke(app, ["generate", f"--path={document}", "--fail-on-warning", f"--output-path={output_path}"]) - assert result.exit_code == 1 - assert result.stdout.replace(str(output_path), "/test-documents-with-errors") == snapshot - shutil.rmtree(output_path, ignore_errors=True) + with generate_client( + document, + extra_args=["--fail-on-warning"], + output_path="test-documents-with-errors", + raise_on_error=False, + ) as g: + result = g.generator_result + assert result.exit_code == 1 + output = result.stdout.replace(str(g.output_path), "/test-documents-with-errors") + assert output == snapshot def test_custom_post_hooks(): - shutil.rmtree(Path.cwd() / "my-test-api-client", ignore_errors=True) - runner = CliRunner() - openapi_document = Path(__file__).parent / "baseline_openapi_3.0.json" config_path = Path(__file__).parent / "custom_post_hooks.config.yml" - result = runner.invoke(app, ["generate", f"--path={openapi_document}", f"--config={config_path}"]) - assert result.exit_code == 1 - assert "this should fail" in result.stdout - shutil.rmtree(Path.cwd() / "my-test-api-client", ignore_errors=True) + with generate_client( + "baseline_openapi_3.0.json", + [f"--config={config_path}"], + raise_on_error=False, + ) as g: + assert g.generator_result.exit_code == 1 + assert "this should fail" in g.generator_result.stdout def test_generate_dir_already_exists(): project_dir = Path.cwd() / "my-test-api-client" if not project_dir.exists(): project_dir.mkdir() - runner = CliRunner() - openapi_document = Path(__file__).parent / "baseline_openapi_3.0.json" - result = runner.invoke(app, ["generate", f"--path={openapi_document}"]) - assert result.exit_code == 1 - assert "Directory already exists" in result.stdout - shutil.rmtree(Path.cwd() / "my-test-api-client", ignore_errors=True) - - -@pytest.mark.parametrize( - ("file_name", "content", "expected_error"), - ( - ("invalid_openapi.yaml", "not a valid openapi document", "Failed to parse OpenAPI document"), - ("invalid_json.json", "Invalid JSON", "Invalid JSON"), - ("invalid_yaml.yaml", "{", "Invalid YAML"), - ), - ids=("invalid_openapi", "invalid_json", "invalid_yaml") -) -def test_invalid_openapi_document(file_name, content, expected_error): - runner = CliRunner() - openapi_document = Path.cwd() / file_name - openapi_document.write_text(content) - result = runner.invoke(app, ["generate", f"--path={openapi_document}"]) - assert result.exit_code == 1 - assert expected_error in result.stdout - openapi_document.unlink() + try: + runner = CliRunner() + openapi_document = Path(__file__).parent / "baseline_openapi_3.0.json" + result = runner.invoke(app, ["generate", f"--path={openapi_document}"]) + assert result.exit_code == 1 + assert "Directory already exists" in result.stdout + finally: + shutil.rmtree(Path.cwd() / "my-test-api-client", ignore_errors=True) def test_update_integration_tests(): @@ -294,17 +258,21 @@ def test_update_integration_tests(): source_path = Path(__file__).parent.parent / "integration-tests" temp_dir = Path.cwd() / "test_update_integration_tests" shutil.rmtree(temp_dir, ignore_errors=True) - shutil.copytree(source_path, temp_dir) - config_path = source_path / "config.yaml" - _run_command( - "generate", - extra_args=["--meta=none", "--overwrite", f"--output-path={source_path / 'integration_tests'}"], - url=url, - config_path=config_path - ) - _compare_directories(temp_dir, source_path, expected_differences={}) - import mypy.api - out, err, status = mypy.api.run([str(temp_dir), "--strict"]) - assert status == 0, f"Type checking client failed: {out}" - shutil.rmtree(temp_dir) + try: + shutil.copytree(source_path, temp_dir) + config_path = source_path / "config.yaml" + _run_command( + "generate", + extra_args=["--meta=none", "--overwrite", f"--output-path={source_path / 'integration_tests'}"], + url=url, + config_path=config_path + ) + _compare_directories(temp_dir, source_path, expected_differences={}) + import mypy.api + + out, err, status = mypy.api.run([str(temp_dir), "--strict"]) + assert status == 0, f"Type checking client failed: {out}" + + finally: + shutil.rmtree(temp_dir) diff --git a/pyproject.toml b/pyproject.toml index 417d7a356..9d8abb34c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,7 +66,7 @@ ignore = ["E501", "PLR0913", "PLR2004"] "tests/*" = ["PLR2004"] [tool.coverage.run] -omit = ["openapi_python_client/__main__.py", "openapi_python_client/templates/*"] +omit = ["openapi_python_client/__main__.py", "openapi_python_client/templates/*", "end_to_end_tests/*", "integration_tests/*", "tests/*"] [tool.mypy] plugins = ["pydantic.mypy"] @@ -118,7 +118,7 @@ re = {composite = ["regen_e2e", "e2e --snapshot-update"]} regen_e2e = "python -m end_to_end_tests.regen_golden_record" [tool.pdm.scripts.test] -cmd = "pytest tests end_to_end_tests/test_end_to_end.py --basetemp=tests/tmp" +cmd = "pytest tests end_to_end_tests/test_end_to_end.py end_to_end_tests/functional_tests --basetemp=tests/tmp" [tool.pdm.scripts.test.env] "TEST_RELATIVE" = "true" diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index 6eeadcd78..18475b1b9 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -4,7 +4,6 @@ import pytest import openapi_python_client.schema as oai -from openapi_python_client import GeneratorError from openapi_python_client.parser.errors import ParseError from openapi_python_client.parser.openapi import Endpoint, EndpointCollection from openapi_python_client.parser.properties import IntProperty, Parameters, Schemas @@ -13,50 +12,6 @@ MODULE_NAME = "openapi_python_client.parser.openapi" -class TestGeneratorData: - def test_from_dict_invalid_schema(self, mocker): - Schemas = mocker.patch(f"{MODULE_NAME}.Schemas") - config = mocker.MagicMock() - - in_dict = {} - - from openapi_python_client.parser.openapi import GeneratorData - - generator_data = GeneratorData.from_dict(in_dict, config=config) - - assert isinstance(generator_data, GeneratorError) - assert generator_data.header == "Failed to parse OpenAPI document" - keywords = ["3 validation errors for OpenAPI", "info", "paths", "openapi", "Field required"] - assert generator_data.detail and all(keyword in generator_data.detail for keyword in keywords) - - Schemas.build.assert_not_called() - Schemas.assert_not_called() - - def test_swagger_document_invalid_schema(self, mocker): - Schemas = mocker.patch(f"{MODULE_NAME}.Schemas") - config = mocker.MagicMock() - - in_dict = {"swagger": "2.0"} - - from openapi_python_client.parser.openapi import GeneratorData - - generator_data = GeneratorData.from_dict(in_dict, config=config) - - assert isinstance(generator_data, GeneratorError) - assert generator_data.header == "Failed to parse OpenAPI document" - keywords = [ - "You may be trying to use a Swagger document; this is not supported by this project.", - "info", - "paths", - "openapi", - "Field required", - ] - assert generator_data.detail and all(keyword in generator_data.detail for keyword in keywords) - - Schemas.build.assert_not_called() - Schemas.assert_not_called() - - class TestEndpoint: def make_endpoint(self): from openapi_python_client.parser.openapi import Endpoint diff --git a/tests/test_parser/test_properties/test_any.py b/tests/test_parser/test_properties/test_any.py deleted file mode 100644 index d80e93e64..000000000 --- a/tests/test_parser/test_properties/test_any.py +++ /dev/null @@ -1,13 +0,0 @@ -from openapi_python_client.parser.properties import AnyProperty -from openapi_python_client.utils import PythonIdentifier - - -def test_default() -> None: - AnyProperty.build( - name="test", - required=True, - default=42, - python_name=PythonIdentifier("test", ""), - description="test", - example="test", - ) diff --git a/tests/test_parser/test_properties/test_boolean.py b/tests/test_parser/test_properties/test_boolean.py deleted file mode 100644 index 0862f1507..000000000 --- a/tests/test_parser/test_properties/test_boolean.py +++ /dev/null @@ -1,55 +0,0 @@ -import pytest - -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import BooleanProperty -from openapi_python_client.utils import PythonIdentifier - - -def test_invalid_default_value() -> None: - err = BooleanProperty.build( - default="not a boolean", - description=None, - example=None, - required=False, - python_name=PythonIdentifier("not_a_boolean", ""), - name="not_a_boolean", - ) - - assert isinstance(err, PropertyError) - - -@pytest.mark.parametrize( - ("value", "expected"), - ( - ("true", "True"), - ("True", "True"), - ("false", "False"), - ("False", "False"), - ), -) -def test_string_default(value, expected) -> None: - prop = BooleanProperty.build( - default=value, - description=None, - example=None, - required=False, - python_name="not_a_boolean", - name="not_a_boolean", - ) - - assert isinstance(prop, BooleanProperty) - assert prop.default.python_code == expected - - -def test_bool_default() -> None: - prop = BooleanProperty.build( - default=True, - description=None, - example=None, - required=False, - python_name="not_a_boolean", - name="not_a_boolean", - ) - - assert isinstance(prop, BooleanProperty) - assert prop.default.python_code == "True" diff --git a/tests/test_parser/test_properties/test_const.py b/tests/test_parser/test_properties/test_const.py deleted file mode 100644 index ab9e29332..000000000 --- a/tests/test_parser/test_properties/test_const.py +++ /dev/null @@ -1,28 +0,0 @@ -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import ConstProperty - - -def test_default_doesnt_match_const() -> None: - err = ConstProperty.build( - name="test", - required=True, - default="not the value", - python_name="test", - description=None, - const="the value", - ) - - assert isinstance(err, PropertyError) - - -def test_non_string_const() -> None: - prop = ConstProperty.build( - name="test", - required=True, - default=123, - python_name="test", - description=None, - const=123, - ) - - assert isinstance(prop, ConstProperty) diff --git a/tests/test_parser/test_properties/test_date.py b/tests/test_parser/test_properties/test_date.py deleted file mode 100644 index bcc3292b6..000000000 --- a/tests/test_parser/test_properties/test_date.py +++ /dev/null @@ -1,28 +0,0 @@ -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import DateProperty - - -def test_invalid_default_value(): - err = DateProperty.build( - default="not a date", - description=None, - example=None, - required=False, - python_name="not_a_date", - name="not_a_date", - ) - - assert isinstance(err, PropertyError) - - -def test_default_with_bad_type(): - err = DateProperty.build( - default=123, - description=None, - example=None, - required=False, - python_name="not_a_date", - name="not_a_date", - ) - - assert isinstance(err, PropertyError) diff --git a/tests/test_parser/test_properties/test_datetime.py b/tests/test_parser/test_properties/test_datetime.py deleted file mode 100644 index 94ea6f09c..000000000 --- a/tests/test_parser/test_properties/test_datetime.py +++ /dev/null @@ -1,28 +0,0 @@ -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import DateTimeProperty - - -def test_invalid_default_value(): - err = DateTimeProperty.build( - default="not a date", - description=None, - example=None, - required=False, - python_name="not_a_date", - name="not_a_date", - ) - - assert isinstance(err, PropertyError) - - -def test_default_with_bad_type(): - err = DateTimeProperty.build( - default=123, - description=None, - example=None, - required=False, - python_name="not_a_date", - name="not_a_date", - ) - - assert isinstance(err, PropertyError) diff --git a/tests/test_parser/test_properties/test_enum_property.py b/tests/test_parser/test_properties/test_enum_property.py deleted file mode 100644 index 282298aaf..000000000 --- a/tests/test_parser/test_properties/test_enum_property.py +++ /dev/null @@ -1,81 +0,0 @@ -from typing import Union - -import pytest - -import openapi_python_client.schema as oai -from openapi_python_client import Config -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import LiteralEnumProperty, Schemas -from openapi_python_client.parser.properties.enum_property import EnumProperty - -PropertyClass = Union[type[EnumProperty], type[LiteralEnumProperty]] - - -@pytest.fixture(params=[EnumProperty, LiteralEnumProperty]) -def property_class(request) -> PropertyClass: - return request.param - - -def test_conflict(config: Config, property_class: PropertyClass) -> None: - schemas = Schemas() - - _, schemas = property_class.build( - data=oai.Schema(enum=["a"]), name="Existing", required=True, schemas=schemas, parent_name="", config=config - ) - err, new_schemas = property_class.build( - data=oai.Schema(enum=["a", "b"]), - name="Existing", - required=True, - schemas=schemas, - parent_name="", - config=config, - ) - - assert schemas == new_schemas - assert err.detail == "Found conflicting enums named Existing with incompatible values." - - -def test_bad_default_value(config: Config, property_class: PropertyClass) -> None: - data = oai.Schema(default="B", enum=["A"]) - schemas = Schemas() - - err, new_schemas = property_class.build( - data=data, name="Existing", required=True, schemas=schemas, parent_name="parent", config=config - ) - - assert schemas == new_schemas - assert err == PropertyError(detail="Value B is not valid for enum Existing", data=data) - - -def test_bad_default_type(config: Config, property_class: PropertyClass) -> None: - data = oai.Schema(default=123, enum=["A"]) - schemas = Schemas() - - err, new_schemas = property_class.build( - data=data, name="Existing", required=True, schemas=schemas, parent_name="parent", config=config - ) - - assert schemas == new_schemas - assert isinstance(err, PropertyError) - - -def test_mixed_types(config: Config, property_class: PropertyClass) -> None: - data = oai.Schema(enum=["A", 1]) - schemas = Schemas() - - err, _ = property_class.build( - data=data, name="Enum", required=True, schemas=schemas, parent_name="parent", config=config - ) - - assert isinstance(err, PropertyError) - - -def test_unsupported_type(config: Config, property_class: PropertyClass) -> None: - data = oai.Schema(enum=[1.4, 1.5]) - schemas = Schemas() - - err, _ = property_class.build( - data=data, name="Enum", required=True, schemas=schemas, parent_name="parent", config=config - ) - - assert isinstance(err, PropertyError) diff --git a/tests/test_parser/test_properties/test_file.py b/tests/test_parser/test_properties/test_file.py index 87298ba03..f399e8278 100644 --- a/tests/test_parser/test_properties/test_file.py +++ b/tests/test_parser/test_properties/test_file.py @@ -3,6 +3,8 @@ def test_no_default_allowed(): + # currently this is testing an unused code path: + # https://github.com/openapi-generators/openapi-python-client/issues/1162 err = FileProperty.build( default="not none", description=None, diff --git a/tests/test_parser/test_properties/test_float.py b/tests/test_parser/test_properties/test_float.py deleted file mode 100644 index 356a61424..000000000 --- a/tests/test_parser/test_properties/test_float.py +++ /dev/null @@ -1,38 +0,0 @@ -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import FloatProperty -from openapi_python_client.parser.properties.protocol import Value - - -def test_invalid_default(): - err = FloatProperty.build( - default="not a float", - description=None, - example=None, - required=False, - python_name="not_a_float", - name="not_a_float", - ) - - assert isinstance(err, PropertyError) - - -def test_convert_from_string(): - assert FloatProperty.convert_value("1.0") == Value(python_code="1.0", raw_value="1.0") - assert FloatProperty.convert_value("1") == Value(python_code="1.0", raw_value="1") - - -def test_convert_from_float(): - assert FloatProperty.convert_value(1.0) == Value(python_code="1.0", raw_value=1.0) - - -def test_invalid_type_default(): - err = FloatProperty.build( - default=True, - description=None, - example=None, - required=False, - python_name="not_a_float", - name="not_a_float", - ) - - assert isinstance(err, PropertyError) diff --git a/tests/test_parser/test_properties/test_init.py b/tests/test_parser/test_properties/test_init.py index 918defcdb..1af6342ad 100644 --- a/tests/test_parser/test_properties/test_init.py +++ b/tests/test_parser/test_properties/test_init.py @@ -1,86 +1,18 @@ -from unittest.mock import MagicMock, call +from unittest.mock import call -import attr import pytest import openapi_python_client.schema as oai from openapi_python_client.parser.errors import ParameterError, PropertyError from openapi_python_client.parser.properties import ( - ListProperty, ReferencePath, Schemas, - StringProperty, - UnionProperty, ) -from openapi_python_client.parser.properties.protocol import ModelProperty, Value -from openapi_python_client.parser.properties.schemas import Class -from openapi_python_client.schema import DataType from openapi_python_client.utils import ClassName, PythonIdentifier MODULE_NAME = "openapi_python_client.parser.properties" -class TestStringProperty: - def test_is_base_type(self, string_property_factory): - assert string_property_factory().is_base_type is True - - @pytest.mark.parametrize( - "required, expected", - ( - (True, "str"), - (False, "Union[Unset, str]"), - ), - ) - def test_get_type_string(self, string_property_factory, required, expected): - p = string_property_factory(required=required) - - assert p.get_type_string() == expected - - -class TestDateTimeProperty: - def test_is_base_type(self, date_time_property_factory): - assert date_time_property_factory().is_base_type is True - - @pytest.mark.parametrize("required", (True, False)) - def test_get_imports(self, date_time_property_factory, required): - p = date_time_property_factory(required=required) - - expected = { - "import datetime", - "from typing import cast", - "from dateutil.parser import isoparse", - } - if not required: - expected |= { - "from typing import Union", - "from ...types import UNSET, Unset", - } - - assert p.get_imports(prefix="...") == expected - - -class TestDateProperty: - def test_is_base_type(self, date_property_factory): - assert date_property_factory().is_base_type is True - - @pytest.mark.parametrize("required", (True, False)) - def test_get_imports(self, date_property_factory, required): - p = date_property_factory(required=required) - - expected = { - "import datetime", - "from typing import cast", - "from dateutil.parser import isoparse", - } - if not required: - expected |= { - "from typing import Union", - "from ...types import UNSET, Unset", - } - - assert p.get_imports(prefix="...") == expected - - class TestFileProperty: def test_is_base_type(self, file_property_factory): assert file_property_factory().is_base_type is True @@ -102,116 +34,6 @@ def test_get_imports(self, file_property_factory, required): assert p.get_imports(prefix="...") == expected -class TestNoneProperty: - def test_is_base_type(self, none_property_factory): - assert none_property_factory().is_base_type is True - - -class TestBooleanProperty: - def test_is_base_type(self, boolean_property_factory): - assert boolean_property_factory().is_base_type is True - - -class TestAnyProperty: - def test_is_base_type(self, any_property_factory): - assert any_property_factory().is_base_type is True - - -class TestIntProperty: - def test_is_base_type(self, int_property_factory): - assert int_property_factory().is_base_type is True - - -class TestListProperty: - def test_is_base_type(self, list_property_factory): - assert list_property_factory().is_base_type is False - - @pytest.mark.parametrize("quoted", (True, False)) - def test_get_base_json_type_string_base_inner(self, list_property_factory, quoted): - p = list_property_factory() - assert p.get_base_json_type_string(quoted=quoted) == "list[str]" - - @pytest.mark.parametrize("quoted", (True, False)) - def test_get_base_json_type_string_model_inner(self, list_property_factory, model_property_factory, quoted): - m = model_property_factory() - p = list_property_factory(inner_property=m) - assert p.get_base_json_type_string(quoted=quoted) == "list[dict[str, Any]]" - - def test_get_lazy_import_base_inner(self, list_property_factory): - p = list_property_factory() - assert p.get_lazy_imports(prefix="..") == set() - - def test_get_lazy_import_model_inner(self, list_property_factory, model_property_factory): - m = model_property_factory() - p = list_property_factory(inner_property=m) - assert p.get_lazy_imports(prefix="..") == {"from ..models.my_module import MyClass"} - - @pytest.mark.parametrize( - "required, expected", - ( - (True, "list[str]"), - (False, "Union[Unset, list[str]]"), - ), - ) - def test_get_type_string_base_inner(self, list_property_factory, required, expected): - p = list_property_factory(required=required) - - assert p.get_type_string() == expected - - @pytest.mark.parametrize( - "required, expected", - ( - (True, "list['MyClass']"), - (False, "Union[Unset, list['MyClass']]"), - ), - ) - def test_get_type_string_model_inner(self, list_property_factory, model_property_factory, required, expected): - m = model_property_factory() - p = list_property_factory(required=required, inner_property=m) - - assert p.get_type_string() == expected - - @pytest.mark.parametrize( - "quoted,expected", - [ - (False, "list[str]"), - (True, "list[str]"), - ], - ) - def test_get_base_type_string_base_inner(self, list_property_factory, quoted, expected): - p = list_property_factory() - assert p.get_base_type_string(quoted=quoted) == expected - - @pytest.mark.parametrize( - "quoted,expected", - [ - (False, "list['MyClass']"), - (True, "list['MyClass']"), - ], - ) - def test_get_base_type_string_model_inner(self, list_property_factory, model_property_factory, quoted, expected): - m = model_property_factory() - p = list_property_factory(inner_property=m) - assert p.get_base_type_string(quoted=quoted) == expected - - @pytest.mark.parametrize("required", (True, False)) - def test_get_type_imports(self, list_property_factory, date_time_property_factory, required): - inner_property = date_time_property_factory() - p = list_property_factory(inner_property=inner_property, required=required) - expected = { - "import datetime", - "from typing import cast", - "from dateutil.parser import isoparse", - } - if not required: - expected |= { - "from typing import Union", - "from ...types import UNSET, Unset", - } - - assert p.get_imports(prefix="...") == expected - - class TestUnionProperty: def test_is_base_type(self, union_property_factory): assert union_property_factory().is_base_type is False @@ -313,299 +135,7 @@ def test_get_type_imports(self, union_property_factory, date_time_property_facto assert p.get_imports(prefix="...") == expected -class TestEnumProperty: - def test_is_base_type(self, enum_property_factory): - assert enum_property_factory().is_base_type is True - - @pytest.mark.parametrize( - "required, expected", - ( - (False, "Union[Unset, {}]"), - (True, "{}"), - ), - ) - def test_get_type_string(self, mocker, enum_property_factory, required, expected): - fake_class = mocker.MagicMock() - fake_class.name = "MyTestEnum" - - p = enum_property_factory(class_info=fake_class, required=required) - - assert p.get_type_string() == expected.format(fake_class.name) - assert p.get_type_string(no_optional=True) == fake_class.name - assert p.get_type_string(json=True) == expected.format("str") - - def test_get_imports(self, mocker, enum_property_factory): - fake_class = mocker.MagicMock(module_name="my_test_enum") - fake_class.name = "MyTestEnum" - prefix = "..." - - enum_property = enum_property_factory(class_info=fake_class, required=False) - - assert enum_property.get_imports(prefix=prefix) == { - f"from {prefix}models.{fake_class.module_name} import {fake_class.name}", - "from typing import Union", # Makes sure unset is handled via base class - "from ...types import UNSET, Unset", - } - - def test_values_from_list(self): - from openapi_python_client.parser.properties import EnumProperty - - data = ["abc", "123", "a23", "1bc", 4, -3, "a Thing WIth spaces", ""] - - result = EnumProperty.values_from_list(data, Class("ClassName", "module_name")) - - assert result == { - "ABC": "abc", - "VALUE_1": "123", - "A23": "a23", - "VALUE_3": "1bc", - "VALUE_4": 4, - "VALUE_NEGATIVE_3": -3, - "A_THING_WITH_SPACES": "a Thing WIth spaces", - "VALUE_7": "", - } - - def test_values_from_list_duplicate(self): - from openapi_python_client.parser.properties import EnumProperty - - data = ["abc", "123", "a23", "abc"] - - with pytest.raises(ValueError): - EnumProperty.values_from_list(data, Class("ClassName", "module_name")) - - -class TestLiteralEnumProperty: - def test_is_base_type(self, literal_enum_property_factory): - assert literal_enum_property_factory().is_base_type is True - - @pytest.mark.parametrize( - "required, expected", - ( - (False, "Union[Unset, {}]"), - (True, "{}"), - ), - ) - def test_get_type_string(self, mocker, literal_enum_property_factory, required, expected): - fake_class = mocker.MagicMock() - fake_class.name = "MyTestEnum" - - p = literal_enum_property_factory(class_info=fake_class, required=required) - - assert p.get_type_string() == expected.format(fake_class.name) - assert p.get_type_string(no_optional=True) == fake_class.name - assert p.get_type_string(json=True) == expected.format("str") - - def test_get_imports(self, mocker, literal_enum_property_factory): - fake_class = mocker.MagicMock(module_name="my_test_enum") - fake_class.name = "MyTestEnum" - prefix = "..." - - literal_enum_property = literal_enum_property_factory(class_info=fake_class, required=False) - - assert literal_enum_property.get_imports(prefix=prefix) == { - "from typing import cast", - f"from {prefix}models.{fake_class.module_name} import {fake_class.name}", - f"from {prefix}models.{fake_class.module_name} import check_my_test_enum", - "from typing import Union", # Makes sure unset is handled via base class - "from ...types import UNSET, Unset", - } - - class TestPropertyFromData: - def test_property_from_data_str_enum(self, enum_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - from openapi_python_client.schema import Schema - - existing = enum_property_factory() - data = Schema(title="AnEnum", enum=["A", "B", "C"], default="B") - name = "my_enum" - required = True - - schemas = Schemas(classes_by_name={ClassName("AnEnum", prefix=""): existing}) - - prop, new_schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - ) - - assert prop == enum_property_factory( - name=name, - required=required, - values={"A": "A", "B": "B", "C": "C"}, - class_info=Class(name=ClassName("ParentAnEnum", ""), module_name=PythonIdentifier("parent_an_enum", "")), - value_type=str, - default=Value(python_code="ParentAnEnum.B", raw_value="B"), - ) - assert schemas != new_schemas, "Provided Schemas was mutated" - assert new_schemas.classes_by_name == { - "AnEnum": existing, - "ParentAnEnum": prop, - } - - def test_property_from_data_str_enum_with_null( - self, enum_property_factory, union_property_factory, none_property_factory, config - ): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - from openapi_python_client.schema import Schema - - existing = enum_property_factory() - data = Schema(title="AnEnum", enum=["A", "B", "C", None], default="B") - name = "my_enum" - required = True - - schemas = Schemas(classes_by_name={ClassName("AnEnum", ""): existing}) - - prop, new_schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - ) - - # None / null is removed from enum, and property is now nullable - assert isinstance(prop, UnionProperty), "Enums with None should be converted to UnionProperties" - enum_prop = enum_property_factory( - name="my_enum_type_1", - required=required, - values={"A": "A", "B": "B", "C": "C"}, - class_info=Class(name=ClassName("ParentAnEnum", ""), module_name=PythonIdentifier("parent_an_enum", "")), - value_type=str, - default=Value(python_code="ParentAnEnum.B", raw_value="B"), - ) - none_property = none_property_factory(name="my_enum_type_0", required=required) - assert prop == union_property_factory( - name=name, - default=Value(python_code="ParentAnEnum.B", raw_value="B"), - inner_properties=[none_property, enum_prop], - ) - assert schemas != new_schemas, "Provided Schemas was mutated" - assert new_schemas.classes_by_name == { - "AnEnum": existing, - "ParentAnEnum": enum_prop, - } - - def test_property_from_data_null_enum(self, enum_property_factory, none_property_factory, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - from openapi_python_client.schema import Schema - - data = Schema(title="AnEnumWithOnlyNull", enum=[None], default=None) - name = "my_enum" - required = True - - schemas = Schemas() - - prop, new_schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - ) - - assert prop == none_property_factory( - name="my_enum", required=required, default=Value(python_code="None", raw_value="None") - ) - - def test_property_from_data_int_enum(self, enum_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - from openapi_python_client.schema import Schema - - name = "my_enum" - required = True - data = Schema.model_construct(title="anEnum", enum=[1, 2, 3], default=3) - - existing = enum_property_factory() - schemas = Schemas(classes_by_name={ClassName("AnEnum", ""): existing}) - - prop, new_schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - ) - - assert prop == enum_property_factory( - name=name, - required=required, - values={"VALUE_1": 1, "VALUE_2": 2, "VALUE_3": 3}, - class_info=Class(name=ClassName("ParentAnEnum", ""), module_name=PythonIdentifier("parent_an_enum", "")), - value_type=int, - default=Value(python_code="ParentAnEnum.VALUE_3", raw_value=3), - ) - assert schemas != new_schemas, "Provided Schemas was mutated" - assert new_schemas.classes_by_name == { - "AnEnum": existing, - "ParentAnEnum": prop, - } - - def test_property_from_data_ref_enum(self, enum_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - - name = "some_enum" - data = oai.Reference.model_construct(ref="#/components/schemas/MyEnum") - existing_enum = enum_property_factory( - name="an_enum", - required=False, - values={"A": "a"}, - class_info=Class(name="MyEnum", module_name="my_enum"), - ) - schemas = Schemas(classes_by_reference={"/components/schemas/MyEnum": existing_enum}) - - prop, new_schemas = property_from_data( - name=name, required=False, data=data, schemas=schemas, parent_name="", config=config - ) - - assert prop == enum_property_factory( - name="some_enum", - required=False, - values={"A": "a"}, - class_info=Class(name="MyEnum", module_name="my_enum"), - ) - assert schemas == new_schemas - - def test_property_from_data_ref_enum_with_overridden_default(self, enum_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - - name = "some_enum" - required = False - data = oai.Schema.model_construct( - default="b", allOf=[oai.Reference.model_construct(ref="#/components/schemas/MyEnum")] - ) - existing_enum = enum_property_factory( - name="an_enum", - default=Value(python_code="MyEnum.A", raw_value="A"), - required=required, - values={"A": "a", "B": "b"}, - class_info=Class(name=ClassName("MyEnum", ""), module_name=PythonIdentifier("my_enum", "")), - ) - schemas = Schemas(classes_by_reference={ReferencePath("/components/schemas/MyEnum"): existing_enum}) - - prop, new_schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="", config=config - ) - new_schemas = attr.evolve(new_schemas, models_to_process=[]) # intermediate state irrelevant to this test - - assert prop == enum_property_factory( - name="some_enum", - default=Value(python_code="MyEnum.B", raw_value="b"), - required=required, - values={"A": "a", "B": "b"}, - class_info=Class(name=ClassName("MyEnum", ""), module_name=PythonIdentifier("my_enum", "")), - ) - assert schemas == new_schemas - - def test_property_from_data_ref_enum_with_invalid_default(self, enum_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - - name = "some_enum" - data = oai.Schema.model_construct( - default="x", allOf=[oai.Reference.model_construct(ref="#/components/schemas/MyEnum")] - ) - existing_enum = enum_property_factory( - name="an_enum", - default=Value(python_code="MyEnum.A", raw_value="A"), - values={"A": "a", "B": "b"}, - class_info=Class(name=ClassName("MyEnum", ""), module_name=PythonIdentifier("my_enum", "")), - python_name=PythonIdentifier("an_enum", ""), - ) - schemas = Schemas(classes_by_reference={ReferencePath("/components/schemas/MyEnum"): existing_enum}) - - prop, new_schemas = property_from_data( - name=name, required=False, data=data, schemas=schemas, parent_name="", config=config - ) - - assert schemas == new_schemas - assert prop == PropertyError(data=data, detail="Value x is not valid for enum an_enum") - def test_property_from_data_ref_model(self, model_property_factory, config): from openapi_python_client.parser.properties import Class, Schemas, property_from_data @@ -689,180 +219,8 @@ def test_property_from_data_invalid_ref(self, mocker): assert prop == PropertyError(data=data, detail="bad stuff") assert schemas == new_schemas - def test_property_from_data_array(self, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - - name = "a_list_prop" - required = True - data = oai.Schema( - type=DataType.ARRAY, - items=oai.Schema(type=DataType.STRING), - ) - schemas = Schemas() - - response = property_from_data( - name=name, - required=required, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - )[0] - - assert isinstance(response, ListProperty) - assert isinstance(response.inner_property, StringProperty) - - def test_property_from_data_union(self, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - - name = "union_prop" - required = True - data = oai.Schema( - anyOf=[oai.Schema(type=DataType.NUMBER)], - oneOf=[ - oai.Schema(type=DataType.INTEGER), - ], - ) - schemas = Schemas() - - response = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - )[0] - - assert isinstance(response, UnionProperty) - assert len(response.inner_properties) == 2 - - def test_property_from_data_list_of_types(self, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - - name = "union_prop" - required = True - data = oai.Schema( - type=[DataType.NUMBER, DataType.NULL], - ) - schemas = Schemas() - - response = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - )[0] - - assert isinstance(response, UnionProperty) - assert len(response.inner_properties) == 2 - - def test_property_from_data_union_of_one_element(self, model_property_factory, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - - name = "new_name" - required = False - class_name = "MyModel" - existing_model: ModelProperty = model_property_factory() - schemas = Schemas(classes_by_reference={f"/{class_name}": existing_model}) - - data = oai.Schema.model_construct( - allOf=[oai.Reference.model_construct(ref=f"#/{class_name}")], - ) - - prop, schemas = property_from_data( - name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=config - ) - - assert prop == attr.evolve(existing_model, name=name, required=required, python_name=PythonIdentifier(name, "")) - - def test_property_from_data_no_valid_props_in_data(self, any_property_factory): - from openapi_python_client.parser.properties import Schemas, property_from_data - - schemas = Schemas() - data = oai.Schema() - name = "blah" - - prop, new_schemas = property_from_data( - name=name, required=True, data=data, schemas=schemas, parent_name="parent", config=MagicMock() - ) - - assert prop == any_property_factory(name=name, required=True, default=None) - assert new_schemas == schemas - class TestStringBasedProperty: - @pytest.mark.parametrize("required", (True, False)) - def test_no_format(self, string_property_factory, required, config): - from openapi_python_client.parser.properties import property_from_data - - name = "some_prop" - data = oai.Schema.model_construct(type="string", default="hello world") - - p, _ = property_from_data( - name=name, required=required, data=data, parent_name=None, config=config, schemas=Schemas() - ) - - assert p == string_property_factory( - name=name, required=required, default=StringProperty.convert_value("hello world") - ) - - def test_datetime_format(self, date_time_property_factory, config): - from openapi_python_client.parser.properties import property_from_data - - name = "datetime_prop" - required = True - data = oai.Schema.model_construct(type="string", schema_format="date-time", default="2020-11-06T12:00:00") - - p, _ = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name="" - ) - - assert p == date_time_property_factory( - name=name, - required=required, - default=Value(python_code=f"isoparse('{data.default}')", raw_value=data.default), - ) - - def test_datetime_bad_default(self, config): - from openapi_python_client.parser.properties import property_from_data - - name = "datetime_prop" - required = True - data = oai.Schema.model_construct(type="string", schema_format="date-time", default="a") - - result, _ = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name="" - ) - - assert isinstance(result, PropertyError) - assert result.detail.startswith("Invalid datetime") - - def test_date_format(self, date_property_factory, config): - from openapi_python_client.parser.properties import property_from_data - - name = "date_prop" - required = True - - data = oai.Schema.model_construct(type="string", schema_format="date", default="2020-11-06") - - p, _ = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name="" - ) - - assert p == date_property_factory( - name=name, - required=required, - default=Value(python_code=f"isoparse('{data.default}').date()", raw_value=data.default), - ) - - def test_date_format_bad_default(self, config): - from openapi_python_client.parser.properties import property_from_data - - name = "date_prop" - required = True - - data = oai.Schema.model_construct(type="string", schema_format="date", default="a") - - p, _ = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name="" - ) - - assert isinstance(p, PropertyError) - assert p.detail.startswith("Invalid date") - def test__string_based_property_binary_format(self, file_property_factory, config): from openapi_python_client.parser.properties import property_from_data @@ -875,19 +233,6 @@ def test__string_based_property_binary_format(self, file_property_factory, confi ) assert p == file_property_factory(name=name, required=required) - def test__string_based_property_unsupported_format(self, string_property_factory, config): - from openapi_python_client.parser.properties import property_from_data - - name = "unknown" - required = True - data = oai.Schema.model_construct(type="string", schema_format="blah") - - p, _ = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name="" - ) - - assert p == string_property_factory(name=name, required=required) - class TestCreateSchemas: def test_skips_references_and_keeps_going(self, mocker, config): diff --git a/tests/test_parser/test_properties/test_int.py b/tests/test_parser/test_properties/test_int.py deleted file mode 100644 index 7f9953761..000000000 --- a/tests/test_parser/test_properties/test_int.py +++ /dev/null @@ -1,34 +0,0 @@ -from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import IntProperty -from openapi_python_client.parser.properties.protocol import Value -from openapi_python_client.utils import PythonIdentifier - - -def test_invalid_default(): - err = IntProperty.build( - default="not a float", - description=None, - example=None, - required=False, - python_name="not_a_float", - name="not_a_float", - ) - - assert isinstance(err, PropertyError) - - -def test_convert_from_string(): - assert IntProperty.convert_value("1") == Value(python_code="1", raw_value="1") - - -def test_invalid_type_default(): - err = IntProperty.build( - default=True, - description=None, - example=None, - required=False, - python_name=PythonIdentifier("not_a_float", ""), - name="not_a_float", - ) - - assert isinstance(err, PropertyError) diff --git a/tests/test_parser/test_properties/test_list_property.py b/tests/test_parser/test_properties/test_list_property.py deleted file mode 100644 index 60fb0a35d..000000000 --- a/tests/test_parser/test_properties/test_list_property.py +++ /dev/null @@ -1,178 +0,0 @@ -import attr - -import openapi_python_client.schema as oai -from openapi_python_client.parser.errors import ParseError, PropertyError -from openapi_python_client.parser.properties import ListProperty -from openapi_python_client.parser.properties.schemas import ReferencePath -from openapi_python_client.schema import DataType -from openapi_python_client.utils import ClassName - - -def test_build_list_property_no_items(config): - from openapi_python_client.parser import properties - - name = "list_prop" - required = True - data = oai.Schema(type=DataType.ARRAY) - schemas = properties.Schemas() - - p, new_schemas = ListProperty.build( - name=name, - required=required, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - process_properties=True, - roots={ReferencePath("root")}, - ) - - assert p == PropertyError(data=data, detail="type array must have items or prefixItems defined") - assert new_schemas == schemas - - -def test_build_list_property_invalid_items(config): - from openapi_python_client.parser import properties - - name = "name" - required = True - data = oai.Schema( - type=DataType.ARRAY, - items=oai.Reference.model_validate({"$ref": "doesnt exist"}), - ) - schemas = properties.Schemas(errors=[ParseError("error")]) - process_properties = False - roots: set[ReferencePath | ClassName] = {ReferencePath("root")} - - p, new_schemas = ListProperty.build( - name=name, - required=required, - data=data, - schemas=attr.evolve(schemas), - parent_name="parent", - config=config, - roots=roots, - process_properties=process_properties, - ) - - assert isinstance(p, PropertyError) - assert p.data == data.items - assert p.header.startswith(f"invalid data in items of array {name}") - assert new_schemas == schemas - - -def test_build_list_property(any_property_factory, config): - from openapi_python_client.parser import properties - - name = "prop" - data = oai.Schema( - type=DataType.ARRAY, - items=oai.Schema(), - ) - schemas = properties.Schemas(errors=[ParseError("error")]) - - p, new_schemas = ListProperty.build( - name=name, - required=True, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - roots={ReferencePath("root")}, - process_properties=True, - ) - - assert isinstance(p, properties.ListProperty) - assert p.inner_property == any_property_factory(name=f"{name}_item") - assert new_schemas == schemas - - -def test_build_list_property_single_prefix_item(any_property_factory, config): - from openapi_python_client.parser import properties - - name = "prop" - data = oai.Schema( - type=DataType.ARRAY, - prefixItems=[oai.Schema()], - ) - schemas = properties.Schemas(errors=[ParseError("error")]) - - p, new_schemas = ListProperty.build( - name=name, - required=True, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - roots={ReferencePath("root")}, - process_properties=True, - ) - - assert isinstance(p, properties.ListProperty) - assert p.inner_property == any_property_factory(name=f"{name}_item") - assert new_schemas == schemas - - -def test_build_list_property_items_and_prefix_items( - union_property_factory, - string_property_factory, - none_property_factory, - int_property_factory, - config, -): - from openapi_python_client.parser import properties - - name = "list_prop" - required = True - data = oai.Schema( - type=DataType.ARRAY, - items=oai.Schema(type=DataType.INTEGER), - prefixItems=[oai.Schema(type=DataType.STRING), oai.Schema(type=DataType.NULL)], - ) - schemas = properties.Schemas() - - p, new_schemas = ListProperty.build( - name=name, - required=required, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - process_properties=True, - roots={ReferencePath("root")}, - ) - - assert isinstance(p, properties.ListProperty) - assert p.inner_property == union_property_factory( - name=f"{name}_item", - inner_properties=[ - string_property_factory(name=f"{name}_item_type_0"), - none_property_factory(name=f"{name}_item_type_1"), - int_property_factory(name=f"{name}_item_type_2"), - ], - ) - assert new_schemas == schemas - - -def test_build_list_property_prefix_items_only(any_property_factory, config): - from openapi_python_client.parser import properties - - name = "list_prop" - required = True - data = oai.Schema(type=DataType.ARRAY, prefixItems=[oai.Schema()]) - schemas = properties.Schemas() - - p, new_schemas = ListProperty.build( - name=name, - required=required, - data=data, - schemas=schemas, - parent_name="parent", - config=config, - process_properties=True, - roots={ReferencePath("root")}, - ) - - assert isinstance(p, properties.ListProperty) - assert p.inner_property == any_property_factory(name=f"{name}_item") - assert new_schemas == schemas diff --git a/tests/test_parser/test_properties/test_none.py b/tests/test_parser/test_properties/test_none.py index d61ca0136..b6289cdb8 100644 --- a/tests/test_parser/test_properties/test_none.py +++ b/tests/test_parser/test_properties/test_none.py @@ -5,6 +5,8 @@ def test_default(): + # currently this is testing an unused code path: + # https://github.com/openapi-generators/openapi-python-client/issues/1162 err = NoneProperty.build( default="not None", description=None, diff --git a/tests/test_parser/test_properties/test_union.py b/tests/test_parser/test_properties/test_union.py index acbbd06d6..84e00067d 100644 --- a/tests/test_parser/test_properties/test_union.py +++ b/tests/test_parser/test_properties/test_union.py @@ -1,63 +1,9 @@ import openapi_python_client.schema as oai -from openapi_python_client.parser.errors import ParseError, PropertyError +from openapi_python_client.parser.errors import ParseError from openapi_python_client.parser.properties import Schemas, UnionProperty -from openapi_python_client.parser.properties.protocol import Value from openapi_python_client.schema import DataType, ParameterLocation -def test_property_from_data_union(union_property_factory, date_time_property_factory, string_property_factory, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - - name = "union_prop" - required = True - data = oai.Schema( - anyOf=[oai.Schema(type=DataType.STRING, default="a")], - oneOf=[ - oai.Schema(type=DataType.STRING, schema_format="date-time"), - ], - ) - expected = union_property_factory( - name=name, - required=required, - inner_properties=[ - string_property_factory(name=f"{name}_type_0", default=Value("'a'", "a")), - date_time_property_factory(name=f"{name}_type_1"), - ], - ) - - p, s = property_from_data( - name=name, required=required, data=data, schemas=Schemas(), parent_name="parent", config=config - ) - - assert p == expected - assert s == Schemas() - - -def test_build_union_property_invalid_property(config): - name = "bad_union" - required = True - reference = oai.Reference.model_construct(ref="#/components/schema/NotExist") - data = oai.Schema(anyOf=[reference]) - - p, s = UnionProperty.build( - name=name, required=required, data=data, schemas=Schemas(), parent_name="parent", config=config - ) - assert p == PropertyError(detail=f"Invalid property in union {name}", data=reference) - - -def test_invalid_default(config): - data = oai.Schema( - type=[DataType.NUMBER, DataType.INTEGER], - default="a", - ) - - err, _ = UnionProperty.build( - data=data, required=True, schemas=Schemas(), parent_name="parent", name="name", config=config - ) - - assert isinstance(err, PropertyError) - - def test_invalid_location(config): data = oai.Schema( type=[DataType.NUMBER, DataType.NULL],