Skip to content

Commit db426a7

Browse files
author
Kaitlyn Parkhurst
committed
Test infra round two.
1 parent 8995c46 commit db426a7

File tree

3 files changed

+57
-33
lines changed

3 files changed

+57
-33
lines changed

Web/lib/BlogDB/Web.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ sub startup ($self) {
2222

2323
# Create $self->db as a BlogDB::DB connection.
2424
$self->helper( db => sub {
25-
return state $db = BlogDB::DB->connect($self->config->{database}->{blogdb});
25+
return state $db = exists $ENV{BLOGDB_TESTMODE} && $ENV{BLOGDB_TESTMODE} == 1
26+
? BlogDB::DB->connect( $ENV{BLOGDB_DSN}, '', '' )
27+
: BlogDB::DB->connect($self->config->{database}->{blogdb});
2628
});
2729

2830
# Create $self->set_template

Web/lib/BlogDB/Web/Test.pm

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package BlogDB::Web::Test;
2+
use Import::Into;
3+
use Test::More;
4+
use Test::Deep;
5+
use Test::Mojo;
6+
use Test::Postgresql58;
7+
8+
push our @ISA, qw( Exporter );
9+
push our @EXPORT, qw( );
10+
11+
sub import {
12+
shift->export_to_level(1);
13+
my $target = caller;
14+
15+
Mojo::Base->import::into($target, '-strict');
16+
warnings ->import::into($target);
17+
strict ->import::into($target);
18+
Test::More->import::into($target);
19+
Test::Deep->import::into($target);
20+
Test::Mojo->import::into($target);
21+
}
22+
23+
our $pgsql = Test::Postgresql58->new()
24+
or BAILOUT( "PSQL Error: " . $Test::Postgresql58::errstr );
25+
26+
load_psql_file("/home/symkat/Code/BlogDB/DB/etc/schema.sql");
27+
28+
$ENV{BLOGDB_TESTMODE} = 1;
29+
$ENV{BLOGDB_DSN} = $pgsql->dsn;
30+
31+
sub load_psql_file {
32+
my ( $file ) = @_;
33+
34+
open my $lf, "<", $file
35+
or die "Failed to open $file for reading: $!";
36+
my $content;
37+
while ( defined( my $line = <$lf> ) ) {
38+
next unless $line !~ /^\s*--/;
39+
$content .= $line;
40+
}
41+
close $lf;
42+
43+
my $dbh = DBI->connect( $pgsql->dsn );
44+
for my $command ( split( /;/, $content ) ) {
45+
next if $command =~ /^\s*$/;
46+
$dbh->do( $command )
47+
or BAIL_OUT( "PSQL Error($file): $command: " . $dbh->errstr);
48+
}
49+
undef $dbh;
50+
}
51+
52+
1;

Web/t/01_index.t

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
#!/usr/bin/env perl
2-
use Mojo::Base -strict;
3-
use Test::Mojo;
4-
use Test::More;
5-
use Test::Postgresql58;
6-
use DBI;
2+
use BlogDB::Web::Test;
73

84
my $t = Test::Mojo->new('BlogDB::Web');
95

106
$t->get_ok( '/' );
117

12-
our $pgsql = Test::Postgresql58->new()
13-
or BAILOUT( "PSQL Error: " . $Test::Postgresql58::errstr );
14-
15-
load_psql_file("/home/symkat/Code/BlogDB/DB/etc/schema.sql");
16-
178
# Create an account.
189
$t->post_ok( '/register', form => {
1910
username => 'fred',
2011
email => 'fred@blog.com',
2112
password => 'SuperSecure',
2213
confirm => 'SuperSecure',
23-
})->status_is( 200 );
14+
})->status_is( 302 );
2415

2516
# Login works and redirects the user.
2617
$t->post_ok( '/login', form => {
@@ -39,24 +30,3 @@ $t->post_ok( '/login', form => {
3930
})->status_is( 500 );
4031

4132
done_testing();
42-
43-
sub load_psql_file {
44-
my ( $file ) = @_;
45-
46-
open my $lf, "<", $file
47-
or die "Failed to open $file for reading: $!";
48-
my $content;
49-
while ( defined( my $line = <$lf> ) ) {
50-
next unless $line !~ /^\s*--/;
51-
$content .= $line;
52-
}
53-
close $lf;
54-
55-
my $dbh = DBI->connect( $pgsql->dsn );
56-
for my $command ( split( /;/, $content ) ) {
57-
next if $command =~ /^\s*$/;
58-
$dbh->do( $command )
59-
or BAIL_OUT( "PSQL Error($file): $command: " . $dbh->errstr);
60-
}
61-
undef $dbh;
62-
}

0 commit comments

Comments
 (0)