|
| 1 | +import pytest |
| 2 | + |
1 | 3 | MODULE_NAME = "openapi_python_client.openapi_parser.openapi"
|
2 | 4 |
|
3 | 5 |
|
@@ -32,3 +34,94 @@ def test_from_dict(self, mocker):
|
32 | 34 | schemas=schemas,
|
33 | 35 | enums=enums,
|
34 | 36 | )
|
| 37 | + |
| 38 | + def test__check_enums(self, mocker): |
| 39 | + # Test that all required and optional properties of all schemas are checked |
| 40 | + # Test that all path and query params of all endpoints of all collections are checked |
| 41 | + # Test that non EnumProperties are skipped |
| 42 | + from openapi_python_client.openapi_parser.openapi import EndpointCollection, OpenAPI |
| 43 | + from openapi_python_client.openapi_parser.properties import EnumProperty, StringProperty |
| 44 | + |
| 45 | + def _make_enum(): |
| 46 | + return EnumProperty(name=mocker.MagicMock(), required=True, default=None, values=mocker.MagicMock(),) |
| 47 | + |
| 48 | + # Multiple schemas with both required and optional properties for making sure iteration works correctly |
| 49 | + schema_1 = mocker.MagicMock() |
| 50 | + schema_1_req_enum_1 = _make_enum() |
| 51 | + schema_1_req_enum_2 = _make_enum() |
| 52 | + schema_1.required_properties = [schema_1_req_enum_1, schema_1_req_enum_2] |
| 53 | + schema_1_opt_enum_1 = _make_enum() |
| 54 | + schema_1_opt_enum_2 = _make_enum() |
| 55 | + non_enum = mocker.MagicMock(autospec=StringProperty) # For checking non-enum properties |
| 56 | + schema_1.optional_properties = [schema_1_opt_enum_1, schema_1_opt_enum_2, non_enum] |
| 57 | + schema_2 = mocker.MagicMock() |
| 58 | + schema_2_req_enum = _make_enum() |
| 59 | + schema_2.required_properties = [schema_2_req_enum] |
| 60 | + schema_2_opt_enum = _make_enum() |
| 61 | + schema_2.optional_properties = [schema_2_opt_enum] |
| 62 | + schemas = [schema_1, schema_2] |
| 63 | + |
| 64 | + collection_1 = mocker.MagicMock(autospec=EndpointCollection) |
| 65 | + collection_1_endpoint_1 = mocker.MagicMock() |
| 66 | + collection_1_endpoint_1_path_enum_1 = _make_enum() |
| 67 | + collection_1_endpoint_1_path_enum_2 = _make_enum() |
| 68 | + collection_1_endpoint_1.path_parameters = [ |
| 69 | + collection_1_endpoint_1_path_enum_1, |
| 70 | + collection_1_endpoint_1_path_enum_2, |
| 71 | + ] |
| 72 | + collection_1_endpoint_1_query_enum_1 = _make_enum() |
| 73 | + collection_1_endpoint_1_query_enum_2 = _make_enum() |
| 74 | + collection_1_endpoint_1.query_parameters = [ |
| 75 | + collection_1_endpoint_1_query_enum_1, |
| 76 | + collection_1_endpoint_1_query_enum_2, |
| 77 | + ] |
| 78 | + collection_1_endpoint_2 = mocker.MagicMock() |
| 79 | + collection_1_endpoint_2_path_enum = _make_enum() |
| 80 | + collection_1_endpoint_2.path_parameters = [collection_1_endpoint_2_path_enum] |
| 81 | + collection_1_endpoint_2_query_enum = _make_enum() |
| 82 | + collection_1_endpoint_2.query_parameters = [collection_1_endpoint_2_query_enum] |
| 83 | + collection_1.endpoints = [collection_1_endpoint_1, collection_1_endpoint_2] |
| 84 | + |
| 85 | + collection_2 = mocker.MagicMock() |
| 86 | + collection_2_endpoint = mocker.MagicMock() |
| 87 | + collection_2_path_enum = _make_enum() |
| 88 | + collection_2_endpoint.path_parameters = [collection_2_path_enum] |
| 89 | + collection_2_query_enum = _make_enum() |
| 90 | + collection_2_endpoint.query_parameters = [collection_2_query_enum] |
| 91 | + collection_2.endpoints = [collection_2_endpoint] |
| 92 | + collections = [collection_1, collection_2] |
| 93 | + |
| 94 | + enums = { |
| 95 | + schema_1_req_enum_1.reference.class_name: schema_1_req_enum_1, |
| 96 | + schema_1_req_enum_2.reference.class_name: schema_1_req_enum_2, |
| 97 | + schema_1_opt_enum_1.reference.class_name: schema_1_opt_enum_1, |
| 98 | + schema_1_opt_enum_2.reference.class_name: schema_1_opt_enum_2, |
| 99 | + schema_2_req_enum.reference.class_name: schema_2_req_enum, |
| 100 | + schema_2_opt_enum.reference.class_name: schema_2_opt_enum, |
| 101 | + collection_1_endpoint_1_path_enum_1.reference.class_name: collection_1_endpoint_1_path_enum_1, |
| 102 | + collection_1_endpoint_1_path_enum_2.reference.class_name: collection_1_endpoint_1_path_enum_2, |
| 103 | + collection_1_endpoint_1_query_enum_1.reference.class_name: collection_1_endpoint_1_query_enum_1, |
| 104 | + collection_1_endpoint_1_query_enum_2.reference.class_name: collection_1_endpoint_1_query_enum_2, |
| 105 | + collection_1_endpoint_2_path_enum.reference.class_name: collection_1_endpoint_2_path_enum, |
| 106 | + collection_1_endpoint_2_query_enum.reference.class_name: collection_1_endpoint_2_query_enum, |
| 107 | + collection_2_path_enum.reference.class_name: collection_2_path_enum, |
| 108 | + collection_2_query_enum.reference.class_name: collection_2_query_enum, |
| 109 | + } |
| 110 | + |
| 111 | + result = OpenAPI._check_enums(schemas=schemas, collections=collections) |
| 112 | + |
| 113 | + assert result == enums |
| 114 | + |
| 115 | + def test__check_enums_bad_duplicate(self, mocker): |
| 116 | + from dataclasses import replace |
| 117 | + from openapi_python_client.openapi_parser.properties import EnumProperty |
| 118 | + from openapi_python_client.openapi_parser.openapi import OpenAPI |
| 119 | + |
| 120 | + schema = mocker.MagicMock() |
| 121 | + |
| 122 | + enum_1 = EnumProperty(name=mocker.MagicMock(), required=True, default=None, values=mocker.MagicMock(),) |
| 123 | + enum_2 = replace(enum_1, values=mocker.MagicMock()) |
| 124 | + schema.required_properties = [enum_1, enum_2] |
| 125 | + |
| 126 | + with pytest.raises(AssertionError): |
| 127 | + OpenAPI._check_enums([schema], []) |
0 commit comments