def test_args():
    class TestView(simple.SimpleView):
        args = ['id']
        def __call__(self, foo):
            return self.request, self.id, foo
    testview = create_view(TestView)
    assert testview('request', 1, 'bar') == ('request', 1, 'bar')
def test_view_functions_are_inherited():
    """A class shares all the view functions of it's bases.
    """
    class TestView(DummyBase):
        pass
    testview = create_view(TestView)
    assert testview.foo() == 42
def test_kwargs():
    class TestView(simple.SimpleView):
        kwargs = {'search_order': 'ASC'}
        def __call__(self):
            return self.search_order
    testview = create_view(TestView)
    assert testview('request') == 'ASC'
    assert testview('request', search_order='DSC') == 'DSC'
def test_processing_hooks_are_inherited():
    """Processing hooks are inherited from base classes if missing.
    """
    class TestView(DummyBase):
        def __call__(self, *args, **kwargs):
            return self.count
    testview = create_view(TestView)
    assert [testview(), testview(), testview()] == [2, 4, 6]
def test_view_functions_can_be_overridden():
    """A class can override view functions of it's bases.

    Also tests that super() calls work.
    """
    class TestView(DummyBase):
        def foo(self, *args, **kwargs):
            return super(TestView, self).foo(*args, **kwargs) - 1
    testview = create_view(TestView)
    assert testview.foo() == 40
def test_proxy_what_attributes_are_wrapped():
    """Private and non-callable attributes of a view are not wrapped.
    """
    class TestView(View):
        foo = 1
        def _bar(self, *args, **kwargs):
            pass
    testview = create_view(TestView)
    assert not hasattr(testview, 'foo')
    assert not hasattr(testview, '_bar')
def test_before_can_return_response():
    """If the before-hook returns a value, the view-function
    is never called.
    """
    class TestView(View):
        def __before__(self, args, kwargs):
            return 99
        def __call__(self, *args, **kwargs):
            return 42
    testview = create_view(TestView)
    assert testview() == 99
def test_processing_hooks_can_be_overridden():
    """Base-class processing hooks can be overridden in child classes.

    Also tests that super() calls work.
    """
    class TestView(DummyBase):
        def __after__(self, response):
            return super(TestView, self).__after__(response) - 1
        def __call__(self, *args, **kwargs):
            return self.count
    testview = create_view(TestView)
    assert [testview(), testview(), testview()] == [1, 3, 5]
def test_before_can_modify_args():
    """Test that the before-hook can modify the call arguments.
    """
    class TestView(View):
        def __before__(self, args, kwargs):
            while args: args.pop(0)
            kwargs['newarg'] = 'foo'
        def __call__(self, *args, **kwargs):
            return len(args), len(kwargs)
    testview = create_view(TestView)
    assert testview() == (0,1)
    assert testview('a', 'b', bla='blub') == (0,2)
def test_after_is_called():
    """Test that the after-hook is called normally.
    """
    class TestView(View):
        def __after__(self, response):
            return response * 2
        def __call__(self, *args, **kwargs):
            return 3
        def foo(self, *args, **kwargs):
            return 4
    testview = create_view(TestView)
    assert testview() == 6
    assert testview.foo() == 8
def test_init_called_on_creation():
    """Check that __init__ is only called when the view is constructed,
    not when it is actually executed.
    """
    class TestView(View):
        count = 0
        def __init__(self):
            self.count += 1
        def __call__(self, *args, **kwargs):
            return self.count
    testview = create_view(TestView)
    assert testview._instance.count == 1   # also tests access to ._instance
    assert testview() == 1
def test_before_is_called():
    """Test that the before-hook is called normally..
    """
    class TestView(View):
        def __before__(self, args, kwargs):
            self.dummy = 42
        def __call__(self, *args, **kwargs):
            return self.dummy
        def foo(self, *args, **kwargs):
            return self.dummy * 2
    testview = create_view(TestView)
    assert testview() == 42
    assert testview.foo() == 84
def test_mixed():
    """Test positional arguments passed via keyword.
    """
    class TestView(simple.SimpleView):
        args = ['id']
        def __call__(self, foo):
            return self.request, self.id, foo
    testview = create_view(TestView)

    # generally is a possibility...
    assert testview('request', foo='bar', id=1) == ('request', 1, 'bar')
    # ...but only if the correct order is maintained: here 'id' would
    # have two values, 'bar' and 1.
    assert_raises(TypeError, testview, 'request', 'bar', id=1)
def test_nested_views():
    """Nested view classes work as expected.
    """
    class TestView(View):
        def __after__(self, response):
            return response * 2
        class sub(View):
            def __call__(self, *args, **kwargs):
                return 1
            def foo(self, *args, **kwargs):
                return 42
    testview = create_view(TestView)

    # the nested view is usable normally; processing hooks of
    # outer levels are not called (TODO: but should they be?)
    assert testview.sub() == 1
    assert testview.sub.foo() == 42