def _fake_json( self, status_code: int, headers: typing.Mapping[str, str], faker_data: FakerData ): entity_name = get_x( faker_data.spec.api.paths[faker_data.path_item], "x-hmt-entity" ) entity: typing.Optional[Entity] = ( self._mock_data_store[faker_data.spec.source].get_entity(entity_name) if entity_name is not None else None ) updated_data = self._update_data( faker_data.path_item, faker_data.method, faker_data.request, entity, ) faker_data = StatefulFakerData( spec=faker_data.spec, path_item=faker_data.path_item, method=faker_data.method, schema=faker_data.schema, request=faker_data.request, updated_data=updated_data, entity=entity, ) return super()._fake_json(status_code, headers, faker_data)
def reset(self): self.clear() for spec in self._specs.values(): storage = self._storages[spec.source] for entity, values in get_x(spec.api, "x-hmt-data", dict()).items(): entity = storage.get_entity(entity) for val in values: entity.insert(copy.deepcopy(val))
def add_mock(self, spec: OpenAPISpecification): """ Adds a mock. The method automatically creates entities defined in an OpenAPI spec and populates them with data if it is defined in the spec. :param mockname: a name of a mock :param spec: an OpenAPI spec """ self._specs[spec.source] = spec storage = MockData() self._storages[spec.source] = storage if spec.api.components is not None and spec.api.components.schemas is not None: for name, schema in spec.api.components.schemas.items(): if get_x(schema, "x-hmt-id-path") is not None: storage.add_entity(Entity(name, spec.api)) for entity, values in get_x(spec.api, "x-hmt-data", dict()).items(): for val in values: storage.get_entity(entity).insert(copy.deepcopy(val))
def __init__(self, name: str, spec: OpenAPIObject): self._name = name self._id_path = parse( spec.components.schemas[name]._x["x-hmt-id-path"]) self._path_config: typing.Dict[str, EntityPathItem] = {} for pathname, path_item in spec.paths.items(): if get_x(path_item, "x-hmt-entity") == self.name: self.add_path(pathname, path_item) self._data = dict()
def _update_data( self, path_item: str, method: Operation, request: Request, entity: typing.Optional[Entity], ) -> typing.Any: operation_type = ApiOperation(get_x(method, "x-hmt-operation", "unknown")) if operation_type == ApiOperation.INSERT: return entity.insert_from_request(path_item, request) elif operation_type == ApiOperation.UPSERT: return entity.upsert_from_request(path_item, request)
def _build_request_entity_selectors(self, path_item: PathItem) -> typing.Dict: res = {} for method_name in self.methods: method: Operation = getattr(path_item, method_name) operation = ( ApiOperation.UNKNOWN if method is None else ApiOperation( get_x(method, "x-hmt-operation", ApiOperation.UNKNOWN))) if operation == ApiOperation.UPSERT or operation == ApiOperation.INSERT: request_body = typing.cast(RequestBody, method.requestBody) if "application/json" in request_body.content: schema = request_body.content["application/json"].schema if schema is not None: entity_path = self._find_entity( convert_from_openapi(schema), "$") if res is not None: res[method_name] = parse(entity_path) return res