from morepath.app import App
from werkzeug.test import Client
from morepath import setup
from morepath.request import Response


def test_extends():
    app = App()
    extending = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(extending)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def edit_user(request, model):
        return "Edit user: %s" % model.username

    c.action(
        extending.view(
            model=User,
            name='edit'),
        edit_user)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/foo/edit')
    assert response.status == '404 NOT FOUND'

    cl = Client(extending, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/foo/edit')
    assert response.data == 'Edit user: foo'


def test_overrides_view():
    app = App()
    overriding = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(overriding)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def render_user2(request, model):
        return "USER: %s" % model.username

    c.action(
        overriding.view(
            model=User),
        render_user2)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'

    cl = Client(overriding, Response)
    response = cl.get('/users/foo')
    assert response.data == 'USER: foo'


def test_overrides_model():
    app = App()
    overriding = App(extends=[app])

    c = setup()
    c.configurable(app)
    c.configurable(overriding)

    class User(object):
        def __init__(self, username):
            self.username = username

    c.action(
        app.model(
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        User)

    def render_user(request, model):
        return "User: %s" % model.username

    c.action(
        app.view(
            model=User),
        render_user)

    def get_user(username):
        if username != 'bar':
            return None
        return User(username)

    c.action(
        overriding.model(
            model=User,
            path='users/{username}',
            variables=lambda model: {'username': model.username}),
        get_user)

    c.commit()

    cl = Client(app, Response)
    response = cl.get('/users/foo')
    assert response.data == 'User: foo'
    response = cl.get('/users/bar')
    assert response.data == 'User: bar'

    cl = Client(overriding, Response)
    response = cl.get('/users/foo')
    assert response.status == '404 NOT FOUND'
    response = cl.get('/users/bar')
    assert response.data == 'User: bar'

@@ -150,379 +150,358 @@
     
-app = App()%0A    extending = App(extends=%5Bapp%5D)%0A%0A    c = setup()%0A    c.configurable(app)%0A    c.configurable(extending)%0A%0A    class User(object):%0A        def __init__(self, username):%0A            self.username = username%0A%0A    c.action(%0A        app.model(%0A            path='users/%7Busername%7D',%0A            variables=lambda model: %7B'username': model.username%7D),%0A        
+config = setup()%0A    app = App(testing_config=config)%0A    extending = App(extends=%5Bapp%5D, testing_config=config)%0A%0A    @app.model(%0A        path='users/%7Busername%7D',%0A        variables=lambda model: %7B'username': model.username%7D)%0A    class User(object):%0A        def __init__(self, username):%0A            self.username = username%0A%0A    @app.view(model=
 User)%0A
-%0A
     
@@ -585,82 +585,48 @@
     
-c.action(%0A        app.view(%0A            model=User),%0A        render_user)%0A
+@extending.view(model=User, name='edit')
 %0A   
@@ -715,116 +715,13 @@
    c
-.action(%0A        extending.view(%0A            model=User,%0A            name='edit'),%0A        edit_user)%0A%0A    c
+onfig
 .com

Rewrite to use testing_config