curia.mock.api_server module¶
- curia.mock.api_server.build_mock_arguments(method: callable) Dict[str, Any] ¶
Builds a dictionary of mock arguments for a method, using the “inspect” module to parse the method signature There is no guarantee that this will work for all methods, but it should work for the ones we use in the PlatformApi
- Parameters:
method – Method to build mock arguments for
- Returns:
Dictionary of mock arguments. If the method has a default value for an argument, the default value is used.
Otherwise, a MagicMock is used.
- curia.mock.api_server.match_call_pattern_to_method_name(call_pattern: Tuple[str, str], call_pattern_to_method_name: Dict[Tuple, str]) Tuple[str, dict] ¶
Matches a URL call pattern to a method name, e.g. (/models/some-id-here, GET) -> get_one_base_model_controller_model :param call_pattern: Realized call pattern to match, e.g. (/models/some-id-here, GET) :param call_pattern_to_method_name: Dictionary of unrealized call patterns (e.g. /models/{id}) to method names :return: Method name that matches the call pattern
- class curia.mock.api_server.Struct(**entries)¶
Bases:
object
A simple class that allows for attribute access to a dictionary
- to_dict() dict ¶
- class curia.mock.api_server.MockResponseMapper¶
Bases:
object
Builds a function capable of handling responses from the mock server in mock.server.py by hijacking the actual api instance and checking which requests are made to the server.
- get_api_client_hijacker() PlatformApi ¶
Returns an instance of the api client that has been hijacked to record the last call made to the server :return: Hijacked api instance with a hijacked api client
- response_map(request: ParseAndExtractDataSummary) dict | list ¶
Performs the actual mapping of the request to a response. This is the function that is called by the mock server to determine the response to a request. We first match the request to a method name, then we call the method on the mock api instance and return the result, which is then serialized to JSON and returned to the client.
The request’s body is prepped to be passed to the mock api instance by converting it to an object with attributes named using snake case. This is done because the mock api instance expects the request body to be an object with attributes named using snake case, but the client sends the request body as a dictionary with attributes named using lower camel case.
The result of the mock api instance method is also prepped to be returned to the client by converting it to a dictionary with attributes named using lower camel case. This is done because the client expects the response body to be JSON with attributes named using lower camel case, but the mock api instance returns the response body as an object with attributes named using snake case.
- Parameters:
request – The request to map to a response
- classmethod get() MockResponseMapper ¶
Singleton getter
- Returns:
MockResponseMapper instance. Note that this is a singleton, so the same instance will be returned
every time. This is necessary because the response_map callable is stateless, but we don’t want to set up the same mock api instance and call pattern mapping every time we need to use it.
- curia.mock.api_server.prep_for_obj(obj: Any) Any ¶
Prepares a dict for use as an object by converting all keys to snake case. This is necessary because the SDK models use snake case, but the actual api uses lower camel case. The outputs of this function are used to create SDK models, so they must match the format of the SDK models.
- Parameters:
obj – Object to prepare. Can be a dict, list, or a single value (str, int, etc.)
- Returns:
object prepared for use as an SDK model
- curia.mock.api_server.prep_for_json(obj: Any) Any ¶
Prepares a model class for json serialization and return to the client by: - Converting objects to dicts using their attributes listed in the attribute_map - Converting the attribute names according to the attribute_map of the object - Recursively applying the above to all values in a dict, list, or sub-object
This is necessary because the SDK models generally snake case, but the actual api generally uses lower camel case. The outputs of this function are returned to the client as json, so they must be in the same format as the actual api. We use the attribute_map of the SDK models to convert the attribute names to their API equivalents.
The objects also have a to_dict method, but this method is overzealous and recursively converts all sub-objects to dicts, which is not desirable because then we can’t tell how to convert the attribute names of the sub-objects as they lose the attribute_map of their class.
Instead, we rely on this function to eventually convert the sub-objects to dicts, but only when it is safe to do so and the attribute names can be converted correctly.
- Parameters:
obj – Object to prepare. Can be a dict, list, object with attribute_map, or a single value (str, int, etc.)
- Returns:
object prepared for json serialization
- curia.mock.api_server.mock_api_request_handler_response_map(request: ParseAndExtractDataSummary, metadata: MockApiConfiguration) dict | list ¶
This function is compatible with the response_map parameter of the TestServer class in mock.server.py. When using the TestServer class, the metadata parameter should be a MockApiConfiguration instance specifying the initial state of the mock api instance which will be utilized by the MockApiInstance class under the hood. :param request: The request to handle. Will be automatically passed by the TestServer class. :param metadata: The MockApiConfiguration instance specifying the initial state of the MockApiInstance utilized under the hood. The value of this parameter will be determined by the response_map_metadata parameter of the TestServer class, or by the response_map_metadata parameter of the configure_mock_api_server function.
- Returns:
The response to return to the client. Should be a dict or list, turnable into json.
- curia.mock.api_server.build_call_pattern_dict(hijacked_api_instance: PlatformApi, methods: list) Dict[Tuple[str, str], str] ¶
Builds a dictionary that maps a call pattern (resource path, method) to a method name in the PlatformApi class and thereby to a method in the MockApiInstance class. This is done by hijacking the api client of the PlatformApi instance and checking which requests are made to the server when a method is called. The call pattern is then mapped to the method name of the method that was called.
In order to call the methods of the PlatformApi instance, mock arguments are built for each method. These mock arguments are built by inspecting the method signature and building mock arguments for each parameter. :param hijacked_api_instance: The PlatformApi instance. The api client of this instance should already be hijacked with an ApiClientHijacker :param methods: A list of method names in the PlatformApi class that should be called in order to build the call pattern dictionary. Method names that end with “_with_http_info” are ignored as these methods are not intended to be called directly and are not mocked in the MockApiInstance class, so calling them would result in an error.
- Returns:
A dictionary mapping a call pattern (resource path, method) to a method name in the PlatformApi class
- curia.mock.api_server.configure_mock_api_server(seed_data: List[object], method_overrides: Dict[str, Callable], port=3000, log_dir='temp/') Callable ¶
Decorator for test functions that configures a mock api server for the duration of the test function. The mock api server is configured to contain the seed data provided in its database. In a similar manner to MockApiInstance, the mock api server can be configured to override the functionality of certain methods by providing a callable for the method in the method_overrides dict. The mock api server is configured to run on the port provided and to log requests to the log_dir provided.
Under the hood, this decorator uses the MockApiInstance class to simulate the functionality of the api server. So when a method on a client’s real sdk is called, the client’s sdk will translate the method call into a request to the mock api server. The mock api server will then translate the request back into its original method call and execute the method call on the MockApiInstance instance. The MockApiInstance instance will then execute the inferred or supplied method override, if any, and return the result to the mock server, which will then convert the result (an object) into the appropriate json response and return it to the client’s sdk, which will then convert the json response into an object and return it to the client.
This decorator combines the functionality of the MockApiInstance class and using_test_server decorator.
The using_test_server decorator can be used to set up an arbitrary mock server for testing purposes. However, it requires the user to provide a response_map callable that maps requests to responses. This decorator provides a default response_map callable that maps requests to responses based on the seed data and method overrides provided, using the MockApiInstance class.
- Parameters:
seed_data – List of objects to seed the mock api server with. The objects should be instances of
the classes in the curia.api.swagger_client.models package. :param method_overrides: Dict of method names to override with custom functions. The method names should be the same as the method names in the PlatformApi class. The custom functions should take the same arguments as the original methods and return the same type as the original methods. The custom functions should not call the original methods. :param port: Port to run the mock api server on :param log_dir: Directory to log requests to