File tree Expand file tree Collapse file tree 3 files changed +57
-33
lines changed
Expand file tree Collapse file tree 3 files changed +57
-33
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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;
Original file line number Diff line number Diff line change 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
84my $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
4132done_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- }
You can’t perform that action at this time.
0 commit comments