[python] Add test for nullable property with pattern (#15625)

* add test for nullable property with pattern

* update samples
This commit is contained in:
William Cheng 2023-05-24 15:13:53 +08:00 committed by GitHub
parent 5b5cb1f7e0
commit 48ef91acc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 386 additions and 0 deletions

View File

@ -2199,3 +2199,16 @@ components:
- type: integer
minimum: 10
- type: string
NullableProperty:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
nullable: true
pattern: "^[A-Z].*"

View File

@ -52,6 +52,7 @@ docs/Model200Response.md
docs/ModelReturn.md
docs/Name.md
docs/NullableClass.md
docs/NullableProperty.md
docs/NumberOnly.md
docs/ObjectWithDeprecatedFields.md
docs/OneOfEnumString.md
@ -139,6 +140,7 @@ petstore_api/models/model200_response.py
petstore_api/models/model_return.py
petstore_api/models/name.py
petstore_api/models/nullable_class.py
petstore_api/models/nullable_property.py
petstore_api/models/number_only.py
petstore_api/models/object_with_deprecated_fields.py
petstore_api/models/one_of_enum_string.py

View File

@ -177,6 +177,7 @@ Class | Method | HTTP request | Description
- [ModelReturn](docs/ModelReturn.md)
- [Name](docs/Name.md)
- [NullableClass](docs/NullableClass.md)
- [NullableProperty](docs/NullableProperty.md)
- [NumberOnly](docs/NumberOnly.md)
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
- [OneOfEnumString](docs/OneOfEnumString.md)

View File

@ -0,0 +1,29 @@
# NullableProperty
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **int** | |
**name** | **str** | |
## Example
```python
from petstore_api.models.nullable_property import NullableProperty
# TODO update the JSON string below
json = "{}"
# create an instance of NullableProperty from a JSON string
nullable_property_instance = NullableProperty.from_json(json)
# print the JSON string representation of the object
print NullableProperty.to_json()
# convert the object into a dict
nullable_property_dict = nullable_property_instance.to_dict()
# create an instance of NullableProperty from a dict
nullable_property_form_dict = nullable_property.from_dict(nullable_property_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -83,6 +83,7 @@ from petstore_api.models.model200_response import Model200Response
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
from petstore_api.models.number_only import NumberOnly
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
from petstore_api.models.one_of_enum_string import OneOfEnumString

View File

@ -59,6 +59,7 @@ from petstore_api.models.model200_response import Model200Response
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
from petstore_api.models.number_only import NumberOnly
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
from petstore_api.models.one_of_enum_string import OneOfEnumString

View File

@ -0,0 +1,87 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
from __future__ import annotations
import pprint
import re # noqa: F401
import json
from typing import Optional
from pydantic import BaseModel, Field, StrictInt, constr, validator
class NullableProperty(BaseModel):
"""
NullableProperty
"""
id: StrictInt = Field(...)
name: Optional[constr(strict=True)] = Field(...)
__properties = ["id", "name"]
@validator('name')
def name_validate_regular_expression(cls, value):
"""Validates the regular expression"""
if value is None:
return value
if not re.match(r"^[A-Z].*", value):
raise ValueError(r"must validate the regular expression /^[A-Z].*/")
return value
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> NullableProperty:
"""Create an instance of NullableProperty from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
},
exclude_none=True)
# set to None if name (nullable) is None
# and __fields_set__ contains the field
if self.name is None and "name" in self.__fields_set__:
_dict['name'] = None
return _dict
@classmethod
def from_dict(cls, obj: dict) -> NullableProperty:
"""Create an instance of NullableProperty from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return NullableProperty.parse_obj(obj)
_obj = NullableProperty.parse_obj({
"id": obj.get("id"),
"name": obj.get("name")
})
return _obj

View File

@ -0,0 +1,57 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
import unittest
import datetime
import petstore_api
from petstore_api.models.nullable_property import NullableProperty # noqa: E501
from petstore_api.rest import ApiException
class TestNullableProperty(unittest.TestCase):
"""NullableProperty unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional):
"""Test NullableProperty
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `NullableProperty`
"""
model = petstore_api.models.nullable_property.NullableProperty() # noqa: E501
if include_optional :
return NullableProperty(
id = 56,
name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>'
)
else :
return NullableProperty(
id = 56,
name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>',
)
"""
def testNullableProperty(self):
"""Test NullableProperty"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()

View File

@ -52,6 +52,7 @@ docs/Model200Response.md
docs/ModelReturn.md
docs/Name.md
docs/NullableClass.md
docs/NullableProperty.md
docs/NumberOnly.md
docs/ObjectWithDeprecatedFields.md
docs/OneOfEnumString.md
@ -139,6 +140,7 @@ petstore_api/models/model200_response.py
petstore_api/models/model_return.py
petstore_api/models/name.py
petstore_api/models/nullable_class.py
petstore_api/models/nullable_property.py
petstore_api/models/number_only.py
petstore_api/models/object_with_deprecated_fields.py
petstore_api/models/one_of_enum_string.py

View File

@ -177,6 +177,7 @@ Class | Method | HTTP request | Description
- [ModelReturn](docs/ModelReturn.md)
- [Name](docs/Name.md)
- [NullableClass](docs/NullableClass.md)
- [NullableProperty](docs/NullableProperty.md)
- [NumberOnly](docs/NumberOnly.md)
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
- [OneOfEnumString](docs/OneOfEnumString.md)

View File

@ -0,0 +1,29 @@
# NullableProperty
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **int** | |
**name** | **str** | |
## Example
```python
from petstore_api.models.nullable_property import NullableProperty
# TODO update the JSON string below
json = "{}"
# create an instance of NullableProperty from a JSON string
nullable_property_instance = NullableProperty.from_json(json)
# print the JSON string representation of the object
print NullableProperty.to_json()
# convert the object into a dict
nullable_property_dict = nullable_property_instance.to_dict()
# create an instance of NullableProperty from a dict
nullable_property_form_dict = nullable_property.from_dict(nullable_property_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -83,6 +83,7 @@ from petstore_api.models.model200_response import Model200Response
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
from petstore_api.models.number_only import NumberOnly
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
from petstore_api.models.one_of_enum_string import OneOfEnumString

View File

@ -59,6 +59,7 @@ from petstore_api.models.model200_response import Model200Response
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
from petstore_api.models.number_only import NumberOnly
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
from petstore_api.models.one_of_enum_string import OneOfEnumString

View File

@ -0,0 +1,99 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
from __future__ import annotations
import pprint
import re # noqa: F401
import json
from typing import Optional
from pydantic import BaseModel, Field, StrictInt, constr, validator
class NullableProperty(BaseModel):
"""
NullableProperty
"""
id: StrictInt = Field(...)
name: Optional[constr(strict=True)] = Field(...)
additional_properties: Dict[str, Any] = {}
__properties = ["id", "name"]
@validator('name')
def name_validate_regular_expression(cls, value):
"""Validates the regular expression"""
if value is None:
return value
if not re.match(r"^[A-Z].*", value):
raise ValueError(r"must validate the regular expression /^[A-Z].*/")
return value
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> NullableProperty:
"""Create an instance of NullableProperty from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
"additional_properties"
},
exclude_none=True)
# puts key-value pairs in additional_properties in the top level
if self.additional_properties is not None:
for _key, _value in self.additional_properties.items():
_dict[_key] = _value
# set to None if name (nullable) is None
# and __fields_set__ contains the field
if self.name is None and "name" in self.__fields_set__:
_dict['name'] = None
return _dict
@classmethod
def from_dict(cls, obj: dict) -> NullableProperty:
"""Create an instance of NullableProperty from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return NullableProperty.parse_obj(obj)
_obj = NullableProperty.parse_obj({
"id": obj.get("id"),
"name": obj.get("name")
})
# store additional fields in additional_properties
for _key in obj.keys():
if _key not in cls.__properties:
_obj.additional_properties[_key] = obj.get(_key)
return _obj

View File

@ -0,0 +1,57 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
import unittest
import datetime
import petstore_api
from petstore_api.models.nullable_property import NullableProperty # noqa: E501
from petstore_api.rest import ApiException
class TestNullableProperty(unittest.TestCase):
"""NullableProperty unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional):
"""Test NullableProperty
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `NullableProperty`
"""
model = petstore_api.models.nullable_property.NullableProperty() # noqa: E501
if include_optional :
return NullableProperty(
id = 56,
name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>'
)
else :
return NullableProperty(
id = 56,
name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>',
)
"""
def testNullableProperty(self):
"""Test NullableProperty"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()

View File

@ -468,6 +468,11 @@ class ModelTests(unittest.TestCase):
self.assertEqual(a.name, "MINUS_EFG")
self.assertEqual(a, "-efg")
def test_nullable_property_pattern(self):
a = petstore_api.NullableProperty(id=12, name=None)
self.assertEqual(a.id, 12)
self.assertEqual(a.name, None)
def test_int_or_string_oneof(self):
a = petstore_api.IntOrString("-efg")
self.assertEqual(a.actual_instance, "-efg")