8 use DBIx::Class::Schema::Loader;
10 my $schema_counter = 0;
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',
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',
22 # test hashref as rel_name_map
23 my $hash_relationship = schema_with(
25 fooref => "got_fooref",
33 is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')),
35 'single level hash in rel_name_map picked up correctly'
37 is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
39 'double level hash in rel_name_map picked up correctly'
42 # test coderef as rel_name_map
43 my $code_relationship = schema_with(
47 if ($args->{local_moniker} eq 'Foo') {
54 "DBICTest::Schema::${schema_counter}::Result::Foo",
55 local_moniker => 'Foo',
56 local_columns => ['fooid'],
58 "DBICTest::Schema::${schema_counter}::Result::Bar",
59 remote_moniker => 'Bar',
60 remote_columns => ['fooref'],
62 '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'
84 return 'fooref_caught';
88 is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
90 'rel_name_map overrode local_info correctly'
92 is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
94 'rel_name_map overrode remote_info correctly'
99 # test relationship_attrs
101 schema_with( relationship_attrs => 'laughably invalid!!!' );
102 } qr/relationship_attrs/, 'throws error for invalid (scalar) relationship_attrs';
105 schema_with( relationship_attrs => [qw/laughably invalid/] );
106 } qr/relationship_attrs/, 'throws error for invalid (arrayref) relationship_attrs';
109 my $nodelete = schema_with( relationship_attrs =>
111 all => { cascade_delete => 0 },
112 belongs_to => { cascade_delete => 1 },
116 my $bars_info = $nodelete->source('Foo')->relationship_info('bars');
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',
123 is( $bars_info->{attrs}->{cascade_delete}, 0,
124 'relationship_attrs settings seem to be getting through to the generated rels',
126 is( $fooref_info->{attrs}->{cascade_delete}, 1,
127 'belongs_to in relationship_attrs overrides all def',
131 # test relationship_attrs coderef
133 my $relationship_attrs_coderef_invoked = 0;
137 $schema = schema_with(relationship_attrs => sub {
140 $relationship_attrs_coderef_invoked++;
142 if ($p{rel_name} eq 'bars') {
143 is $p{local_table}, 'foo', 'correct local_table';
144 is_deeply $p{local_cols}, [ 'fooid' ], 'correct local_cols';
145 is $p{remote_table}, 'bar', 'correct remote_table';
146 is_deeply $p{remote_cols}, [ 'fooref' ], 'correct remote_cols';
147 is_deeply $p{attrs}, {
150 }, "got default rel attrs for $p{rel_name} in $p{local_table}";
152 like $p{local_source}->result_class,
153 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
154 'correct local source';
156 like $p{remote_source}->result_class,
157 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
158 'correct remote source';
160 $p{attrs}{snoopy} = 1;
164 elsif ($p{rel_name} eq 'fooref') {
165 is $p{local_table}, 'bar', 'correct local_table';
166 is_deeply $p{local_cols}, [ 'fooref' ], 'correct local_cols';
167 is $p{remote_table}, 'foo', 'correct remote_table';
168 is_deeply $p{remote_cols}, [ 'fooid' ], 'correct remote_cols';
169 is_deeply $p{attrs}, {
170 on_delete => 'NO ACTION',
171 on_update => 'NO ACTION',
173 }, "got correct rel attrs for $p{rel_name} in $p{local_table}";
175 like $p{local_source}->result_class,
176 qr/^DBICTest::Schema::\d+::Result::Bar\z/,
177 'correct local source';
179 like $p{remote_source}->result_class,
180 qr/^DBICTest::Schema::\d+::Result::Foo\z/,
181 'correct remote source';
183 $p{attrs}{scooby} = 1;
188 fail "unknown rel $p{rel_name} in $p{local_table}";
191 } 'dumping schema with coderef relationship_attrs survived';
193 is $relationship_attrs_coderef_invoked, 2,
194 'relationship_attrs coderef was invoked correct number of times';
196 is ((try { $schema->source('Foo')->relationship_info('bars')->{attrs}{snoopy} }) || undef, 1,
197 "correct relationship attributes for 'bars' in 'Foo'");
199 is ((try { $schema->source('Bar')->relationship_info('fooref')->{attrs}{scooby} }) || undef, 1,
200 "correct relationship attributes for 'fooref' in 'Bar'");
205 #### generates a new schema with the given opts every time it's called
208 DBIx::Class::Schema::Loader::make_schema_at(
209 'DBICTest::Schema::'.$schema_counter,
210 { naming => 'current', @_ },
211 [ $make_dbictest_db::dsn ],
213 "DBICTest::Schema::$schema_counter"->clone;