On Wed, Oct 08, 2008 at 01:17:16PM +0100, David Cantrell wrote:
> On Wed, Oct 08, 2008 at 10:56:27AM +0200, Philippe Bruhat (BooK) wrote:
> 
> > Is there any way for a test suite to use the preferred database of a
> > tester? Something like a TEST_DATABASE_DSN, TEST_DATABASE_USERNAME,
> > TEST_DATABASE_PASSWORD triplet that CPAN testers could set to enable
> > testing on different databases?
> > 
> > A colleague of mine is working on a very interesting module, but even
> > though he could release a test suite relying on sqlite, I thought it
> > would be better to use any configured database that was available on the
> > tester's system.
> > 
> > Is there a recommended way to do that?
> 
> If you use a pre-existing database, you risk polluting your tests with
> any rubbish that's already in there - perhaps left behind by the
> previous module to be tested.  So I recommend creating a new database
> for your tests, and clearing up afterwards.

The idea was that the test database would exist, and being a test
database would accept being wiped out by any script that would connect
to it.

> This is easy for SQLite - just use File::Temp:
>   
> http://search.cpan.org/src/DCANTRELL/Class-DBI-ClassGenerator-1.01/t/sqlite_create_db.pl
> 
> For MySQL it's a little more involved, but only a little bit:
>   http://search.cpan.org/src/DCANTRELL/Class-DBI-ClassGenerator-1.01/t/mysql.t
>   
> http://search.cpan.org/src/DCANTRELL/Class-DBI-ClassGenerator-1.01/t/mysql_create_db.pl
> 
> I've not (yet) bothered with PG, Oracle etc.  When I need them, I'll
> probably extract it out into a nice neat Test::something module with a
> bunch of database-specific back-ends.

The main issue we have with databases other than sqlite are always
the same: it needs to be configured, and the test script needs some
credentials. Connecting as 'root' on 'localhost' will of course not work
everywhere.

It should be fairly easy for willing CPAN testers to setup any database
they like, and provide some connection information for throwaway tables
and data (assuming the test script WILL probably drop all tables in
there and dump its own crap there).

Maybe a module with a configuration file à la CPAN.pm would be
sufficient. Something like:

    use Test::Database;    # not a good name

    my (@handles) = Test::Database->test_handles();

    my ($mysql_handle) = Test::Database->test_handles('mysql');

    for my $handle (@handles) {
        diag "Testing with " . $handle->driver();
        my $dbh = $handle->dbh();    # automatically connects us

        # also available:
        # - username()
        # - password()
        # - dsn()

        # test away!
    }

For simple stuff like SQLite or the DBD::CSV driver, the module would
create the appropriate file in a temporary directory/file, but it should
be possible to require a semi-permanent one (e.g. by having a setup
method that would create the needed stuff if missing and just return
the appropriate handler object if the stuff is already configured and
available).

After seeing your code, I suppose it could also be somewhat easy to try
out a few series of basic/default credentials on localhost for engines
like MySQL and Postgres, and try to setup a test database from there.

Does that sound like an interesting tool to have?

-- 
 Philippe Bruhat (BooK)

 The faster the climb, the swifter the fall.
                                    (Moral from Groo The Wanderer #87 (Epic))

Reply via email to