# coding: utf-8
import pytest
from sqlalchemy.orm import sessionmaker
from webtest import TestApp

from bottle_application import app, engine, User


@pytest.fixture(scope="function")
def session(request):
    return sessionmaker(bind=engine)()


@pytest.fixture(scope="function")
def app_test(request):
    app_test = TestApp(app)
    return app_test


def test_routes(session, app_test):
    assert app_test.get('/admin').status == '200 OK'
    assert app_test.get('/admin/user').status == '200 OK'
    assert app_test.get('/admin/user/add').status == '200 OK'
    assert app_test.get('/admin/user/delete/1').status == '200 OK'
    assert app_test.get('/admin/product').status == '200 OK'
    assert app_test.get('/admin/product/add').status == '200 OK'
    assert app_test.get('/admin/product/delete/1').status == '200 OK'


def test_delete_model_controller(session, app_test):
    app_test.get('/admin/user/delete/1').mustcontain('has not been found')

    user1 = User(name='name', fullname='full', password='pass')
    session.add(user1)
    user2 = User(name='name2', fullname='full', password='pass')
    session.add(user2)
    session.commit()

    status = app_test.get('/admin/user/delete/{0}'.format(user1.id)).status
    assert status == '302 Found'
    request = app_test.get('/admin/user/delete/{0}'.format(user1.id))
    request.mustcontain('has not been found')

    status = app_test.get('/admin/user/delete/{0}'.format(user2.id)).status
    assert status == '302 Found'
    request = app_test.get('/admin/user/delete/{0}'.format(user2.id))
    request.mustcontain('has not been found')

@@ -82,16 +82,26 @@
 t import
+ AppError,
  TestApp
@@ -213,22 +213,25 @@
 t):%0A    
-return
+session =
  session
@@ -251,16 +251,68 @@
 gine)()%0A
+    session.query(User).delete()%0A    return session%0A
 %0A%0A@pytes
@@ -425,14 +425,32 @@
 est_
-routes
+add_model_get_controller
 (ses
@@ -474,380 +474,212 @@
     
-assert app_test.get('/admin').status == '200 OK'%0A    assert app_test.get('/admin/user').status == '200 OK'%0A    assert app_test.get('/admin/user/add').status == '200 OK'%0A    assert app_test.get('/admin/user/delete/1').status == '200 OK'%0A    assert app_test.get('/admin/product').status == '200 OK'%0A    assert app_test.get('/admin/product/add').status == '200 OK'%0A    assert
+# TODO: tests with selenium or similar tool%0A    assert app_test.get('/admin/user/add').status == '200 OK'%0A%0A%0Adef test_add_model_post_controller(session, app_test):%0A    with pytest.raises(AppError):%0A       
  app
@@ -676,34 +676,35 @@
        app_test.
-ge
+pos
 t('/admin/produc
@@ -701,24 +701,16 @@
 min/
-product/delete/1
+user/add
 ').s
@@ -1162,37 +1162,38 @@
 02 Found'%0A    re
-quest
+sponse
  = app_test.get(
@@ -1233,37 +1233,38 @@
 ser1.id))%0A    re
-quest
+sponse
 .mustcontain('ha
@@ -1488,28 +1488,1075 @@
 ntain('has not been found')%0A
+%0A%0Adef test_list_model_controller(session, app_test):%0A    response = app_test.get('/admin/user')%0A    response.mustcontain('not found')%0A%0A    user1 = User(name='name', fullname='full', password='pass')%0A    session.add(user1)%0A    session.commit()%0A%0A    response = app_test.get('/admin/user')%0A    rows = len(response.html.find_all('tr'))%0A    assert rows == 2%0A%0A    user2 = User(name='name2', fullname='full', password='pass')%0A    session.add(user2)%0A    session.commit()%0A%0A    response = app_test.get('/admin/user')%0A    rows = len(response.html.find_all('tr'))%0A    assert rows == 3%0A%0A%0Adef test_routes(session, app_test):%0A    assert app_test.get('/admin').status == '200 OK'%0A    assert app_test.get('/admin/user').status == '200 OK'%0A    assert app_test.get('/admin/user/add').status == '200 OK'%0A    assert app_test.get('/admin/user/delete/1').status == '200 OK'%0A    assert app_test.get('/admin/product').status == '200 OK'%0A    assert app_test.get('/admin/product/add').status == '200 OK'%0A    assert app_test.get('/admin/product/delete/1').status == '200 OK'%0A

Improve tests