Test::DB - Temporary Testing Databases
Temporary Databases for Testing
use Test::DB; my $tdb = Test::DB->new; # my $tdbo = $tdb->create(database => 'sqlite'); # my $dbh = $tdbo->dbh;
This package provides a framework for setting up and tearing down temporary databases for testing purposes. This framework requires a user (optionally with password) which has the ability to create new databases and works by creating test-specific databases owned by the user specified. Note: Test databases are not automatically destroyed and should be cleaned up manually by call the destroy method on the database-specific test database object.
destroy
on create, clone
Establish a connection to the DB using some "initial" database.
my $tdbo = $tdb->postgres(initial => 'template0');
Using the established connection, create the test/temporary database.
$tdbo->create;
Establish a connection to the newly created test/temporary database.
$tdbo->create->dbh;
Make the test database object immutable.
$tdbo->create->database('example'); # error
on destroy
Establish a connection to the DB using the "initial" database.
# using the created test/temporary database object
Using the established connection, drop the test/temporary database.
$tdbo->destroy;
using DBI
my $tdb = Test::DB->new; my $dbh = $tdb->sqlite(%options)->dbh;
using DBIx::Class
my $tdb = Test::DB->new; my $tdbo = $tdb->postgres(%options)->create; my $schema = DBIx::Class::Schema->connect( dsn => $tdbo->dsn, username => $tdbo->username, password => $tdbo->password, );
using Mojo::mysql
my $tdb = Test::DB->new; my $tdbo = $tdb->mysql(%options)->create; my $mysql = Mojo::mysql->new($tdbo->uri);
using Mojo::Pg
my $tdb = Test::DB->new; my $tdbo = $tdb->postgres(%options)->create; my $postgres = Mojo::Pg->new($tdbo->uri);
using Mojo::Pg (with cloning)
my $tdb = Test::DB->new; my $tdbo = $tdb->postgres(%options)->clone('template0'); my $postgres = Mojo::Pg->new($tdbo->uri);
using Mojo::SQLite
my $tdb = Test::DB->new; my $tdbo = $tdb->sqlite(%options)->create; my $sqlite = Mojo::SQLite->new($tdbo->uri);
This package uses type constraints from:
Types::Standard
This package implements the following methods:
clone(Str :$database, Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The clone method generates a database based on the type and database template specified and returns a Test::DB::Object with an active connection, dbh and dsn. If the database specified doesn't have a corresponding database driver this method will returned the undefined value. The type of database can be omitted if the TESTDB_DATABASE environment variable is set, if not the type of database must be either sqlite, mysql, mssql or postgres. Any options provided are passed along to the test database object class constructor.
Test::DB::Object
dbh
dsn
TESTDB_DATABASE
sqlite
mysql
mssql
postgres
# given: synopsis $ENV{TESTDB_DATABASE} = 'postgres'; $tdb->clone(template => 'template0');
# given: synopsis $ENV{TESTDB_DATABASE} = 'postgres'; $ENV{TESTDB_TEMPLATE} = 'template0'; $tdb->clone;
# given: synopsis $ENV{TESTDB_TEMPLATE} = 'template0'; $tdb->clone(database => 'postgres');
create(Str :$database, Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The create method generates a database based on the type specified and returns a Test::DB::Object with an active connection, dbh and dsn. If the database specified doesn't have a corresponding database driver this method will returned the undefined value. The type of database can be omitted if the TESTDB_DATABASE environment variable is set, if not the type of database must be either sqlite, mysql, mssql or postgres. Any options provided are passed along to the test database object class constructor.
# given: synopsis $tdb->create;
# given: synopsis $ENV{TESTDB_DATABASE} = 'sqlite'; $tdb->create;
# given: synopsis $tdb->create(database => 'sqlite');
mssql(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The mssql method builds and returns a Test::DB::Mssql object.
# given: synopsis $tdb->mssql;
mysql(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The mysql method builds and returns a Test::DB::Mysql object.
# given: synopsis $tdb->mysql;
postgres(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The postgres method builds and returns a Test::DB::Postgres object.
# given: synopsis $tdb->postgres;
sqlite(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The sqlite method builds and returns a Test::DB::Sqlite object.
# given: synopsis $tdb->sqlite;
Al Newkirk, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated in the "license file".
Wiki
Project
Initiatives
Milestones
Contributing
Issues
To install Test::DB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::DB
CPAN shell
perl -MCPAN -e shell install Test::DB
For more information on module installation, please visit the detailed CPAN module installation guide.