From: Rob Kinyon Date: Wed, 11 Nov 2009 02:35:27 +0000 (-0500) Subject: Fixed bug where rules overriding has_many for a given class weren't respected X-Git-Tag: 1.001011~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=06b7a1cc89850d44bc424d43b5dc48148f999522;p=dbsrgits%2FDBIx-Class-Fixtures.git Fixed bug where rules overriding has_many for a given class weren't respected --- diff --git a/Changes b/Changes index b67c7c0..0d16e98 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for DBIx-Class-Fixtures +1.001005 +- Fixed problem where rules containing an override of has_many weren't applied + correctly. + 1.001004 - Fixed problem where descending a has_many to something with two belongs_to relationships wouldn't get the second belongs_to. diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index b6cd2a2..3c8f038 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -26,7 +26,7 @@ __PACKAGE__->mk_group_accessors( 'simple' => qw/config_dir =head1 VERSION -Version 1.001004 +Version 1.001005 =cut @@ -524,6 +524,12 @@ sub dump { $config->{rules} ||= {}; my @sources = sort { $a->{class} cmp $b->{class} } @{delete $config->{sets}}; + while ( my ($k,$v) = each %{ $config->{rules} } ) { + if ( my $rs = $schema->resultset($k) ) { + $config->{rules}{$rs->result_source->source_name} = $v; + } + } + foreach my $source (@sources) { # apply rule to set if specified my $rule = $config->{rules}->{$source->{class}}; @@ -716,7 +722,7 @@ sub dump_object { # don't bother looking at rels unless we are actually planning to dump at least one type my ($might_have, $belongs_to, $has_many) = map { - $set->{$_}{fetch}; + $set->{$_}{fetch} || $set->{rules}{$src->source_name}{$_}{fetch} } qw/might_have belongs_to has_many/; return unless $might_have diff --git a/t/16-rules-hasmany.t b/t/16-rules-hasmany.t new file mode 100644 index 0000000..7014e4f --- /dev/null +++ b/t/16-rules-hasmany.t @@ -0,0 +1,36 @@ +#!perl + +use DBIx::Class::Fixtures; +use Test::More tests => 11; +use lib qw(t/lib); +use DBICTest; +use Path::Class; +use Data::Dumper; + +# set up and populate schema +ok(my $schema = DBICTest->init_schema(), 'got schema'); + +my $config_dir = 't/var/configs'; + +# do dump +ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir'); +ok($fixtures->dump({ config => 'rules2.json', schema => $schema, directory => 't/var/fixtures' }), 'quantity dump executed okay'); + +# check dump is okay +foreach my $test ( + [ 'artist', 1, 'Artist', 'artistid' ], + [ 'cd', 2, 'CD', 'cdid' ], +) { + my ($dirname, $count, $moniker, $id) = @$test; + my $dir = dir("t/var/fixtures/$dirname"); + my @children = $dir->children; + is(scalar(@children), $count, "right number of $dirname fixtures created"); + + foreach my $fix_file (@children) { + my $HASH1; eval($fix_file->slurp()); + is(ref $HASH1, 'HASH', 'fixture evals into hash'); + my $obj = $schema->resultset($moniker)->find($HASH1->{$id}); + is_deeply({$obj->get_columns}, $HASH1, "dumped fixture is equivalent to $dirname row"); + } +} + diff --git a/t/var/configs/rules2.json b/t/var/configs/rules2.json new file mode 100644 index 0000000..3ab6719 --- /dev/null +++ b/t/var/configs/rules2.json @@ -0,0 +1,19 @@ +{ + "might_have": { + "fetch": 0 + }, + "has_many": { + "fetch": 0 + }, + "sets": [{ + "class": "CD", + "ids": ["5"] + }], + "rules": { + "Artist": { + "has_many": { + "fetch": 1 + } + } + } +}