Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Regression test with nested data structures -- Or how to use Test::Deep?

by LanX (Canon)
on Oct 18, 2012 at 10:48 UTC ( #999704=perlquestion: print w/ replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Moin Monks!

I need some help using Test::Deep ...

my problem: I wanna refactor a rather complicated module which produces nested data structures like the following and I wanna test that the output stays stable:

my $h_data1 = { '756' => { 'firstname' => [ '17833329', '17785333', ], 'lastname' => [ '17785333', '17785974', ], 'pks' => [ '17833329', '17785333', '17785974', ], 'time' => '0.00718307495117188', # How long + it took }, '755' => { 'lastname' => [ '17833329' ], 'pks' => [ '17833329' ], 'time' => '0.00639796257019043', } };
to be able to use Test::More::is_deeply() between to runs I need to ignore the time benchmark.

I already solved this quite complicatedly by copying new filtered hashes...

use strict; use warnings; use Test::More; use Data::Dump qw/pp/; sub ignore_fields { my $h_in = shift; my $h_out; my @to_ignore =qw(time); while ( my ($pk, $h_matches) = each %$h_in ) { my $h_filtered = { %$h_matches }; delete @$h_filtered{@to_ignore}; $h_out->{$pk}=$h_filtered; } return $h_out; } pp ignore_fields($h_data1); pp $h_data1; is_deeply(ignore_fields($h_data1), ignore_fields($h_data2), "is_deeply +");

But Test::Deep should be the right module to do this appropriately, unfortunately today my brain is to slow to understand the docs... (it's flue season ;-)

Any help appreciated!

Cheers Rolf

Comment on Regression test with nested data structures -- Or how to use Test::Deep?
Select or Download Code
Re: Regression test with nested data structures -- Or how to use Test::Deep?
by ELISHEVA (Prior) on Oct 18, 2012 at 13:31 UTC

    I haven't tried this myself, but looking at the docs, another possibility would be to use num($number, $tolerance) to test the time field. If you set the time tolerance wide enough, then you should be able to pass a test on that field without having to pre-filter the hash and remove the timestamp key.

      Thanks, interesting option! :)

      But I fear using a fixed time tolerace is a good possibility to produce tests which fail occasionally...

      Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999704]
Approved by Ratazong
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-07-04 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls