| 1 | #!/bin/perl | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use autodie qw(:all); | ||
| 7 | no indirect ':fatal'; | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | #TODO: 1. Add some form of support for Pre/Post init/cleardown condition generation. | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use DBI; | ||
| 15 | |||
| 16 | use VSGDR::UnitTest::TestSet::Test; | ||
| 17 | use VSGDR::UnitTest::TestSet::Test::TestCondition; | ||
| 18 | use VSGDR::UnitTest::TestSet::Representation; | ||
| 19 | use VSGDR::UnitTest::TestSet::Resx; | ||
| 20 | |||
| 21 | use VSGDR::SQLServer::DataType; | ||
| 22 | |||
| 23 | use Readonly ; | ||
| 24 | use List::MoreUtils qw(any) ; | ||
| 25 | |||
| 26 | use File::Basename; | ||
| 27 | use Smart::Comments; | ||
| 28 | use Try::Tiny; | ||
| 29 | |||
| 30 | |||
| 31 | use Getopt::Euclid qw( :vars<opt_> ); | ||
| 32 | use List::MoreUtils qw{firstidx} ; | ||
| 33 | use Data::Dumper; | ||
| 34 | |||
| 35 | use version ; our $VERSION = qv('1.4.1'); | ||
| 36 | |||
| 37 | our @opt_infile; | ||
| 38 | our @opt_outfile; | ||
| 39 | our $opt_noscalarValues; | ||
| 40 | our $opt_notypes; | ||
| 41 | our $opt_version; | ||
| 42 | our $opt_connection; | ||
| 43 | our @opt_resultSets; | ||
| 44 | our $opt_pconnection; | ||
| 45 | our $opt_initfile; | ||
| 46 | our $opt_prefile; | ||
| 47 | our $opt_postfile; | ||
| 48 | our $opt_cleanupfile; | ||
| 49 | our $opt_namespace; | ||
| 50 | our $opt_noRunTestsToGenerateResults; | ||
| 51 | |||
| 52 | #warn $opt_norunTestsToGenerateResults; | ||
| 53 | #exit; | ||
| 54 | my $version = $opt_version ; | ||
| 55 | |||
| 56 | |||
| 57 | my $opt_scalarValues = !$opt_noscalarValues; | ||
| 58 | my $opt_types = !$opt_notypes; | ||
| 59 | my $gen_types = 0; | ||
| 60 | $gen_types = (!$opt_notypes) if defined $opt_notypes ; | ||
| 61 | |||
| 62 | my $dataBase = $opt_connection; | ||
| 63 | my $priv_dataBase = undef ; | ||
| 64 | $priv_dataBase = $opt_pconnection if defined $opt_pconnection ; | ||
| 65 | |||
| 66 | |||
| 67 | my @resultSets = () ; | ||
| 68 | @resultSets = @opt_resultSets if @opt_resultSets ; | ||
| 69 | my $generateAllResultSets = ! ( scalar @resultSets ) ; | ||
| 70 | my $generateScalarChecks = $opt_scalarValues ; | ||
| 71 | my $generatePassedResults = $opt_norunTestsToGenerateResults ; | ||
| 72 | |||
| 73 | my %Parsers = () ; | ||
| 74 | my %ValidParserMakeArgs = ( vb => 'NET::VB' | ||
| 75 | , cs => 'NET::CS' | ||
| 76 | , xls => 'XLS' | ||
| 77 | , xml => 'XML' | ||
| 78 | ) ; | ||
| 79 | my %ValidParserMakeArgs2 = ( vb => "NET2::VB" | ||
| 80 | , cs => "NET2::CS" | ||
| 81 | ) ; | ||
| 82 | |||
| 83 | my @validSuffixes = map { '.'.$_ } keys %ValidParserMakeArgs ; | ||
| 84 | |||
| 85 | ### Connect to database | ||
| 86 | |||
| 87 | my $dbh = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, RaiseError => 1 }); | ||
| 88 | my $dbh_typeinfo = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, RaiseError => 1 }); | ||
| 89 | |||
| 90 | # Always create a $priv_dbh handle, re-use the normal database dsn if no privileged dsn specified. | ||
| 91 | my $priv_dbh = undef ; | ||
| 92 | if ( defined $priv_dataBase ) { | ||
| 93 | $priv_dbh = DBI->connect("dbi:ODBC:${priv_dataBase}", q{}, q{}, { AutoCommit => 1, RaiseError => 1 }) | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $priv_dbh = DBI->connect("dbi:ODBC:${dataBase}", q{}, q{}, { AutoCommit => 1, RaiseError => 1 }) | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | my $initSQL = undef ; | ||
| 101 | my $cleardownSQL = undef ; | ||
| 102 | my $PreTestSQL = undef ; | ||
| 103 | my $PostTestSQL = undef ; | ||
| 104 | |||
| 105 | $initSQL = getFile($opt_initfile) if defined $opt_initfile ; | ||
| 106 | $cleardownSQL = getFile($opt_cleanupfile) if defined $opt_cleanupfile ; | ||
| 107 | $PreTestSQL = getFile($opt_prefile) if defined $opt_prefile ; | ||
| 108 | $PostTestSQL = getFile($opt_postfile) if defined $opt_postfile ; | ||
| 109 | |||
| 110 | my $origInitSQL = $initSQL ; | ||
| 111 | my $origCleardownSQL = $cleardownSQL ; | ||
| 112 | my $origPreTestSQL = $PreTestSQL ; | ||
| 113 | my $origPostTestSQL = $PostTestSQL ; | ||
| 114 | |||
| 115 | $initSQL = $dbh->quote($initSQL) if $initSQL ; | ||
| 116 | $cleardownSQL = $dbh->quote($cleardownSQL) if $cleardownSQL ; | ||
| 117 | $PreTestSQL = $dbh->quote($PreTestSQL) if $PreTestSQL ; | ||
| 118 | $PostTestSQL = $dbh->quote($PostTestSQL) if $PostTestSQL; | ||
| 119 | |||
| 120 | $initSQL = 'exec sp_executesql N' . $initSQL if $initSQL ; | ||
| 121 | $cleardownSQL = 'exec sp_executesql N' . $cleardownSQL if $cleardownSQL ; | ||
| 122 | $PreTestSQL = 'exec sp_executesql N' . $PreTestSQL if $PreTestSQL ; | ||
| 123 | $PostTestSQL = 'exec sp_executesql N' . $PostTestSQL if $PostTestSQL; | ||
| 124 | |||
| 125 | |||
| 126 | for ( my $i=0; $i <= $#opt_infile; $i++ ) { ## Process SQL scripts::: done | ||
| 127 | |||
| 128 | my $infile = $opt_infile[$i]; | ||
| 129 | my($infname, $indirectories, $insfx) = fileparse($infile, @validSuffixes); | ||
| 130 | croak 'Invalid input file' unless defined $insfx ; | ||
| 131 | $insfx = lc $insfx ; | ||
| 132 | |||
| 133 | my $outfile = $opt_outfile[$i]; | ||
| 134 | my($outfname, $outdirectories, $outsfx) = fileparse($outfile, @validSuffixes); | ||
| 135 | croak 'Invalid output file' unless defined $outsfx ; | ||
| 136 | $outsfx = substr(lc $outsfx,1) ; | ||
| 137 | |||
| 138 | #warn Dumper $insfx; | ||
| 139 | #warn Dumper $outsfx; | ||
| 140 | |||
| 141 | croak 'Invalid output file' unless exists $ValidParserMakeArgs{$outsfx} ; | ||
| 142 | |||
| 143 | # if output is needed in ssdt unit test format add in a .net2 parser to the list | ||
| 144 | if ($version == 1) { | ||
| 145 | $Parsers{${outsfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${outsfx}} } ); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | $Parsers{"${outsfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${outsfx}} } ); | ||
| 149 | } | ||
| 150 | |||
| 151 | my $testSet = VSGDR::UnitTest::TestSet->new( { NAMESPACE => "${opt_namespace}" | ||
| 152 | , CLASSNAME => "${outfname}_cls" | ||
| 153 | } | ||
| 154 | ) ; | ||
| 155 | |||
| 156 | |||
| 157 | $testSet->initializeAction($testSet->initializeActionLiteral()) | ||
| 158 | if $initSQL ; | ||
| 159 | $testSet->cleanupAction($testSet->cleanupActionLiteral()) | ||
| 160 | if $cleardownSQL ; | ||
| 161 | |||
| 162 | my $PreTestActionName = 'null' ; | ||
| 163 | my $PostTestActionName = 'null' ; | ||
| 164 | my $TestActionName = "${outfname}_TestAction" ; | ||
| 165 | |||
| 166 | $PreTestActionName = "${outfname}_PreTestAction" if $PreTestSQL ; | ||
| 167 | $PostTestActionName = "${outfname}_PostTestAction" if $PostTestSQL ; | ||
| 168 | |||
| 169 | my $test = VSGDR::UnitTest::TestSet::Test->new( { TESTNAME => "${outfname}" | ||
| 170 | , TESTACTIONDATANAME => "${outfname}Data" | ||
| 171 | , PRETESTACTION => ${PreTestActionName} | ||
| 172 | , TESTACTION => ${TestActionName} | ||
| 173 | , POSTTESTACTION => ${PostTestActionName} | ||
| 174 | } | ||
| 175 | ) ; | ||
| 176 | |||
| 177 | ### Open script files | ||
| 178 | |||
| 179 | my $TestSQL = getFile($infile) ; | ||
| 180 | my $origTestSQL = $TestSQL ; | ||
| 181 | |||
| 182 | $TestSQL = $dbh->quote($TestSQL) ; | ||
| 183 | $TestSQL = 'exec sp_executesql N' . $TestSQL ; | ||
| 184 | |||
| 185 | |||
| 186 | my @testConditions = () ; | ||
| 187 | my $conditionNamePrefix = "${outfname}_Res_" ; | ||
| 188 | |||
| 189 | Readonly::Scalar my $testConditionTypeRC => 'RowCount' ; | ||
| 190 | Readonly::Scalar my $testConditionTypeSV => 'ScalarValue' ; | ||
| 191 | |||
| 192 | if ( $generatePassedResults ) { | ||
| 193 | |||
| 194 | my $testConditionRC = VSGDR::UnitTest::TestSet::Test::TestCondition->make( | ||
| 195 | { TESTCONDITIONTYPE => $testConditionTypeRC | ||
| 196 | , CONDITIONTESTACTIONNAME => ${TestActionName} | ||
| 197 | , CONDITIONNAME => ${conditionNamePrefix} . "1_RowCount" | ||
| 198 | , CONDITIONENABLED => 'True' | ||
| 199 | , CONDITIONROWCOUNT => 1 | ||
| 200 | , CONDITIONRESULTSET => 1 | ||
| 201 | } ) ; | ||
| 202 | push @testConditions, $testConditionRC ; | ||
| 203 | |||
| 204 | my $testConditionSV = VSGDR::UnitTest::TestSet::Test::TestCondition->make( | ||
| 205 | { TESTCONDITIONTYPE => $testConditionTypeSV | ||
| 206 | , CONDITIONTESTACTIONNAME => ${TestActionName} | ||
| 207 | , CONDITIONNAME => ${conditionNamePrefix} . '1_Col_1' | ||
| 208 | , CONDITIONENABLED => 'True' | ||
| 209 | , CONDITIONEXPECTEDVALUE => '"Passed"' | ||
| 210 | , CONDITIONNULLEXPECTED => 'False' | ||
| 211 | , CONDITIONRESULTSET => 1 | ||
| 212 | , CONDITIONROWNUMBER => 1 | ||
| 213 | , CONDITIONCOLUMNNUMBER => 1 | ||
| 214 | } ) ; | ||
| 215 | |||
| 216 | push @testConditions, $testConditionSV ; | ||
| 217 | |||
| 218 | } | ||
| 219 | else { | ||
| 220 | if ( $initSQL ) { | ||
| 221 | ### Run initialisation script | ||
| 222 | my $p_sth = $priv_dbh->prepare($initSQL,{odbc_exec_direct => 1}); | ||
| 223 | $p_sth->execute(); | ||
| 224 | } | ||
| 225 | if ( $PreTestSQL ) { | ||
| 226 | ### Run pre-test script | ||
| 227 | my $p_sth = $priv_dbh->prepare($PreTestSQL,{odbc_exec_direct => 1}); | ||
| 228 | $p_sth->execute(); | ||
| 229 | } | ||
| 230 | |||
| 231 | ### Run test script once | ||
| 232 | |||
| 233 | my @run1_res ; | ||
| 234 | my @res_col ; | ||
| 235 | my @res_type ; | ||
| 236 | my $sth = $dbh->prepare($TestSQL,{odbc_exec_direct => 1}); | ||
| 237 | |||
| 238 | try { | ||
| 239 | $sth->execute; | ||
| 240 | } catch { | ||
| 241 | #warn "caught error: $_\n"; | ||
| 242 | warn "File :- $infile\n"; | ||
| 243 | #warn "TRYING :- \n$TestSQL"; | ||
| 244 | }; | ||
| 245 | |||
| 246 | |||
| 247 | do { | ||
| 248 | push @res_type, $sth->{TYPE} ; | ||
| 249 | push @res_col, $sth->{NAME} ; | ||
| 250 | |||
| 251 | if ( $gen_types ) { | ||
| 252 | { | ||
| 253 | my @names = map { scalar $dbh_typeinfo->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} } ; | ||
| 254 | my @colSize = map { scalar $dbh_typeinfo->type_info($_)->{COLUMN_SIZE} } @{ $sth->{TYPE} } ; | ||
| 255 | |||
| 256 | my @types = List::MoreUtils::pairwise { $a =~ m{char}ism ? "$a($b)" : "$a" } @names, @colSize ; | ||
| 257 | my @spec = List::MoreUtils::pairwise { "$a\t\t\t$b" } @{$sth->{NAME}}, @types ; | ||
| 258 | |||
| 259 | do { local $"= "\n,\t" ; | ||
| 260 | say {*STDERR} "ResultSet(\n\t@{spec}\n)"; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | push @run1_res, $sth->fetchall_arrayref() ; | ||
| 266 | |||
| 267 | } while ($sth->{odbc_more_results}) ; | ||
| 268 | |||
| 269 | |||
| 270 | foreach my $row (@res_col) { | ||
| 271 | for ( my $i = 0; $i < scalar (@$row) ; $i++ ) { | ||
| 272 | my $y=$i+1; | ||
| 273 | $$row[$i] = "Col_${y}" if $$row[$i] eq q{} ; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | ### Run test script twice | ||
| 278 | |||
| 279 | my @run2_res ; | ||
| 280 | $sth = $dbh->prepare($TestSQL,{odbc_exec_direct => 1}); | ||
| 281 | $sth->execute; | ||
| 282 | do { | ||
| 283 | push @run2_res, $sth->fetchall_arrayref() ; | ||
| 284 | |||
| 285 | } while ($sth->{odbc_more_results}) ; | ||
| 286 | |||
| 287 | my $G_ln = 0 ; | ||
| 288 | |||
| 289 | ### Build conditions | ||
| 290 | |||
| 291 | for ( my $ra_arr = 0; $ra_arr < scalar (@res_col) ; $ra_arr++ ) { | ||
| 292 | |||
| 293 | unless ( $generateAllResultSets ) { | ||
| 294 | next unless any { $_ == ($ra_arr+1) } @resultSets ; | ||
| 295 | } | ||
| 296 | #warn Dumper @resultSets ; | ||
| 297 | #warn "!!!!\n"; | ||
| 298 | #warn Dumper $ra_arr ; | ||
| 299 | |||
| 300 | my $testConditionRC = VSGDR::UnitTest::TestSet::Test::TestCondition->make( | ||
| 301 | { TESTCONDITIONTYPE => $testConditionTypeRC | ||
| 302 | , CONDITIONTESTACTIONNAME => ${TestActionName} | ||
| 303 | , CONDITIONNAME => ${conditionNamePrefix} . ($ra_arr+1) . "_RowCount" | ||
| 304 | , CONDITIONENABLED => 'True' | ||
| 305 | , CONDITIONROWCOUNT => scalar(@{$run1_res[$ra_arr]}) | ||
| 306 | , CONDITIONRESULTSET => $ra_arr+1 | ||
| 307 | } ) ; | ||
| 308 | push @testConditions, $testConditionRC ; | ||
| 309 | |||
| 310 | my $single_row_output = ( scalar(@{$run1_res[$ra_arr]}) == 1 | ||
| 311 | ? 1 | ||
| 312 | : 0 ); | ||
| 313 | |||
| 314 | |||
| 315 | if ( $generateScalarChecks) { | ||
| 316 | for ( my $ra_row = 0; $ra_row < scalar ( @{$run1_res[$ra_arr]} ) ; $ra_row++ ) { | ||
| 317 | |||
| 318 | for ( my $ra_col = 0; $ra_col < scalar ( @{$res_col[$ra_arr]} ) ; $ra_col++ ) { | ||
| 319 | |||
| 320 | my $run1_dataValue = VSGDR::SQLServer::DataType->make( $res_type[$ra_arr][$ra_col] | ||
| 321 | , $run1_res[$ra_arr][$ra_row][$ra_col] | ||
| 322 | ) ; | ||
| 323 | |||
| 324 | my $run2_dataValue = VSGDR::SQLServer::DataType->make( $res_type[$ra_arr][$ra_col] | ||
| 325 | , $run2_res[$ra_arr][$ra_row][$ra_col] | ||
| 326 | ) ; | ||
| 327 | |||
| 328 | # check the values are stable from run to run before generating test condition. | ||
| 329 | if ( ( ( ! defined $run1_dataValue->value() ) and (! defined $run2_dataValue->value() ) ) or | ||
| 330 | ( ( defined $run1_dataValue->value() and defined $run2_dataValue->value() ) | ||
| 331 | and $run1_dataValue eq $run2_dataValue | ||
| 332 | ) | ||
| 333 | ) { | ||
| 334 | my $testConditionSV = VSGDR::UnitTest::TestSet::Test::TestCondition->make( | ||
| 335 | { TESTCONDITIONTYPE => $testConditionTypeSV | ||
| 336 | , CONDITIONTESTACTIONNAME => ${TestActionName} | ||
| 337 | , CONDITIONNAME => ( $single_row_output == 0 | ||
| 338 | ? ${conditionNamePrefix} . ($ra_arr+1) . '_Row_' . ($ra_row+1) . '_' . $res_col[$ra_arr][$ra_col] | ||
| 339 | : ${conditionNamePrefix} . ($ra_arr+1) . '_' . $res_col[$ra_arr][$ra_col] | ||
| 340 | ) | ||
| 341 | , CONDITIONENABLED => 'True' | ||
| 342 | , CONDITIONEXPECTEDVALUE => ( defined $run1_dataValue->getValue() | ||
| 343 | ? q{"} . # " kill highlighting | ||
| 344 | $run1_dataValue->quoteValue($run1_dataValue->getValue()) | ||
| 345 | . q{"} # " kill highlighting | ||
| 346 | : 'null' | ||
| 347 | ) | ||
| 348 | , CONDITIONNULLEXPECTED => defined $run1_res[$ra_arr][$ra_row][$ra_col] ? 'False' : 'True' | ||
| 349 | , CONDITIONRESULTSET => $ra_arr+1 | ||
| 350 | , CONDITIONROWNUMBER => $ra_row+1 | ||
| 351 | , CONDITIONCOLUMNNUMBER => $ra_col+1 | ||
| 352 | } ) ; | ||
| 353 | |||
| 354 | push @testConditions, $testConditionSV ; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | say {*STDERR} "Skipping mutating values for resultset\[${ra_arr}\], row\[${ra_row}\], column\[${ra_col}\] .." ; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | if ( $PostTestSQL ) { | ||
| 366 | ### Run post-test script | ||
| 367 | my $p_sth = $priv_dbh->prepare($PostTestSQL,{odbc_exec_direct => 1}); | ||
| 368 | $p_sth->execute(); | ||
| 369 | } | ||
| 370 | |||
| 371 | if ( $cleardownSQL ) { | ||
| 372 | ### Run cleardown script | ||
| 373 | my $p_sth = $priv_dbh->prepare($cleardownSQL,{odbc_exec_direct => 1}); | ||
| 374 | $p_sth->execute(); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | ### Build GDR files | ||
| 379 | |||
| 380 | $test->testAction(${TestActionName}) ; | ||
| 381 | $test->test_conditions(\@testConditions) ; | ||
| 382 | $testSet->tests([$test]) ; | ||
| 383 | |||
| 384 | my $o_resx = VSGDR::UnitTest::TestSet::Resx->new() ; | ||
| 385 | my %code = ( ${TestActionName} => $origTestSQL ) ; | ||
| 386 | |||
| 387 | $code{$PreTestActionName} = $origPreTestSQL | ||
| 388 | if $origPreTestSQL ; | ||
| 389 | $code{$PostTestActionName} = $origPostTestSQL | ||
| 390 | if $origPostTestSQL ; | ||
| 391 | $code{$testSet->initializeActionLiteral()} = $origInitSQL | ||
| 392 | if $origInitSQL ; | ||
| 393 | $code{$testSet->cleanupActionLiteral()} = $origCleardownSQL | ||
| 394 | if $origCleardownSQL ; | ||
| 395 | |||
| 396 | |||
| 397 | $o_resx->scripts(\%code) ; | ||
| 398 | $o_resx->serialise($outfname.'.resx',$o_resx) ; | ||
| 399 | |||
| 400 | if ($version == 1) { | ||
| 401 | $Parsers{$outsfx}->serialise($opt_outfile[$i],$testSet); | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | $Parsers{"${outsfx}2"}->serialise($opt_outfile[$i],$testSet); | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | exit ; | ||
| 413 | |||
| 414 | # ####################################################################################### | ||
| 415 | |||
| 416 | sub getFile { | ||
| 417 | local $_ = undef ; | ||
| 418 | my $infile = shift or croak 'no input filename' ; | ||
| 419 | my $SQL = q{} ; | ||
| 420 | open my $infh, '<', $infile ; | ||
| 421 | { local $/=undef ; $SQL = <$infh> ; close $infh ; } ; | ||
| 422 | return scalar $SQL ; | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | END { | ||
| 427 | $dbh->disconnect() if $dbh ; | ||
| 428 | $dbh_typeinfo->disconnect() if $dbh_typeinfo ; | ||
| 429 | $priv_dbh->disconnect() if $priv_dbh ; | ||
| 430 | } | ||
| 431 | |||
| 432 | __DATA__ |