From: Arthur Axel "fREW" Schmidt Date: Wed, 5 May 2010 23:49:25 +0000 (+0000) Subject: test caching X-Git-Tag: v0.08122~72^2~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=85439e0c6d096fb21fdcb1d65140ddef286dfcc9;p=dbsrgits%2FDBIx-Class.git test caching --- diff --git a/lib/DBIx/Class/FilterColumn.pm b/lib/DBIx/Class/FilterColumn.pm index 9ae5007..2f9b4f1 100644 --- a/lib/DBIx/Class/FilterColumn.pm +++ b/lib/DBIx/Class/FilterColumn.pm @@ -62,6 +62,15 @@ sub get_filtered_column { return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val); } +sub set_column { + my ($self, $col) = (shift, @_); + + # blow cache + delete $self->{_filtered_column}{$col}; + + $self->next::method(@_); +} + sub set_filtered_column { my ($self, $col, $filtered) = @_; diff --git a/t/row/filter_column.t b/t/row/filter_column.t index 6441e33..60c305d 100644 --- a/t/row/filter_column.t +++ b/t/row/filter_column.t @@ -5,11 +5,13 @@ use Test::More; use lib qw(t/lib); use DBICTest; +my $from_storage_ran = 0; +my $to_storage_ran = 0; my $schema = DBICTest->init_schema(); DBICTest::Schema::Artist->load_components('FilterColumn'); DBICTest::Schema::Artist->filter_column(rank => { - filter_from_storage => sub { $_[1] * 2 }, - filter_to_storage => sub { $_[1] / 2 }, + filter_from_storage => sub { $from_storage_ran++; $_[1] * 2 }, + filter_to_storage => sub { $to_storage_ran++; $_[1] / 2 }, }); Class::C3->reinitialize(); @@ -66,4 +68,51 @@ MC: { is $cd->artist->rank, 20, 'artist rank gets correctly filtered w/ MC'; } +my $initial_from = $from_storage_ran; +my $initial_to = $to_storage_ran; + +# ensure we are creating a fresh obj +$artist = $schema->resultset('Artist')->single($artist->ident_condition); + +is $initial_from, $from_storage_ran, 'from has not run yet'; +is $initial_from, $from_storage_ran, 'to has not run yet'; + +$artist->rank; +$artist->get_filtered_column('rank'); +$artist->get_column('rank'); + +is $from_storage_ran, $initial_from + 1, 'from ran once, therefor caches'; +is $to_storage_ran, $initial_to, 'to ran none'; +$initial_from = $from_storage_ran; +$initial_to = $to_storage_ran; + +$artist->rank(1); + +is $from_storage_ran, $initial_from, 'from ran none'; +is $to_storage_ran, $initial_to + 1, 'to ran once'; +$initial_from = $from_storage_ran; +$initial_to = $to_storage_ran; + +$artist->rank; + +is $from_storage_ran, $initial_from + 1, 'from ran once'; +is $to_storage_ran, $initial_to, 'to ran none'; +$initial_from = $from_storage_ran; +$initial_to = $to_storage_ran; + +$artist->rank; + +is $from_storage_ran, $initial_from, 'from ran none'; +is $to_storage_ran, $initial_to, 'to ran none'; +$initial_from = $from_storage_ran; +$initial_to = $to_storage_ran; + +$artist->set_column(rank => 1); +$artist->rank; + +is $from_storage_ran, $initial_from + 1, 'from ran once (set column blows cache)'; +is $to_storage_ran, $initial_to, 'to ran none'; +$initial_from = $from_storage_ran; +$initial_to = $to_storage_ran; + done_testing;