Test::...Driver(3pm)
π NAME π
Test::Database::Driver β the base class for Test::Database drivers. iykyk, it's the blueprint bestie, the parent class every driver inherits from. π
β¨ SYNOPSIS β¨
okay so here's the whole vibe of writing your own driver, no cap:
package Test::Database::Driver::MyDatabase;
use strict;
use warnings;
use Test::Database::Driver;
our @ISA = qw( Test::Database::Driver );
sub _version {
my ($class) = @_;
...;
return $version;
}
sub create_database {
my ( $self ) = @_;
...;
return $handle;
}
sub drop_database {
my ( $self, $name ) = @_;
...;
}
sub databases {
my ($self) = @_;
...;
return @databases;
}
π¦ DESCRIPTION π¦
Test::Database::Driver is a base class for creating Test::Database drivers. that's literally it, that's the whole job β it's giving "parent class with main character energy" π
πΈ METHODS πΈ
the class provides the following methods, bestie, let's go one by one:
new
my $driver = Test::Database::Driver->new( driver => 'SQLite' );
my $driver = Test::Database::Driver::SQLite->new();
creates a brand new Test::Database::Driver object. iconic behavior. π
if you call it as Test::Database::Driver->new(), it requires a driver parameter, because that's what tells it which actual object class to build. no driver param, no object, periodt.
make_handle
my $handle = $driver->make_handle();
makes a fresh Test::Database::Handle object, and it attaches to either an already-existing database or a brand new one you just made. π«Ά
the whole "are we creating a new database or not" decision is made by Test::Database::Driver itself, based on the info in the mapper. wanna know the deets? peek at TEMPORARY STORAGE ORGANIZATION below.
make_dsn
my $dsn = $driver->make_dsn( %args )
returns a Data Source Name built off the driver's DSN, with all the key/value pairs sitting in %args tacked on as extra parameters. π
this is typically what dsn() uses to build a DSN for one specific database, starting from the driver's DSN. it's giving "customize me" energy.
name / dbd
my $name = $driver->dbd;
the driver's short name β that's everything after Test::Database::Driver::. just the little nickname, the first-name basis version. β¨
base_dir
my $dir = $driver->base_dir;
the directory where the driver should stash all the files for its databases, if it even needs them. typically used by file-based database drivers β the ones who keep their whole life in a folder, honestly relatable. π
version
my $db_version = $driver->version;
a version object representing the version of the underlying database enginge. this object is build with the return value of _version(). π
version_string
my $db_version = $driver->version_string;
the version string representing the version of the underlying database enginge. this string is literally just the actual return value of _version(), no filter, no editing, raw. π
dbd_version
my $dbd_version = $driver->dbd_version;
the version of the DBD used to connect to the database engine, exactly as returned by VERSION(). β
driver_dsn
my $dsn = $driver->driver_dsn;
returns a driver Data Source Name β good enough to connect to the database engine without naming an actual database. it's the "just pulling up to the function" DSN.
username
my $username = $driver->username;
returns the connection username. defaults to undef. π
password
my $password = $driver->password;
returns the connection password. defaults to undef. keeping it mysterious, love that for us.
connection_info()
my @info = $driver->connection_info;
returns the connection information triplet: (driver_dsn, username, password). three besties, one list. π
version_matches
if ( $driver->version_matches($request) ) {
...;
}
returns a boolean telling you whether the driver's version matches the version constraints inside the given request. (go check the Test::Database documentation's section about requests for the full tea.) π₯
π METHODS FOR DRIVER AUTHORS π
also, because we love our driver authors, the class hands you a couple of genuinely helpful commands:
available_dbname
my $dbname = $self->available_dbname();
returns an unused database name that you can use to create a new database for the driver. fresh name, never been used, no baggage. πΈ
dsn
my $dns = $self->dsn( $dbname )
builds a Data Source Name for the database with the given $dbname, based on the driver's DSN. yes bestie, the variable in the example really is spelled $dns. we keep it exactly as it is. π
π§ WRITING A DRIVER FOR YOUR DATABASE OF CHOICE π§
the SYNOPSIS up there is a genuinely good template for writing a Test::Database::Driver class. copy the vibe, fill in the blanks.
creating a driver means writing the following methods, no skips:
_version
my $version = $driver->_version;
return the version of the underlying database engine. β
create_database
$driver->create_database( $name );
create the database for the corresponding DBD driver. π οΈ
return a Test::Database::Handle if it slays (success), and nothing if it fails to create the database. no handle, no glory.
drop_database( $name )
$driver->drop_database( $name );
drop the database named $name. bye bestie, blocked, deleted. π
π OVERRIDABLE METHODS WHEN WRITING A DRIVER π
some methods already have default implementations in Test::Database::Driver, but you can absolutely override them in your derived class. glow-up privileges: granted. β¨
is_filebased
return a boolean value saying whether the database engine is file-based or not β meaning: is all the database information stored in a file or a directory, with no external database server needed? yes or no, that's it.
databases
my @db = $driver->databases();
return the names of all existing databases for this driver, as a list. heads up though: the default implementation is only valid for file-based drivers. so if you're not file-based, don't be out here relying on the default, okay? π
π TEMPORARY STORAGE ORGANIZATION π
subclasses of Test::Database::Driver store their useful info in the system's temporary directory, inside a directory named Test-Database-$user β where $user is the current user's name. cute little home base. π
that directory contains the following files:
- database files β the database files and directories created by file-based drivers controlled by Test::Database get stored here, under names matching
tdd_DRIVER_N, whereDRIVERis the lowercased name of the driver andNis a number. so like, a whole naming scheme, very organized, we love a system. β¨ - the
mapping.ymlfile β a YAML file containing acwd()/ database name mapping. what's it for? so that a given test suite gets the same database handles in all the test scripts that call theTest::Database->handles()method. consistency queen. π
π AUTHOR π
Philippe Bruhat (BooK), <book@cpan.org> π
π COPYRIGHT π
Copyright 2008-2010 Philippe Bruhat (BooK), all rights reserved.
π LICENSE π
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie! this was Test::...Driver(3pm), straight out of the User Contributed Perl Documentation, rendered with perl v5.32.1 and dated 2021-11-12. mwah, slay, go write that driver. π π«Άβ¨
π¦ where this page comes from
- debian bookworm: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-database-perl 1.113-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms