X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Finflate%2Fserialize.t;h=2da03476ae006d66fc0c3a55fadaf623130d9534;hb=c0329273268971824784f239f32c7246e68da9c5;hp=59c099753807729491c3d220b9ae7e5162c4f58c;hpb=c6e0ee83e0c8e607d62871633570fe1e94079e63;p=dbsrgits%2FDBIx-Class.git diff --git a/t/inflate/serialize.t b/t/inflate/serialize.t index 59c0997..2da0347 100644 --- a/t/inflate/serialize.t +++ b/t/inflate/serialize.t @@ -1,22 +1,22 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; -use warnings; +use warnings; use Test::More; -use lib qw(t/lib); + use DBICTest; my $schema = DBICTest->init_schema(); -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 => '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) }, + { module => 'Storable.pm', + inflater => sub { Storable::thaw (shift) }, + deflater => sub { die "Expecting a reference" unless (ref $_[0]); Storable::nfreeze (shift) }, }, ); @@ -25,34 +25,31 @@ my $selected; foreach my $serializer (@serializers) { eval { require $serializer->{module} }; unless ($@) { - $selected = $serializer; - last; + $selected = $serializer; + last; } } -plan (skip_all => "No suitable serializer found") unless $selected; - -plan (tests => 8); DBICTest::Schema::Serialized->inflate_column( 'serialized', { inflate => $selected->{inflater}, deflate => $selected->{deflater}, }, ); -Class::C3->reinitialize; +Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO; my $struct_hash = { a => 1, - b => [ + b => [ { c => 2 }, ], d => 3, }; my $struct_array = [ - 'a', - { - b => 1, - c => 2 + 'a', + { + b => 1, + c => 2, }, 'd', ]; @@ -62,25 +59,54 @@ my $inflated; #======= testing hashref serialization -my $object = $rs->create( { - id => 1, +my $object = $rs->create( { serialized => '', } ); ok($object->update( { serialized => $struct_hash } ), 'hashref deflation'); ok($inflated = $object->serialized, 'hashref inflation'); is_deeply($inflated, $struct_hash, 'inflated hash matches original'); -$object = $rs->create( { - id => 2, +$object = $rs->create( { serialized => '', } ); -eval { $object->set_inflated_column('serialized', $struct_hash) }; -ok(!$@, 'set_inflated_column to a hashref'); +$object->set_inflated_column('serialized', $struct_hash); is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); +$object = $rs->new({}); +$object->serialized ($struct_hash); +$object->insert; +is_deeply ( + $rs->find ({id => $object->id})->serialized, + $struct_hash, + 'new/insert works', +); #====== testing arrayref serialization ok($object->update( { serialized => $struct_array } ), 'arrayref deflation'); ok($inflated = $object->serialized, 'arrayref inflation'); is_deeply($inflated, $struct_array, 'inflated array matches original'); + +$object = $rs->new({}); +$object->serialized ($struct_array); +$object->insert; +is_deeply ( + $rs->find ({id => $object->id})->serialized, + $struct_array, + 'new/insert works', +); + +#===== make sure make_column_dirty interacts reasonably with inflation +$object = $rs->search({}, { rows => 1 })->next; +$object->update ({serialized => { x => 'y'}}); + +$object->serialized->{x} = 'z'; # change state without notifying $object +ok (!$object->get_dirty_columns, 'no dirty columns yet'); +is_deeply ($object->serialized, { x => 'z' }, 'object data correct'); + +$object->make_column_dirty('serialized'); +$object->update; + +is_deeply ($rs->first->serialized, { x => 'z' }, 'changes made it to the db' ); + +done_testing;