Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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?
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?
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please
[thepkd]: i used a series of {}'s but it dont work
[choroba]: You need square brackets for arrays
[GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
[thepkd]: to dereference i mean
LanX .oO( oh tempz, oh moretz)
[thepkd]: @GotToBTru Sure. But.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2016-12-06 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.