Create an ephemeral PostgreSQL database per test

This commit is contained in:
Graham Christensen
2021-02-22 13:47:41 -05:00
parent b15d8edab1
commit fe1f2f0806
3 changed files with 34 additions and 10 deletions

View File

@ -1,13 +1,16 @@
use strict;
use Hydra::Schema;
use Hydra::Model::DB;
use Cwd;
use Setup;
my $db = Hydra::Model::DB->new;
my $pgsql = dbinit();
my $dsn = $pgsql->dsn;
require Hydra::Schema;
require Hydra::Model::DB;
use Test::Simple tests => 76;
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my $res;

View File

@ -2,12 +2,27 @@ package Setup;
use strict;
use Exporter;
use Hydra::Helper::Nix;
use Hydra::Model::DB;
use Test::PostgreSQL;
use Cwd;
our @ISA = qw(Exporter);
our @EXPORT = qw(hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild updateRepository);
our @EXPORT = qw(dbinit hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild updateRepository);
sub dbinit() {
my $pgsql = Test::PostgreSQL->new();
$ENV{'HYDRA_DBI'} = $pgsql->dsn;
system("hydra-init") == 0 or die;
return $pgsql;
}
sub captureStdoutStderr {
# "Lazy"-load Hydra::Helper::Nix to avoid the compile-time
# import of Hydra::Model::DB. Early loading of the DB class
# causes fixation of the DSN, and we need to fixate it after
# the temporary DB is setup.
require Hydra::Helper::Nix;
return Hydra::Helper::Nix::captureStdoutStderr(@_)
}
sub hydra_setup {
my ($db) = @_;