X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frow%2Ffilter_column.t;h=af7a951f2b3e3ddb40b56edce7dd9fde993a6b45;hb=c0329273268971824784f239f32c7246e68da9c5;hp=066fbfa8097fe7902b030da13a40eb80fbd86265;hpb=1bc006cfc0c25ea943851c7c0686eb6a37ef912d;p=dbsrgits%2FDBIx-Class.git diff --git a/t/row/filter_column.t b/t/row/filter_column.t index 066fbfa..af7a951 100644 --- a/t/row/filter_column.t +++ b/t/row/filter_column.t @@ -1,9 +1,11 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; use warnings; use Test::More; use Test::Exception; -use lib qw(t/lib); + use DBICTest; my $from_storage_ran = 0; @@ -11,8 +13,8 @@ my $to_storage_ran = 0; my $schema = DBICTest->init_schema( no_populate => 1 ); DBICTest::Schema::Artist->load_components(qw(FilterColumn InflateColumn)); DBICTest::Schema::Artist->filter_column(charfield => { - filter_from_storage => sub { $from_storage_ran++; $_[1] * 2 }, - filter_to_storage => sub { $to_storage_ran++; $_[1] / 2 }, + filter_from_storage => sub { $from_storage_ran++; defined $_[1] ? $_[1] * 2 : undef }, + filter_to_storage => sub { $to_storage_ran++; defined $_[1] ? $_[1] / 2 : undef }, }); Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO; @@ -118,6 +120,18 @@ CACHE_TEST: { is ($artist->charfield, '8', 'Cache properly blown'); is $from_storage_ran, ++$expected_from, 'from did not run'; is $to_storage_ran, $expected_to, 'to did not run'; + + $artist->update({ charfield => undef }); + is $from_storage_ran, $expected_from, 'from did not run'; + is $to_storage_ran, ++$expected_to, 'to did run'; + + $artist->discard_changes; + is ( $artist->get_column('charfield'), undef, 'Got back null' ); + is ( $artist->charfield, undef, 'Got back null through filter' ); + + is $from_storage_ran, ++$expected_from, 'from did run'; + is $to_storage_ran, $expected_to, 'to did not run'; + } # test in-memory operations @@ -247,4 +261,30 @@ throws_ok { DBICTest::Schema::Artist->filter_column( charfield => {} ) } 'Correctly throws exception for empty attributes' ; +FC_ON_PK_TEST: { + # there are cases in the wild that autovivify stuff deep in the + # colinfo guts. While this is insane, there is no alternative + # so at leats make sure it keeps working... + + $schema->source('Artist')->column_info('artistid')->{_filter_info} ||= {}; + + for my $key ('', 'primary') { + lives_ok { + $schema->resultset('Artist')->find_or_create({ artistid => 42 }, { $key ? ( key => $key ) : () }); + }; + } + + + DBICTest::Schema::Artist->filter_column(artistid => { + filter_to_storage => sub { $_[1] * 100 }, + filter_from_storage => sub { $_[1] - 100 }, + }); + + for my $key ('', 'primary') { + throws_ok { + $schema->resultset('Artist')->find_or_create({ artistid => 42 }, { $key ? ( key => $key ) : () }); + } qr/\QUnable to satisfy requested constraint 'primary', FilterColumn values not usable for column(s): 'artistid'/; + } +} + done_testing;