X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F100populate.t;h=4b7f9292d3d3d2e11631c3929dcbeacafa79b566;hb=7e017742f708618487923261292bc92660a2031c;hp=16c1e6db404047ac2e3ec17071bac7c191b82ae3;hpb=d0cefd99a98e7fb2304fe6a5182d321fe7c551fc;p=dbsrgits%2FDBIx-Class.git diff --git a/t/100populate.t b/t/100populate.t index 16c1e6d..4b7f929 100644 --- a/t/100populate.t +++ b/t/100populate.t @@ -6,11 +6,23 @@ use Test::Exception; use Test::Warn; use lib qw(t/lib); use DBICTest; -use DBIx::Class::_Util 'sigwarn_silencer'; -use Path::Class::File (); +use DBIx::Class::_Util qw(sigwarn_silencer serialize); use Math::BigInt; use List::Util qw/shuffle/; -use Storable qw/nfreeze dclone/; + +{ + package DBICTest::StringifiesOnly; + use overload + '""' => sub { $_[0]->[0] }, + fallback => 0, + ; +} +{ + package DBICTest::StringifiesViaFallback; + use overload + 'bool' => sub { $_[0]->[0] }, + ; +} my $schema = DBICTest->init_schema(); @@ -98,7 +110,7 @@ is(scalar @links, 2); is($links[0]->url, undef); is($links[1]->url, 'url42'); -## make sure populate -> insert_bulk honors fields/orders in void context +## make sure populate -> _insert_bulk honors fields/orders in void context ## schema order $schema->populate('Link', [ [ qw/id url title/ ], @@ -191,7 +203,7 @@ is($links[2]->title, undef); my $rs = $schema->resultset('Link'); $rs->delete; - # test insert_bulk with all literal sql (no binds) + # test populate with all literal sql (no binds) $rs->populate([ (+{ @@ -229,7 +241,7 @@ is($links[2]->title, undef); my $rs = $schema->resultset('Link'); $rs->delete; - # test insert_bulk with all literal/bind sql + # test populate with all literal/bind sql $rs->populate([ (+{ url => \['?', [ {} => 'cpan.org' ] ], @@ -244,7 +256,7 @@ is($links[2]->title, undef); $rs->delete; - # test insert_bulk with mix literal and literal/bind + # test populate with mix literal and literal/bind $rs->populate([ (+{ url => \"'cpan.org'", @@ -383,107 +395,134 @@ lives_ok { } 'literal+bind with semantically identical attrs works after normalization'; # test all kinds of population with stringified objects +# or with empty sets warnings_like { - local $ENV{DBIC_RT79576_NOWARN}; - my $rs = $schema->resultset('Artist')->search({}, { columns => [qw(name rank)], order_by => 'artistid' }); # the stringification has nothing to do with the artist name # this is solely for testing consistency - my $fn = Path::Class::File->new ('somedir/somefilename.tmp'); - my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp'); + my $fn = bless [ 'somedir/somefilename.tmp' ], 'DBICTest::StringifiesOnly'; + my $fn2 = bless [ 'somedir/someotherfilename.tmp' ], 'DBICTest::StringifiesViaFallback'; my $rank = Math::BigInt->new(42); my $args = { - 'stringifying objects after regular values' => [ map - { { name => $_, rank => $rank } } - ( + 'stringifying objects after regular values' => { AoA => [ + [qw( name rank )], + ( map { [ $_, $rank ] } ( 'supplied before stringifying objects', 'supplied before stringifying objects 2', $fn, $fn2, - ) - ], - 'stringifying objects before regular values' => [ map - { { name => $_, rank => $rank } } - ( + )), + ]}, + + 'stringifying objects before regular values' => { AoA => [ + [qw( rank name )], + ( map { [ $rank, $_ ] } ( $fn, $fn2, 'supplied after stringifying objects', 'supplied after stringifying objects 2', - ) - ], - 'stringifying objects between regular values' => [ map - { { name => $_, rank => $rank } } - ( + )), + ]}, + + 'stringifying objects between regular values' => { AoA => [ + [qw( name rank )], + ( map { [ $_, $rank ] } ( 'supplied before stringifying objects', $fn, $fn2, 'supplied after stringifying objects', - ) - ], - 'stringifying objects around regular values' => [ map - { { name => $_, rank => $rank } } - ( + )) + ]}, + + 'stringifying objects around regular values' => { AoA => [ + [qw( rank name )], + ( map { [ $rank, $_ ] } ( $fn, 'supplied between stringifying objects', $fn2, - ) - ], + )) + ]}, + + 'single stringifying object' => { AoA => [ + [qw( rank name )], + [ $rank, $fn ], + ]}, + + 'empty set' => { AoA => [ + [qw( name rank )], + ]}, }; + # generate the AoH equivalent based on the AoAs above + # also generate the expected HRI output ( is_deeply is too smart for its own good ) + for my $bag (values %$args) { + $bag->{AoH} = []; + $bag->{Expected} = []; + my @hdr = @{$bag->{AoA}[0]}; + for my $v ( @{$bag->{AoA}}[1..$#{$bag->{AoA}}] ) { + push @{$bag->{AoH}}, my $h = {}; + @{$h}{@hdr} = @$v; + + push @{$bag->{Expected}}, my $hs = {}; + @{$hs}{@hdr} = map { "$_" } @$v; + } + } + local $Storable::canonical = 1; - my $preimage = nfreeze([$fn, $fn2, $rank, $args]); + my $preimage = serialize($args); - for my $tst (keys %$args) { - # test void ctx - $rs->delete; - $rs->populate($args->{$tst}); - is_deeply( - $rs->all_hri, - $args->{$tst}, - "Populate() $tst in void context" - ); - - # test non-void ctx - $rs->delete; - my $dummy = $rs->populate($args->{$tst}); - is_deeply( - $rs->all_hri, - $args->{$tst}, - "Populate() $tst in non-void context" - ); + for my $tst (keys %$args) { + for my $type (qw(AoA AoH)) { + + # test void ctx + $rs->delete; + $rs->populate($args->{$tst}{$type}); + is_deeply( + $rs->all_hri, + $args->{$tst}{Expected}, + "Populate() $tst in void context" + ); + + # test scalar ctx + $rs->delete; + my $dummy = $rs->populate($args->{$tst}{$type}); + is_deeply( + $rs->all_hri, + $args->{$tst}{Expected}, + "Populate() $tst in non-void context" + ); + + # test list ctx + $rs->delete; + my @dummy = $rs->populate($args->{$tst}{$type}); + is_deeply( + $rs->all_hri, + $args->{$tst}{Expected}, + "Populate() $tst in non-void context" + ); + } # test create() as we have everything set up already $rs->delete; - $rs->create($_) for @{$args->{$tst}}; + $rs->create($_) for @{$args->{$tst}{AoH}}; is_deeply( $rs->all_hri, - $args->{$tst}, + $args->{$tst}{Expected}, "Create() $tst" ); } ok ( - ($preimage eq nfreeze( [$fn, $fn2, $rank, $args] )), + ($preimage eq serialize($args)), 'Arguments fed to populate()/create() unchanged' ); $rs->delete; -} [ - # warning to be removed around Apr 1st 2015 - # smokers start failing a month before that - ( - ( DBICTest::RunMode->is_author and ( time() > 1427846400 ) ) - or - ( DBICTest::RunMode->is_smoker and ( time() > 1425168000 ) ) - ) - ? () - # one unique for populate() and create() each - : (qr/\QPOSSIBLE *PAST* DATA CORRUPTION detected \E.+\QTrigger condition encountered at @{[ __FILE__ ]} line\E \d/) x 2 -], 'Data integrity warnings as planned'; +} [], 'Data integrity warnings gone as planned'; $schema->is_executed_sql_bind( sub {