Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Writing tests for modules using database handles

by astaines (Curate)
on Aug 02, 2001 at 01:17 UTC ( #101486=perlquestion: print w/replies, xml ) Need Help??
astaines has asked for the wisdom of the Perl Monks concerning the following question:


    I am writing a module which wraps the table_info() function in DBI. Clearly it need a working database handle to operate on. I want to write some test scripts for it. These work fine on my system, where I can feed them a working databse handle easily.

    How should I provide tests for other people to use, assuming that anyone else is ever mad enough to use this module? I may be missing something fundamental here, but all advice is gratefully accepted.

(Any comments on the basic idea of this module, and whether there's a better solution also welcome)

Anthony Staines


SYNOPSIS use DBIx::Information; my $dbinf->new($dbh); # Get a list of tables in the database my @tables = @{ $dbinf->tables()}; # Get a list of variables in the whole database my @variables = @{ $dbinf->variables()}; # Get a list of the variable in the table called $table_name; my @myvariable = @{ $dbinf->variable($table_name)}; # Get two useful hashes- # List keyed by table to anonymous arrays of the names of th +e variables in each table # Index keyed by variable name to anonymous arrays of the na +mes of the table # (or tables) in which each variable occurs. # my %list = %{$dbinf->list()}; my %index = %{$dbinf->index()}; #Does the variable exist in the specified table my $exists = $dbinf->IsVarinTable($variable_name,$table_name); DESCRIPTION This module provides a convenient interface to the contents of a s +ingle database, or (possibly) a singe schema. I only have Mysql, and it +*will* work with this. If you have other databases fell free to test... EXPORT Nothing. don't even try. AUTHOR SEE ALSO the perl manpage.

Replies are listed 'Best First'.
Re: Writing tests for modules using database handles
by chromatic (Archbishop) on Aug 02, 2001 at 04:48 UTC

    Make it possible to set up your program with a different database handle. In your test program, open a handle to a file or memory database with just enough test data to make things work.

    I used DBD::CSV in a project, stuffed the applicable data after __DATA__, and wrote a small sub to dump things to a CSV file. Very simple:

    sub make_db { open(OUT, '>page_data') or die "Can't open output db: $!"; while (<DATA>) { chomp; print OUT $_, "\015\012"; } close OUT; }

    Depending on how tricky your SQL calls are, you may have to get more tricky here. Hmm, it would be good to have someone write a DBI testing module. Any takers?

Re: Writing tests for modules using database handles
by bikeNomad (Priest) on Aug 02, 2001 at 04:45 UTC
    Your test script could require a database handle to be set in a config file (and, of course, print a message if it isn't). The handle could be passed to the Makefile, as well. Look at the way that the various DBD modules do their testing. Isn't there a test database in the standard MySQL installation? That could be your default.

    The Device::SerialPort module does tests on a live serial port; if opening it fails, it prints out a message about how to change the configuration by editing a file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://101486]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-04-24 01:01 GMT
Find Nodes?
    Voting Booth?