Skip to content

Commit 73e671d

Browse files
authored
Merge pull request #107 from MarketSquare/95_better_name_for_dto
Dto renamed to RelationsMapping, constraint_mapping renamed to relati…
2 parents 6ac9019 + 65ced71 commit 73e671d

23 files changed

+224
-288
lines changed

docs/releases.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
- Fixed an exception during validation caused by `charset` being included in the `Content-Type` header for `application/json`.
2121

2222
### Breaking changes
23-
- `invalid_property_default_response` library parameter renamed to `invalid_data_default_response`.
23+
- Addressing [issue #95: Refactor: better name for Dto](https://github.com/MarketSquare/robotframework-openapitools/issues/95) introduces a number breaking renames:
24+
- `Dto` has been renamed to `RelationsMapping`.
25+
- `constraint_mapping` has been renamed to `relations_mapping` in a number of places.
26+
- `DTO_MAPPING` has been renamed to `RELATIONS_MAPPING`.
2427
- The `RequestData` class that is returned by a number of keywords has been changed:
2528
- The `dto` property was removed.
2629
- The `valid_data` property was added.
27-
- The `constrait_mapping` property was added.
30+
- The `relations_mapping` property was added.
31+
- `invalid_property_default_response` library parameter renamed to `invalid_data_default_response`.
2832

2933
### Additional changes
3034
- Special handling of `"format": "byte"` for `"type": "string"` (OAS 3.0) was removed.

src/OpenApiDriver/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
- IdDependency, IdReference, PathPropertiesConstraint, PropertyValueConstraint,
77
UniquePropertyValueConstraint: Classes to be subclassed by the library user
88
when implementing a custom mapping module (advanced use).
9-
- Dto, Relation: Base classes that can be used for type annotations.
9+
- RelationsMapping, Relation: Base classes that can be used for type annotations.
1010
- IGNORE: A special constant that can be used as a value in the PropertyValueConstraint.
1111
"""
1212

1313
from importlib.metadata import version
1414

1515
from OpenApiDriver.openapidriver import OpenApiDriver
16-
from OpenApiLibCore.data_constraints.dto_base import (
17-
Dto,
18-
)
16+
from OpenApiLibCore.data_relations.relations_base import RelationsMapping
1917
from OpenApiLibCore.keyword_logic.validation import ValidationLevel
2018
from OpenApiLibCore.models import IGNORE
2119
from OpenApiLibCore.models.resource_relations import (
@@ -35,12 +33,12 @@
3533

3634
__all__ = [
3735
"IGNORE",
38-
"Dto",
3936
"IdDependency",
4037
"IdReference",
4138
"OpenApiDriver",
4239
"PathPropertiesConstraint",
4340
"PropertyValueConstraint",
41+
"RelationsMapping",
4442
"ResourceRelation",
4543
"UniquePropertyValueConstraint",
4644
"ValidationLevel",

src/OpenApiDriver/openapi_executors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ def test_endpoint(self, path: str, method: str, status_code: int) -> None:
250250
if status_code >= int(HTTPStatus.BAD_REQUEST):
251251
invalidation_keywords: list[str] = []
252252

253-
if request_data.constraint_mapping.get_body_relations_for_error_code(
253+
if request_data.relations_mapping.get_body_relations_for_error_code(
254254
status_code
255255
):
256256
invalidation_keywords.append("get_invalid_body_data")
257-
if request_data.constraint_mapping.get_parameter_relations_for_error_code(
257+
if request_data.relations_mapping.get_parameter_relations_for_error_code(
258258
status_code
259259
):
260260
invalidation_keywords.append("get_invalidated_parameters")
@@ -300,7 +300,7 @@ def test_endpoint(self, path: str, method: str, status_code: int) -> None:
300300
)
301301
else:
302302
raise AssertionError(
303-
f"No constraint mapping found to cause status_code {status_code}."
303+
f"No relation found to cause status_code {status_code}."
304304
)
305305
_run_keyword(
306306
"perform_validated_request",

src/OpenApiLibCore/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
- IdDependency, IdReference, PathPropertiesConstraint, PropertyValueConstraint,
88
UniquePropertyValueConstraint: Classes to be subclassed by the library user
99
when implementing a custom mapping module (advanced use).
10-
- Dto, Relation: Base classes that can be used for type annotations.
10+
- RelationsMapping, Relation: Base classes that can be used for type annotations.
1111
- IGNORE: A special constant that can be used as a value in the PropertyValueConstraint.
1212
"""
1313

1414
from importlib.metadata import version
1515

16-
from OpenApiLibCore.data_constraints.dto_base import Dto
16+
from OpenApiLibCore.data_relations.relations_base import RelationsMapping
1717
from OpenApiLibCore.keyword_logic.validation import ValidationLevel
1818
from OpenApiLibCore.models import IGNORE, UNSET
1919
from OpenApiLibCore.models.request_data import RequestData, RequestValues
@@ -62,12 +62,12 @@
6262
__all__ = [
6363
"IGNORE",
6464
"UNSET",
65-
"Dto",
6665
"IdDependency",
6766
"IdReference",
6867
"OpenApiLibCore",
6968
"PathPropertiesConstraint",
7069
"PropertyValueConstraint",
70+
"RelationsMapping",
7171
"RequestData",
7272
"RequestValues",
7373
"ResourceRelation",

src/OpenApiLibCore/data_generation/data_generation_core.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import OpenApiLibCore.keyword_logic.path_functions as _path_functions
1414
from OpenApiLibCore.annotations import JSON
15-
from OpenApiLibCore.data_constraints.dto_base import Dto
15+
from OpenApiLibCore.data_relations.relations_base import RelationsMapping
1616
from OpenApiLibCore.models import IGNORE
1717
from OpenApiLibCore.models.oas_models import (
1818
ArraySchema,
@@ -28,7 +28,7 @@
2828
PropertyValueConstraint,
2929
ResourceRelation,
3030
)
31-
from OpenApiLibCore.protocols import ConstraintMappingType
31+
from OpenApiLibCore.protocols import RelationsMappingType
3232
from OpenApiLibCore.utils.parameter_utils import get_safe_name_for_oas_name
3333

3434

@@ -49,26 +49,26 @@ def get_request_data(
4949
operation_spec: OperationObject | None = getattr(path_item, method)
5050
if operation_spec is None:
5151
raise AttributeError
52-
constraint_mapping = operation_spec.constraint_mapping
52+
relations_mapping = operation_spec.relations_mapping
5353
except AttributeError:
5454
logger.info(
5555
f"method '{method}' not supported on '{spec_path}, using empty spec."
5656
)
5757
operation_spec = OperationObject(operationId="")
58-
constraint_mapping = None
58+
relations_mapping = None
5959

6060
parameters, params, headers = get_request_parameters(
61-
constraint_mapping=constraint_mapping, method_spec=operation_spec
61+
relations_mapping=relations_mapping, method_spec=operation_spec
6262
)
6363
if operation_spec.requestBody is None:
64-
constraint_mapping = _get_mapping_dataclass_for_empty_body(
65-
constraint_mapping=constraint_mapping,
64+
relations_mapping = _get_mapping_dataclass_for_empty_body(
65+
relations_mapping=relations_mapping,
6666
mapping_cls_name=mapping_cls_name,
6767
method_spec=operation_spec,
6868
)
6969
return RequestData(
7070
valid_data=None,
71-
constraint_mapping=constraint_mapping,
71+
relations_mapping=relations_mapping,
7272
parameters=parameters,
7373
params=params,
7474
headers=headers,
@@ -93,16 +93,16 @@ def get_request_data(
9393
operation_id=operation_spec.operationId
9494
)
9595

96-
constraint_mapping = _get_mapping_dataclass_from_valid_data(
96+
relations_mapping = _get_mapping_dataclass_from_valid_data(
9797
schema=schema_used_for_data_generation,
98-
constraint_mapping=constraint_mapping,
98+
relations_mapping=relations_mapping,
9999
valid_data=valid_data,
100100
method_spec=operation_spec,
101101
mapping_cls_name=mapping_cls_name,
102102
)
103103
return RequestData(
104104
valid_data=valid_data,
105-
constraint_mapping=constraint_mapping,
105+
relations_mapping=relations_mapping,
106106
body_schema=schema_used_for_data_generation,
107107
parameters=parameters,
108108
params=params,
@@ -111,12 +111,12 @@ def get_request_data(
111111

112112

113113
def _get_mapping_dataclass_for_empty_body(
114-
constraint_mapping: ConstraintMappingType | None,
114+
relations_mapping: RelationsMappingType | None,
115115
mapping_cls_name: str,
116116
method_spec: OperationObject,
117-
) -> ConstraintMappingType:
117+
) -> RelationsMappingType:
118118
cls_name = method_spec.operationId if method_spec.operationId else mapping_cls_name
119-
base = constraint_mapping if constraint_mapping else Dto
119+
base = relations_mapping if relations_mapping else RelationsMapping
120120
mapping_class = make_dataclass(
121121
cls_name=cls_name,
122122
fields=[],
@@ -127,22 +127,22 @@ def _get_mapping_dataclass_for_empty_body(
127127

128128
def _get_mapping_dataclass_from_valid_data(
129129
schema: ResolvedSchemaObjectTypes,
130-
constraint_mapping: ConstraintMappingType | None,
130+
relations_mapping: RelationsMappingType | None,
131131
valid_data: JSON,
132132
method_spec: OperationObject,
133133
mapping_cls_name: str,
134-
) -> ConstraintMappingType:
134+
) -> RelationsMappingType:
135135
if not isinstance(schema, (ObjectSchema, ArraySchema)):
136136
return _get_mapping_dataclass_for_empty_body(
137-
constraint_mapping=constraint_mapping,
137+
relations_mapping=relations_mapping,
138138
mapping_cls_name=mapping_cls_name,
139139
method_spec=method_spec,
140140
)
141141

142142
if isinstance(schema, ArraySchema):
143143
if not valid_data or not isinstance(valid_data, list):
144144
return _get_mapping_dataclass_for_empty_body(
145-
constraint_mapping=constraint_mapping,
145+
relations_mapping=relations_mapping,
146146
mapping_cls_name=mapping_cls_name,
147147
method_spec=method_spec,
148148
)
@@ -160,7 +160,7 @@ def _get_mapping_dataclass_from_valid_data(
160160

161161
mapping_dataclass = _get_mapping_dataclass_from_valid_data(
162162
schema=matched_schema,
163-
constraint_mapping=constraint_mapping,
163+
relations_mapping=relations_mapping,
164164
valid_data=first_item_data,
165165
method_spec=method_spec,
166166
mapping_cls_name=mapping_cls_name,
@@ -172,7 +172,7 @@ def _get_mapping_dataclass_from_valid_data(
172172
)
173173
fields = get_dataclass_fields(object_schema=schema, valid_data=valid_data)
174174
cls_name = method_spec.operationId if method_spec.operationId else mapping_cls_name
175-
base = constraint_mapping if constraint_mapping else Dto
175+
base = relations_mapping if relations_mapping else RelationsMapping
176176
mapping_dataclass = make_dataclass(
177177
cls_name=cls_name,
178178
fields=fields,
@@ -210,12 +210,12 @@ def get_mapping_cls_name(path: str, method: str) -> str:
210210

211211

212212
def get_request_parameters(
213-
constraint_mapping: ConstraintMappingType | None, method_spec: OperationObject
213+
relations_mapping: RelationsMappingType | None, method_spec: OperationObject
214214
) -> tuple[list[ParameterObject], dict[str, Any], dict[str, str]]:
215215
"""Get the methods parameter spec and params and headers with valid data."""
216216
parameters = method_spec.parameters if method_spec.parameters else []
217217
parameter_relations = (
218-
constraint_mapping.get_parameter_relations() if constraint_mapping else []
218+
relations_mapping.get_parameter_relations() if relations_mapping else []
219219
)
220220
query_params = [p for p in parameters if p.in_ == "query"]
221221
header_params = [p for p in parameters if p.in_ == "header"]

src/OpenApiLibCore/data_generation/data_invalidation.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
from robot.libraries.BuiltIn import BuiltIn
1313

1414
from OpenApiLibCore.annotations import JSON
15-
from OpenApiLibCore.data_constraints.dto_base import (
16-
Dto,
17-
)
15+
from OpenApiLibCore.data_relations.relations_base import RelationsMapping
1816
from OpenApiLibCore.models import IGNORE
1917
from OpenApiLibCore.models.oas_models import (
2018
ArraySchema,
@@ -69,7 +67,7 @@ def get_invalid_body_data(
6967
invalid_data_default_response: int,
7068
) -> JSON:
7169
method = method.lower()
72-
data_relations = request_data.constraint_mapping.get_body_relations_for_error_code(
70+
data_relations = request_data.relations_mapping.get_body_relations_for_error_code(
7371
status_code
7472
)
7573
if not data_relations:
@@ -107,7 +105,7 @@ def get_invalid_body_data(
107105
url,
108106
method,
109107
request_data.valid_data,
110-
request_data.constraint_mapping,
108+
request_data.relations_mapping,
111109
status_code,
112110
)
113111
if isinstance(resource_relation, IdReference):
@@ -147,7 +145,7 @@ def get_invalidated_parameters(
147145
raise ValueError("No params or headers to invalidate.")
148146

149147
# ensure the status_code can be triggered
150-
relations = request_data.constraint_mapping.get_parameter_relations_for_error_code(
148+
relations = request_data.relations_mapping.get_parameter_relations_for_error_code(
151149
status_code
152150
)
153151
relations_for_status_code = [
@@ -185,7 +183,7 @@ def get_invalidated_parameters(
185183
# non-default status_codes can only be the result of a Relation
186184
parameter_names = relation_property_names
187185

188-
# Constraint mappings may contain generic mappings for properties that are
186+
# Relation mappings may contain generic mappings for properties that are
189187
# not present in this specific schema
190188
request_data_parameter_names = [p.name for p in request_data.parameters]
191189
additional_relation_property_names = {
@@ -235,7 +233,7 @@ def get_invalidated_parameters(
235233
except ValueError:
236234
invalid_value_for_error_code = NOT_SET
237235

238-
# get the constraint values if available for the chosen parameter
236+
# get the constrained values if available for the chosen parameter
239237
try:
240238
[values_from_constraint] = [
241239
r.values
@@ -327,13 +325,13 @@ def get_json_data_with_conflict(
327325
base_url: str,
328326
method: str,
329327
json_data: dict[str, JSON],
330-
constraint_mapping: type[Dto],
328+
relations_mapping: type[RelationsMapping],
331329
conflict_status_code: int,
332330
) -> dict[str, Any]:
333331
method = method.lower()
334332
unique_property_value_constraints = [
335333
r
336-
for r in constraint_mapping.get_relations()
334+
for r in relations_mapping.get_relations()
337335
if isinstance(r, UniquePropertyValueConstraint)
338336
]
339337
for relation in unique_property_value_constraints:
@@ -342,7 +340,7 @@ def get_json_data_with_conflict(
342340
if method in ["patch", "put"]:
343341
post_url_parts = url.split("/")[:-1]
344342
post_url = "/".join(post_url_parts)
345-
# the PATCH or PUT may use a different constraint_mapping than required for
343+
# the PATCH or PUT may use a different relations_mapping than required for
346344
# POST so valid POST data must be constructed
347345
path = post_url.replace(base_url, "")
348346
request_data = _run_keyword("get_request_data", path, "post")
@@ -372,5 +370,5 @@ def get_json_data_with_conflict(
372370
return json_data
373371
raise ValueError(
374372
f"No UniquePropertyValueConstraint in the get_relations list on "
375-
f"constraint_mapping {constraint_mapping}."
373+
f"relations_mapping {relations_mapping}."
376374
)
File renamed without changes.

src/OpenApiLibCore/data_constraints/dto_base.py renamed to src/OpenApiLibCore/data_relations/relations_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
ResourceRelation,
1818
)
1919
from OpenApiLibCore.protocols import (
20-
ConstraintMappingType,
2120
IGetIdPropertyName,
21+
RelationsMappingType,
2222
)
2323
from OpenApiLibCore.utils.id_mapping import dummy_transformer
2424

2525

2626
@dataclass
27-
class Dto(ABC):
28-
"""Base class for the Dto class."""
27+
class RelationsMapping(ABC):
28+
"""Base class for the RelationsMapping classes."""
2929

3030
@staticmethod
3131
def get_path_relations() -> list[PathPropertiesConstraint]:
@@ -78,21 +78,21 @@ def get_body_relations_for_error_code(
7878
return relations
7979

8080

81-
def get_constraint_mapping_dict(
81+
def get_relations_mapping_dict(
8282
mappings_module_name: str,
83-
) -> dict[tuple[str, str], ConstraintMappingType]:
83+
) -> dict[tuple[str, str], RelationsMappingType]:
8484
try:
8585
mappings_module = import_module(mappings_module_name)
86-
return mappings_module.DTO_MAPPING # type: ignore[no-any-return]
86+
return mappings_module.RELATIONS_MAPPING # type: ignore[no-any-return]
8787
except (ImportError, AttributeError, ValueError) as exception:
8888
if mappings_module_name != "no mapping":
89-
logger.error(f"DTO_MAPPING was not imported: {exception}")
89+
logger.error(f"RELATIONS_MAPPING was not imported: {exception}")
9090
return {}
9191

9292

9393
def get_path_mapping_dict(
9494
mappings_module_name: str,
95-
) -> dict[str, ConstraintMappingType]:
95+
) -> dict[str, RelationsMappingType]:
9696
try:
9797
mappings_module = import_module(mappings_module_name)
9898
return mappings_module.PATH_MAPPING # type: ignore[no-any-return]

src/OpenApiLibCore/keyword_logic/path_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def get_valid_url(
113113
f"{path} not found in paths section of the OpenAPI document."
114114
) from None
115115

116-
constraint_mapping = path_item.constraint_mapping
117-
relations = constraint_mapping.get_path_relations() if constraint_mapping else []
116+
relations_mapping = path_item.relations_mapping
117+
relations = relations_mapping.get_path_relations() if relations_mapping else []
118118
paths = [p.path for p in relations]
119119
if paths:
120120
url = f"{base_url}{choice(paths)}"

0 commit comments

Comments
 (0)