X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F45relationships.t;h=e9e48d877a8e85c5de0acf716d7a9ae4a657e766;hb=48c1a6c5fea9f320f94d583f79936b43c6d38710;hp=b2eb18ad34951e3fd98b6f71f1188099ce671250;hpb=59388920507cf773795544ab948421ec58e20d1f;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/t/45relationships.t b/t/45relationships.t index b2eb18a..e9e48d8 100644 --- a/t/45relationships.t +++ b/t/45relationships.t @@ -1,6 +1,7 @@ use strict; -use Test::More tests => 12; +use Test::More; use Test::Exception; +use Try::Tiny; use lib qw(t/lib); use make_dbictest_db; @@ -41,7 +42,7 @@ is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')), # test coderef as rel_name_map my $code_relationship = schema_with( rel_name_map => sub { - my ($args) = @_; + my ($args, $orig) = @_; if ($args->{local_moniker} eq 'Foo') { is_deeply( @@ -60,7 +61,6 @@ my $code_relationship = schema_with( }, 'correct args for Foo passed' ); - return 'bars_caught'; } elsif ($args->{local_moniker} eq 'Bar') { is_deeply( @@ -79,9 +79,15 @@ my $code_relationship = schema_with( }, 'correct args for Foo passed' ); - - return 'fooref_caught'; } + else { + fail( 'correct args passed to rel_name_map' ); + diag "args were: ", explain $args; + } + return $orig->({ + Bar => { fooref => 'fooref_caught' }, + Foo => { bars => 'bars_caught' }, + }); } ); is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')), @@ -93,13 +99,19 @@ is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')), 'rel_name_map overrode remote_info correctly' ); +throws_ok { + schema_with( rel_name_map => sub { $_[-1]->(sub{}) } ), +} qr/reentered rel_name_map must be a hashref/, 'throws error for invalid (code) rel_name_map callback map'; # test relationship_attrs throws_ok { schema_with( relationship_attrs => 'laughably invalid!!!' ); -} qr/relationship_attrs/, 'throws error for invalid relationship_attrs'; +} qr/relationship_attrs/, 'throws error for invalid (scalar) relationship_attrs'; +throws_ok { + schema_with( relationship_attrs => [qw/laughably invalid/] ); +} qr/relationship_attrs/, 'throws error for invalid (arrayref) relationship_attrs'; { my $nodelete = schema_with( relationship_attrs => @@ -124,6 +136,82 @@ throws_ok { ); } +# test relationship_attrs coderef +{ + my $relationship_attrs_coderef_invoked = 0; + my $schema; + + lives_ok { + $schema = schema_with(relationship_attrs => sub { + my %p = @_; + + $relationship_attrs_coderef_invoked++; + + if ($p{rel_name} eq 'bars') { + is $p{rel_type}, 'has_many', 'correct rel_type'; + is $p{local_table}, 'foo', 'correct local_table'; + is_deeply $p{local_cols}, [ 'fooid' ], 'correct local_cols'; + is $p{remote_table}, 'bar', 'correct remote_table'; + is_deeply $p{remote_cols}, [ 'fooref' ], 'correct remote_cols'; + is_deeply $p{attrs}, { + cascade_delete => 0, + cascade_copy => 0, + }, "got default rel attrs for $p{rel_name} in $p{local_table}"; + + like $p{local_source}->result_class, + qr/^DBICTest::Schema::\d+::Result::Foo\z/, + 'correct local source'; + + like $p{remote_source}->result_class, + qr/^DBICTest::Schema::\d+::Result::Bar\z/, + 'correct remote source'; + + $p{attrs}{snoopy} = 1; + + return $p{attrs}; + } + elsif ($p{rel_name} eq 'fooref') { + is $p{rel_type}, 'belongs_to', 'correct rel_type'; + is $p{local_table}, 'bar', 'correct local_table'; + is_deeply $p{local_cols}, [ 'fooref' ], 'correct local_cols'; + is $p{remote_table}, 'foo', 'correct remote_table'; + is_deeply $p{remote_cols}, [ 'fooid' ], 'correct remote_cols'; + is_deeply $p{attrs}, { + on_delete => 'NO ACTION', + on_update => 'NO ACTION', + is_deferrable => 0, + }, "got correct rel attrs for $p{rel_name} in $p{local_table}"; + + like $p{local_source}->result_class, + qr/^DBICTest::Schema::\d+::Result::Bar\z/, + 'correct local source'; + + like $p{remote_source}->result_class, + qr/^DBICTest::Schema::\d+::Result::Foo\z/, + 'correct remote source'; + + $p{attrs}{scooby} = 1; + + return $p{attrs}; + } + else { + fail "unknown rel $p{rel_name} in $p{local_table}"; + } + }); + } 'dumping schema with coderef relationship_attrs survived'; + + is $relationship_attrs_coderef_invoked, 2, + 'relationship_attrs coderef was invoked correct number of times'; + + is ((try { $schema->source('Foo')->relationship_info('bars')->{attrs}{snoopy} }) || undef, 1, + "correct relationship attributes for 'bars' in 'Foo'"); + + is ((try { $schema->source('Bar')->relationship_info('fooref')->{attrs}{scooby} }) || undef, 1, + "correct relationship attributes for 'fooref' in 'Bar'"); +} + +done_testing; + #### generates a new schema with the given opts every time it's called sub schema_with { $schema_counter++;