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