"""Pylons application test package

This package assumes the Pylons environment is already loaded, such as
when this script is imported from the `nosetests --with-pylons=test.ini`
command.

This module initializes the application via ``websetup`` (`paster
setup-app`) and provides the base testing objects.
"""
from unittest import TestCase

from paste.deploy import loadapp
from paste.script.appinstall import SetupCommand
from pylons import config, url
from routes.util import URLGenerator
from webtest import TestApp

import pylons.test
from elixir import *
from floof.model import *
from floof.model import meta
from floof import model as model
from sqlalchemy import engine_from_config

__all__ = ['environ', 'url', 'TestController', 'TestModel']


# Invoke websetup with the current config file
# SetupCommand('setup-app').run([config['__file__']])

# additional imports ...
import os
from paste.deploy import appconfig
from floof.config.environment import load_environment

here_dir = os.path.dirname(__file__)
conf_dir = os.path.dirname(os.path.dirname(here_dir))

test_file = os.path.join(conf_dir, 'test.ini')
conf = appconfig('config:' + test_file)
load_environment(conf.global_conf, conf.local_conf)
environ = {}

engine = elixir.metadata.bind


def setup():
    """Setup for the entire test suite."""
    setup_all(create_tables=True)
    pass

def teardown():
    """Teardown for the entire test suite."""
    drop_all(engine)
    pass


class TestModel(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass


class TestController(TestCase):

    def setUp(self):
        """Setup for a controller test class.
        """
        pass

    def tearDown(self):
        """Teardown for a controller test class.
        """
        pass

    def __init__(self, *args, **kwargs):
        if pylons.test.pylonsapp:
            wsgiapp = pylons.test.pylonsapp
        else:
            wsgiapp = loadapp('config:%s' % config['__file__'])
        self.app = TestApp(wsgiapp)
        url._push_object(URLGenerator(config['routes.map'], environ))
        TestCase.__init__(self, *args, **kwargs)
