import sys
import nose
from nose.tools import eq_, assert_raises

from skylines import db, app
from tests import setup_db, teardown_db
from sqlalchemy import Column, Integer, String, Unicode

context = None


def setup():
    global context
    context = app.app_context()
    context.push()

    # Setup the database
    setup_db()
    db.session.add(TestTable(name='John Doe', uni='Jane and John Doe'))
    db.session.commit()


def teardown():
    # Remove the database again
    teardown_db()

    context.pop()


class TestTable(db.Model):
    __tablename__ = 'ilike_test'

    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(String(32))
    uni = Column(Unicode(32))


def test_weighted_ilike():
    """ String.weighted_ilike() works as expected """

    eq_(db.session.query(
        TestTable.name.weighted_ilike('%John%', 1)).scalar(), 1)
    eq_(db.session.query(
        TestTable.name.weighted_ilike('%John%', 5)).scalar(), 5)
    eq_(db.session.query(
        TestTable.name.weighted_ilike('%John%', 100)).scalar(), 100)

    eq_(db.session.query(
        TestTable.name.weighted_ilike('%John%')).scalar(), 1)

    eq_(float(db.session.query(
        TestTable.name.weighted_ilike('%John%', 0.1)).scalar()), 0.1)


def test_weighted_ilike_exception():
    """ String.weighted_ilike() fails as expected """

    assert_raises(AssertionError, TestTable.name.weighted_ilike, '%John%', '5')


if __name__ == "__main__":
    sys.argv.append(__name__)
    nose.run()

@@ -189,61 +189,248 @@
 de%0A%0A
-context = None%0A%0A%0Adef setup():%0A    global context%0A    
+%0Aclass TestTable(db.Model):%0A    __tablename__ = 'ilike_test'%0A%0A    id = Column(Integer, autoincrement=True, primary_key=True)%0A    name = Column(String(32))%0A    uni = Column(Unicode(32))%0A%0A%0Aclass TestSqlLib:%0A%0A    def setup(self):%0A        self.
 cont
@@ -453,24 +453,33 @@
 ntext()%0A    
+    self.
 context.push
@@ -478,24 +478,28 @@
 ext.push()%0A%0A
+    
     # Setup 
@@ -515,16 +515,20 @@
 ase%0A    
+ 
+   
 setup_db
@@ -530,16 +530,20 @@
 up_db()%0A
+    
     db.s
@@ -606,24 +606,28 @@
  Doe'))%0A    
+  
+  
 db.session.c
@@ -635,17 +635,20 @@
 mmit()%0A%0A
-%0A
+    
 def tear
@@ -652,19 +652,27 @@
 eardown(
-):%0A
+self):%0A    
     # Re
@@ -699,16 +699,20 @@
 ain%0A    
+ 
+   
 teardown
@@ -726,210 +726,36 @@
     
-context.pop()%0A%0A%0Aclass TestTable(db.Model):%0A    __tablename__ = 'ilike_test'%0A%0A    id = Column(Integer, autoincrement=True, primary_key=True)%0A    name = Column(String(32))%0A    uni = Column(Unicode(32))%0A%0A%0A
+    self.context.pop()%0A%0A    
 def 
@@ -774,19 +774,27 @@
 d_ilike(
-):%0A
+self):%0A    
     %22%22%22 
@@ -840,24 +840,28 @@
 ed %22%22%22%0A%0A    
+ 
+   
 eq_(db.sessi
@@ -862,32 +862,36 @@
 .session.query(%0A
+    
         TestTabl
@@ -939,24 +939,28 @@
 r(), 1)%0A    
+ 
+   
 eq_(db.sessi
@@ -961,32 +961,36 @@
 .session.query(%0A
+    
         TestTabl
@@ -1038,24 +1038,28 @@
 r(), 5)%0A    
+ 
+   
 eq_(db.sessi
@@ -1060,32 +1060,36 @@
 .session.query(%0A
+    
         TestTabl
@@ -1142,24 +1142,28 @@
 , 100)%0A%0A    
+ 
+   
 eq_(db.sessi
@@ -1164,32 +1164,36 @@
 .session.query(%0A
+    
         TestTabl
@@ -1239,24 +1239,28 @@
 (), 1)%0A%0A    
+ 
+   
 eq_(float(db
@@ -1267,32 +1267,36 @@
 .session.query(%0A
+    
         TestTabl
@@ -1350,17 +1350,20 @@
 , 0.1)%0A%0A
-%0A
+    
 def test
@@ -1388,19 +1388,27 @@
 ception(
-):%0A
+self):%0A    
     %22%22%22 
@@ -1450,24 +1450,28 @@
 pected %22%22%22%0A%0A
+    
     assert_r

Use test class to get rid of global context variable