Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to test different back ends?

by skazat (Chaplain)
on May 31, 2007 at 06:01 UTC ( [id://618404]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: How to test different back ends?
by andreas1234567 (Vicar) on May 31, 2007 at 12:17 UTC
    looks like there's just different test files for each backend
    Why don't you do the same? It is probably easier to write, understand and maintain separate test files for each module than having a single, large test for everything.

    On the other hand, you can experiment with Symbol and delete_package. Note the BUGS section in the docs though:
    "Symbol::delete_package" is a bit too powerful. It undefines every symbol that lives in the specified package and in its sub-packages. Since perl, for performance reasons, does not perform a symbol table lookup each time a function is called or a global variable is accessed, some code that has already been loaded and that makes use of symbols in package "Foo" may stop working after you delete "Foo", even if you reload the "Foo" module afterwards.
    And, please do make sure your source code compiles before you post. Thanks.

    Andreas
    --

      Why don't you do the same? It is probably easier to write, understand and maintain separate test files for each module than having a single, large test for everything.

      basically, no matter what the backend you use, the API is the same. This means, the test files are going to be almost exactly the same too, and as it has been hammered into my head, don't ever repeat yourself :)

       

      -justin simoni
      skazat me

Re: How to test different back ends?
by chromatic (Archbishop) on May 31, 2007 at 18:07 UTC
    What do you all do to test the various backends in your app?

    I use Test::Class and design my test classes as I do my classes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found