From: Rob Kinyon Date: Wed, 28 Oct 2009 19:38:24 +0000 (-0400) Subject: Added a test to demonstrate failure of not following belongs_to relationships as... X-Git-Tag: 1.001011~19 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=610be08941aae88b19eba97b6ad28a32b238d8ae;p=dbsrgits%2FDBIx-Class-Fixtures.git Added a test to demonstrate failure of not following belongs_to relationships as far as possible --- diff --git a/t/15-multiple-belongs-to.t b/t/15-multiple-belongs-to.t new file mode 100644 index 0000000..d52b5cf --- /dev/null +++ b/t/15-multiple-belongs-to.t @@ -0,0 +1,62 @@ +#!perl + +use DBIx::Class::Fixtures; +use Test::More tests => 7; +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 => 'multiple-has-many.json', schema => $schema, directory => 't/var/fixtures' }), 'fetch dump executed okay'); + +# check dump is okay +my $dir = dir('t/var/fixtures'); + +ok( -e 't/var/fixtures/producer', "We fetched some producers" ); +ok( -e 't/var/fixtures/cd_to_producer', "We fetched some cd/producer xrefs" ); +ok( -e 't/var/fixtures/cd', "We fetched some cds" ); +ok( -e 't/var/fixtures/artist', "We fetched some artists" ); + +__END__ +while ( my ($dirname, $sourcename) = each %dirs ) { + my $this_dir = dir($dir, $dirname); +} + +my $cd_dir = dir($dir, 'cd'); +my $track_dir = dir($dir, 'track'); + +# check only artist1's cds that matched the rule were fetched +my $artist1 = $schema->resultset('Artist')->find(1); +my $artist1_cds = $artist1->cds; +while (my $a1_cd = $artist1_cds->next) { + my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix'); + if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) { + ok(-e $cd_fix_file, 'cd matching rule fetched'); + } else { + isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched'); + } +} + +# check only cds' tracks that matched the rule were fetched +foreach my $cd_fix_file ($cd_dir->children) { + my $HASH1; eval($cd_fix_file->slurp()); + is(ref $HASH1, 'HASH', 'cd fixture evals into hash'); + + my $cd = $schema->resultset('CD')->find($HASH1->{cdid}); + foreach my $track ($cd->tracks->all) { + my $track_fix_file = file($track_dir, $track->id . '.fix'); + if ($track->get_column('position') eq 2) { + is(-e $track_fix_file, 1, 'track matching rule fetched'); + } else { + isnt(-e $track_fix_file, 1, 'track not matching rule not fetched'); + } + } +} + diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index 9de9e27..a0414cf 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -176,6 +176,9 @@ sub populate_schema { [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], + [ 2, 1 ], + [ 2, 2 ], + [ 3, 3 ], ]); $schema->populate('Track', [ diff --git a/t/lib/DBICTest/Schema/Producer.pm b/t/lib/DBICTest/Schema/Producer.pm index 036f9f2..da90f6d 100644 --- a/t/lib/DBICTest/Schema/Producer.pm +++ b/t/lib/DBICTest/Schema/Producer.pm @@ -17,4 +17,10 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('producerid'); __PACKAGE__->add_unique_constraint(prod_name => [ qw/name/ ]); +__PACKAGE__->has_many( + producer_to_cd => 'DBICTest::Schema::CD_to_Producer' => 'producer' +); + +__PACKAGE__->many_to_many( cds => producer_to_cd => 'cd' ); + 1; diff --git a/t/var/configs/multiple-has-many.json b/t/var/configs/multiple-has-many.json new file mode 100644 index 0000000..db5c79a --- /dev/null +++ b/t/var/configs/multiple-has-many.json @@ -0,0 +1,17 @@ +{ + "might_have": { + "fetch": 0 + }, + "has_many": { + "fetch": 0 + }, + "sets": [{ + "class": "Producer", + "quantity": "all", + "fetch": [{ + "rel": "producer_to_cd", + "quantity": "all" + }] + }] +} +