kdani
kdani

Reputation: 847

Accessing test database in (Selenium based) functional tests for a Pyramid + sqlalchemy application

I have managed to create functional tests using Splinter (Selenium) and StoppableWSGIServer. Here's my code:

...

engine = engine_from_config(settings, prefix='sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)

...

class FunctionalTest(...):
    ...

    def setUp(self):
        ...
        self.server = http.StopableWSGIServer.create(app)
        self.server.wait()
        self.browser = splinter.Browser("chrome")

    def tearDown(self):
        ...
        self.browser.quit()
        self.server.shutdown()

Where app is created using Configurator.make_wsgi_app.

When running test cases using my FunctionalTest, the browsers shows up, the server starts the database works, the tables are created. However, the test server can't access rows created in the test cases, even though both are initialized using the same settings file.

I have tried mocking DBSession and engine in my models.py and views.py and this way both DBSession and Base.metadata.bind have the very same id() in both my test cases and my view functions. (So, to my understanding, are the very same objects) Yet, querying for rows created in the test cases returns [] in the view, and the tests fail. I have called DBSession.flush() after creating the rows and DBSession is defined in models.py like this:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

How do I make the test server see rows created in the test case code?

Upvotes: 1

Views: 132

Answers (1)

kdani
kdani

Reputation: 847

This seems to do the trick:

import trasaction
transaction.commit()

However I'm not absolutely certain about my implementation, so I'm still awaiting answers. Also, this way I have to DBSession.drop_all before each test case.

Upvotes: 1

Related Questions