Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
[1nickt]: stevieb it turns out that Dell still has the Precision line of "mobile workstations", just not displayed on their main website. I found the Precision 7710 with 4k display/full-size keybd as req'd.
[1nickt]: List $2,788 (!), on sale for $1766, also a $200 coupon on the site thru 3/30 ... final price $1,566. Includes 3-yr on-site service.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-03-27 12:48 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.