LRpt/CSVEADiff.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 163 CC 9 SCAR 23.5
CC
9
Cov
0%
load_csvs
30.0
compare
12.0
ea_diff
6.0
CRAP 10.5
1 ##########################################################################
2 #
3 # $Id: CSVEADiff.pm,v 1.5 2006/09/17 19:28:42 pkaluski Exp $
4 # $Name: Stable_0_16 $
5 #
6 # Object of this class compares a set of csv files with expectations.
7 #
8 # $Log: CSVEADiff.pm,v $
9 # Revision 1.5 2006/09/17 19:28:42 pkaluski
10 # Adjusted command line parameters for new key definitions. Adjusted to performance related changes in Collection class
11 #
12 # Revision 1.4 2006/09/10 18:18:17 pkaluski
13 # Converted from dos to Unix format
14 #
15 # Revision 1.3 2006/04/09 15:42:19 pkaluski
16 # Small code clean-up. Each module has comprehensive POD
17 #
18 #
19 ###############################################################
20 package LRpt::CSVEADiff;
21  
22 use strict;
23 use Getopt::Long;
24 use LRpt::RKeysRdr;
25 use XML::Simple;
26 use LRpt::Collection;
27 use LRpt::CollEADiff;
28 use File::Basename;
29  
30 =head1 NAME
31  
32 LRpt::CSVEADiff - A module for comparing a set of csv files with expectations
33  
34 =head1 SYNOPSIS
35  
36 lcsveadiff.pl --keys_file=keys.txt --cmp_rules=cmp_rules.xml
37 --log_file=logfile.txt --expectations=exp.xml actual_dir
38  
39 =head1 DESCRIPTION
40  
41 This module is a part of L<C<LRpt>|LRpt> (B<LReport>) library.
42 It is used to compare set of I<csv> files with expectations and
43 report found differences.
44 You should not use C<LRpt::CSVEADiff> module directly in your code.
45 Instead you should use B<lcsveadiff.pl> tool, which is a simple wrapper
46 around the module. B<lcsveadiff.pl> looks like this:
47  
48 use strict;
49 use LRpt::CSVEADiff;
50
51 ea_diff( @ARGV );
52
53  
54 =head1 COMMAND LINE OPTIONS
55  
56 =over 4
57  
58 =item --key=string
59  
60 Row key defined in command line as one string. Don't use it. I will probably
61 get read of this switch in future
62  
63 =item --keys_file=file
64  
65 Name of the file containing row keys definitions
66  
67 =item --cmp_rules=$cmp_rules
68  
69 Name of a file containing comparing rules.
70  
71 =item --log_file=$log_file
72  
73 Name of a file to which log messages should be written
74  
75 =item --expectations=$e_file
76  
77 Name of a file containing expectations
78  
79 =item --help
80  
81 Prints help screen.
82  
83 =item actual_dir
84  
85 A file name or a directory containing I<actual> state.
86  
87 =back
88  
89 =cut
90  
91 use vars qw( @EXPORT @ISA );
92 @ISA = qw( Exporter );
93 @EXPORT = qw( ea_diff );
94  
95 my $rkeys_rdr = "";
96 my %actual_colls = ();
97 my %expected_colls = ();
98  
99 =head1 METHODS
100  
101 In this sections you will find a more or less complete listing of all
102 methods provided by the package.
103  
104 =cut
105  
106 ############################################################################
107  
108 =head2 C<ea_diff>
109  
110 ea_diff( @ARGV );
111  
112 Main function. @ARGV is processes by standard Getopt::Long module. Meaning
113 of each switch is given in L<SYNOPSIS|"SYNOPSIS">.
114  
115 =cut
116  
117 #############################################################################
118 sub ea_diff
119 {
120 local ( @ARGV ) = @_;
121 my @key = ();
122 my @key_cols = ();
123 my $keys_file = "";
124 my $cmp_rules = "";
125 my $global_keys = "";
126 my $exp_file = "";
127 my $log_file = "";
128 my $help = "";
129 GetOptions( "key=s" => \@key,
130 "key_cols=s" => \@key_cols,
131 "keys_file=s" => \$keys_file,
132 "global_keys" => \$global_keys,
133 "cmp_rules=s" => \$cmp_rules,
134 "log_file=s" => \$log_file,
135 "expectations=s" => \$exp_file,
136 "help" => \$help );
137  
138 if( $help ){
139 print_usage();
140 }
141  
142 $rkeys_rdr = LRpt::RKeysRdr->new( 'fname' => $keys_file,
143 'key' => \@key,
144 'key_cols' => \@key_cols,
145 'global_keys' => $global_keys );
146 load_csvs();
147 load_expectations( $exp_file );
148 compare( $cmp_rules, $log_file );
149 }
150  
151 ############################################################################
152  
153 =head2 C<load_expectations>
154  
155 load_expectations( $exp_file );
156  
157 Loads expecations from the file C<$exp_file>.
158  
159 =cut
160  
161 #############################################################################
162 sub load_expectations
163 {
164 my $exp_file = shift;
165 my $exp = XMLin( $exp_file, 'forcearray' => [ 'row' ],
166 'keyattr' => [] );
167 foreach my $select ( keys %$exp ){
168 my @rows = ();
169 foreach my $row ( @{ $exp->{ $select }->{ 'row' } } ){
170 push( @rows, $row );
171 }
172 my $coll = LRpt::Collection->new_empty_copy(
173 'src_coll' => $actual_colls{ $select },
174 'chunk_size' => 0 );
175 $coll->adopt_rows( \@rows );
176 $expected_colls{ $select } = $coll;
177 }
178 return;
179 }
180  
181 ############################################################################
182  
183 =head2 C<load_csvs>
184  
185 load_csvs();
186  
187 Loads all csvs, which names are given in a command line
188  
189 =cut
190  
191 #############################################################################
192 sub load_csvs
193 {
194 my $file = "";
195 my @files = ();
196 if( !@ARGV ){
197 die "CSV files names not given";
198 }else{
199 $file = shift( @ARGV );
200 if( -d $file ){
201 if( @ARGV ){
202 die "When directory name given, other files " .
203 "must not be specified";
204 }else{
205 my $config = LRpt::Config->new();
206 my $ext = $config->get_value( 'ext' );
207 @files = glob( "$file/*.$ext" );
208 }
209 }else{
210 while( $file = shift( @ARGV ) ){
211 if( -f $file ){
212 die "No such file $file";
213 }
214 push( @files, $file );
215 }
216 }
217 }
218 my @act_colls = ();
219 foreach $file ( @files ){
220 push( @act_colls, load_csv_file( $file ) );
221 }
222 %actual_colls = @act_colls;
223 my $ref = \%actual_colls;
224 return;
225 }
226  
227  
228 ############################################################################
229  
230 =head2 C<load_csv_file>
231  
232 load_csv_file();
233  
234 Loads one csv file.
235  
236 =cut
237  
238 #############################################################################
239 sub load_csv_file
240 {
241 my $file = shift;
242 my $config = LRpt::Config->new();
243 my $ext = $config->get_value( 'ext' );
244 my $name = basename( $file );
245 $name =~ s/\.$ext$//;
246 my $coll = "";
247 my $rkey = "";
248 if( $rkeys_rdr and $rkey = $rkeys_rdr->find_key( $name ) ){
249 $coll = LRpt::Collection->new_from_csv( 'name' => $name,
250 'data_file' => $file,
251 'key' => $rkey,
252 'chunk_size' => 0 );
253 }
254 return ( $name, $coll );
255 }
256  
257 ############################################################################
258  
259 =head2 C<compare>
260  
261 compare( $cmp_rules_file, $log_file );
262  
263 Compare loaded data with expectations.
264  
265 =cut
266  
267 #############################################################################
268 sub compare
269 {
270 my $cmp_rules_file = shift;
271 my $log_file = shift;
272 my $rules = XMLin( $cmp_rules_file );
273 print "<eadiff_report>\n";
274 if( $log_file ){
275 open( LOG_FILE, ">$log_file" ) or
276 die "Cannot open log file $log_file";
277 }
278 my %params = ( 'logger_stream' => *LOG_FILE );
279 foreach my $name ( keys %actual_colls ){
280 $params{ 'expected' } = $expected_colls{ $name };
281 $params{ 'actual' } = $actual_colls{ $name };
282 $params{ 'comparing_rules' } = $rules->{ $name };
283 my $cdiff = LRpt::CollEADiff->new( %params );
284 $cdiff->compare_collections();
285 $cdiff->create_xml_report( 4 );
286 }
287 print "</eadiff_report>\n";
288 if( $log_file ){
289 close( LOG_FILE ) or die "Cannot close log file $log_file";
290 }
291 }
292  
293 ######################################################################
294  
295 =head2 C<print_usage>
296  
297 print_usage();
298  
299 Prints usage text.
300  
301 =cut
302  
303 ######################################################################
304 sub print_usage
305 {
306 print "Usage: $0 --help \n";
307 print " --rkeys=<string> --rkeys_file=<name>" .
308 " --cmp_rules=<name> --log_file=<name> --expectations=<name>" .
309 " --help file_actual\n";
310 print "\n";
311 print " --cmp_rules - Name of a file containing comparing rules\n";
312 print " --expectations - Name of a file containing expectations\n";
313 print " --help - Prints this help screen\n";
314 print " --log_file - Name of a log file\n";
315 print " --rkeys - Command line row key definition. Don't use\n";
316 print " --rkeys_file - Name of the file containing row keys " .
317 "definitions\n";
318 print " file_actual - A file name or a directory containing files\n" .
319 " with actual state.\n";
320 exit( 0 );
321 }
322  
323 1;
324  
325 =head1 SEE ALSO
326  
327 The project is maintained on Source Forge L<http://lreport.sourceforge.net>.
328 You can find there links to some helpful documentation like tutorial.
329  
330 =head1 AUTHORS
331  
332 Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt>
333  
334 =head1 COPYRIGHT
335  
336 Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved.
337  
338 You may distribute under the terms of either the GNU General Public License
339 or the Artistic License, as specified in the Perl README file.
340  
341 =cut
342  
343