X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Finflate%2Fhri.t;h=eaf9128228d392b7a78d584034ce98a708b4af06;hb=c97338007ede15e7c62095a642b3de382a3508bd;hp=79f96c63931cbc3b250666f2339a55ed7c5d6de9;hpb=27ffa6c0c4e12265ea84d2b0b954a3b8954e4d69;p=dbsrgits%2FDBIx-Class.git diff --git a/t/inflate/hri.t b/t/inflate/hri.t index 79f96c6..eaf9128 100644 --- a/t/inflate/hri.t +++ b/t/inflate/hri.t @@ -1,7 +1,8 @@ use strict; -use warnings; +use warnings; -use Test::More qw(no_plan); +use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); @@ -9,26 +10,69 @@ my $schema = DBICTest->init_schema(); # Under some versions of SQLite if the $rs is left hanging around it will lock # So we create a scope here cos I'm lazy { - my $rs = $schema->resultset('CD'); - - # get the defined columns - my @dbic_cols = sort $rs->result_source->columns; - - # use the hashref inflator class as result class - $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); - - # fetch first record - my $datahashref1 = $rs->first; - - my @hashref_cols = sort keys %$datahashref1; - - is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' ); + my $rs = $schema->resultset('CD')->search ({}, { + order_by => 'cdid', + }); + + my $orig_resclass = $rs->result_class; + eval "package DBICTest::CDSubclass; use base '$orig_resclass'"; + +# override on a specific $rs object, should not chain + $rs->result_class ('DBICTest::CDSubclass'); + + my $cd = $rs->find ({cdid => 1}); + is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find'); + + $cd = $rs->search({ cdid => 1 })->single; + is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single'); + + $cd = $rs->search()->find ({ cdid => 1 }); + is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find'); + +# set as attr - should propagate + my $hri_rs = $rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); + is ($rs->result_class, 'DBICTest::CDSubclass', 'original class unchanged'); + is ($hri_rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'result_class accessor pre-set via attribute'); + + + my $datahashref1 = $hri_rs->next; + is_deeply( + [ sort keys %$datahashref1 ], + [ sort $rs->result_source->columns ], + 'returned correct columns', + ); + + $cd = $hri_rs->find ({cdid => 1}); + is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)'); + + $cd = $hri_rs->search({ cdid => 1 })->single; + is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)'); + + $hri_rs->result_class ('DBIx::Class::Row'); # something bogus + is( + $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', + 'result_class set using accessor does not propagate over unused search' + ); + +# test result class auto-loading + throws_ok ( + sub { $rs->result_class ('nonexsitant_bogus_class') }, + qr/Can't locate nonexsitant_bogus_class.pm/, + 'Attempt to load on accessor override', + ); + is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); + + throws_ok ( + sub { $rs->search ({}, { result_class => 'nonexsitant_bogus_class' }) }, + qr/Can't locate nonexsitant_bogus_class.pm/, + 'Attempt to load on accessor override', + ); + is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); } - sub check_cols_of { my ($dbic_obj, $datahashref) = @_; - + foreach my $col (keys %$datahashref) { # plain column if (not ref ($datahashref->{$col}) ) { @@ -42,14 +86,14 @@ sub check_cols_of { elsif (ref ($datahashref->{$col}) eq 'ARRAY') { my @dbic_reltable = $dbic_obj->$col; my @hashref_reltable = @{$datahashref->{$col}}; - - is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries'); + + is (scalar @dbic_reltable, scalar @hashref_reltable, 'number of related entries'); # for my $index (0..scalar @hashref_reltable) { for my $index (0..scalar @dbic_reltable) { my $dbic_reltable_obj = $dbic_reltable[$index]; my $hashref_reltable_entry = $hashref_reltable[$index]; - + check_cols_of($dbic_reltable_obj, $hashref_reltable_entry); } } @@ -86,7 +130,7 @@ for my $index (0 .. $#hashrefinf) { } # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways -# check the inflator over a non-fetching join +# check the inflator over a non-fetching join $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { prefetch => { cds => 'tracks' }, order_by => [qw/cds.cdid tracks.trackid/], @@ -110,8 +154,8 @@ for my $index (0 .. $#hashrefinf) { my $datahashref = $hashrefinf[$index]; is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist'); - for my $col (keys %{$datahashref->{cds}[0]{tracks}[0]}) { - is ($track->get_column ($col), $datahashref->{cds}[0]{tracks}[0]{$col}, "Correct track '$col'"); + for my $col (keys %{$datahashref->{cds}{tracks}}) { + is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'"); } } @@ -135,3 +179,6 @@ is_deeply( [{ $artist->get_columns, cds => [] }], 'nested has_many prefetch without entries' ); + +done_testing; +