Test::PostgreSQL::Docker - A Postgresql mock server for testing perl programs
use Test::More; use Test::PostgreSQL::Docker; # 1. create a instance of Test::PostgreSQL::Docker with postgres:12-alpine image my $server = Test::PostgreSQL::Docker->new(tag => '12-alpine'); # 2. create/run a container $server->run(); # 3. puke initialization data into postgresql on a container $server->run_psql_scripts("/path/to/fixture.sql"); # 4. get a Database Handler(a DBI::db object) from mock server object my $dbh = $server->dbh(); # (or call steps of 2 to 4 as method-chain) my $dbh = $server->run->run_psql_scripts("/path/to/fixture.sql")->dbh; # 5. query to database my $sth = $dbh->prepare("SELECT * FROM Users WHERE id=?"); $sth->execute(1); # 6. put your own test code below my $row $sth->fetchrow_hashref(); is $row->{name}, "ytnobody"; done_testing;
Test::PostgreSQL::Docker run the postgres container on the Docker, for testing your perl programs.
**NOTE** Maybe this module doesn't work on the Windows, because we don't any test on Windows machine.
$server = Test::PostgreSQL::Docker->new(%opt)
The path to docker. Default is /usr/bin/docker.
docker
/usr/bin/docker
A distribution name. Default is postgres.
postgres
A tag of the PostgreSQL. Default is latest.
latest
An uniqe id. Default is the object memory addres.
Default is postgres.
Default is test.
test
Show docker error. Default is true value.
true value
$server = $server->run(%opt)
1. Check image with docker pull.
docker pull
2. docker run
docker run
3. connect database
connect database
Skip image check. Default is true.
true
Skip connect database. Default is false.
false
$oid = $server->oid()
Return an unique id.
$container_name = $server->container_name()
Return the docker container name sprintf('%s-%s-%s', $pgname, $tag, $oid).
sprintf('%s-%s-%s', $pgname, $tag, $oid)
$image_name = $server->image_name()
Return the docker image name.
$dsn = $server->dsn(%opt)
$port = $server->port()
Return a PostgreSQL server port.
$dbh = $server->dbh()
$psql_args = $server->psql_args() $psql_args = $server->psql_args($args)
Arguments to psql in run_psql and run_psql_scripts. Default is sprintf('-h %s -p %s -U %s -d %s', $self-{host}, 5432, $self->{dbowner}, $self->{dbname})>.
psql
run_psql
run_psql_scripts
sprintf('-h %s -p %s -U %s -d %s', $self-
$server = $server->run_psql(@args) $server->run_psql('-c', q|INSERT INTO foo (bar) VALUES ('baz')|);
$server = $server->run_psql_scripts($path)
$server = $server->docker_is_running()
Return true if docker container is running.
$server = $server->docker_daemon_is_accessible()
Return true if docker daemon is accessible.
This module uses the Docker as ephemeral environment.
Copyright (C) Satoshi Azuma.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Satoshi Azuma <ytnobody@gmail.com>
Test::PostgreSQL https://hub.docker.com/_/postgres
To install Test::PostgreSQL::Docker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::PostgreSQL::Docker
CPAN shell
perl -MCPAN -e shell install Test::PostgreSQL::Docker
For more information on module installation, please visit the detailed CPAN module installation guide.