from morepath.app import App
from morepath import setup

from webtest import TestApp as Client
import morepath
from morepath import generic
from reg import KeyIndex


def setup_module(module):
    morepath.disable_implicit()


def test_view_predicates():
    config = setup()

    class app(App):
        testing_config = config

    @app.path(path='')
    class Root(object):
        pass

    @app.view(model=Root, name='foo', request_method='GET')
    def get(self, request):
        return 'GET'

    @app.view(model=Root, name='foo', request_method='POST')
    def post(self, request):
        return 'POST'

    config.commit()

    c = Client(app())

    response = c.get('/foo')
    assert response.body == b'GET'
    response = c.post('/foo')
    assert response.body == b'POST'


def test_extra_predicates():
    config = setup()

    class app(App):
        testing_config = config

    @app.path(path='{id}')
    class Model(object):
        def __init__(self, id):
            self.id = id

    @app.view(model=Model, name='foo', id='a')
    def get_a(self, request):
        return 'a'

    @app.view(model=Model, name='foo', id='b')
    def get_b(self, request):
        return 'b'

    @app.predicate(generic.view, name='id', default='', index=KeyIndex,
                   after=morepath.request_method_predicate)
    def id_predicate(obj):
        return obj.id

    config.commit()

    c = Client(app())

    response = c.get('/a/foo')
    assert response.body == b'a'
    response = c.get('/b/foo')
    assert response.body == b'b'

@@ -1,20 +1,35 @@
+import dectate%0A
 from morepath.app im
@@ -263,38 +263,16 @@
 ates():%0A
-    config = setup()%0A%0A
     clas
@@ -291,39 +291,20 @@
         
-testing_config = config
+pass
 %0A%0A    @a
@@ -581,38 +581,39 @@
 'POST'%0A%0A    
-config
+dectate
 .commit(
 )%0A%0A    c = C
@@ -592,32 +592,37 @@
  dectate.commit(
+%5Bapp%5D
 )%0A%0A    c = Clien
@@ -796,30 +796,8 @@
 ():%0A
-    config = setup()%0A%0A
     
@@ -824,31 +824,12 @@
     
-testing_config = config
+pass
 %0A%0A  
@@ -1320,22 +1320,23 @@
     
-config
+dectate
 .commit(
 )%0A%0A 
@@ -1331,16 +1331,21 @@
 .commit(
+%5Bapp%5D
 )%0A%0A    c

make this work.