Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-12 Thread alchemy1
DBSession is like this (not even sure if I need to do it this way, just 
following the examples)
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension
()))


If change to bind=connection

self.session = DBSession(bind=connection)


then I get an error sqlalchemy.exc.InvalidRequestError: Scoped session is 
already present; no new arguments may be specified. when a second test 
method is added to the test class.

Instead of connection.begin_nested should I be using session.begin_nested?


On Monday, August 11, 2014 10:49:18 PM UTC+2, Michael Bayer wrote:

  
 On 08/11/2014 04:37 PM, alchemy1 wrote:
  
 I have combined several examples I've found to try to get the 
 'transactional-style' of unit tests to work, where you roll back the 
 database after each test. However when I run this, the test fails when 
 trying to insert the object with DBSession.add, complaining that the tables 
 don't exist. 


 are you setting up the Connection in DBSession?   I see you doing 
 something with self.session, but that is not the same session as 
 DBSession.   If DBSession is a scoped session you'd want to say 
 DBSession(bind=connection) for each test.


  I thought Base.metadata.create_all(connection) would create the tables? 
 I'd like to create the tables within the setup_module and roll it back in 
 teardown_module so that the testdb database always goes back to being 
 empty. This is to ensure that the tests are always running against a known 
 state. (Start with empty db, create the tables, do the tests, then empty 
 the db)

 Also, since a lot of copy-pasting was involved in creating this, could you 
 please take a look and see what isn't necessary? I'm just trying to do 
 simple tests (in Pyramid). For example is self.session = Session(
 connection) required? And is using the global variables the way I am a 
 good way of doing it? Quite new to this so just trying to learn the best 
 practices.


  from sqlalchemy.engine import create_engine
 from sqlalchemy.orm.session import Session

 from pyramid import testing

 from .models import Base
 from .models import DBSession

 transaction = None
 connection = None
 engine = None


 def setup_module():
 global transaction, connection, engine

 engine = create_engine(
 'postgresql+psycopg2://username:password@host:5432/testdb')
 DBSession.configure(bind=engine)
 connection = engine.connect()
 transaction = connection.begin()
 Base.metadata.create_all(connection)


 def teardown_module():
 global transaction, connection, engine
 transaction.rollback()
 connection.close()
 engine.dispose()


 class DatabaseTest(object):

 def setup(self):
 self.__transaction = connection.begin_nested()
 self.session = Session(connection)

 def teardown(self):
 self.session.close()
 self.__transaction.rollback()


 class TestCustom(DatabaseTest):

 def test_passing_view(self):
 from .models import MyModel
 model = MyModel(name='one', value=55)
 DBSession.add(model)

 from .views import my_view
 request = testing.DummyRequest()
 info = my_view(request)
 assert info['one'].name == 'one'
  




  -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+...@googlegroups.com javascript:.
 To post to this group, send email to sqlal...@googlegroups.com 
 javascript:.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.


  

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-12 Thread alchemy1
I've tried to simplify the code but it still isn't working:


from sqlalchemy.orm import scoped_session, sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension
()))
engine = create_engine('postgresql+psycopg2://..')

class DatabaseTest:

def setup_method(self, method):
self.config = testing.setUp()
self.connection = engine.connect()
self.trans = self.connection.begin()
self.session = DBSession(bind=self.connection)

def teardown_method(self, method):
self.session.remove()
self.trans.rollback()
self.connection.close()
testing.tearDown()


class TestCustom(DatabaseTest):

def test1(self):
from .models import MyModel
model = MyModel(name='one', value=1)
self.session.add(model)

from .views import my_view
request = testing.DummyRequest()
info = my_view(request)
assert info['one'].name == 'one'
assert info['one'].value == 1


And the view is :

def my_view(request):
one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
return {'one': one}

I'm getting an error:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on 
mapper Mapper|MyModel|models, SQL expression or this Session

Any ideas?


On Tuesday, August 12, 2014 11:44:05 AM UTC+2, alchemy1 wrote:

 DBSession is like this (not even sure if I need to do it this way, just 
 following the examples)
 DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension
 ()))


 If change to bind=connection

 self.session = DBSession(bind=connection)


 then I get an error sqlalchemy.exc.InvalidRequestError: Scoped session is 
 already present; no new arguments may be specified. when a second test 
 method is added to the test class.

 Instead of connection.begin_nested should I be using session.begin_nested?


 On Monday, August 11, 2014 10:49:18 PM UTC+2, Michael Bayer wrote:

  
 On 08/11/2014 04:37 PM, alchemy1 wrote:
  
 I have combined several examples I've found to try to get the 
 'transactional-style' of unit tests to work, where you roll back the 
 database after each test. However when I run this, the test fails when 
 trying to insert the object with DBSession.add, complaining that the tables 
 don't exist. 


 are you setting up the Connection in DBSession?   I see you doing 
 something with self.session, but that is not the same session as 
 DBSession.   If DBSession is a scoped session you'd want to say 
 DBSession(bind=connection) for each test.


  I thought Base.metadata.create_all(connection) would create the tables? 
 I'd like to create the tables within the setup_module and roll it back in 
 teardown_module so that the testdb database always goes back to being 
 empty. This is to ensure that the tests are always running against a known 
 state. (Start with empty db, create the tables, do the tests, then empty 
 the db)

 Also, since a lot of copy-pasting was involved in creating this, could 
 you please take a look and see what isn't necessary? I'm just trying to do 
 simple tests (in Pyramid). For example is self.session = Session(
 connection) required? And is using the global variables the way I am a 
 good way of doing it? Quite new to this so just trying to learn the best 
 practices.


  from sqlalchemy.engine import create_engine
 from sqlalchemy.orm.session import Session

 from pyramid import testing

 from .models import Base
 from .models import DBSession

 transaction = None
 connection = None
 engine = None


 def setup_module():
 global transaction, connection, engine

 engine = create_engine(
 'postgresql+psycopg2://username:password@host:5432/testdb')
 DBSession.configure(bind=engine)
 connection = engine.connect()
 transaction = connection.begin()
 Base.metadata.create_all(connection)


 def teardown_module():
 global transaction, connection, engine
 transaction.rollback()
 connection.close()
 engine.dispose()


 class DatabaseTest(object):

 def setup(self):
 self.__transaction = connection.begin_nested()
 self.session = Session(connection)

 def teardown(self):
 self.session.close()
 self.__transaction.rollback()


 class TestCustom(DatabaseTest):

 def test_passing_view(self):
 from .models import MyModel
 model = MyModel(name='one', value=55)
 DBSession.add(model)

 from .views import my_view
 request = testing.DummyRequest()
 info = my_view(request)
 assert info['one'].name == 'one'
  




  -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+...@googlegroups.com.
 To post to this group, send email to sqlal...@googlegroups.com.
 Visit this group at 

Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-12 Thread Michael Bayer

On Aug 12, 2014, at 7:26 AM, alchemy1 veerukrish...@hotmail.com wrote:

 I've tried to simplify the code but it still isn't working:
 
 
 from sqlalchemy.orm import scoped_session, sessionmaker
 from zope.sqlalchemy import ZopeTransactionExtension
 DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
 engine = create_engine('postgresql+psycopg2://..')
 
 class DatabaseTest:
 
 def setup_method(self, method):
 self.config = testing.setUp()
 self.connection = engine.connect()
 self.trans = self.connection.begin()


this line here:

 self.session = DBSession(bind=self.connection)


creates a Session() instance, which is bound to your Connection.  OK, great.   
DBSession is a scoped_session, so subsequent access to this object will give 
you that same Session object.  It's a registry, the session will stay there 
until remove() is called.  

then in this line:

 
 def my_view(request):
 one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
 return {'one': one}
 

a view somewhere is calling into DBSession and there's no bind.  But, where is 
this?  This view is in your controllers modules somewhere?   Where is it 
getting DBSession from?   Your test module?   DBSession is just a plain 
Python object like any other - if the one that you're setting up is in your 
test module, then you'd have to patch it in so that your view sees the same 
object.

What I do is, in the test fixture, locate *the actual DBSession*, and inject 
the connection into it.   Making another scoped_session somewhere else isn't 
very useful if your actual code isn't using it.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Trying to get tables to be created by unit tests

2014-08-11 Thread alchemy1
I have combined several examples I've found to try to get the 
'transactional-style' of unit tests to work, where you roll back the 
database after each test. However when I run this, the test fails when 
trying to insert the object with DBSession.add, complaining that the tables 
don't exist. I thought Base.metadata.create_all(connection) would create 
the tables? I'd like to create the tables within the setup_module and roll 
it back in teardown_module so that the testdb database always goes back to 
being empty. This is to ensure that the tests are always running against a 
known state. (Start with empty db, create the tables, do the tests, then 
empty the db)

Also, since a lot of copy-pasting was involved in creating this, could you 
please take a look and see what isn't necessary? I'm just trying to do 
simple tests (in Pyramid). For example is self.session = Session(connection) 
required? And is using the global variables the way I am a good way of 
doing it? Quite new to this so just trying to learn the best practices.


from sqlalchemy.engine import create_engine
from sqlalchemy.orm.session import Session

from pyramid import testing

from .models import Base
from .models import DBSession

transaction = None
connection = None
engine = None


def setup_module():
global transaction, connection, engine

engine = create_engine(
'postgresql+psycopg2://username:password@host:5432/testdb')
DBSession.configure(bind=engine)
connection = engine.connect()
transaction = connection.begin()
Base.metadata.create_all(connection)


def teardown_module():
global transaction, connection, engine
transaction.rollback()
connection.close()
engine.dispose()


class DatabaseTest(object):

def setup(self):
self.__transaction = connection.begin_nested()
self.session = Session(connection)

def teardown(self):
self.session.close()
self.__transaction.rollback()


class TestCustom(DatabaseTest):

def test_passing_view(self):
from .models import MyModel
model = MyModel(name='one', value=55)
DBSession.add(model)

from .views import my_view
request = testing.DummyRequest()
info = my_view(request)
assert info['one'].name == 'one'





-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-11 Thread Mike Bayer

On 08/11/2014 04:37 PM, alchemy1 wrote:
 I have combined several examples I've found to try to get the
 'transactional-style' of unit tests to work, where you roll back the
 database after each test. However when I run this, the test fails when
 trying to insert the object with DBSession.add, complaining that the
 tables don't exist.

are you setting up the Connection in DBSession?   I see you doing
something with self.session, but that is not the same session as
DBSession.   If DBSession is a scoped session you'd want to say
DBSession(bind=connection) for each test.


 I thought Base.metadata.create_all(connection) would create the
 tables? I'd like to create the tables within the setup_module and roll
 it back in teardown_module so that the testdb database always goes
 back to being empty. This is to ensure that the tests are always
 running against a known state. (Start with empty db, create the
 tables, do the tests, then empty the db)

 Also, since a lot of copy-pasting was involved in creating this, could
 you please take a look and see what isn't necessary? I'm just trying
 to do simple tests (in Pyramid). For example is |self.session
 =Session(connection)required? And is using the global variables the
 way I am a good way of doing it? Quite new to this so just trying to
 learn the best practices.
 |

 |
 fromsqlalchemy.engine importcreate_engine
 fromsqlalchemy.orm.session importSession

 frompyramid importtesting

 from.models importBase
 from.models importDBSession

 transaction =None
 connection =None
 engine =None


 defsetup_module():
 globaltransaction,connection,engine

 engine
 =create_engine('postgresql+psycopg2://username:password@host:5432/testdb')
 DBSession.configure(bind=engine)
 connection =engine.connect()
 transaction =connection.begin()
 Base.metadata.create_all(connection)


 defteardown_module():
 globaltransaction,connection,engine
 transaction.rollback()
 connection.close()
 engine.dispose()


 classDatabaseTest(object):

 defsetup(self):
 self.__transaction =connection.begin_nested()
 self.session =Session(connection)

 defteardown(self):
 self.session.close()
 self.__transaction.rollback()


 classTestCustom(DatabaseTest):

 deftest_passing_view(self):
 from.models importMyModel
 model =MyModel(name='one',value=55)
 DBSession.add(model)

 from.views importmy_view
 request =testing.DummyRequest()
 info =my_view(request)
 assertinfo['one'].name =='one'
 |





 -- 
 You received this message because you are subscribed to the Google
 Groups sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to sqlalchemy+unsubscr...@googlegroups.com
 mailto:sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com
 mailto:sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.