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.

Reply via email to