[Catalyst] Using test database with TWAM?

Bill Moseley moseley at hank.org
Thu Apr 30 19:53:43 GMT 2009


On Thu, Apr 30, 2009 at 03:10:13PM -0400, Jesse Sheidlower wrote:
> 
> Thanks--this is all very useful. I do not, in fact, have any
> reason for a different schema; a different DSN is all I need.
> 
> I do keep the connection details in a config file.
> 
> I guess my question then becomes the more prosaic one of
> detail, esp. as I haven't really done much with tests before:
> How do I populate this database from within my test script?

I have a module that knows how to create a database and then import a
database dump that's kept in svn.  We use Postgresql so the module
simply does a createdb and a pg_restore.  The database is dropped on
DESTROY after the test is run.

The module sets environment variables pointing to the database it
created that are then used by the test script (or really Catalyst)
when the tests run.

This module is used at the top of any test script that needs a
database for the tests.


Like Peter, I have a config file that has different sections for
different "modes" of operation.  Mostly used for dev, testing, qa,
staging, and production configurations.

I have a module that manages the configuration.  That is, it's a
module that you pass an application "home" directory and a "mode" and
it returns a hash of the config.  It's separate from Catalyst because
it's used by more than just Catalyst.

Now, normally the configuration for the database is defined for
production, staging, etc. but for "make_test" (which is the default)
the dsn is empty:

    make_test:
        database:
            dsn:    dbi:Pg:
            user:
            password:

It's possible to override the host, port, etc. if the database server
is on another machine.


The module that creates the test database sees this and populates the
PG* environment variables, creates the database and imports the data,
then the test script runs and likewise the application then uses the
same PG* environment variables to access the test database.


> How do I get my test script to use a different config file for
> the test database?

Well, mostly it's not the test script that needs the configuration
data, but the application.  In my case all that's needed is an
environment variable that says what "mode" to run in.

I have a plugin for Catalyst that loads the configuration file.  It's
a thin wrapper around the module that loads config data.  It uses
an environment variable to select the "mode", but the plugin defaults
to "development", which is the more common case (when running the
built in server).


So, test scripts can ask for a database from a common module.  That
module figures out where the application home is and loads the config.
It's loaded in "make_test" mode by default.  Since the dsn is not set
the PG* environment variables are used to tell everything what
database to use.  When Catalyst then goes to load the config an
environment variable is set that says to use "make_test".

It's probably less complex actually than my description above. ;)


-- 
Bill Moseley.
moseley at hank.org
Sent from my iMutt



More information about the Catalyst mailing list