Update Firebird ODBC driver download URL
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 45relationships.t
CommitLineData
c8c27020 1use strict;
f8c2ca5e 2use warnings;
7b6a8d73 3use Test::More;
c8c27020 4use Test::Exception;
7b6a8d73 5use Try::Tiny;
c8c27020 6use lib qw(t/lib);
7use make_dbictest_db;
8
9use DBIx::Class::Schema::Loader;
10
59388920 11my $schema_counter = 0;
12
c8c27020 13# test skip_relationships
14my $regular = schema_with();
15is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH',
16 'regularly-made schema has fooref rel',
494e0205 17);
c8c27020 18my $skip_rel = schema_with( skip_relationships => 1 );
19is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef,
20 'skip_relationships blocks generation of fooref rel',
494e0205 21);
c8c27020 22
59388920 23# test hashref as rel_name_map
24my $hash_relationship = schema_with(
25 rel_name_map => {
26 fooref => "got_fooref",
27 bars => "ignored",
28 Foo => {
29 bars => "got_bars",
30 fooref => "ignored",
31 },
32 }
33);
34is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')),
35 'HASH',
36 'single level hash in rel_name_map picked up correctly'
494e0205 37);
59388920 38is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
39 'HASH',
40 'double level hash in rel_name_map picked up correctly'
494e0205 41);
59388920 42
43# test coderef as rel_name_map
44my $code_relationship = schema_with(
45 rel_name_map => sub {
c4d629ab 46 my ($args, $orig) = @_;
59388920 47
48 if ($args->{local_moniker} eq 'Foo') {
49 is_deeply(
50 $args,
51 {
494e0205 52 name => 'bars',
53 type => 'has_many',
59388920 54 local_class =>
55 "DBICTest::Schema::${schema_counter}::Result::Foo",
494e0205 56 local_moniker => 'Foo',
57 local_columns => ['fooid'],
59388920 58 remote_class =>
59 "DBICTest::Schema::${schema_counter}::Result::Bar",
494e0205 60 remote_moniker => 'Bar',
61 remote_columns => ['fooref'],
62 },
63 'correct args for Foo passed'
64 );
59388920 65 }
494e0205 66 elsif ($args->{local_moniker} eq 'Bar') {
59388920 67 is_deeply(
68 $args,
69 {
494e0205 70 name => 'fooref',
71 type => 'belongs_to',
59388920 72 local_class =>
73 "DBICTest::Schema::${schema_counter}::Result::Bar",
494e0205 74 local_moniker => 'Bar',
75 local_columns => ['fooref'],
59388920 76 remote_class =>
77 "DBICTest::Schema::${schema_counter}::Result::Foo",
494e0205 78 remote_moniker => 'Foo',
79 remote_columns => ['fooid'],
80 },
81 'correct args for Foo passed'
82 );
83 }
c4d629ab 84 else {
85 fail( 'correct args passed to rel_name_map' );
86 diag "args were: ", explain $args;
87 }
494e0205 88 return $orig->({
89 Bar => { fooref => 'fooref_caught' },
90 Foo => { bars => 'bars_caught' },
91 });
59388920 92 }
494e0205 93);
59388920 94is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
95 'HASH',
96 'rel_name_map overrode local_info correctly'
494e0205 97);
59388920 98is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
99 'HASH',
100 'rel_name_map overrode remote_info correctly'
494e0205 101);
59388920 102
c4d629ab 103throws_ok {
104 schema_with( rel_name_map => sub { $_[-1]->(sub{}) } ),
105} qr/reentered rel_name_map must be a hashref/, 'throws error for invalid (code) rel_name_map callback map';
59388920 106
c8c27020 107
108# test relationship_attrs
109throws_ok {
110 schema_with( relationship_attrs => 'laughably invalid!!!' );
7b6a8d73 111} qr/relationship_attrs/, 'throws error for invalid (scalar) relationship_attrs';
c8c27020 112
7b6a8d73 113throws_ok {
114 schema_with( relationship_attrs => [qw/laughably invalid/] );
115} qr/relationship_attrs/, 'throws error for invalid (arrayref) relationship_attrs';
c8c27020 116
117{
494e0205 118 my $nodelete = schema_with( relationship_attrs => {
119 all => { cascade_delete => 0 },
120 belongs_to => { cascade_delete => 1 },
121 });
c8c27020 122
123 my $bars_info = $nodelete->source('Foo')->relationship_info('bars');
124 #use Data::Dumper;
125 #die Dumper([ $nodelete->source('Foo')->relationships() ]);
126 my $fooref_info = $nodelete->source('Bar')->relationship_info('fooref');
127 is( ref($fooref_info), 'HASH',
494e0205 128 'fooref rel is present',
129 );
c8c27020 130 is( $bars_info->{attrs}->{cascade_delete}, 0,
494e0205 131 'relationship_attrs settings seem to be getting through to the generated rels',
132 );
c8c27020 133 is( $fooref_info->{attrs}->{cascade_delete}, 1,
494e0205 134 'belongs_to in relationship_attrs overrides all def',
135 );
c8c27020 136}
137
7b6a8d73 138# test relationship_attrs coderef
139{
140 my $relationship_attrs_coderef_invoked = 0;
141 my $schema;
142
143 lives_ok {
144 $schema = schema_with(relationship_attrs => sub {
145 my %p = @_;
146
147 $relationship_attrs_coderef_invoked++;
148
149 if ($p{rel_name} eq 'bars') {
3492170f 150 is $p{rel_type}, 'has_many', 'correct rel_type';
7b6a8d73 151 is $p{local_table}, 'foo', 'correct local_table';
152 is_deeply $p{local_cols}, [ 'fooid' ], 'correct local_cols';
153 is $p{remote_table}, 'bar', 'correct remote_table';
154 is_deeply $p{remote_cols}, [ 'fooref' ], 'correct remote_cols';
155 is_deeply $p{attrs}, {
156 cascade_delete => 0,
157 cascade_copy => 0,
158 }, "got default rel attrs for $p{rel_name} in $p{local_table}";
159
160 like $p{local_source}->result_class,
161 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
162 'correct local source';
163
164 like $p{remote_source}->result_class,
165 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
166 'correct remote source';
494e0205 167
7b6a8d73 168 $p{attrs}{snoopy} = 1;
169
170 return $p{attrs};
171 }
172 elsif ($p{rel_name} eq 'fooref') {
3492170f 173 is $p{rel_type}, 'belongs_to', 'correct rel_type';
7b6a8d73 174 is $p{local_table}, 'bar', 'correct local_table';
175 is_deeply $p{local_cols}, [ 'fooref' ], 'correct local_cols';
176 is $p{remote_table}, 'foo', 'correct remote_table';
177 is_deeply $p{remote_cols}, [ 'fooid' ], 'correct remote_cols';
178 is_deeply $p{attrs}, {
179 on_delete => 'NO ACTION',
180 on_update => 'NO ACTION',
181 is_deferrable => 0,
182 }, "got correct rel attrs for $p{rel_name} in $p{local_table}";
183
184 like $p{local_source}->result_class,
185 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
186 'correct local source';
187
188 like $p{remote_source}->result_class,
189 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
190 'correct remote source';
494e0205 191
7b6a8d73 192 $p{attrs}{scooby} = 1;
193
194 return $p{attrs};
195 }
196 else {
197 fail "unknown rel $p{rel_name} in $p{local_table}";
198 }
199 });
200 } 'dumping schema with coderef relationship_attrs survived';
201
202 is $relationship_attrs_coderef_invoked, 2,
203 'relationship_attrs coderef was invoked correct number of times';
204
205 is ((try { $schema->source('Foo')->relationship_info('bars')->{attrs}{snoopy} }) || undef, 1,
206 "correct relationship attributes for 'bars' in 'Foo'");
207
208 is ((try { $schema->source('Bar')->relationship_info('fooref')->{attrs}{scooby} }) || undef, 1,
209 "correct relationship attributes for 'fooref' in 'Bar'");
210}
211
212done_testing;
213
c8c27020 214#### generates a new schema with the given opts every time it's called
c8c27020 215sub schema_with {
216 $schema_counter++;
217 DBIx::Class::Schema::Loader::make_schema_at(
218 'DBICTest::Schema::'.$schema_counter,
219 { naming => 'current', @_ },
220 [ $make_dbictest_db::dsn ],
221 );
222 "DBICTest::Schema::$schema_counter"->clone;
223}