NAME

Test::Database::Temp - Create temporary test databases and run tests in all available ones

VERSION

version 0.003

SYNOPSIS

use Test2::V0;
use Test::Database::Temp;
use DBI;
Test::Database::Temp->use_all_available(
    build => sub {
        my ($driver) = @_;
        my %params = ( args => {} );
        return \%params;
    },
    init => sub { },
    deinit => sub { },
    do => sub {
        my ($db) = @_;
        my $dbh = DBI->connect( $db->connection_info );
        my ( $db_driver, $db_name) = ($db->driver, $db->name);
        subtest "Testing with $db_driver in db $db_name" => sub {
            my @row_ary;
            my $r = try {
                @row_ary = $dbh->selectrow_array('SELECT 1+2');
                1;
            } catch {
                diag 'Failed to select 1+2';
            };
            is( $row_ary[0], 3, 'returned correct' );

            done_testing;
        };
    },
    demolish => sub { },
);
done_testing;

DESCRIPTION

Test::Database::Temp is an extension to Database::Temp. It provides a way to easily test several different databases with the same set of test.

Test::Database::Temp has one main function: use_all_available. Using this subroutine user can safely test his code in all available (preconfigured to test site) databases. The ones which are not available are simply skipped over.

Test::Database::Temp supports all databases available in Database::Temp.

Test::Database::Temp uses Log::Any to produce logging messages.

STATUS

This module is currently being developed so changes in the API are possible.

FUNCTIONS

available_drivers

Return a list of available Database::Temp drivers.

If you specify the list of drivers yourself, then availabity check will only be limited to those and the available ones will be returned. If same driver is specified several times, it will be returned as many times. The ordering of drivers will not changes.

my @drivers = Test::Database::Temp->available_drivers( drivers => [ qw( SQLite SQLite Pg CSV ) ] );

use_all_available

Execute same code with all temporary databases. This method is partly an extended version of Database::Temp-new()>. Just like with Database::Temp-new()>, you can specify subroutines init() and deinit() to initialize and teardown the databases. It also has subroutines build and demolish.

Use build() to provide arguments to when Database::Temp-new()> is called and demolish() to clean up after database has been removed if necessary.

You can either go with all available Database::Temp drivers, or you can specify a list of the wanted drivers. In the latter case, if any of the wanted drivers is unavailable, subroutine will throw an exception.

One of the benefits of using use_all_available() is that temporary databases are created one by one, and deleted immediately after use.

See "SYNOPSIS" for an example.

AUTHOR

Mikko Koivunalho <mikkoi@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Mikko Johannes Koivunalho.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.