Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by LanX (Chancellor)
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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999704]
Approved by Ratazong
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-08-17 20:40 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (292 votes). Check out past polls.