1# Copyright (c) 2006 Jeffrey I Cohen.  All rights reserved.
2#
3# Before `make install' is performed this script should be runnable with
4# `make test'. After `make install' it should work as `perl test.pl'
5
6######################### We start with some black magic to print on failure.
7
8# Change 1..1 below to 1..last_test_to_print .
9# (It may become useful if the test is moved to ./t subdirectory.)
10
11BEGIN { $| = 1; print "1..4\n"; }
12END {print "not ok 1\n" unless $loaded;}
13use Genezzo::GenDBI;
14$loaded = 1;
15print "ok 1\n";
16
17######################### End of black magic.
18
19# Insert your test code below (better if it prints "ok 13"
20# (correspondingly "not ok 13") depending on the success of chunk 13
21# of the test code):
22use strict;
23use warnings;
24use File::Path;
25use File::Spec;
26
27my $TEST_COUNT;
28
29$TEST_COUNT = 2;
30
31my $dbinit   = 1;
32my $gnz_home = File::Spec->catdir("t", "gnz_home");
33my $gnz_restore = File::Spec->catdir("t", "restore");
34my $gnz_sql = File::Spec->catdir("t", "SQL");
35my $gnz_log = File::Spec->catdir("t", "log");
36#rmtree($gnz_home, 1, 1);
37#mkpath($gnz_home, 1, 0755);
38
39our $GZERR = sub {
40    my %args = (@_);
41
42    return
43        unless (exists($args{msg}));
44
45    # to process spooling to multiple files
46    my $outfile_h = $args{outfile_list} || undef;
47
48    my $warn = 0;
49    if (exists($args{severity}))
50    {
51        my $sev = uc($args{severity});
52        $sev = 'WARNING'
53            if ($sev =~ m/warn/i);
54
55        # don't print 'INFO' prefix
56        if ($args{severity} !~ m/info/i)
57        {
58            printf ("%s: ", $sev);
59
60            if (defined($outfile_h))
61            {
62                while (my ($kk, $vv) = each (%{$outfile_h}))
63                {
64                    printf $vv ("%s: ", $sev);
65                }
66            }
67
68            $warn = 1;
69        }
70        else
71        {
72            if (exists($args{no_info}))
73            {
74                # don't print info if no_info set...
75                return;
76            }
77        }
78
79    }
80    print $args{msg};
81    # add a newline if necessary
82    print "\n" unless $args{msg}=~/\n$/;
83#    carp $args{msg}
84#      if (warnings::enabled() && $warn);
85
86    if (defined($outfile_h))
87    {
88        while (my ($kk, $vv) = each (%{$outfile_h}))
89        {
90            print $vv $args{msg};
91            # add a newline if necessary
92            print $vv "\n" unless $args{msg}=~/\n$/;
93        }
94    }
95
96};
97
98
99{
100    use Genezzo::TestSetup;
101
102    my $fb =
103        Genezzo::TestSetup::CreateOrRestoreDB(
104                                               gnz_home => $gnz_home,
105                                               restore_dir => $gnz_restore
106                                               );
107
108    unless (defined($fb))
109    {
110        not_ok ("could not create database");
111        exit 1;
112    }
113    ok();
114    $dbinit = 0;
115
116}
117
118{
119    use Genezzo::Util;
120    use Genezzo::TestSQL;
121
122#    my $dbh = Genezzo::GenDBI->connect($gnz_home, "NOUSER", "NOPASSWORD");
123    my $dbh = Genezzo::GenDBI->new(gnz_home => $gnz_home,
124                                   GZERR    => $GZERR);
125
126    unless (defined($dbh))
127    {
128        not_ok ("could not find database");
129        exit 1;
130    }
131    ok();
132
133    my $dir_h;
134
135    if ( !opendir($dir_h, $gnz_sql) )
136    {
137        not_ok ("could not open $gnz_sql");
138    }
139    else
140    {
141        my $fnam;
142        while ($fnam  = readdir($dir_h))
143        {
144            next
145                unless ($fnam =~ m/sql$/);
146
147            # test each sql file in the directory
148
149            my $sql_script =
150#                File::Spec->rel2abs(
151                                    File::Spec->catfile(
152                                                        $gnz_sql,
153                                                        $fnam
154                                                        );
155
156            my $stat =
157                Genezzo::TestSQL::TestSQL(dbh => $dbh,
158                                          log_dir => $gnz_log,
159                                          sql_script => $sql_script);
160
161            unless (defined($stat))
162            {
163                not_ok("bad stat for $sql_script");
164                next;
165            }
166
167            if ($stat =~ m/no differences found/)
168            {
169#                ok();
170                next;
171            }
172            else
173            {
174                not_ok($stat);
175            }
176        } # end while
177    }
178
179    ok();
180}
181
182sub ok
183{
184    print "ok $TEST_COUNT\n";
185
186    $TEST_COUNT++;
187}
188
189
190sub not_ok
191{
192    my ( $message ) = @_;
193
194    print "not ok $TEST_COUNT #  $message\n";
195
196        $TEST_COUNT++;
197}
198
199
200sub skip
201{
202    my ( $message ) = @_;
203
204    print "ok $TEST_COUNT # skipped: $message\n";
205
206        $TEST_COUNT++;
207}
208
209