def test_unique_context(): """ Ensure that we get a new context on each DAL enter. """ data_manager = DataManager() with data_manager.dal(): context1 = data_manager.get_active_context() with data_manager.dal(): context2 = data_manager.get_active_context() assert context1 != context2
def test_middleware(): """ Verify that Middleware can access Resources and that the Middleware is setup and torndown. """ class TestMiddleware(object): """ Example middleware that sets it's state at setup and teardown. """ def __call__(self, context): context.store['state'] = 'setup' yield context.store['state'] = 'torndown' data_store = {} data_manager = DataManager() data_manager.register_services(test=StateTestService()) # Middleware that starts and ends with the context data_manager.register_context_middleware(TestMiddleware(), ) data_manager.register_resources(store=ValueResource(data_store)) with data_manager.dal() as dal: assert dal.test.get_state() == 'setup' dal.test.change_state('middle') assert dal.test.get_state() == 'middle' assert data_store['state'] == 'torndown'
def test_middleware(): """ Verify that Middleware can access Resources and that the Middleware is setup and torndown. """ class TestMiddleware(object): """ Example middleware that sets it's state at setup and teardown. """ def __call__(self, context): context.store['state'] = 'setup' yield context.store['state'] = 'torndown' data_store = {} data_manager = DataManager() data_manager.register_services(test=TestService()) # Middleware that starts and ends with the context data_manager.register_context_middleware( TestMiddleware(), ) data_manager.register_resources( store=ValueResource(data_store) ) with data_manager.dal() as dal: assert dal.test.get_state() == 'setup' dal.test.change_state('middle') assert dal.test.get_state() == 'middle' assert data_store['state'] == 'torndown'
def test_replace_unique_service(): """ Verify you can replace an existing service """ data_manager = DataManager() data_manager.register_services(test=Service()) new_service = Service() data_manager.replace_service('test', new_service) with data_manager.dal() as dal: assert dal.test is new_service
def test_middleware_that_yields_too_much(): """ Verify that middleware that yields more than once raises a RuntimeError """ def chatty_middleware(context): """ Middleware should only yield once """ yield yield data_manager = DataManager() data_manager.register_context_middleware(chatty_middleware) with pytest.raises(RuntimeError): with data_manager.dal(): pass
def test_service_context(): """ Verify that if you access a Service's context when no context is active, you get a RuntimeError. """ class TestService(Service): def get_context(self): return self._ctx data_manager = DataManager() data_manager.register_services(test=TestService()) with data_manager.dal() as dal: ctx = dal.test.get_context() get_context = dal.test.get_context assert isinstance(ctx, DataAccessContext) with pytest.raises(RuntimeError): get_context()
def test_middleware_that_has_setup_error(): """ Verify that middleware that raises an exception on setup prevents dal context from starting """ class SetupError(Exception): pass def error_middleware(context): if True: raise SetupError() yield data_manager = DataManager() data_manager.register_services(test=StateTestService()) data_manager.register_context_middleware(error_middleware) with pytest.raises(SetupError): with data_manager.dal(): pytest.fail('Context should not have continued')
def test_middleware_that_has_setup_error(): """ Verify that middleware that raises an exception on setup prevents dal context from starting """ class SetupError(Exception): pass def error_middleware(context): if True: raise SetupError() yield data_manager = DataManager() data_manager.register_services(test=TestService()) data_manager.register_context_middleware(error_middleware) with pytest.raises(SetupError): with data_manager.dal(): pytest.fail('Context should not have continued')
def test_middleware_that_does_not_yield(): """ Verify that middleware that does not yield raises a MiddlewareSetupException and prevents dal context from starting. """ def bogus_middleware(context): """ This is a valid generator function because of the ``yield`` but the ``yield`` will never occur so this is invalid middleware. Middleware must ``yield`` once to be valid. """ if False: yield data_manager = DataManager() data_manager.register_services(test=StateTestService()) data_manager.register_context_middleware(bogus_middleware) with pytest.raises(MiddlewareSetupException): with data_manager.dal(): pytest.fail('Context should not have continued')
def test_middleware_that_does_not_yield(): """ Verify that middleware that does not yield raises a MiddlewareSetupException and prevents dal context from starting. """ def bogus_middleware(context): """ This is a valid generator function because of the ``yield`` but the ``yield`` will never occur so this is invalid middleware. Middleware must ``yield`` once to be valid. """ if False: yield data_manager = DataManager() data_manager.register_services(test=TestService()) data_manager.register_context_middleware(bogus_middleware) with pytest.raises(MiddlewareSetupException): with data_manager.dal(): pytest.fail('Context should not have continued')