Exemple #1
0
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'
Exemple #2
0
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'
Exemple #3
0
def get_dam():
    data_manager = DataManager()
    data_manager.register_services(users=UserService().register_services(profile=UserProfileService()))
    data_manager.register_resources(
        user_db=MockStoreResource(MockStore()), user_profile_db=MockStoreResource(MockStore())
    )
    return data_manager
Exemple #4
0
def get_dam():
    data_manager = DataManager()
    data_manager.register_services(users=UserService().register_services(
        profile=UserProfileService()))
    data_manager.register_resources(
        user_db=MockStoreResource(MockStore()),
        user_profile_db=MockStoreResource(MockStore()),
    )
    return data_manager
Exemple #5
0
def test_register_unique_service():
    """
    Verify you can not register an existing service
    """
    data_manager = DataManager()
    data_manager.register_services(test=Service())

    with pytest.raises(AlreadyExistsException):
        data_manager.register_services(test=Service())
Exemple #6
0
def test_register_unique_service():
    """
    Verify you can not register an existing service
    """
    data_manager = DataManager()
    data_manager.register_services(test=Service())

    with pytest.raises(AlreadyExistsException):
        data_manager.register_services(test=Service())
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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()
Exemple #10
0
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')
Exemple #11
0
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')
Exemple #12
0
def test_meta():
    """
    Verify that meta can be given and retrieved from a context
    """
    class TestService(Service):
        def get_user(self):
            return self._ctx.meta.user

    user = object()

    dm = DataManager()
    dm.register_services(test=TestService())

    with dm.context(meta={'user': user}) as ctx:
        assert ctx.dal.test.get_user() is user

    # Verify meta can not change
    with dm.context(meta={'user': user}) as ctx:
        with pytest.raises(AttributeError):
            ctx.meta.user = '******'
        assert ctx.dal.test.get_user() is user
Exemple #13
0
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')
Exemple #14
0
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')
Exemple #15
0
def test_meta():
    """
    Verify that meta can be given and retrieved from a context
    """
    class TestService(Service):
        def get_user(self):
            return self._ctx.meta.user

    user = object()

    dm = DataManager()
    dm.register_services(test=TestService())

    with dm.context(meta={'user': user}) as ctx:
        assert ctx.dal.test.get_user() is user

    # Verify meta can not change
    with dm.context(meta={'user': user}) as ctx:
        with pytest.raises(AttributeError):
            ctx.meta.user = '******'
        assert ctx.dal.test.get_user() is user
Exemple #16
0
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()