From: Matt S Trout Date: Fri, 10 Mar 2006 12:11:02 +0000 (+0000) Subject: test fixups, Schema optimisation tweaks X-Git-Tag: v0.06000~60^2~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e9100ff73a57d6c0e997848d38144f2901c8715e;p=dbsrgits%2FDBIx-Class.git test fixups, Schema optimisation tweaks --- diff --git a/Changes b/Changes index 7e36070..5324fa1 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for DBIx::Class 0.05990_01 Pending + - remove test dep on YAML + - additional speed tweaks for C3 - allow scalarefs passed to order_by to go straight through to SQL - renamed insert_or_update to update_or_insert (with compat alias) - hidden lots of packages from the PAUSE Indexer diff --git a/lib/DBIx/Class/InflateColumn.pm b/lib/DBIx/Class/InflateColumn.pm index 6efbe13..226913a 100644 --- a/lib/DBIx/Class/InflateColumn.pm +++ b/lib/DBIx/Class/InflateColumn.pm @@ -130,7 +130,8 @@ sub update { foreach my $key (keys %$attrs) { if (ref $attrs->{$key} && exists $class->column_info($key)->{_inflate_info}) { - $attrs->{$key} = $class->_deflated_column($key, $attrs->{$key}); +# $attrs->{$key} = $class->_deflated_column($key, $attrs->{$key}); + $class->set_inflated_column ($key, delete $attrs->{$key}); } } return $class->next::method($attrs, @rest); diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 65c235c..84e5263 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -289,14 +289,19 @@ sub compose_namespace { my %target; my %map; my $schema = $self->clone; - foreach my $moniker ($schema->sources) { - my $source = $schema->source($moniker); - my $target_class = "${target}::${moniker}"; - $self->inject_base( - $target_class => $source->result_class, ($base ? $base : ()) - ); - $source->result_class($target_class); + { + no warnings qw/redefine/; + local *Class::C3::reinitialize = sub { }; + foreach my $moniker ($schema->sources) { + my $source = $schema->source($moniker); + my $target_class = "${target}::${moniker}"; + $self->inject_base( + $target_class => $source->result_class, ($base ? $base : ()) + ); + $source->result_class($target_class); + } } + Class::C3->reinitialize(); { no strict 'refs'; foreach my $meth (qw/class source resultset/) { diff --git a/t/basicrels/08inflate_serialize.t b/t/basicrels/08inflate_serialize.t new file mode 100644 index 0000000..3676643 --- /dev/null +++ b/t/basicrels/08inflate_serialize.t @@ -0,0 +1,7 @@ +use Test::More; +use lib qw(t/lib); +use DBICTest; +use DBICTest::BasicRels; + +require "t/run/08inflate_serialize.tl"; +run_tests(DBICTest->schema); diff --git a/t/helperrels/08inflate_serialize.t b/t/helperrels/08inflate_serialize.t new file mode 100644 index 0000000..e0ca1d8 --- /dev/null +++ b/t/helperrels/08inflate_serialize.t @@ -0,0 +1,7 @@ +use Test::More; +use lib qw(t/lib); +use DBICTest; +use DBICTest::HelperRels; + +require "t/run/08inflate_serialize.tl"; +run_tests(DBICTest->schema); diff --git a/t/run/08inflate.tl b/t/run/08inflate.tl index 97d0778..e21a6c6 100644 --- a/t/run/08inflate.tl +++ b/t/run/08inflate.tl @@ -4,7 +4,7 @@ my $schema = shift; eval { require DateTime }; plan skip_all => "Need DateTime for inflation tests" if $@; -plan tests => 5; +plan tests => 3; DBICTest::Schema::CD->inflate_column( 'year', { inflate => sub { DateTime->new( year => shift ) }, @@ -27,34 +27,6 @@ $cd->update; ($cd) = $schema->resultset("CD")->search( year => $now->year ); is( $cd->year->year, $now->year, 'deflate ok' ); -use YAML; -DBICTest::Schema::Serialized->inflate_column( 'serialized', - { inflate => sub { Load (shift) }, - deflate => sub { die "Expecting a reference" unless (ref $_[0]); Dump (shift) } } -); -Class::C3->reinitialize; - -my $complex1 = { - id => 1, - serialized => { - a => 1, - b => 2, - }, -}; - -my $complex2 = { - id => 1, - serialized => [qw/a b 1 2/], -}; - -my $rs = $schema->resultset('Serialized'); - -my $entry = $rs->create($complex2); - -ok($entry->update ($complex1), "update with hashref deflating ok"); - -ok($entry->update ($complex2), "update with arrayref deflating ok"); - } 1; diff --git a/t/run/08inflate_serialize.tl b/t/run/08inflate_serialize.tl new file mode 100644 index 0000000..ae5ca7a --- /dev/null +++ b/t/run/08inflate_serialize.tl @@ -0,0 +1,72 @@ +sub run_tests { +my $schema = shift; + +use Data::Dumper; + +my @serializers = ( + { module => 'YAML.pm', + inflater => sub { YAML::Load (shift) }, + deflater => sub { die "Expecting a reference" unless (ref $_[0]); YAML::Dump (shift) }, + }, + { module => 'Storable.pm', + inflater => sub { Storable::thaw (shift) }, + deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) }, + }, +); + + +my $selected; +foreach my $serializer (@serializers) { + eval { require $serializer->{module} }; + unless ($@) { + $selected = $serializer; + last; + } +} + +plan (skip_all => "No suitable serializer found") unless $selected; + +plan (tests => 6); +DBICTest::Schema::Serialized->inflate_column( 'serialized', + { inflate => $selected->{inflater}, + deflate => $selected->{deflater}, + }, +); +Class::C3->reinitialize; + +my $complex1 = { + id => 1, + serialized => { + a => 1, + b => [ + { c => 2 }, + ], + d => 3, + }, +}; + +my $complex2 = { + id => 1, + serialized => [ + 'a', + { b => 1, c => 2}, + 'd', + ], +}; + +my $rs = $schema->resultset('Serialized'); +my $entry = $rs->create({ id => 1, serialized => ''}); + +my $inflated; + +ok($entry->update ({ %{$complex1} }), 'hashref deflation ok'); +ok($inflated = $entry->serialized, 'hashref inflation ok'); +is_deeply($inflated, $complex1->{serialized}, 'inflated hash matches original'); + +ok($entry->update ({ %{$complex2} }), 'arrayref deflation ok'); +ok($inflated = $entry->serialized, 'arrayref inflation ok'); +is_deeply($inflated, $complex2->{serialized}, 'inflated array matches original'); + +} + +1;