9 use DBIx::Class::Schema::Loader;
11 my $schema_counter = 0;
13 # test skip_relationships
14 my $regular = schema_with();
15 is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH',
16 'regularly-made schema has fooref rel',
18 my $skip_rel = schema_with( skip_relationships => 1 );
19 is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef,
20 'skip_relationships blocks generation of fooref rel',
23 # test hashref as rel_name_map
24 my $hash_relationship = schema_with(
26 fooref => "got_fooref",
34 is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')),
36 'single level hash in rel_name_map picked up correctly'
38 is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
40 'double level hash in rel_name_map picked up correctly'
43 # test coderef as rel_name_map
44 my $code_relationship = schema_with(
46 my ($args, $orig) = @_;
48 if ($args->{local_moniker} eq 'Foo') {
55 "DBICTest::Schema::${schema_counter}::Result::Foo",
56 local_moniker => 'Foo',
57 local_columns => ['fooid'],
59 "DBICTest::Schema::${schema_counter}::Result::Bar",
60 remote_moniker => 'Bar',
61 remote_columns => ['fooref'],
63 'correct args for Foo passed'
66 elsif ($args->{local_moniker} eq 'Bar') {
73 "DBICTest::Schema::${schema_counter}::Result::Bar",
74 local_moniker => 'Bar',
75 local_columns => ['fooref'],
77 "DBICTest::Schema::${schema_counter}::Result::Foo",
78 remote_moniker => 'Foo',
79 remote_columns => ['fooid'],
81 'correct args for Foo passed'
85 fail( 'correct args passed to rel_name_map' );
86 diag "args were: ", explain $args;
89 Bar => { fooref => 'fooref_caught' },
90 Foo => { bars => 'bars_caught' },
94 is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
96 'rel_name_map overrode local_info correctly'
98 is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
100 'rel_name_map overrode remote_info correctly'
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';
108 # test relationship_attrs
110 schema_with( relationship_attrs => 'laughably invalid!!!' );
111 } qr/relationship_attrs/, 'throws error for invalid (scalar) relationship_attrs';
114 schema_with( relationship_attrs => [qw/laughably invalid/] );
115 } qr/relationship_attrs/, 'throws error for invalid (arrayref) relationship_attrs';
118 my $nodelete = schema_with( relationship_attrs => {
119 all => { cascade_delete => 0 },
120 belongs_to => { cascade_delete => 1 },
123 my $bars_info = $nodelete->source('Foo')->relationship_info('bars');
125 #die Dumper([ $nodelete->source('Foo')->relationships() ]);
126 my $fooref_info = $nodelete->source('Bar')->relationship_info('fooref');
127 is( ref($fooref_info), 'HASH',
128 'fooref rel is present',
130 is( $bars_info->{attrs}->{cascade_delete}, 0,
131 'relationship_attrs settings seem to be getting through to the generated rels',
133 is( $fooref_info->{attrs}->{cascade_delete}, 1,
134 'belongs_to in relationship_attrs overrides all def',
138 # test relationship_attrs coderef
140 my $relationship_attrs_coderef_invoked = 0;
144 $schema = schema_with(relationship_attrs => sub {
147 $relationship_attrs_coderef_invoked++;
149 if ($p{rel_name} eq 'bars') {
150 is $p{rel_type}, 'has_many', 'correct rel_type';
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}, {
158 }, "got default rel attrs for $p{rel_name} in $p{local_table}";
160 like $p{local_source}->result_class,
161 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
162 'correct local source';
164 like $p{remote_source}->result_class,
165 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
166 'correct remote source';
168 $p{attrs}{snoopy} = 1;
172 elsif ($p{rel_name} eq 'fooref') {
173 is $p{rel_type}, 'belongs_to', 'correct rel_type';
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',
182 }, "got correct rel attrs for $p{rel_name} in $p{local_table}";
184 like $p{local_source}->result_class,
185 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
186 'correct local source';
188 like $p{remote_source}->result_class,
189 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
190 'correct remote source';
192 $p{attrs}{scooby} = 1;
197 fail "unknown rel $p{rel_name} in $p{local_table}";
200 } 'dumping schema with coderef relationship_attrs survived';
202 is $relationship_attrs_coderef_invoked, 2,
203 'relationship_attrs coderef was invoked correct number of times';
205 is ((try { $schema->source('Foo')->relationship_info('bars')->{attrs}{snoopy} }) || undef, 1,
206 "correct relationship attributes for 'bars' in 'Foo'");
208 is ((try { $schema->source('Bar')->relationship_info('fooref')->{attrs}{scooby} }) || undef, 1,
209 "correct relationship attributes for 'fooref' in 'Bar'");
214 #### generates a new schema with the given opts every time it's called
217 DBIx::Class::Schema::Loader::make_schema_at(
218 'DBICTest::Schema::'.$schema_counter,
219 { naming => 'current', @_ },
220 [ $make_dbictest_db::dsn ],
222 "DBICTest::Schema::$schema_counter"->clone;