+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;
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;
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';
+
+ ok ! $artist->is_changed, 'object clean';
+ is_deeply
+ { $artist->get_dirty_columns },
+ {},
+ 'dirty columns as expected',
+ ;
+
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, $expected_to, 'to did not run';
+
+ $artist->charfield(42);
+
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, ++$expected_to, 'to ran once, determining dirtyness';
+
+ is $artist->charfield, 42, 'setting once works';
+ ok $artist->is_column_changed('charfield'), 'column changed';
+ ok $artist->is_changed, 'object changed';
+ is_deeply
+ { $artist->get_dirty_columns },
+ { charfield => 21 },
+ 'dirty columns as expected',
+ ;
+
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, $expected_to, 'to did not run';
+
+ $artist->charfield(66);
+ is $artist->charfield, 66, 'setting twice works';
+ ok $artist->is_column_changed('charfield'), 'column changed';
+ ok $artist->is_changed, 'object changed';
+
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, $expected_to, 'to did not run a second time on dirty column';
+
+ is_deeply
+ { $artist->get_dirty_columns },
+ { charfield => 33 },
+ 'dirty columns as expected',
+ ;
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, ++$expected_to, 'to did run producing a new dirty_columns set';
+
+ is_deeply
+ { $artist->get_dirty_columns },
+ { charfield => 33 },
+ 'dirty columns still as expected',
+ ;
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, $expected_to, 'to did not run on re-invoked get_dirty_columns';
+
+ $artist->update;
+ is $artist->charfield, 66, 'value still there';
+
+ is $from_storage_ran, $expected_from, 'from did not run';
+ is $to_storage_ran, $expected_to, 'to did not run ';
+
+ $artist->discard_changes;
+
+ is $from_storage_ran, $expected_from, 'from did not run after discard_changes';
+ is $to_storage_ran, $expected_to, 'to did not run after discard_changes';
+
+ is $artist->charfield, 66, 'value still there post reload';
+
+ is $from_storage_ran, ++$expected_from, 'from did run';
+ is $to_storage_ran, $expected_to, 'to did not run';
}
# test in-memory operations
sub { $schema->resultset('Artist')->new({ charfield => 42 }) },
sub { my $art = $schema->resultset('Artist')->new({}); $art->charfield(42); $art },
) {
+ $schema->resultset('Artist')->delete;
my $expected_from = $from_storage_ran;
my $expected_to = $to_storage_ran;
ok( $artist->has_column_loaded('charfield'), 'Filtered column marked as loaded under new' );
is( $artist->charfield, 42, 'Proper unfiltered value' );
is( $artist->get_column('charfield'), 21, 'Proper filtered value' );
+
+ $artist->insert;
+ ($raw_db_charfield) = $schema->resultset('Artist')
+ ->search ($artist->ident_condition)
+ ->get_column('charfield')
+ ->next;
+
+ is $raw_db_charfield, 21, 'Proper value in database';
}
# test literals
'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;