script/runGDRTests.pl untested

filter: Statementstmt 0.0% 0 / 329 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 487 SCAR 34.0
CC
5
Cov
0%
END
20.0
get_Priv_dbh
6.0
BEGIN
2.0
CRAP 30.0
1 #!/usr/local/bin/perl
2  
3 use Modern::Perl;
4 use autodie qw(:all);
5 no indirect ':fatal';
6  
7 use version ; our $VERSION = qv('1.3.10');
8  
9 use Try::Tiny ;
10  
11 #DONE: 1. Most of it.
12 #TODO: 1. Improve reporting - rerunning etc
13 #DONE: 2. Honour , but warn about disabled tests and test conditions. This might exist but looks broken.
14 #TODO: 3. Rowcount seems broken. runGDRTests.pl -c local_loaddecisioning -i up_CollectPaymentStatus.cs
15 #TODO: 4. BUM. There was something else !
16 #TODO: 5. Tighten exactness of debugging over sql exec errors.
17  
18 use Carp;
19 use Text::Diff;
20 use File::Basename;
21  
22 use DBI;
23  
24 use VSGDR::UnitTest::TestSet::Test;
25 use VSGDR::UnitTest::TestSet::Test::TestCondition;
26 use VSGDR::UnitTest::TestSet::Representation;
27 use VSGDR::UnitTest::TestSet::Resx;
28  
29 use List::MoreUtils qw(any) ;
30  
31 #use Smart::Comments;
32  
33 our $opt_connection;
34 our $opt_pconnection;
35 our $opt_noReInit;
36 our $opt_noDebug;
37 our $opt_noWarn;
38 our @opt_infile;
39  
40 use Getopt::Euclid qw( :vars<opt_> );
41 use List::MoreUtils qw{firstidx} ;
42 use Data::Dumper;
43 #use Data::Printer;
44  
45 my $dataBase = $opt_connection;
46 my $priv_dataBase = undef ;
47 $priv_dataBase = $opt_pconnection if defined $opt_pconnection ;
48  
49  
50  
51 my %Parsers = () ;
52 my %ValidParserMakeArgs = ( vb => 'NET::VB'
53 , cs => 'NET::CS'
54 , xls => 'XLS'
55 , xml => 'XML'
56 ) ;
57 my %ValidParserMakeArgs2 = ( vb => "NET2::VB"
58 , cs => "NET2::CS"
59 ) ;
60  
61 #my @validSuffixes = keys %ValidParserMakeArgs ;
62 my @validSuffixes = map { '.'.$_ } keys %ValidParserMakeArgs ;
63  
64 #warn Dumper @validSuffixes ;
65 #exit;
66  
67 my $reInit = 1 ;
68 $reInit = (!$opt_noReInit) if defined $opt_noReInit ;
69  
70 my $Warn = 1 ;
71 $Warn = (!$opt_noWarn) if defined $opt_noWarn ;
72  
73 my $Debug = 0 ;
74 $Debug = (!$opt_noDebug) if defined $opt_noDebug ;
75  
76 ### Connect to database
77 my $dbh ;
78 my $dbh_quote_conn ;
79 $dbh = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, PrintWarn => 1, PrintError => 1, RaiseError => 1});
80 $dbh_quote_conn = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, PrintWarn => 1, PrintError => 1, RaiseError => 1});
81  
82 # Always create a $priv_dbh handle, re-use the normal database dsn if no privileged dsn specified.
83 my $priv_dbh = undef ;
84 $priv_dbh = get_Priv_dbh() ;
85  
86 # if noreinit specified
87 # loop over input files, picking up init and cleardown acction
88 # if compatible run init and teardown once out side loop
89 # else fallback to previous implementation
90  
91 my $commonInitSQL = undef ;
92 my $commonCleardownSQL = undef ;
93  
94 my $compatibleactions = 1 ;
95 my @cleanupScripts = () ;
96 my @initScripts = () ;
97  
98 my @cleanupActions = () ;
99 my @initActions = () ;
100  
101 my @testSets = () ;
102  
103 if ( ! $reInit && ( scalar @opt_infile > 1 )) {
104  
105 # for ( my $i=0; $i <= $#opt_infile; $i++ ) { ## Process SQL scripts::: done
106 foreach my $infile (@opt_infile) { ## Process SQL scripts::: done
107  
108 my($infname, $directories, $insfx) = fileparse($infile, @validSuffixes);
109 croak 'Invalid input file' unless defined $insfx ;
110 $insfx = lc $insfx ;
111
112 croak 'Invalid input file' unless exists $ValidParserMakeArgs{$insfx} ;
113 $Parsers{${insfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${insfx}} } )
114 unless exists $Parsers{${insfx}} ;
115 # if input is in a .net language, add in a .net2 parser to the list
116 if ( firstidx { $_ eq ${insfx} } ['cs','vb'] != -1 ) {
117 $Parsers{"${insfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${insfx}} } )
118 unless exists $Parsers{"${insfx}2"} ;
119 }
120  
121 my $testSet = undef ;
122  
123 eval {
124 $testSet = $Parsers{$insfx}->deserialise($infile);
125 } ;
126 if ( not defined $testSet ) {
127 if ( exists $Parsers{"${insfx}2"}) {
128 eval {
129 $testSet = $Parsers{"${insfx}2"}->deserialise($infile);
130 }
131 }
132 else {
133 croak 'Parsing failed.';
134 }
135 }
136
137 push @testSets, $testSet ;
138  
139 my $resxname = $infname . '.resx' ;
140 my $i_resx = VSGDR::UnitTest::TestSet::Resx->new() ;
141 $i_resx->deserialise($resxname) ;
142  
143 my $rh_testScripts = $i_resx->scripts() ;
144  
145 push @cleanupScripts, $rh_testScripts->{testCleanupAction} if exists $rh_testScripts->{testCleanupAction} ;
146 push @initScripts, $rh_testScripts->{testInitializeAction} if exists $rh_testScripts->{testInitializeAction} ;
147  
148 $commonInitSQL = $rh_testScripts->{$testSet->initializeActionLiteral()} ;
149 $commonCleardownSQL = $rh_testScripts->{$testSet->cleanupActionLiteral()} ;
150  
151 $testSet = undef ;
152  
153 # map { $test_testScripts{$_} = $$rh_testScripts{$_} } keys %{$rh_testScripts} ;
154  
155 }
156  
157 @cleanupActions = grep { defined($_->cleanupAction()) } @testSets ;
158 @initActions = grep { defined($_->initializeAction()) } @testSets ;
159  
160 if ( ( scalar @cleanupActions and ( scalar @testSets != scalar @cleanupActions ) )
161 or ( scalar @initActions and ( scalar @testSets != scalar @initActions ) )
162 ) {
163 $compatibleactions = 0 ;
164 }
165 else {
166 if (@testSets > 1) {
167  
168 my $firstInitSQL = $initScripts[0] ;
169 ( my $fi = $firstInitSQL) =~ s{\s+}{\ }xmsi ;
170 shift @initScripts ;
171  
172 my $firstCleanupSQL = $cleanupScripts[0] ;
173 ( my $fc = $firstCleanupSQL) =~ s{\s+}{\ }xmsi ;
174 shift @cleanupScripts ;
175  
176 local $_ = undef ;
177 while ( $_ = shift @initScripts) {
178 s{\s+}{\ }xmsi ;
179 $compatibleactions = 0
180 if diff(\$_,\$fi) ;
181 }
182 while ( $_ = shift @cleanupScripts) {
183 s{\s+}{\ }xmsi ;
184 $compatibleactions = 0
185 if diff(\$_,\$fc) ;
186 }
187 }
188 }
189 }
190 else {
191 $compatibleactions = 0 ; ## fallback to normal behaviour
192 }
193  
194 say {*STDERR} 'Incompatible init/cleanup scripts, falling back to per-test init/cleanup'
195 if not $compatibleactions and not $reInit ;
196  
197 # don't bother with conditions if short-cutting init/teardown. we're implicitly short-cutting things anyway
198 if ( ! $reInit ) {
199 if ( $compatibleactions ) {
200 if ( $commonInitSQL ) {
201 say {*STDERR} 'Running init';
202 my $p_sth = $priv_dbh->prepare($commonInitSQL,{odbc_exec_direct => 1});
203 $p_sth->execute();
204 $p_sth->finish();
205 }
206 }
207 }
208  
209  
210 #for ( my $i=0; $i <= $#opt_infile; $i++ ) { ## Process SQL scripts::: done
211 foreach my $infile (@opt_infile) { ## Process SQL scripts::: done
212  
213 my($infname, $directories, $insfx) = fileparse($infile, @validSuffixes);
214 $insfx = substr(lc $insfx,1) ;
215 croak 'Invalid input file' unless exists $ValidParserMakeArgs{$insfx} ;
216  
217 my $parser = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${insfx}} } );
218 my $testSet = undef ;
219 eval {
220 $testSet = $parser->deserialise($infile);
221 } ;
222 if ( ! defined $testSet) {
223 eval {
224 $parser = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${insfx}} } );
225 $testSet = $parser->deserialise($infile);
226 }
227 }
228  
229 # my $testSet = $parser->deserialise($infile) ; ## doesn't die on a bad language eg cs for vb - probably a PRD replacement issue
230  
231 my $ra_tests = $testSet->tests() ;
232  
233  
234 my $resxname = $infname . '.resx' ;
235 #say {*STDERR} "Running tests in $resxname" ;
236 my $i_resx = VSGDR::UnitTest::TestSet::Resx->new() ;
237 $i_resx->deserialise($resxname) ;
238  
239 my $rh_testScripts = $i_resx->scripts() ;
240  
241 my $TestSQL = undef ;
242 my $PreTestSQL = undef ;
243 my $PostTestSQL = undef ;
244  
245 my $initSQL = undef ;
246 my $cleardownSQL = undef ;
247  
248 $initSQL = $rh_testScripts->{$testSet->initializeActionLiteral()} ;
249 $cleardownSQL = $rh_testScripts->{$testSet->cleanupActionLiteral()} ;
250  
251 $initSQL = $priv_dbh->quote($initSQL) if $initSQL ;
252 $cleardownSQL = $priv_dbh->quote($cleardownSQL) if $cleardownSQL ;
253 $initSQL = 'exec sp_executesql N' . $initSQL if $initSQL ;
254 $cleardownSQL = 'exec sp_executesql N' . $cleardownSQL if $cleardownSQL ;
255  
256  
257 # don't bother with conditions if short-cutting init/teardown. we're implicitly short-cutting things anyway
258 if ( ! $reInit ) {
259 if ( ! $compatibleactions ) {
260 if ( $initSQL ) {
261 say {*STDERR} 'Running init';
262 my $p_sth = $priv_dbh->prepare($initSQL,{odbc_exec_direct => 1});
263 $p_sth->execute();
264 $p_sth->finish();
265 }
266 }
267 }
268  
269 for my $test ( @{$ra_tests} ) {
270 #warn Dumper $test;
271 my %test_res ;
272 my @test_res = () ;
273  
274 try {
275  
276 say {*STDERR} "Performing test :- @{[$test->testName()]}" ;
277  
278  
279 if ( $initSQL and $reInit ) {
280 say {*STDERR} 'Running setup';
281 my $p_sth = $priv_dbh->prepare($initSQL,{odbc_exec_direct => 1});
282 $p_sth->execute();
283  
284 @test_res = () ;
285 do {
286 $p_sth->{PrintError} = 0;
287 $p_sth->{RaiseError} = 0;
288 push @test_res, $p_sth->fetchall_arrayref() ;
289  
290 } while ($p_sth->{odbc_more_results}) ;
291 $p_sth->finish();
292 { my @res = @test_res ;
293 $test_res{INIT} = [ @res ] ;
294 }
295 }
296 $TestSQL = q{} ;
297 $PreTestSQL = q{} ;
298 $PostTestSQL = q{} ;
299  
300 #warn Dumper $test->testAction() ;
301 #warn Dumper $rh_testScripts->{$test->testAction()};
302 #exit;
303  
304 $TestSQL = $rh_testScripts->{$test->testAction()} if $test->testAction() !~ m{\A \s* (?:null|nothing) \s* \z}ixsm;
305 $PreTestSQL = $rh_testScripts->{$test->preTestAction()} if $test->preTestAction() !~ m{\A \s* (?:null|nothing) \s* \z}ixsm;
306 $PostTestSQL = $rh_testScripts->{$test->postTestAction()} if $test->postTestAction() !~ m{\A \s* (?:null|nothing) \s* \z}ixsm;
307 #warn Dumper $TestSQL ;
308 $TestSQL = $dbh_quote_conn->quote($TestSQL) if $TestSQL ;
309 $PreTestSQL = $dbh_quote_conn->quote($PreTestSQL) if $PreTestSQL ;
310 $PostTestSQL = $dbh_quote_conn->quote($PostTestSQL) if $PostTestSQL;
311 #warn Dumper $TestSQL ;
312 $TestSQL = 'exec sp_executesql N' . $TestSQL if $TestSQL ;
313 $PreTestSQL = 'exec sp_executesql N' . $PreTestSQL if $PreTestSQL ;
314 $PostTestSQL = 'exec sp_executesql N' . $PostTestSQL if $PostTestSQL;
315  
316 #warn Dumper $TestSQL ;
317  
318 if ( $PreTestSQL ) {
319 say {*STDERR} 'Running PreTest' ;
320 #warn Dumper $test->preTestAction() ;
321 #warn Dumper $rh_testScripts->{$test->preTestAction()} ;
322 #warn Dumper $PreTestSQL ;
323  
324 my $p_sth = $priv_dbh->prepare($PreTestSQL,{odbc_exec_direct => 1});
325 $p_sth->execute();
326  
327 @test_res = () ;
328 do {
329 $p_sth->{PrintError} = 0;
330 $p_sth->{RaiseError} = 0;
331 push @test_res, $p_sth->fetchall_arrayref() ;
332  
333 } while ($p_sth->{odbc_more_results}) ;
334 { my @res = @test_res ;
335 $test_res{PRETEST} = [ @res ] ;
336 }
337 }
338  
339 #warn $TestSQL;
340 say {*STDERR} 'Running Test' ;
341 my $sth = $dbh->prepare($TestSQL,{odbc_exec_direct => 1});
342
343 try {
344 $sth->{PrintError} = 0;
345 $sth->{RaiseError} = 1;
346 $sth->execute;
347 }
348 catch {
349 if ($Debug ) {
350 say {*STDERR} $TestSQL ;
351 }
352 }
353 finally {
354 # say {*STDERR} "ARSE!";
355 }
356
357 #warn Dumper $TestSQL ;
358 @test_res = () ;
359 do {
360 $sth->{PrintError} = 0;
361 $sth->{RaiseError} = 0;
362 push @test_res, $sth->fetchall_arrayref() ;
363  
364 } while ($sth->{odbc_more_results}) ;
365 #warn Dumper @test_res;
366 $sth->finish();
367 { my @res = @test_res ;
368 $test_res{TEST} = [ @res ] ;
369 }
370 if ( $PostTestSQL ) {
371 say {*STDERR} 'Running PostTest' ;
372 my $p_sth = $priv_dbh->prepare($PostTestSQL,{odbc_exec_direct => 1});
373 $p_sth->execute();
374  
375 @test_res = () ;
376 do {
377 $p_sth->{PrintError} = 0;
378 $p_sth->{RaiseError} = 0;
379 push @test_res, $p_sth->fetchall_arrayref() ;
380  
381 } while ($sth->{odbc_more_results}) ;
382 $p_sth->finish();
383 { my @res = @test_res ;
384 $test_res{POSTTEST} = [ @res ] ;
385 }
386 }
387  
388  
389 if ( $cleardownSQL and $reInit ) {
390 say {*STDERR} 'Running cleardown';
391 my $p_sth = $priv_dbh->prepare($cleardownSQL,{odbc_exec_direct => 1});
392 $p_sth->execute();
393  
394 @test_res = () ;
395 do {
396 $p_sth->{PrintError} = 0;
397 $p_sth->{RaiseError} = 0;
398 push @test_res, $p_sth->fetchall_arrayref() ;
399  
400 } while ($sth->{odbc_more_results}) ;
401 { my @res = @test_res ;
402 $test_res{CLEARDOWN} = [ @res ] ;
403 }
404 }
405 #warn Dumper @{$test->test_conditions()} ;
406 foreach my $tc (@{$test->test_conditions()}) {
407 # say STDERR $tc->conditionName . " failed."
408 # unless $tc->check( $test_res{TEST} ) ;
409 # warn Dumper $tc
410 # unless $tc->check( $test_res{TEST} ) ;
411 #say $tc->conditionEnabled;
412 if ( $tc->conditionISEnabled() ) {
413 my $res = $tc->check( $test_res{TEST} );
414 #say $res;
415 }
416 else {
417 say {*STDERR} 'Condition ' . $tc->conditionName . ' disabled.'
418 if $Warn ;
419 };
420  
421 }
422  
423 %test_res = () ;
424  
425 }
426 catch {
427 carp $_ ;
428 if ($Debug ) {
429 say {*STDERR} $rh_testScripts->{$test->testAction()} ;
430 }
431 }
432 finally {
433 }
434 }
435  
436 # don't bother with conditions if short-cutting init/teardown. we're implicitly short-cutting things anmyway
437 if ( ! $reInit ) {
438 if ( ! $compatibleactions ) {
439 if ( $cleardownSQL ) {
440 say {*STDERR} 'Running cleardown';
441 my $p_sth = $priv_dbh->prepare($cleardownSQL,{odbc_exec_direct => 1});
442 $p_sth->execute();
443 $p_sth->finish();
444 }
445 }
446 }
447 }
448  
449 # don't bother with conditions if short-cutting init/teardown. we're implicitly short-cutting things anmyway
450 if ( ! $reInit ) {
451 if ( $compatibleactions ) {
452 if ( $commonCleardownSQL ) {
453 say {*STDERR} 'Running cleardown';
454 my $p_sth = $priv_dbh->prepare($commonCleardownSQL,{odbc_exec_direct => 1});
455 $p_sth->execute();
456 $p_sth->finish();
457 }
458 }
459 }
460  
461 exit ;
462  
463 # #######################################################################################
464  
465  
466 END {
467 $dbh->disconnect() if $dbh ;
468 $dbh_quote_conn->disconnect() if $dbh_quote_conn ;
469 $priv_dbh->disconnect() if $priv_dbh ;
470 }
471  
472 # #######################################################################################
473  
474  
475 sub get_Priv_dbh {
476 local $_ = undef ;
477 my $_dbh ;
478  
479 if ( defined $priv_dataBase ) {
480 $_dbh = DBI->connect("dbi:ODBC:${priv_dataBase}", q{}, q{}, { AutoCommit => 1, PrintWarn => 1, PrintError => 1, RaiseError => 1})
481 }
482 else {
483 $_dbh = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, PrintWarn => 1, PrintError => 1, RaiseError => 1})
484 }
485 return $_dbh ;
486 }
487  
488 __DATA__