from pyramid import testing
from sqlalchemy import engine_from_config
from pyramid.i18n import get_localizer, TranslationStringFactory
from paste.deploy.loadwsgi import appconfig
from ccvpn.models import DBSession, Base, get_user
from ccvpn import Messages, referral_handler, methods
import unittest
import os

here = os.path.dirname(__file__)

localtest = os.path.join(here, '../../', 'test.local.ini')
if os.path.isfile(localtest):
    settings = appconfig('config:' + localtest)
else:
    test = os.path.join(here, '../../', 'test.ini')
    settings = appconfig('config:' + test)

class BaseTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.engine = engine_from_config(settings, 'sqlalchemy.')

    def setUp(self):
        if not 'mako.directories' in settings:
            settings['mako.directories'] = 'ccvpn:templates/'
        if not 'mako.imports' in settings:
            settings['mako.imports'] = 'from ccvpn.filters import check'

        self.config = testing.setUp(settings=settings)
        self.config.include('pyramid_mailer.testing')
        self.config.include('pyramid_mako')
        self.config.include('pyramid_beaker')
        self.config.include('pyramid_mailer.testing')
        setup_routes(self.config)

        DBSession.remove()

        #self.engine = engine_from_config(settings, 'sqlalchemy.')
        self.conn = self.engine.connect()
        self.trans = self.conn.begin()

        DBSession.configure(bind=self.conn)
        Base.metadata.create_all(self.engine)
        self.session = DBSession

    def tearDown(self):
        testing.tearDown()
        self.trans.rollback()
        self.session.remove()
        self.conn.close()

class DummyRequest(testing.DummyRequest):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.messages = Messages(self)
        self.referrer = None
        set
        self.remote_addr = kwargs.get('remote_addr')
        self.translate = TranslationStringFactory('ccvpn')

        methods_objs = {}
        for c in methods.Method.__subclasses__():
            obj = c({})
            methods_objs[c.name] = obj
            methods_objs[c.id] = obj
        self.payment_methods = methods_objs

        referral_handler(self)

    @property
    def user(self):
        return get_user(self)

from ccvpn.tests.filters import *  # noqa
from ccvpn.tests.models import *  # noqa
from ccvpn.tests.scripts import *  # noqa
from ccvpn.tests.views_account import *  # noqa
from ccvpn.tests.views_api import *  # noqa
from ccvpn.tests.views_order import *  # noqa


@@ -304,16 +304,102 @@
 ort os%0A%0A
+%0Aclass BaseTest(unittest.TestCase):%0A    @classmethod%0A    def setUpClass(cls):%0A        
 here = o
@@ -424,16 +424,24 @@
 ile__)%0A%0A
+        
 localtes
@@ -491,16 +491,24 @@
 l.ini')%0A
+        
 if os.pa
@@ -529,24 +529,36 @@
 ltest):%0A    
+        cls.
 settings = a
@@ -593,14 +593,30 @@
 st)%0A
-else:%0A
+        else:%0A        
     
@@ -663,24 +663,36 @@
 t.ini')%0A    
+        cls.
 settings = a
@@ -723,85 +723,8 @@
 t)%0A%0A
-class BaseTest(unittest.TestCase):%0A    @classmethod%0A    def setUpClass(cls):%0A
     
@@ -751,32 +751,36 @@
 ine_from_config(
+cls.
 settings, 'sqlal
@@ -843,24 +843,29 @@
 ctories' in 
+self.
 settings:%0A  
@@ -866,32 +866,37 @@
 gs:%0A            
+self.
 settings%5B'mako.d
@@ -962,16 +962,21 @@
 rts' in 
+self.
 settings
@@ -989,16 +989,21 @@
         
+self.
 settings
@@ -1101,16 +1101,21 @@
 ettings=
+self.
 settings

Move test config loading to BaseTest class