| 1 | ############################################################################## | ||
| 2 | # | ||
| 3 | # $Id: CollEADiff.pm,v 1.10 2006/02/10 22:32:16 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # Object of this class is used to compare a set of expected rows with a | ||
| 7 | # set of actual rows from the database. | ||
| 8 | # | ||
| 9 | # $Log: CollEADiff.pm,v $ | ||
| 10 | # Revision 1.10 2006/02/10 22:32:16 pkaluski | ||
| 11 | # Major redesign in progress. Updated POD. Works. | ||
| 12 | # | ||
| 13 | # Revision 1.9 2006/01/14 21:08:25 pkaluski | ||
| 14 | # New design of lreport. | ||
| 15 | # | ||
| 16 | # Revision 1.8 2006/01/14 12:52:33 pkaluski | ||
| 17 | # New tool design in progress | ||
| 18 | # | ||
| 19 | # Revision 1.7 2005/09/02 19:59:31 pkaluski | ||
| 20 | # Next refinement of PODs. Ready for distribution. Some work on clarity still to be done | ||
| 21 | # | ||
| 22 | # Revision 1.6 2005/09/01 20:00:18 pkaluski | ||
| 23 | # Refined PODs. Separation between public and private methods still to be done | ||
| 24 | # | ||
| 25 | # Revision 1.5 2005/01/23 07:25:43 pkaluski | ||
| 26 | # Pod documentation added | ||
| 27 | # | ||
| 28 | # Revision 1.4 2004/10/17 18:22:43 pkaluski | ||
| 29 | # Added test for unkeyed A-E comparison. The test pass. | ||
| 30 | # | ||
| 31 | # Revision 1.3 2004/10/17 08:30:32 pkaluski | ||
| 32 | # Added handling of unkeyed A-E comparison. Old tests still work. New tests to be added | ||
| 33 | # | ||
| 34 | # Revision 1.2 2004/10/15 21:51:10 pkaluski | ||
| 35 | # Added test case for logging when comparing with expecations. Fixed some bugs. | ||
| 36 | # | ||
| 37 | # Revision 1.1.1.1 2004/10/02 11:30:56 pkaluski | ||
| 38 | # Changed the naming convention. All packages start with LRpt | ||
| 39 | # | ||
| 40 | # | ||
| 41 | # | ||
| 42 | ############################################################################# | ||
| 43 | package LRpt::CollEADiff; | ||
| 44 | use strict; | ||
| 45 | use LRpt::CollUnkeyed; | ||
| 46 | use XML::Simple; | ||
| 47 | use LRpt::CollDiff; | ||
| 48 | |||
| 49 | @LRpt::CollEADiff::ISA = qw( LRpt::CollDiff ); | ||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | LRpt::CollEADiff - A module for comparing collection of expectations with | ||
| 54 | a collection of actual data taken from the database. | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | This class is a part of L<C<LRpt>|LRpt> library. | ||
| 59 | Object of this class is used to compare a set of expected rows with a | ||
| 60 | set of actual rows from the database. It subclasses | ||
| 61 | L<C<LRpt::CollDiff>|LRpt::CollDiff>. However, there are some important | ||
| 62 | differences. During that type of comparison only columns which are present | ||
| 63 | in 'expected' collection are compared. If there is a column, which | ||
| 64 | is not present in 'expected' but is present in 'actual', the comparison of | ||
| 65 | a column is skipped. | ||
| 66 | |||
| 67 | Since this class inherits from L<C<LRpt::CollDiff>|LRpt::CollDiff>, | ||
| 68 | in its core it uses the notion of 'before' and 'after' collection. | ||
| 69 | In its interface with the user it uses notions of: | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =item expected collection | ||
| 74 | |||
| 75 | Collection of expected rows. When using L<C<LRpt::CollDiff>|LRpt::CollDiff> | ||
| 76 | functions it is treated as 'before' collection | ||
| 77 | |||
| 78 | =item actual collection | ||
| 79 | |||
| 80 | Collection of actual rows. When using L<C<LRpt::CollDiff>|LRpt::CollDiff> | ||
| 81 | functions it is treated as 'after' collection | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | The class also use a notion of comparing rules. Comparing rules help to | ||
| 86 | specify what to do, when some expectations are not defined. | ||
| 87 | Sometimes a defualt value may be used. Sometimes the comparison should | ||
| 88 | be terminated (if a value of important column is missing). | ||
| 89 | |||
| 90 | =head1 METHODS | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | ############################################################################# | ||
| 95 | |||
| 96 | =head2 C<new> | ||
| 97 | |||
| 98 | my $cdiff = LRpt::CollEADiff->new( expected => $exp_coll, | ||
| 99 | actual => $act_coll, | ||
| 100 | key => $key, | ||
| 101 | comparing_rules_file => $rules_fname, | ||
| 102 | comparing_rules => $rules, | ||
| 103 | logger_stream => *STREAM ); | ||
| 104 | |||
| 105 | Constructor. Initializes internal structures. Meaning of parameters: | ||
| 106 | |||
| 107 | =over 4 | ||
| 108 | |||
| 109 | =item C<expected> | ||
| 110 | |||
| 111 | Reference to a L<C<LRpt::Collection>|LRpt::Collection> object, | ||
| 112 | which contain expectations for rows. | ||
| 113 | |||
| 114 | =item C<actual> | ||
| 115 | |||
| 116 | Reference to a L<C<LRpt::Collection>|LRpt::Collection> object, | ||
| 117 | which contain actual rows. | ||
| 118 | |||
| 119 | =item C<key> | ||
| 120 | |||
| 121 | Name of a L<C<LRpt::Collection>|LRpt::Collection> key used for rows ordering | ||
| 122 | |||
| 123 | =item C<comparing_rules_file> | ||
| 124 | |||
| 125 | Name of the file containing comparing rules. Does not have to be specified | ||
| 126 | if C<comparing_rules parameter> is given. | ||
| 127 | |||
| 128 | =item C<comparing_rules> | ||
| 129 | |||
| 130 | Reference to a structure containing comparing rules. Does not have to | ||
| 131 | be specified if C<comparing_rules_file> parameter is given. | ||
| 132 | |||
| 133 | =item C<logger_stream> [optional] | ||
| 134 | |||
| 135 | Reference to an output logger stream | ||
| 136 | |||
| 137 | =back | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | ############################################################################ | ||
| 142 | sub new | ||
| 143 | { | ||
| 144 | my $proto = shift; | ||
| 145 | my %params = @_; | ||
| 146 | |||
| 147 | my $class = ref( $proto ) || $proto; | ||
| 148 | |||
| 149 | my $self = {}; | ||
| 150 | bless( $self, $class ); | ||
| 151 | if( !$params{ 'expected' } ){ | ||
| 152 | die "No expected collection given"; | ||
| 153 | }else{ | ||
| 154 | $self->{ 'before' } = $params{ 'expected' }; | ||
| 155 | } | ||
| 156 | if( !$params{ 'actual' } ){ | ||
| 157 | die "No actual collection given"; | ||
| 158 | }else{ | ||
| 159 | $self->{ 'after' } = $params{ 'actual' }; | ||
| 160 | } | ||
| 161 | |||
| 162 | $self->init_rules( %params ); | ||
| 163 | |||
| 164 | if( $params{ 'key' } ){ | ||
| 165 | $self->{ 'key' } = $params{ 'key' }; | ||
| 166 | } | ||
| 167 | if( $params{ 'logger_stream' } ){ | ||
| 168 | $self->{ 'logger_stream' } = $params{ 'logger_stream' }; | ||
| 169 | } | ||
| 170 | return $self; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | ######################################################################### | ||
| 175 | |||
| 176 | =head2 C<init_rules> | ||
| 177 | |||
| 178 | $ceadiff->init_rules( comparing_rules => $cmp_rules, | ||
| 179 | comparing_rules_file => $cmp_rules_file ); | ||
| 180 | |||
| 181 | Initializes comparing rules structure. If C<comparing_rules> parameter is | ||
| 182 | given, C<$cmp_rules> should point to a compring rules structure. If | ||
| 183 | C<comparing_rules_file> is given, rules are loaded from the file | ||
| 184 | C<$cmp_rules_file> | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | ######################################################################### | ||
| 189 | sub init_rules | ||
| 190 | { | ||
| 191 | my $self = shift; | ||
| 192 | my %params = @_; | ||
| 193 | |||
| 194 | my $cmp_rules = ""; | ||
| 195 | if( $params{ "comparing_rules_file" } ){ | ||
| 196 | $cmp_rules = XMLin( $params{ 'comparing_rules_file' } ); | ||
| 197 | } | ||
| 198 | if( $params{ "comparing_rules" } ){ | ||
| 199 | if( $cmp_rules ){ | ||
| 200 | die "Cannot use comparing_rules and comparing_rules_file " . | ||
| 201 | "parameters at the same time"; | ||
| 202 | }else{ | ||
| 203 | $cmp_rules = $params{ "comparing_rules" }; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | if( !$cmp_rules ){ | ||
| 207 | die "No comparing rules specified for expected_actual comparing ". | ||
| 208 | "mode"; | ||
| 209 | } | ||
| 210 | $self->{ 'comparing_rules' } = $cmp_rules; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | ######################################################################### | ||
| 215 | |||
| 216 | =head2 C<compare_rows> | ||
| 217 | |||
| 218 | $ceadiff->compare_rows( $key_name ); | ||
| 219 | |||
| 220 | Compare expected and actual rows, which have the same key value given in | ||
| 221 | C<$key_value>. If a difference is found for any column, 'not_equal' type of | ||
| 222 | difference is reported. | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | ######################################################################### | ||
| 227 | sub compare_rows | ||
| 228 | { | ||
| 229 | my $self = shift; | ||
| 230 | my $key = shift; | ||
| 231 | my $expected_row = $self->get_expected_row( $key ); | ||
| 232 | my $actual_row = $self->get_actual_row( $key ); | ||
| 233 | |||
| 234 | foreach my $col ( keys %{ $expected_row } ){ | ||
| 235 | $expected_row->{ $col } =~ s/\s+$//; | ||
| 236 | $actual_row->{ $col } =~ s/\s+$//; | ||
| 237 | if( $expected_row->{ $col } ne $actual_row->{ $col } ){ | ||
| 238 | $self->add_diff_not_equal( $key, | ||
| 239 | 'column' => $col, | ||
| 240 | 'before' => $expected_row->{ $col }, | ||
| 241 | 'after' => $actual_row->{ $col } ); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | ######################################################################### | ||
| 248 | |||
| 249 | =head2 C<compare_collections> | ||
| 250 | |||
| 251 | $ceadiff->compare_collections(); | ||
| 252 | |||
| 253 | Main public function. Runs the whole comparing machinery. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | ######################################################################### | ||
| 258 | sub compare_collections | ||
| 259 | { | ||
| 260 | my $self = shift; | ||
| 261 | $self->set_expectations(); | ||
| 262 | $self->SUPER::compare_collections( 'nothing' ); | ||
| 263 | $self->compare_with_unkeyed_rows(); | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | ######################################################################### | ||
| 268 | |||
| 269 | =head2 C<compare_with_unkeyed_rows> | ||
| 270 | |||
| 271 | $ceadiff->compare_with_unkeyed_rows(); | ||
| 272 | |||
| 273 | Called when keyed comparison is done and there are still some actual rows | ||
| 274 | left. Compares those actual rows with expectation rows, for which key is | ||
| 275 | not defined. | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | ############################################################################# | ||
| 280 | sub compare_with_unkeyed_rows | ||
| 281 | { | ||
| 282 | my $self = shift; | ||
| 283 | my @additional_rows = $self->get_additional_rows(); | ||
| 284 | my $unkeyed_coll = $self->get_unkeyed_coll(); | ||
| 285 | |||
| 286 | if( $unkeyed_coll->is_empty() ){ | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | |||
| 290 | foreach my $add_row ( @additional_rows ){ | ||
| 291 | if( $unkeyed_coll->find_matching_row( $add_row ) ){ | ||
| 292 | $self->remove_diff_additional( $add_row ); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | # | ||
| 296 | # If any unkeyed row remained, it should be reported as missing | ||
| 297 | # expectation | ||
| 298 | # | ||
| 299 | my @remaining_rows = $unkeyed_coll->get_remaining_rows(); | ||
| 300 | $self->{ 'differences_uk' } = []; | ||
| 301 | foreach my $r_row ( @remaining_rows ){ | ||
| 302 | $self->add_diff_missing_unkeyed( $r_row ); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | ######################################################################### | ||
| 308 | |||
| 309 | =head2 C<get_unkeyed_coll> | ||
| 310 | |||
| 311 | my @unk_colls = $ceadiff->get_unkeyed_coll(); | ||
| 312 | |||
| 313 | Returns collection of expectation rows, for which not have whole key | ||
| 314 | populated | ||
| 315 | |||
| 316 | =cut | ||
| 317 | |||
| 318 | ######################################################################### | ||
| 319 | sub get_unkeyed_coll | ||
| 320 | { | ||
| 321 | my $self = shift; | ||
| 322 | |||
| 323 | return $self->{ 'before' }->get_unkeyed_coll(); | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | ######################################################################### | ||
| 328 | |||
| 329 | =head2 C<get_diffs_missing_unkeyed> | ||
| 330 | |||
| 331 | my @rows_info = $ceadiff->get_diffs_missing_unkeyed(); | ||
| 332 | |||
| 333 | Returns information about all expected, 'unkeyed' rows for which there is | ||
| 334 | no actual rows. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | ######################################################################### | ||
| 339 | sub get_diffs_missing_unkeyed | ||
| 340 | { | ||
| 341 | my $self = shift; | ||
| 342 | my @rows_info = (); | ||
| 343 | if( not exists $self->{ 'differences_uk' } ){ | ||
| 344 | return (); | ||
| 345 | } | ||
| 346 | else{ | ||
| 347 | foreach my $diff ( @{ $self->{ 'differences_uk' } } ){ | ||
| 348 | if( $diff->{ 'diff_type' } eq "missing_unkeyed" ){ | ||
| 349 | push( @rows_info, $diff->{ 'rows' } ); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | return @rows_info; | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | ######################################################################### | ||
| 358 | |||
| 359 | =head2 C<add_diff_missing_unkeyed> | ||
| 360 | |||
| 361 | $ceadiff->add_diff_missing_unkeyed( $row_info ); | ||
| 362 | |||
| 363 | Stores information that there is no match for an 'unkeyed' expected row. | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | ########################################################################## | ||
| 368 | sub add_diff_missing_unkeyed | ||
| 369 | { | ||
| 370 | my $self = shift; | ||
| 371 | my $row_info = shift; | ||
| 372 | |||
| 373 | push( @{ $self->{ 'differences_uk' } }, | ||
| 374 | { 'diff_type' => "missing_unkeyed", | ||
| 375 | 'rows' => $row_info } ); | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | ######################################################################### | ||
| 380 | |||
| 381 | =head2 C<set_expectations> | ||
| 382 | |||
| 383 | $ceadiff->set_expecations(); | ||
| 384 | |||
| 385 | Using comparing rules and a set of expectations, creates a set of row, which | ||
| 386 | will be used in further processing as a expected collection | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | ######################################################################### | ||
| 391 | sub set_expectations | ||
| 392 | { | ||
| 393 | my $self = shift; | ||
| 394 | my $exp_coll = $self->{ 'before' }; | ||
| 395 | my $all_rows = $exp_coll->get_keyed_rows(); | ||
| 396 | |||
| 397 | foreach my $key ( keys %{ $all_rows } ){ | ||
| 398 | my $row = $all_rows->{ $key }; | ||
| 399 | my $pop_row = $self->create_exp_row( $row ); | ||
| 400 | $all_rows->{ $key } = $pop_row; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | |||
| 405 | ######################################################################### | ||
| 406 | |||
| 407 | =head2 C<create_exp_row> | ||
| 408 | |||
| 409 | my $exp_row = $ceadiff->create_exp_row( $existing_values ); | ||
| 410 | |||
| 411 | Basing on comparison rules and given expected values, | ||
| 412 | creates a row of expected values. | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | ######################################################################### | ||
| 417 | sub create_exp_row | ||
| 418 | { | ||
| 419 | my $self = shift; | ||
| 420 | my $existing_values = shift; | ||
| 421 | my $col = ""; | ||
| 422 | my $exp_row = {}; | ||
| 423 | |||
| 424 | my %warnings = (); | ||
| 425 | |||
| 426 | my $cmp_rls = $self->{ 'comparing_rules' }; | ||
| 427 | # | ||
| 428 | # You should iterate through the sum of columns present in the row and | ||
| 429 | # in the comparing rules file. | ||
| 430 | # | ||
| 431 | my @all_cols = ( keys %$cmp_rls , keys %$existing_values ); | ||
| 432 | foreach my $col ( @all_cols ){ | ||
| 433 | if( !$existing_values->{ $col } ){ | ||
| 434 | # If there is no rule for a column... | ||
| 435 | if( !( keys %{ $cmp_rls->{ $col } } ) ){ | ||
| 436 | next; | ||
| 437 | } | ||
| 438 | elsif( $cmp_rls->{ $col }->{ 'if_no_value' } eq "skip_and_warn" ) | ||
| 439 | { | ||
| 440 | $warnings{ $col } = { warning => "skip_and_warn" }; | ||
| 441 | $self->log_msg( | ||
| 442 | "No expected value specified for field '$col' ". | ||
| 443 | $self->descr_row_location( $existing_values ) ); | ||
| 444 | } | ||
| 445 | elsif( $cmp_rls->{ $col }->{ 'if_no_value' } eq "die" ) | ||
| 446 | { | ||
| 447 | $self->log_msg( | ||
| 448 | "No expected value specified for field '$col' " . | ||
| 449 | $self->descr_row_location( $existing_values ) . | ||
| 450 | ". The field is too important. Terminating" ); | ||
| 451 | die "No expected value for $col"; | ||
| 452 | } | ||
| 453 | elsif( $cmp_rls->{ $col }->{ 'if_no_value' } eq "use_default" ) | ||
| 454 | { | ||
| 455 | $exp_row->{ $col } = $cmp_rls->{ $col }->{ 'default' }; | ||
| 456 | } | ||
| 457 | elsif( $cmp_rls->{ $col }->{ 'if_no_value' } | ||
| 458 | eq "use_default_and_warn" ) | ||
| 459 | { | ||
| 460 | $exp_row->{ $col } = $cmp_rls->{ $col }->{ 'default' }; | ||
| 461 | $warnings{ $col } = | ||
| 462 | { warning => "use_default_and_warn", | ||
| 463 | value => $cmp_rls->{ $col }->{ default } }; | ||
| 464 | |||
| 465 | $self->log_msg( | ||
| 466 | "No expected value specified for field '$col' " . | ||
| 467 | $self->descr_row_location( $existing_values ) . | ||
| 468 | ". Using default value : '" . | ||
| 469 | $cmp_rls->{ $col }->{ 'default' } . "'" ); | ||
| 470 | } | ||
| 471 | elsif( $cmp_rls->{ $col }->{ 'if_no_value' } eq "skip" ) | ||
| 472 | { | ||
| 473 | next; | ||
| 474 | }else{ | ||
| 475 | die "Unknown type of if_no_value action : ". | ||
| 476 | $cmp_rls->{ $col }->{ 'if_no_value' }; | ||
| 477 | $self->log_msg( "Unknown type of if_no_value action : ". | ||
| 478 | $cmp_rls->{ $col }->{ 'if_no_value' } ); | ||
| 479 | } | ||
| 480 | }else{ | ||
| 481 | $exp_row->{ $col } = $existing_values->{ $col }; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | return $exp_row; | ||
| 485 | } | ||
| 486 | |||
| 487 | |||
| 488 | ######################################################################### | ||
| 489 | |||
| 490 | =head2 C<descr_row_location> | ||
| 491 | |||
| 492 | my $loc_str = $ceadiff->descr_row_location( $row ); | ||
| 493 | |||
| 494 | Creates a string describing a row - collection name and row's key value. | ||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | ######################################################################### | ||
| 499 | sub descr_row_location | ||
| 500 | { | ||
| 501 | my $self = shift; | ||
| 502 | my $row = shift; | ||
| 503 | return "in a row with a key value '". | ||
| 504 | $self->{ 'after' }->get_key_value( $row ) . "' " . | ||
| 505 | "from collection '" . | ||
| 506 | $self->{ 'after' }->get_name() . "'"; | ||
| 507 | } | ||
| 508 | |||
| 509 | |||
| 510 | ######################################################################### | ||
| 511 | |||
| 512 | =head2 C<get_expected_row> | ||
| 513 | |||
| 514 | my $row = $ceadiff->get_expected_row( $key_value ); | ||
| 515 | |||
| 516 | Returns expected row with a given key value. | ||
| 517 | |||
| 518 | =cut | ||
| 519 | |||
| 520 | ######################################################################### | ||
| 521 | sub get_expected_row | ||
| 522 | { | ||
| 523 | my $self = shift; | ||
| 524 | my $key = shift; | ||
| 525 | return $self->get_before_row( $key ); | ||
| 526 | |||
| 527 | } | ||
| 528 | |||
| 529 | |||
| 530 | ######################################################################### | ||
| 531 | |||
| 532 | =head2 C<get_actual_row> | ||
| 533 | |||
| 534 | my $row = $ceadiff->get_actual_row( $key_value ); | ||
| 535 | |||
| 536 | Returns actual row with a given key value. | ||
| 537 | |||
| 538 | =cut | ||
| 539 | |||
| 540 | ######################################################################### | ||
| 541 | sub get_actual_row | ||
| 542 | { | ||
| 543 | my $self = shift; | ||
| 544 | my $key = shift; | ||
| 545 | return $self->get_after_row( $key ); | ||
| 546 | } | ||
| 547 | |||
| 548 | |||
| 549 | ######################################################################### | ||
| 550 | |||
| 551 | =head2 C<set_log_stream> | ||
| 552 | |||
| 553 | $ceadiff->set_log_stream( $fh ); | ||
| 554 | |||
| 555 | Sets a logging stream. | ||
| 556 | |||
| 557 | =cut | ||
| 558 | |||
| 559 | ######################################################################### | ||
| 560 | sub set_log_stream | ||
| 561 | { | ||
| 562 | my $self = shift; | ||
| 563 | my $fh = shift; | ||
| 564 | $self->{ 'logger_stream' } = $fh; | ||
| 565 | } | ||
| 566 | |||
| 567 | |||
| 568 | ######################################################################### | ||
| 569 | |||
| 570 | =head2 C<log_msg> | ||
| 571 | |||
| 572 | $ceadiff->log_msg( $message ); | ||
| 573 | |||
| 574 | Puts a message in the log file. | ||
| 575 | |||
| 576 | =cut | ||
| 577 | |||
| 578 | ######################################################################### | ||
| 579 | sub log_msg | ||
| 580 | { | ||
| 581 | my $self = shift; | ||
| 582 | my $message = shift; | ||
| 583 | if( ! $self->{ 'logger_stream' } ){ | ||
| 584 | return; | ||
| 585 | } | ||
| 586 | my $fh = $self->{ 'logger_stream' }; | ||
| 587 | print $fh "" . localtime() . "--> $message\n"; | ||
| 588 | } | ||
| 589 | |||
| 590 | ######################################################################### | ||
| 591 | |||
| 592 | =head2 C<create_xml_report> | ||
| 593 | |||
| 594 | $ceadiff->create_xml_report( $indent ); | ||
| 595 | |||
| 596 | Creates a report of comparison in XML format. I<$indent> is a number | ||
| 597 | of indentation spaces. | ||
| 598 | |||
| 599 | =cut | ||
| 600 | |||
| 601 | ######################################################################### | ||
| 602 | sub create_xml_report | ||
| 603 | { | ||
| 604 | my $self = shift; | ||
| 605 | my $indent = shift; | ||
| 606 | print " " x $indent . "<" . $self->{ 'after' }->get_name() . ">\n"; | ||
| 607 | $self->missing_to_xml( $indent + 4 ); | ||
| 608 | $self->additional_to_xml( $indent + 4 ); | ||
| 609 | $self->not_equal_to_xml( $indent + 4 ); | ||
| 610 | $self->unmatched_to_xml( $indent + 4 ); | ||
| 611 | print " " x $indent . "</" . $self->{ 'after' }->get_name() . ">\n"; | ||
| 612 | } | ||
| 613 | |||
| 614 | ######################################################################### | ||
| 615 | |||
| 616 | =head2 C<missing_to_xml> | ||
| 617 | |||
| 618 | $ceadiff->missing_to_xml( $indent ); | ||
| 619 | |||
| 620 | converts to XML format report on missing rows. I<$indent> is a number | ||
| 621 | of indentation spaces. | ||
| 622 | |||
| 623 | =cut | ||
| 624 | |||
| 625 | ######################################################################### | ||
| 626 | sub missing_to_xml | ||
| 627 | { | ||
| 628 | my $self = shift; | ||
| 629 | my $indent = shift; | ||
| 630 | my %miss_keys = (); | ||
| 631 | @miss_keys{ $self->get_missing_keys() } = 1; | ||
| 632 | my @keys = sort keys %miss_keys; | ||
| 633 | if( @keys ){ | ||
| 634 | print " " x $indent . "<missing>\n"; | ||
| 635 | my $cols = $self->{ 'before' }->get_columns(); | ||
| 636 | foreach my $key( @keys ){ | ||
| 637 | print " " x ( $indent + 4 ) . "<row>\n"; | ||
| 638 | $self->xmlize_row( $self->get_before_row( $key ), | ||
| 639 | $cols, | ||
| 640 | $indent + 8 ); | ||
| 641 | print " " x ( $indent + 4 ) . "</row>\n"; | ||
| 642 | } | ||
| 643 | print " " x $indent . "</missing>\n"; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | ######################################################################### | ||
| 648 | |||
| 649 | =head2 C<additional_to_xml> | ||
| 650 | |||
| 651 | $ceadiff->additional_to_xml( $indent ); | ||
| 652 | |||
| 653 | converts to XML format report on additional rows. I<$indent> is a number | ||
| 654 | of indentation spaces. | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | ######################################################################### | ||
| 659 | sub additional_to_xml | ||
| 660 | { | ||
| 661 | my $self = shift; | ||
| 662 | my $indent = shift; | ||
| 663 | my %add_keys = (); | ||
| 664 | @add_keys{ $self->get_additional_keys() } = 1; | ||
| 665 | my @keys = sort keys %add_keys; | ||
| 666 | if( @keys ){ | ||
| 667 | print " " x $indent . "<additional>\n"; | ||
| 668 | my $cols = $self->{ 'after' }->get_columns(); | ||
| 669 | foreach my $key( @keys ){ | ||
| 670 | print " " x ( $indent + 4 ) . "<row>\n"; | ||
| 671 | $self->xmlize_row( $self->get_after_row( $key ), | ||
| 672 | $cols, | ||
| 673 | $indent + 8 ); | ||
| 674 | print " " x ( $indent + 4 ) . "</row>\n"; | ||
| 675 | } | ||
| 676 | print " " x $indent . "</additional>\n"; | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | ######################################################################### | ||
| 681 | |||
| 682 | =head2 C<not_equal_to_xml> | ||
| 683 | |||
| 684 | $ceadiff->not_equal_to_xml( $indent ); | ||
| 685 | |||
| 686 | converts to XML format report on not equal rows. I<$indent> is a number | ||
| 687 | of indentation spaces. | ||
| 688 | |||
| 689 | =cut | ||
| 690 | |||
| 691 | ######################################################################### | ||
| 692 | sub not_equal_to_xml | ||
| 693 | { | ||
| 694 | my $self = shift; | ||
| 695 | my $indent = shift; | ||
| 696 | my %ne_keys = (); | ||
| 697 | @ne_keys{ $self->get_not_equal_keys() } = 1; | ||
| 698 | my @keys = sort keys %ne_keys; | ||
| 699 | if( @keys ){ | ||
| 700 | print " " x $indent . "<not_equal>\n"; | ||
| 701 | my $cols = $self->{ 'after' }->get_columns(); | ||
| 702 | my @key_cols = $self->{ 'after' }->get_key_columns(); | ||
| 703 | print " " x ( $indent + 4 ) . "<key>\n"; | ||
| 704 | foreach my $key ( @key_cols ){ | ||
| 705 | print " " x ( $indent + 8 ) . "<$key/>\n"; | ||
| 706 | } | ||
| 707 | print " " x ( $indent + 4 ) . "</key>\n"; | ||
| 708 | foreach my $key( @keys ){ | ||
| 709 | print " " x ( $indent + 4 ) . "<row>\n"; | ||
| 710 | my @diff_cols = $self->get_diff_columns( $key ); | ||
| 711 | $self->xmlize_row_pair( $self->get_expected_row( $key ), | ||
| 712 | $self->get_actual_row( $key ), | ||
| 713 | $cols, | ||
| 714 | \@diff_cols, | ||
| 715 | $indent + 8 ); | ||
| 716 | print " " x ( $indent + 4 ) . "</row>\n"; | ||
| 717 | } | ||
| 718 | print " " x $indent . "</not_equal>\n"; | ||
| 719 | } | ||
| 720 | } | ||
| 721 | |||
| 722 | ######################################################################### | ||
| 723 | |||
| 724 | =head2 C<xmlize_row> | ||
| 725 | |||
| 726 | $ceadiff->xmlize_row( $row, $cols, $indent ); | ||
| 727 | |||
| 728 | Converts to XML format contents of a given row. I<$indent> is a number | ||
| 729 | of indentation spaces. | ||
| 730 | |||
| 731 | =cut | ||
| 732 | |||
| 733 | ######################################################################### | ||
| 734 | sub xmlize_row | ||
| 735 | { | ||
| 736 | my $self = shift; | ||
| 737 | my $row = shift; | ||
| 738 | my $cols = shift; | ||
| 739 | my $indent = shift; | ||
| 740 | |||
| 741 | foreach my $col ( @$cols ){ | ||
| 742 | if( exists $row->{ $col } ){ | ||
| 743 | print " " x $indent . "<$col>" . $row->{ $col } . "</$col>\n"; | ||
| 744 | } | ||
| 745 | } | ||
| 746 | } | ||
| 747 | |||
| 748 | ######################################################################### | ||
| 749 | |||
| 750 | =head2 C<xmlize_row_pair> | ||
| 751 | |||
| 752 | $ceadiff->xmlize_row_pair( $exp_row, $act_row, $cols, $diff_cols, $indent ); | ||
| 753 | |||
| 754 | Converts to XML format contents of expected-actual rows. I<$indent> is a number | ||
| 755 | of indentation spaces. | ||
| 756 | |||
| 757 | =cut | ||
| 758 | |||
| 759 | ######################################################################### | ||
| 760 | sub xmlize_row_pair | ||
| 761 | { | ||
| 762 | my $self = shift; | ||
| 763 | my $exp_row = shift; | ||
| 764 | my $act_row = shift; | ||
| 765 | my $cols = shift; | ||
| 766 | my $diff_cols = shift; | ||
| 767 | my $indent = shift; | ||
| 768 | |||
| 769 | my %diffs = (); | ||
| 770 | @diffs{ @$diff_cols } = 1; | ||
| 771 | |||
| 772 | foreach my $col ( @$cols ){ | ||
| 773 | if( exists $exp_row->{ $col } ){ | ||
| 774 | print " " x $indent . "<$col>"; | ||
| 775 | if( exists $diffs{ $col } ){ | ||
| 776 | print "\n" . " " x ( $indent + 4 ) . | ||
| 777 | "<expected>" . $exp_row->{ $col } . | ||
| 778 | "</expected>\n"; | ||
| 779 | print " " x ( $indent + 4 ) . | ||
| 780 | "<actual>" . $act_row->{ $col } . | ||
| 781 | "</actual>\n"; | ||
| 782 | print " " x $indent . "<$col>\n"; | ||
| 783 | }else{ | ||
| 784 | print "" . $exp_row->{ $col } . "</$col>\n"; | ||
| 785 | } | ||
| 786 | } | ||
| 787 | } | ||
| 788 | } | ||
| 789 | |||
| 790 | ######################################################################### | ||
| 791 | |||
| 792 | =head2 C<unmatched_to_xml> | ||
| 793 | |||
| 794 | $ceadiff->unmatched_to_xml( $indent ); | ||
| 795 | |||
| 796 | Converts to XML format contents of unmatched rows. I<$indent> is a number | ||
| 797 | of indentation spaces. | ||
| 798 | |||
| 799 | =cut | ||
| 800 | |||
| 801 | ######################################################################### | ||
| 802 | sub unmatched_to_xml | ||
| 803 | { | ||
| 804 | my $self = shift; | ||
| 805 | my $indent = shift; | ||
| 806 | my @rows_info = $self->get_diffs_missing_unkeyed(); | ||
| 807 | if( @rows_info ){ | ||
| 808 | print " " x $indent . "<unmatched>\n"; | ||
| 809 | foreach my $row_info ( @rows_info ){ | ||
| 810 | if( $row_info->{ 'quantity' } == 0 ){ | ||
| 811 | next; | ||
| 812 | } | ||
| 813 | my %unm_cols = (); | ||
| 814 | @unm_cols{ @{ $row_info->{ 'unm_cols' } } } = 1; | ||
| 815 | print " " x ( $indent + 4 ) . "<row count=\"" . | ||
| 816 | $row_info->{ 'quantity' } . "\">\n"; | ||
| 817 | my $row = $row_info->{ 'row' }; | ||
| 818 | my @unord_cols = keys %$row; | ||
| 819 | my @cols = @{ $self->{ 'after' }->order_columns( \@unord_cols ) }; | ||
| 820 | foreach my $col ( @cols ){ | ||
| 821 | print " " x ( $indent + 8 ) . "<$col>"; | ||
| 822 | if( exists $unm_cols{ $col } ){ | ||
| 823 | print "\n" . " " x ( $indent + 12 ) . "<unmatched>" . | ||
| 824 | $row->{ $col } . "</unmatched>\n"; | ||
| 825 | print " " x ( $indent + 8 ) . "</$col>\n"; | ||
| 826 | }else{ | ||
| 827 | print "" . $row->{ $col } . "</$col>\n"; | ||
| 828 | } | ||
| 829 | } | ||
| 830 | print " " x ( $indent + 4 ) . "</row>\n"; | ||
| 831 | } | ||
| 832 | print " " x $indent . "</unmatched>\n"; | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | |||
| 837 | =head1 SEE ALSO | ||
| 838 | |||
| 839 | The project is maintained on Source Forge L<http://lreport.sourceforge.net>. | ||
| 840 | You can find there links to some helpful documentation like tutorial. | ||
| 841 | |||
| 842 | =head1 AUTHORS | ||
| 843 | |||
| 844 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 845 | |||
| 846 | =head1 COPYRIGHT | ||
| 847 | |||
| 848 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 849 | |||
| 850 | You may distribute under the terms of either the GNU General Public License | ||
| 851 | or the Artistic License, as specified in the Perl README file. | ||
| 852 | |||
| 853 | =cut | ||
| 854 | |||
| 855 | 1; |