Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Testing & Databases

by blue_cowdawg (Monsignor)
on Jan 28, 2004 at 13:57 UTC ( [id://324669]=note: print w/replies, xml ) Need Help??


in reply to Testing & Databases

      I want to test DB operations etc, i.e. create some entries, update, delete them, etc. Now, my problem is that all this testing interfers with the 'real' data if I run the tests against the 'real' database.

There are as many ways of skinning that cat as there are folks trying to skin it. Here are some of the aproaches I've seen without passing judgement on any of them.

Environmental values

As you pointed out you an always use your environmental values to drive your application's behavior. You showed one way, but here's an alternative:
# .bashrc sniglet export DBHOST="mytestdbhost.mydomain.tld" export DBDB="mytestdb" export DBUSER="user" export DBPASSWORD="SeCrEt" export DBDRIVER="mysql" ------- 8<snip! 8<-------------------------- # in some perl code, far far away use DBI; my $dbhost = $ENV{'DBHOST'}; my $dbname = $ENV{'DBDB'}; my $dbuser = $ENV{'DBUSER'}; my $dbpassword = $ENV{'DBPASSWORD'}; my $dbdriver = $ENV{'DBDRIVER'}; my $DSN=sprintf("DBI:%s:host=%s:database=%s", $dbdriver,$dbhost,$dbnaem); my $dbh=DBI->connect($DSN,$dbuser,$dbpassword) or die DBI->errstr; ... etc....
The above ripped from some code I'm maintaining. The actual values changed to protect the guilty.

Configuration Files

There are two general methods I've seen for doing this. One uses an external configuration file that you either parse yourself or you can use one of several AppConfig family of CPAN modules to do your dirty work.

The other method I've seen (and used myself) is to create your own module that encapsulates your database access configuration. The one I use over and over again I call (oddly enough) DbConfig.

use DBConfig; use DBI; my $dbh=DBI->connect(DBConfig::DSH,DBConfig::User,DBConfig::Password) +or die $DBI::errstr;

When I install DBConfig on my production boxes I make the appropriate changes to the module. Otherwise the code is identical.

As always in Perl TIMTOWTDI!


Peter L. Berghold -- Unix Professional
Peter at Berghold dot Net
   Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://324669]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found