LRpt/CollUnkeyed.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 15 SCAR 27.6
CC
15
Cov
0%
is_matching
30.0
is_equal
20.0
find_matching_row
20.0
CRAP 15.7
1 #####################################################################
2 #
3 # $Id: CollUnkeyed.pm,v 1.8 2006/02/10 22:32:16 pkaluski Exp $
4 # $Name: Stable_0_16 $
5 #
6 # Collection of unkeyed rows. Unkeyed means that there is no key specified
7 # for these rows. This collection is used for Expected-Actual comparison.
8 # Sometimes the key of the row is not known or diffucult to predict (if
9 # this is a unique ID automaticaly generated by the server).
10 #
11 # $Log: CollUnkeyed.pm,v $
12 # Revision 1.8 2006/02/10 22:32:16 pkaluski
13 # Major redesign in progress. Updated POD. Works.
14 #
15 # Revision 1.7 2005/09/02 19:59:31 pkaluski
16 # Next refinement of PODs. Ready for distribution. Some work on clarity still to be done
17 #
18 # Revision 1.6 2005/09/01 20:00:18 pkaluski
19 # Refined PODs. Separation between public and private methods still to be done
20 #
21 # Revision 1.5 2005/01/24 21:22:51 pkaluski
22 # Added pod documentation
23 #
24 # Revision 1.4 2005/01/22 06:17:43 pkaluski
25 # Improved column coloring and underlining in differences reports
26 #
27 # Revision 1.3 2005/01/18 20:46:50 pkaluski
28 # Fixed Bug 1095692 - Incorrect comparison of numerical fields
29 #
30 # Revision 1.2 2004/12/21 21:21:29 pkaluski
31 # Columns ordering introduced for reporting rows different from expected
32 #
33 # Revision 1.1 2004/10/17 08:29:03 pkaluski
34 # Initial revision
35 #
36 #
37 ####################################################################
38 package LRpt::CollUnkeyed;
39 use strict;
40  
41 =head1 NAME
42  
43 LRpt::CollUnkeyed - Object of this class represents a collection of unkeyed
44 rows
45  
46 =head1 DESCRIPTION
47  
48 This class is a part of L<C<LRpt>|LRpt> library.
49 Object of this class represents a collection of unkeyed rows. Unkeyed
50 means that there is no key specified for these rows. This collection
51 is used for Expected-Actual comparison. Sometimes a key of the exected row is
52 not known or diffucult to predict (if this is a unique ID automaticaly
53 generated by the server).
54  
55 =head1 METHODS
56  
57 =cut
58  
59 ############################################################################
60  
61 =head2 C<new>
62  
63 my $uk_coll = LRpt::CollUnkeyed->new();
64  
65 Constructor. Initializes fields.
66  
67 =cut
68  
69 ############################################################################
70 sub new
71 {
72 my $proto = shift;
73 my $class = ref( $proto ) || $proto;
74 my $self = {};
75 bless( $self, $class );
76 $self->{ 'groups ' } = {};
77 return $self;
78 }
79  
80  
81 #########################################################################
82  
83 =head2 C<add_row>
84  
85 $uk_coll->add_row( $row );
86  
87 Adds a row to a group. A group contains all rows, which have have same
88 amount of fields defined. Each group is devided on groups of rows having
89 the same values of all defined columns.
90  
91 =cut
92  
93 #########################################################################
94 sub add_row
95 {
96 my $self = shift;
97 my $row = shift;
98  
99 my $nbr_defined = keys %$row;
100 if( not exists $self->{ 'groups' }->{ $nbr_defined } ){
101 $self->{ 'groups' }->{ $nbr_defined } = [ { 'row' => $row,
102 'quantity' => 1 } ];
103 }else{
104 $self->add_to_group( $row ) ;
105 }
106 }
107  
108  
109 #########################################################################
110  
111 =head2 C<add_to_group>
112  
113 $uk_coll->add_to_group( $row );
114  
115 It increases the number of equal rows (the same amount of fields, all
116 fields have the same values).
117  
118 =cut
119  
120 #########################################################################
121 sub add_to_group
122 {
123 my $self = shift;
124 my $row = shift;
125  
126 my $nbr_defined = scalar( keys %$row );
127 my $group = $self->{ 'groups' }->{ $nbr_defined };
128 foreach my $gr_row ( @$group ){
129 if( $self->is_equal( $gr_row->{ 'row' }, $row ) ){
130 $gr_row->{ 'quantity' }++;
131 return;
132 }
133 }
134 push( @$group, { 'row' => $row, 'quantity' => 1 } );
135 }
136  
137  
138 #########################################################################
139  
140 =head2 C<is_equal>
141  
142 my $res = $uk_coll->is_equal( $row1, $row2 );
143  
144 Checks that C<$row1> and C<$row2> are totally equal -
145 the same amount of fields,
146 all fields have the same values.
147  
148 =cut
149  
150 ################################################################
151 sub is_equal
152 {
153 my $self = shift;
154 my $row1 = shift;
155 my $row2 = shift;
156  
157 my @all_cols = ( keys %$row1 );
158  
159 foreach my $col ( @all_cols ){
160 if( ! exists $row1->{ $col } ){
161 return 0;
162 }
163 if( ! exists $row2->{ $col } ){
164 return 0;
165 }
166 if( $row1->{ $col } ne $row2->{ $col } ){
167 return 0;
168 }
169 }
170 return 1;
171 }
172  
173  
174 #########################################################################
175  
176 =head2 C<is_matching>
177  
178 my $res = $uk_coll->is_matching( $row1, $row2 );
179  
180 Checks if a given row matches a row in collection. By matching, I mean
181 that the row matches all fields from the row in the collection.
182  
183 =cut
184  
185 ########################################################################
186 sub is_matching
187 {
188 my $self = shift;
189 my $row1 = shift;
190 my $row2 = shift;
191 my @unm_cols = ();
192  
193 foreach my $col ( keys %$row1 ){
194 if( $row1->{ $col } =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ and
195 $row2->{ $col } =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
196 {
197 if( $row1->{ $col } != $row2->{ $col } ){
198 push( @unm_cols, $col );
199 }
200 }else{
201 if( $row1->{ $col } ne $row2->{ $col } ){
202 push( @unm_cols, $col );
203 }
204 }
205 }
206 if( @unm_cols ){
207 return ( 0, @unm_cols );
208 }else{
209 return 1;
210 }
211 }
212  
213  
214 #########################################################################
215  
216 =head2 C<find_matching_row>
217  
218 my $res = $uk_coll->find_matching_row( $row );
219  
220 Finds a row in a collection, which matches a given row. It tries to find a
221 matching row starting from the row, which has the biggest number of
222 fields defined.
223  
224 =cut
225  
226 ###############################################################################
227 sub find_matching_row
228 {
229 my $self = shift;
230 my $row = shift;
231
232 my @ordered_groups =
233 reverse sort { $a <=> $b } keys %{ $self->{ 'groups' } };
234  
235 #
236 # Start with a row, which has the biggest number of columns defined
237 #
238 foreach my $n_col_def ( @ordered_groups ){
239 my $group = $self->{ 'groups' }->{ $n_col_def };
240 foreach my $gr_row ( @$group ){
241 if( $gr_row->{ 'quantity' } == 0 ){
242 next;
243 }
244 my ( $cmp_res, @unm_cols ) =
245 $self->is_matching( $gr_row->{ 'row' }, $row );
246 if( $cmp_res ){
247 $gr_row->{ 'quantity' }--;
248 return 1;
249 }
250 else{
251 if( not exists $gr_row->{ 'unm_cols' } or
252 @{ $gr_row->{ 'unm_cols' } } > @unm_cols )
253 {
254 $gr_row->{ 'unm_cols' } = [ @unm_cols ];
255 }
256 }
257 }
258 }
259 return 0;
260 }
261  
262  
263 #########################################################################
264  
265 =head2 C<is_empty>
266  
267 my $res = $uk_coll->is_empty();
268  
269 Returns 1 if a collection is empty. O otherwise
270  
271 =cut
272  
273 #########################################################################
274 sub is_empty
275 {
276 my $self = shift;
277 if( scalar( keys %{ $self->{ 'groups' } } ) ){
278 return 0;
279 }else{
280 return 1;
281 }
282 }
283  
284  
285 #########################################################################
286  
287 =head2 C<get_remaining_row>
288  
289 my @rows = $uk_coll->get_remaining_rows();
290  
291 Returns all rows, which are not matched by anything.
292  
293 =cut
294  
295 ###############################################################################
296 sub get_remaining_rows
297 {
298 my $self = shift;
299 my @output = ();
300  
301 foreach my $grp ( keys %{ $self->{ 'groups' } } ){
302 foreach my $r_grp ( @{ $self->{ 'groups' }->{ $grp } } ){
303 if( $r_grp->{ 'quantity' } > 0 ){
304 push( @output, $r_grp );
305 }
306 }
307 }
308 return @output;
309 }
310  
311 =head1 SEE ALSO
312  
313 The project is maintained on Source Forge L<http://lreport.sourceforge.net>.
314 You can find there links to some helpful documentation like tutorial.
315  
316 =head1 AUTHORS
317  
318 Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt>
319  
320 =head1 COPYRIGHT
321  
322 Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved.
323  
324 You may distribute under the terms of either the GNU General Public License
325 or the Artistic License, as specified in the Perl README file.
326  
327 =cut
328  
329 1;