change all the tests to use the new test schema namespace
[dbsrgits/DBIx-Class-DeploymentHandler.git] / t / postgresql / basic.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5 use lib 't/lib';
6
7 use Test::More;
8 use Test::Requires qw(Test::postgresql);
9 use Test::Requires qw(POSIX);
10 use File::Spec::Functions 'catdir', 'catfile';
11 use File::Path 'remove_tree';
12 use DBIx::Class::DeploymentHandler;
13 use DBICDHTest;
14
15 ok my $testdb = build_test_postgresql(),
16   'good test db';
17
18 my $dbh = DBI->connect("DBI:Pg:dbname=test;host=127.0.0.1;port=${\$testdb->port}",'postgres','');
19
20 VERSION1: {
21   use_ok 'DBICVersion_v1';
22   $DBICVersion::Schema1::VERSION = 1;
23   ok my $schema = DBICVersion::Schema1->connect(sub { $dbh }),
24     'got schema';
25
26   ok my $dbic_dh = build_dh($schema),
27     'got dbicdh';
28
29   $dbic_dh->prepare_install;
30   make_perl_runfile();
31   $dbic_dh->install;
32
33   is $dbic_dh->database_version, 1, 'correct db version';
34 }
35
36 VERSION2: {
37   use_ok 'DBICVersion_v2';
38   $DBICVersion::Schema2::VERSION = 2;
39   ok my $schema = DBICVersion::Schema2->connect(sub { $dbh }),
40     'got schema';
41
42   ok my $dbic_dh = build_dh($schema,1),
43     'got dbicdh';
44
45   $dbic_dh->prepare_install();
46   $dbic_dh->prepare_upgrade();
47   $dbic_dh->prepare_downgrade();
48 }
49
50 ok -d catdir('t','share','var','pg-deploy','PostgreSQL','downgrade','2-1'),
51   'reasonable defaults properly creates a downgrade';
52
53 $testdb->stop(POSIX::SIGINT);  ## We need this to stop Pg
54 done_testing();
55
56 sub build_dh {
57   DBIx::Class::DeploymentHandler->new({
58     script_directory => catdir('t','share','var','pg-deploy'),
59     schema => shift,
60     databases => ['PostgreSQL']});
61 }
62
63 sub build_test_postgresql {
64   my %config = (
65     base_dir => catdir('t','share','var','pg'),
66     initdb_args => $Test::postgresql::Defaults{initdb_args},
67     postmaster_args => $Test::postgresql::Defaults{postmaster_args});
68
69   if(my $testdb = Test::postgresql->new(%config)) {
70     return $testdb;
71   } else {
72     die $Test::postgresql::errstr;
73   }
74 }
75
76 sub make_perl_runfile {
77   open(
78     my $perl_run,
79     ">",
80     catfile('t','share','var','pg-deploy','PostgreSQL', 'deploy', '1', '002-test.pl')
81   ) || do { $testdb->stop(POSIX::SIGINT); die "Cannot open: $!"};
82
83   print $perl_run <<'END';
84   sub {
85     my $schema = shift;
86   };
87 END
88
89   close $perl_run;
90 }
91
92 END {
93   remove_tree catdir('t','share','var','pg');
94   remove_tree catdir('t','share','var','pg-deploy');
95 }