Rose::DBx::TestDB - test Rose::DB::Object modules


Rose-DBx-TestDB documentation Contained in the Rose-DBx-TestDB distribution.

Index


Code Index:

NAME

Top

Rose::DBx::TestDB - test Rose::DB::Object modules

SYNOPSIS

Top

 use Rose::DBx::TestDB;
 my $db = Rose::DBx::TestDB->new;

 # do something with $db

 exit;

 # END block will automatically clean up all temp db files

METHODS

Top

new

Returns a new Rose::DB object using a temp sqlite database.

AUTHOR

Top

Peter Karman, <perl at peknet.com>

BUGS

Top

Please report any bugs or feature requests to bug-rose-dbx-testdb at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rose-DBx-TestDB. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Rose::DBx::TestDB

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Rose-DBx-TestDB

* CPAN Ratings

http://cpanratings.perl.org/d/Rose-DBx-TestDB

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-DBx-TestDB

* Search CPAN

http://search.cpan.org/dist/Rose-DBx-TestDB

ACKNOWLEDGEMENTS

Top

Inspired by DBICx::TestDatabase.

COPYRIGHT & LICENSE

Top


Rose-DBx-TestDB documentation Contained in the Rose-DBx-TestDB distribution.

package Rose::DBx::TestDB;

use warnings;
use strict;
use File::Temp 'tempfile';
use Rose::DB;
use Carp;

# we want our END to run no matter what (even if ^C)
use sigtrap qw(die normal-signals error-signals);

our $VERSION = '0.05';

my @TMPFILES;

# check for sqlite version per Rose::DB tests
eval { require DBD::SQLite };

if ( $@ || $DBD::SQLite::VERSION < 1.08 || $ENV{'RDBO_NO_SQLITE'} ) {
    croak 'Missing DBD::SQLite 1.08+';
}
elsif ( $DBD::SQLite::VERSION == 1.13 ) {
    carp 'DBD::SQLite 1.13 is broken but we will try testing anyway';
}

sub new {
    my ( undef, $filename ) = tempfile();
    push @TMPFILES, $filename;

    Rose::DB->register_db(

        domain          => 'test',
        type            => 'sqlite',
        driver          => 'sqlite',
        database        => $filename,
        auto_create     => 0,
        connect_options => {
            AutoCommit => 1,
            ( ( rand() < 0.5 ) ? ( FetchHashKeyName => 'NAME_lc' ) : () ),
        },
        post_connect_sql =>
            [ 'PRAGMA synchronous = OFF', 'PRAGMA temp_store = MEMORY', ],
    );
    Rose::DB->default_domain('test');
    Rose::DB->default_type('sqlite');

    my $db = Rose::DB->new()
        or croak "could not create new Rose::DB instance: $!";

    return $db;
}

# in theory File::Temp should clean these up. In theory.
END {
    for my $file (@TMPFILES) {
        unlink $file unless -e $file;    # Sets $! correctly
        1 while unlink $file;
    }
}

1;