When I use Fixture with SqlAlchemy in my unit tests, why am I unable to confirm changes to the database during test?

StackOverflow https://stackoverflow.com/questions/9999765

Pergunta

I am testing a message processer that uses SqlAlchemy (v0.7.4). In my test, I am using Fixture (v1.4) with Sqlite to set up and tear down a temporary database. My fixture data includes a file table with a status field that should get updated when the processor runs.

I have confirmed that the test, the processor being tested, and the fixture are all sharing the same database session.

I query the status field on the file record before the processor is run and afterwards. The value should change (from an int representing "Processing" to "Complete"). I have added debug code within the processor to verify that the field is being updated with the correct new status value. I am also able to independently verify that the processor runs successfully by checking the contents of an output file it produces. However, when I query the status at the end of my test using my test's database session, it is always the same as the value at the beginning.

I have tried explicitly committing and flushing the session before the final status query. Nothing works. Any ideas?

Foi útil?

Solução

The issue here was twofold: 1) My test was using a temporary Sqlite database in memory. 2) Within my functional test, the processor was being spawned within a new process.

So even though I had hacked the processor class to use the same database session as the test itself, since processor and database were in separate memory spaces, the database updates the processor was making were invisible to the test code trying to verify the results.

Solution: set up the temporary Sqlite database in-file rather than in-memory.

Additionally, I discovered that, when Fixture does its teardown, it will throw an error if your fixture data isn't in the same state as it was setup in (noted at the end of this section). But that was a separate issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top