Package: simcm
simcm 2.0.1simcm.Simulateis a context manager
which can simulate responses from external resources.Use this tool to develop unit tests
which obtain complete coverage
without using the external resource.While testing there is often
a need to simulate the reponses of calls to external resources.
Perhaps the resource is not available in the test environment,
or there is code which handles exceptional cases
which cannot be triggered on demand.A good strategy is to implement subordinate methods
so they can be tested in isolation.
Even so, the context required to test a particular method
may be difficult to arrange.In these cases, a simulator which simulates the response
is simpler to use because it lets the application run as is
and handle the (supposedly) actual response.DESCRIPTIONSimulate a series of calls to a function with a list of mocked
responses.TheSimulateclass is a context manager.
On__enter__, the target function is replaced withsimulateOn__exit__, the target function is restored.with Simulate(
        target_string,
        target_globals,
        response_list):
    test_the_application()target_string:
The function to simulate, passed as a string.target_globals:
A dict used to resolve any global references in target_string.response_list:
The list of responses thatsimulatewill return.
Each element of the list is either a callable (typically the target,
passed as a function) or a mocked response.The elements of theresponse_listare put onto a FIFO queue.simulatereads the next element from the queue.
If the element is callable,
it is called with all the arguments
the application had passed to the target,
and the result is returned;
otherwise the element is returned.There are two events to consider.simulateis called, but there is no next element.
In this case,queue_empty_on_simulate(*args, **kwargs)is called.queue_empty_on_simulateraisesQueueEmptyError.
As an aid in preparing theresponse_list,argsandkwargsare included in the exception message.__exit__is called, but elements remain in the queue.
In this case,queue_not_empty_on_exit(exception_type, exception_value, traceback)is called.queue_not_empty_on_exitraisesQueueNotEmptyError.
As an aid in preparing theresponse_list,
the number of elements remaining in the queue
is included in the exception message.Both exceptions are sub-classes ofSimulateError.To change this behavior,
sub-classSimulate, and overwrite these methods.
Ifqueue_empty_on_simulatereturns,
it should return a callable or a mocked response.EXAMPLEWhen the test is run,
the first GET request is sent
and the actual response is returned.
The second GET request is not sent,
the MockedResponse is returned instead.my_app.pyimport requests

def my_app():
    response1 = requests.request(
            method='GET',
            url='https://pypi.org')
    if response1.status_code == 200:
        response2 = requests.request(
                method='GET',
                url='http://google.com')
        if reponse2.status_code == 500:
            raise RuntimeError('Google 500')
    else:
        raise RuntimeError('Pypi not 200')test_my_app.pyimport pytest
import requests
import simcm

import my_app

class MockedResponse:
    def __init__(self, status_code, text):
        self.status_code = status_code
        self.text=text

def test_my_app_google_500():
    with pytest.raises(RuntimeError) as exc: 
        with simcm.Simulate(
                target_string='requests.request',
                target_globals=dict(requests=requests),
                response_list=[
                        requests.request,
                        MockedResponse(status_code=500, text='')]):
            my_app.my_app()
    result = str(exc.value)
    expect = 'Google 500'
    assert result == expect, f'result: {result}'CLASSESbuiltins.Exception(builtins.BaseException)
        SimulateError
            QueueEmptyError
            QueueNotEmptyError
    builtins.object
        Simulate
    
    class QueueEmptyError(SimulateError)
     |  queue empty
     |  
     |  Method resolution order:
     |      QueueEmptyError
     |      SimulateError
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
    class QueueNotEmptyError(SimulateError)
     |  queue not empty
     |  
     |  Method resolution order:
     |      QueueNotEmptyError
     |      SimulateError
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
    class Simulate(builtins.object)
     |  Simulate(target_string: str, target_globals: dict, response_list: list = None)
     |  
     |  Create a context which replaces the target callable
     |  with self.simulate.
     |  
     |  Methods defined here:
     |  
     |  __enter__(self)
     |      Save target.
     |      Replace target with self.simulate.
     |      Return self.
     |  
     |  __exit__(self, exception_type, exception_value, traceback)
     |      Restore target.
     |      Call queue_not_empty_on_exit if the queue is not empty.
     |  
     |  __init__(self, target_string: str, target_globals: dict, response_list: list = None)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  enqueue(self, response_list=None)
     |      Put response_list items onto self.queue.
     |      Default is self.response_list.
     |  
     |  queue_empty_on_simulate(self, *args, **kwargs)
     |      What to do when the queue is empty
     |      and simulate is called.
     |      Raise QueueEmptyError.
     |  
     |  queue_not_empty_on_exit(self, exception_type, exception_value, traceback)
     |      What to do when the queue is not empty on exit.
     |      Raise QueueNotEmptyError.
     |  
     |  simulate(self, *args, **kwargs)
     |      Interpret the next response.
     |      If it is callable, call it, and return the result;
     |      otherwise return it.
     |  
     |  ----------------------------------------------------------------------
    class SimulateError(builtins.Exception)
     |  base error
     |  
     |  Method resolution order:
     |      SimulateError
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |