Exemplo n.º 1
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
Exemplo n.º 2
0
def test_resource_setup_and_teardown():
    """
    Verify that Resources are setup and torndown.
    """
    class TestResource(object):
        """
        Example resource that sets as state at
        setup/teardown.
        """
        def __init__(self, store):
            self._store = store

        def __call__(self, context):
            self._store['state'] = 'setup'
            yield self._store['state']
            self._store['state'] = 'torndown'

    store = {}
    data_manager = DataManager()
    data_manager.register_resources(
        test_state=TestResource(store)
    )

    with data_manager.context() as ctx:
        assert 'test_state' not in ctx, 'Resource should be lazy created'
        assert ctx.test_state == 'setup', 'Resource should have been setup'

    assert store['state'] == 'torndown', 'Resource should have been torndown'
    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 3
0
def test_resource_that_has_setup_error():
    """
    Verify that a resource that raises an exception on setup
    prevents dal context from continuing.
    """
    class SetupError(Exception):
        pass

    def error_resource(context):
        if True:
            raise SetupError()
        pytest.fail('Should never get to the point of yielding')
        yield
        pytest.fail('Should never recover')

    data_manager = DataManager()
    data_manager.register_resources(test=error_resource)

    with pytest.raises(SetupError):
        with data_manager.context() as ctx:
            try:
                assert ctx.test
            except SetupError:
                # Expected
                raise
            else:
                pytest.fail('Should have raised SetupError inside context')

    # Even though an error occurred during setup, the resource
    # never has a chance to recover so can't generate any new
    # errors on exit
    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 4
0
def test_resource_setup_and_teardown():
    """
    Verify that Resources are setup and torndown.
    """
    class TestResource(object):
        """
        Example resource that sets as state at
        setup/teardown.
        """
        def __init__(self, store):
            self._store = store

        def __call__(self, context):
            self._store['state'] = 'setup'
            yield self._store['state']
            self._store['state'] = 'torndown'

    store = {}
    data_manager = DataManager()
    data_manager.register_resources(test_state=TestResource(store))

    with data_manager.context() as ctx:
        assert 'test_state' not in ctx, 'Resource should be lazy created'
        assert ctx.test_state == 'setup', 'Resource should have been setup'

    assert store['state'] == 'torndown', 'Resource should have been torndown'
    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 5
0
def test_resource_that_has_setup_error():
    """
    Verify that a resource that raises an exception on setup
    prevents dal context from continuing.
    """
    class SetupError(Exception):
        pass

    def error_resource(context):
        if True:
            raise SetupError()
        pytest.fail('Should never get to the point of yielding')
        yield
        pytest.fail('Should never recover')

    data_manager = DataManager()
    data_manager.register_resources(test=error_resource)

    with pytest.raises(SetupError):
        with data_manager.context() as ctx:
            try:
                assert ctx.test
            except SetupError:
                # Expected
                raise
            else:
                pytest.fail('Should have raised SetupError inside context')

    # Even though an error occurred during setup, the resource
    # never has a chance to recover so can't generate any new
    # errors on exit
    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 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())
Exemplo n.º 7
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'
Exemplo n.º 8
0
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
Exemplo n.º 9
0
def test_register_unique_resource():
    """
    Verify you can not register an existing resource
    """
    def resource(context):
        yield

    data_manager = DataManager()
    data_manager.register_resources(test=resource)

    with pytest.raises(AlreadyExistsException):
        data_manager.register_resources(test=resource)
Exemplo n.º 10
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'
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
def test_resource_that_yields_too_much():
    """
    Verify that a resource that yields more
    than once raises a RuntimeError, but is suppressed
    """
    def chatty_resource(context):
        """
        Resource should only yield once
        """
        yield True
        yield False

    data_manager = DataManager()
    data_manager.register_resources(test=chatty_resource)

    with data_manager.context() as ctx:
        assert ctx.test

    assert ctx.get_resource_exit_errors()[0][0] is RuntimeError
Exemplo n.º 14
0
def test_resource_that_yields_too_much():
    """
    Verify that a resource that yields more
    than once raises a RuntimeError, but is suppressed
    """
    def chatty_resource(context):
        """
        Resource should only yield once
        """
        yield True
        yield False

    data_manager = DataManager()
    data_manager.register_resources(test=chatty_resource)

    with data_manager.context() as ctx:
        assert ctx.test

    assert ctx.get_resource_exit_errors()[0][0] is RuntimeError
Exemplo n.º 15
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()
Exemplo n.º 16
0
def test_resource_active_context():
    """
    Verify that Resources can not be used if the context is not active.
    """
    data_manager = DataManager()
    data_manager.register_resources(test=ValueResource(True))

    ctx = data_manager.context()

    with pytest.raises(RuntimeError):
        assert ctx.test
        pytest.fail('Resource should not be useable when context has not been setup')

    with ctx:
        assert ctx.test is True

    assert len(ctx.get_resource_exit_errors()) == 0

    with pytest.raises(RuntimeError):
        assert ctx.test
        pytest.fail('Resource should not be useable when context has been torndown')
Exemplo n.º 17
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
Exemplo n.º 18
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()
Exemplo n.º 19
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
Exemplo n.º 20
0
def test_resource_that_does_not_yield():
    """
    Verify that a resource that does not yield raises a
    ResourceSetupException and ends the context
    """
    def bogus_resource(context):
        """
        This is a valid generator function because of the ``yield``
        but the ``yield`` will never occur so this is an invalid resource.
        Resources must ``yield`` once to be valid.
        """
        if False:
            yield

    data_manager = DataManager()
    data_manager.register_resources(test=bogus_resource)

    with pytest.raises(ResourceSetupException):
        with data_manager.context() as ctx:
            assert ctx.test
            pytest.fail('Context should not have continued')

    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 21
0
def test_resource_active_context():
    """
    Verify that Resources can not be used if the context is not active.
    """
    data_manager = DataManager()
    data_manager.register_resources(test=ValueResource(True))

    ctx = data_manager.context()

    with pytest.raises(RuntimeError):
        assert ctx.test
        pytest.fail(
            'Resource should not be useable when context has not been setup')

    with ctx:
        assert ctx.test is True

    assert len(ctx.get_resource_exit_errors()) == 0

    with pytest.raises(RuntimeError):
        assert ctx.test
        pytest.fail(
            'Resource should not be useable when context has been torndown')
Exemplo n.º 22
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())
Exemplo n.º 23
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
Exemplo n.º 24
0
def test_resource_that_does_not_yield():
    """
    Verify that a resource that does not yield raises a
    ResourceSetupException and ends the context
    """
    def bogus_resource(context):
        """
        This is a valid generator function because of the ``yield``
        but the ``yield`` will never occur so this is an invalid resource.
        Resources must ``yield`` once to be valid.
        """
        if False:
            yield

    data_manager = DataManager()
    data_manager.register_resources(test=bogus_resource)


    with pytest.raises(ResourceSetupException):
        with data_manager.context() as ctx:
            assert ctx.test
            pytest.fail('Context should not have continued')

    assert len(ctx.get_resource_exit_errors()) == 0
Exemplo n.º 25
0
def test_register_unique_resource():
    """
    Verify you can not register an existing resource
    """
    def resource(context):
        yield

    data_manager = DataManager()
    data_manager.register_resources(test=resource)

    with pytest.raises(AlreadyExistsException):
        data_manager.register_resources(test=resource)
Exemplo n.º 26
0
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
Exemplo n.º 27
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
Exemplo n.º 28
0
def test_replace_unique_resource():
    """
    Verify you can replace an existing resource
    """
    def resource_a(context):
        yield 'a'

    def resource_b(context):
        yield 'b'

    data_manager = DataManager()
    data_manager.register_resources(test=resource_a)

    data_manager.replace_resource('test', resource_b)

    with data_manager.context() as ctx:
        assert ctx.test == 'b'
Exemplo n.º 29
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')
Exemplo n.º 30
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')
Exemplo n.º 31
0
def test_replace_unique_resource():
    """
    Verify you can replace an existing resource
    """

    def resource_a(context):
        yield 'a'

    def resource_b(context):
        yield 'b'

    data_manager = DataManager()
    data_manager.register_resources(test=resource_a)

    data_manager.replace_resource('test', resource_b)

    with data_manager.context() as ctx:
        assert ctx.test == 'b'
Exemplo n.º 32
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')
Exemplo n.º 33
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
Exemplo n.º 34
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')