Add author test for use strict/warnings
[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',
17 );
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',
21 );
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'
37 );
38is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
39 'HASH',
40 'double level hash in rel_name_map picked up correctly'
41 );
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 {
52 name => 'bars',
53 type => 'has_many',
54 local_class =>
55 "DBICTest::Schema::${schema_counter}::Result::Foo",
56 local_moniker => 'Foo',
57 local_columns => ['fooid'],
58 remote_class =>
59 "DBICTest::Schema::${schema_counter}::Result::Bar",
60 remote_moniker => 'Bar',
61 remote_columns => ['fooref'],
62 },
63 'correct args for Foo passed'
64 );
59388920 65 }
66 elsif ($args->{local_moniker} eq 'Bar') {
67 is_deeply(
68 $args,
69 {
70 name => 'fooref',
71 type => 'belongs_to',
72 local_class =>
73 "DBICTest::Schema::${schema_counter}::Result::Bar",
74 local_moniker => 'Bar',
75 local_columns => ['fooref'],
76 remote_class =>
77 "DBICTest::Schema::${schema_counter}::Result::Foo",
78 remote_moniker => 'Foo',
79 remote_columns => ['fooid'],
80 },
81 'correct args for Foo passed'
82 );
59388920 83 }
c4d629ab 84 else {
85 fail( 'correct args passed to rel_name_map' );
86 diag "args were: ", explain $args;
87 }
88 return $orig->({
89 Bar => { fooref => 'fooref_caught' },
90 Foo => { bars => 'bars_caught' },
91 });
59388920 92 }
93 );
94is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
95 'HASH',
96 'rel_name_map overrode local_info correctly'
97 );
98is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
99 'HASH',
100 'rel_name_map overrode remote_info correctly'
101 );
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{
118 my $nodelete = schema_with( relationship_attrs =>
119 {
120 all => { cascade_delete => 0 },
121 belongs_to => { cascade_delete => 1 },
122 },
123 );
124
125 my $bars_info = $nodelete->source('Foo')->relationship_info('bars');
126 #use Data::Dumper;
127 #die Dumper([ $nodelete->source('Foo')->relationships() ]);
128 my $fooref_info = $nodelete->source('Bar')->relationship_info('fooref');
129 is( ref($fooref_info), 'HASH',
130 'fooref rel is present',
131 );
132 is( $bars_info->{attrs}->{cascade_delete}, 0,
133 'relationship_attrs settings seem to be getting through to the generated rels',
134 );
135 is( $fooref_info->{attrs}->{cascade_delete}, 1,
136 'belongs_to in relationship_attrs overrides all def',
137 );
138}
139
7b6a8d73 140# test relationship_attrs coderef
141{
142 my $relationship_attrs_coderef_invoked = 0;
143 my $schema;
144
145 lives_ok {
146 $schema = schema_with(relationship_attrs => sub {
147 my %p = @_;
148
149 $relationship_attrs_coderef_invoked++;
150
151 if ($p{rel_name} eq 'bars') {
3492170f 152 is $p{rel_type}, 'has_many', 'correct rel_type';
7b6a8d73 153 is $p{local_table}, 'foo', 'correct local_table';
154 is_deeply $p{local_cols}, [ 'fooid' ], 'correct local_cols';
155 is $p{remote_table}, 'bar', 'correct remote_table';
156 is_deeply $p{remote_cols}, [ 'fooref' ], 'correct remote_cols';
157 is_deeply $p{attrs}, {
158 cascade_delete => 0,
159 cascade_copy => 0,
160 }, "got default rel attrs for $p{rel_name} in $p{local_table}";
161
162 like $p{local_source}->result_class,
163 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
164 'correct local source';
165
166 like $p{remote_source}->result_class,
167 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
168 'correct remote source';
169
170 $p{attrs}{snoopy} = 1;
171
172 return $p{attrs};
173 }
174 elsif ($p{rel_name} eq 'fooref') {
3492170f 175 is $p{rel_type}, 'belongs_to', 'correct rel_type';
7b6a8d73 176 is $p{local_table}, 'bar', 'correct local_table';
177 is_deeply $p{local_cols}, [ 'fooref' ], 'correct local_cols';
178 is $p{remote_table}, 'foo', 'correct remote_table';
179 is_deeply $p{remote_cols}, [ 'fooid' ], 'correct remote_cols';
180 is_deeply $p{attrs}, {
181 on_delete => 'NO ACTION',
182 on_update => 'NO ACTION',
183 is_deferrable => 0,
184 }, "got correct rel attrs for $p{rel_name} in $p{local_table}";
185
186 like $p{local_source}->result_class,
187 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
188 'correct local source';
189
190 like $p{remote_source}->result_class,
191 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
192 'correct remote source';
193
194 $p{attrs}{scooby} = 1;
195
196 return $p{attrs};
197 }
198 else {
199 fail "unknown rel $p{rel_name} in $p{local_table}";
200 }
201 });
202 } 'dumping schema with coderef relationship_attrs survived';
203
204 is $relationship_attrs_coderef_invoked, 2,
205 'relationship_attrs coderef was invoked correct number of times';
206
207 is ((try { $schema->source('Foo')->relationship_info('bars')->{attrs}{snoopy} }) || undef, 1,
208 "correct relationship attributes for 'bars' in 'Foo'");
209
210 is ((try { $schema->source('Bar')->relationship_info('fooref')->{attrs}{scooby} }) || undef, 1,
211 "correct relationship attributes for 'fooref' in 'Bar'");
212}
213
214done_testing;
215
c8c27020 216#### generates a new schema with the given opts every time it's called
c8c27020 217sub schema_with {
218 $schema_counter++;
219 DBIx::Class::Schema::Loader::make_schema_at(
220 'DBICTest::Schema::'.$schema_counter,
221 { naming => 'current', @_ },
222 [ $make_dbictest_db::dsn ],
223 );
224 "DBICTest::Schema::$schema_counter"->clone;
225}