""" Inspired by: http://blog.ianbicking.org/2010/03/12/a-webob-app-example/
"""
from __future__ import absolute_import, print_function
from webob import Request, Response, exc
from routes import Mapper

from corker.controller import BaseController, route
from corker.app import Application

class Index(BaseController):
    @route('')
    def index(self):
        return Response('Hi index!\n')

    @route('view/{item}')
    def view(self, item):
        return Response('Hi view %r!\n' % item)

from webtest import TestApp
from nose.tools import eq_

def test_main():
    mapper = Mapper()
    Index.setup_routes(mapper)

    def bob(request, link, **config):
        def inner():
            return Response("Bob!")
        return inner

    mapper.connect('bob', '/bob/', controller=bob)

    test_app = Application(mapper)

    app = TestApp(test_app)

    ret = app.get('/view/4')
    eq_(ret.body, "Hi view u'4'!\n")

    ret = app.get('/')
    eq_(ret.body, "Hi index!\n")

    ret = app.get('/bob/')
    eq_(ret.body, "Bob!")

def test_config():    
    mapper = Mapper()

    def bob(request, link, **config):
        def inner():
            print("C:", config)
            return Response("Bob! %r" % config)
        return inner
    mapper.connect('bob', '/bob/', controller=bob)

    test_app = Application(mapper, config={'DB_URL': 'sqlite://'}) #('./')
    app = TestApp(test_app)

    ret = app.get('/bob/')
    eq_(ret.body, "Bob! {'config': {'DB_URL': 'sqlite://'}}")

def test_config_controller():
    class Index2(BaseController):
        @route('view/{item}')
        def view(self, item):
            return Response('Hi view %r!\n%r' % (item, self.bdb))
    mapper = Mapper()
    Index2.setup_routes(mapper)

    test_app = Application(mapper, bdb={'bdb': 5})
    app = TestApp(test_app)

    ret = app.get('/view/4')
    eq_(ret.body, "Hi view u'4'!\n{'bdb': 5}")


@@ -479,21 +479,26 @@
 ew %25
-r
+d
 !%5Cn' %25 
+int(
 item)
+)
 %0A%0Afr
@@ -895,32 +895,33 @@
   eq_(ret.body, 
+b
 %22Hi view u'4'!%5Cn
@@ -913,20 +913,17 @@
 Hi view 
-u'4'
+4
 !%5Cn%22)%0A%0A 
@@ -958,24 +958,25 @@
 _(ret.body, 
+b
 %22Hi index!%5Cn
@@ -1020,24 +1020,25 @@
 _(ret.body, 
+b
 %22Bob!%22)%0A%0Adef
@@ -1441,16 +1441,17 @@
 t.body, 
+b
 %22Bob! %7B'
@@ -1649,17 +1649,17 @@
 i view %25
-r
+d
 !%5Cn%25r' %25
@@ -1659,21 +1659,26 @@
 %5Cn%25r' %25 
+(int
 (item
+)
 , self.b
@@ -1864,16 +1864,17 @@
 t.body, 
+b
 %22Hi view
@@ -1878,12 +1878,9 @@
 iew 
-u'4'
+4
 !%5Cn%7B

Fix some python3 errors. Tests pass in python3, but no testing in a python 3 app yet.