http://qs321.pair.com?node_id=618404

skazat has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a hard time understanding how I can test an application the supports more than one type of data backend.

For example, I have an app that can save its data in both a plaintext backend, and an SQL backend.

(Say) I have a module called, "App::Backend" that holds all the shared subroutines/methods, and two other modules, called, "App::Backend::PlainText" and App::Backend::SQL" which hold different versions of the same methods - anything that can't be shared.

App::Backend looks like this:

package App::Backend; use App::Config; use base "App::Backend::App::$Config::Backend_Type"; sub shared_method_1 { my $self = shift; #.... } # ... the rest of the module... 1;

(App::Config just holds the configuration variables)

Say, App::Backend::SQL has a method, like this:

sub type { my $self = shift; return 'SQL'; }

and App::Backend::PlainText has a method, like this:

sub type { my $self = shift; return 'PlainText'; }

My test file looks something like this:

#!/usr/bin/perl use Test::More qw(no_plan); for(qw(PlainText SQL){ require App:Config; $App::Config::Backend_Type = $_; require App::Backend; ok($App::Config::Backend_Type eq $_); my $backend = App::Backend; ok($backend->type eq $_); # Wrong! }

It's wrong, because App::Backend is never refreshed and the, "use base" line is never called again. I've tried deleting it from %INC, but that doesn't seem to do the trick (and seems wholly wrong)

Changing the backend like this isn't something I ever want to do in code, but in testing, it's real useful to make sure every backend is checking out on the tests.

The only thing I can think of, is to make a test file for each backend, that are exactly the same, except that the $App::Config::Backend_Type is set to a different thing, before I call App::Backend->new;

Any other ideas? What do you all do to test the various backends in your app? Is my, "use base" way of doing things not the best? I'm starting to look at other packages, like CGI::Session on CPAN - looks like there's just different test files for each backend. Sigh.

 

-justin simoni
skazat me