[Catalyst] Using test database with TWAM?

Iain iainhubbard at googlemail.com
Thu Apr 30 16:29:12 GMT 2009


On Thu, 2009-04-30 at 11:42 -0400, Jesse Sheidlower wrote:
[snip]
> I quickly hit a wall because I couldn't figure out how to run
> this with a test database. Since I need to test modification
> and deletion stuff, but have real data, I can't just run this
> against my actual app. So I figured I would dump the sql from
> my production app and have my test program load this into a
> database called "test-db" or whatever, having the same format
> and schema as the real db, and then just drop the test db at
> the end of the test.

The only way I managed to do this and keep my sanity was to use the "fat
model" method and then test the model layer specifically (DBIC in my
case) outside of catalyst.

To help with this I started using with DBICx::TestDatabase

http://search.cpan.org/~jrockway/DBICx-TestDatabase-0.02/lib/DBICx/TestDatabase.pm

This worked great, until I ran into problems with code relying on
features of MySQL (yes I know....)

I then cooked up a bit (ok a lot) of a hack that relies on MySQL
allowing you to create a temporary table with the same name as an
existing table, the real table gets hidden. Leaving you with an empty
clone of your table that disappears when you close the connection.

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

so now when I run my tests I clone an empty DB for the duration of the
test run. You have to populate it with canned data but this gives you a
very consistent way to test.

Again you need to be doing your heavy lifting in your model (DBIC) for
this to work well.

How safe this is in the long run is anybody's guess but so far its
working quite well.

Just don't do it on a replicating master!!!!

If your wondering why I went to all this trouble. We have several
developers so running multiple tests at the same time caused all kinds
of strange problems and nobody wanted to maintain a DB per developer!

Iain.







More information about the Catalyst mailing list