Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
cb551b07 |
2 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy'; |
3 | |
0e2c6809 |
4 | use strict; |
5 | use warnings; |
206d1995 |
6 | |
8b60b921 |
7 | BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 } |
8 | |
0e2c6809 |
9 | use Test::More; |
a07ae2aa |
10 | use Test::Warn; |
8fd10683 |
11 | use Test::Exception; |
06e15b8e |
12 | use Scalar::Util (); |
0e2c6809 |
13 | |
c0329273 |
14 | |
4bea1fe7 |
15 | use DBICTest; |
052a832c |
16 | use DBIx::Class::_Util 'sigwarn_silencer'; |
4bea1fe7 |
17 | |
06e15b8e |
18 | # Test for SQLT-related leaks |
19 | { |
20 | my $s = DBICTest::Schema->clone; |
48775dd1 |
21 | |
22 | my @schemas = ( |
23 | create_schema ({ schema => $s }), |
a07ae2aa |
24 | create_schema ({ args => { parser_args => { dbic_schema => $s } } }), |
48775dd1 |
25 | ); |
26 | |
a07ae2aa |
27 | for my $parser_args_key (qw( |
28 | DBIx::Class::Schema |
29 | DBIx::Schema |
30 | package |
31 | )) { |
32 | warnings_exist { |
33 | push @schemas, create_schema({ |
34 | args => { parser_args => { $parser_args_key => $s } } |
35 | }); |
569b96bb |
36 | } qr/\Qparser_args => {\E.+?is deprecated.+\Q@{[__FILE__]}/, |
a07ae2aa |
37 | "deprecated crazy parser_arg '$parser_args_key' warned"; |
38 | } |
39 | |
06e15b8e |
40 | Scalar::Util::weaken ($s); |
41 | |
42 | ok (!$s, 'Schema not leaked'); |
02730621 |
43 | |
48775dd1 |
44 | isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced") |
45 | for @schemas; |
06e15b8e |
46 | } |
47 | |
02730621 |
48 | # make sure classname-style works |
49 | lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') }; |
50 | |
31399b48 |
51 | # make sure a connected instance passed via $args does not get the $dbh improperly serialized |
52 | SKIP: { |
53 | |
461e818a |
54 | DBIx::Class::Optional::Dependencies->skip_without( 'YAML>=0' ); |
31399b48 |
55 | |
56 | lives_ok { |
57 | |
58 | my $s = DBICTest->init_schema(no_populate => 1); |
59 | ok ($s->storage->connected, '$schema instance connected'); |
60 | |
61 | # roundtrip through YAML |
62 | my $yaml_rt_schema = SQL::Translator->new( |
63 | parser => 'SQL::Translator::Parser::YAML' |
64 | )->translate( |
65 | data => SQL::Translator->new( |
569b96bb |
66 | parser_args => { dbic_schema => $s }, |
31399b48 |
67 | parser => 'SQL::Translator::Parser::DBIx::Class', |
68 | producer => 'SQL::Translator::Producer::YAML', |
69 | )->translate |
70 | ); |
71 | |
72 | isa_ok ( $yaml_rt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced after YAML roundtrip'); |
73 | |
74 | ok ($s->storage->connected, '$schema instance still connected'); |
75 | } |
76 | |
77 | eval <<'EOE' or die $@; |
78 | END { |
052a832c |
79 | # we are in END - everything remains global |
80 | # |
31399b48 |
81 | $^W = 1; # important, otherwise DBI won't trip the next fail() |
82 | $SIG{__WARN__} = sub { |
83 | fail "Unexpected global destruction warning" |
84 | if $_[0] =~ /is not a DBI/; |
85 | warn @_; |
86 | }; |
87 | } |
88 | EOE |
89 | |
90 | } |
06e15b8e |
91 | |
6ddb4ac0 |
92 | my $schema = DBICTest->init_schema( no_deploy => 1 ); |
93 | |
0fc7cd47 |
94 | # Dummy was yanked out by the sqlt hook test |
8e8a8d91 |
95 | # CustomSql tests the horrific/deprecated ->name(\$sql) hack |
e0cd97a4 |
96 | # YearXXXXCDs are views |
8e8a8d91 |
97 | # |
98 | my @sources = grep |
d6c322f8 |
99 | { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x } |
8e8a8d91 |
100 | $schema->sources |
101 | ; |
0fc7cd47 |
102 | |
f0ac764e |
103 | my $idx_exceptions = { |
104 | 'Artwork' => -1, |
105 | 'ForceForeign' => -1, |
106 | 'LinerNotes' => -1, |
827a808f |
107 | 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def |
f0ac764e |
108 | }; |
109 | |
c49ff507 |
110 | { |
206d1995 |
111 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } }); |
0e2c6809 |
112 | |
620df7e8 |
113 | foreach my $source_name (@sources) { |
114 | my $table = get_table($sqlt_schema, $schema, $source_name); |
0e2c6809 |
115 | |
206d1995 |
116 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
f0ac764e |
117 | $fk_count += $idx_exceptions->{$source_name} || 0; |
206d1995 |
118 | my @indices = $table->get_indices; |
620df7e8 |
119 | |
206d1995 |
120 | my $index_count = scalar(@indices); |
620df7e8 |
121 | is($index_count, $fk_count, "correct number of indices for $source_name with no args"); |
c49ff507 |
122 | |
620df7e8 |
123 | for my $index (@indices) { |
124 | my $source = $schema->source($source_name); |
3eaae0f2 |
125 | my $pk_test = join("\x00", $source->primary_columns); |
126 | my $idx_test = join("\x00", $index->fields); |
c1092055 |
127 | isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name"); |
620df7e8 |
128 | } |
206d1995 |
129 | } |
0e2c6809 |
130 | } |
131 | |
c49ff507 |
132 | { |
206d1995 |
133 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } }); |
0e2c6809 |
134 | |
f0ac764e |
135 | foreach my $source_name (@sources) { |
136 | my $table = get_table($sqlt_schema, $schema, $source_name); |
0e2c6809 |
137 | |
206d1995 |
138 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
f0ac764e |
139 | $fk_count += $idx_exceptions->{$source_name} || 0; |
206d1995 |
140 | my @indices = $table->get_indices; |
141 | my $index_count = scalar(@indices); |
f0ac764e |
142 | is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1"); |
206d1995 |
143 | } |
0e2c6809 |
144 | } |
145 | |
c49ff507 |
146 | { |
206d1995 |
147 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } }); |
0e2c6809 |
148 | |
206d1995 |
149 | foreach my $source (@sources) { |
150 | my $table = get_table($sqlt_schema, $schema, $source); |
0e2c6809 |
151 | |
206d1995 |
152 | my @indices = $table->get_indices; |
153 | my $index_count = scalar(@indices); |
154 | is($index_count, 0, "correct number of indices for $source with add_fk_index => 0"); |
155 | } |
0e2c6809 |
156 | } |
157 | |
c49ff507 |
158 | { |
ab7e74aa |
159 | { |
160 | package # hide from PAUSE |
161 | DBICTest::Schema::NoViewDefinition; |
162 | |
163 | use base qw/DBICTest::BaseResult/; |
8f1617e2 |
164 | |
ab7e74aa |
165 | __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); |
166 | __PACKAGE__->table('noviewdefinition'); |
8f1617e2 |
167 | |
ab7e74aa |
168 | 1; |
8f1617e2 |
169 | } |
170 | |
ab7e74aa |
171 | my $schema_invalid_view = $schema->clone; |
172 | $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition'); |
8f1617e2 |
173 | |
8fd10683 |
174 | throws_ok { create_schema({ schema => $schema_invalid_view }) } |
175 | qr/view noviewdefinition is missing a view_definition/, |
176 | 'parser detects views with a view_definition'; |
8f1617e2 |
177 | } |
178 | |
a7f4b74c |
179 | lives_ok (sub { |
180 | my $sqlt_schema = create_schema ({ |
181 | schema => $schema, |
182 | args => { |
183 | parser_args => { |
184 | sources => ['CD'] |
185 | }, |
186 | }, |
187 | }); |
188 | |
189 | is_deeply ( |
190 | [$sqlt_schema->get_tables ], |
191 | ['cd'], |
192 | 'sources limitng with relationships works', |
193 | ); |
194 | |
195 | }); |
196 | |
5b9ecfcc |
197 | { |
198 | package DBICTest::PartialSchema; |
199 | |
200 | use base qw/DBIx::Class::Schema/; |
201 | |
202 | __PACKAGE__->load_classes( |
203 | { 'DBICTest::Schema' => [qw/ |
204 | CD |
205 | Track |
206 | Tag |
207 | Producer |
208 | CD_to_Producer |
209 | /]} |
210 | ); |
211 | } |
212 | |
213 | { |
214 | my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database); |
215 | |
216 | lives_ok (sub { |
217 | my $sqlt_schema = do { |
218 | |
052a832c |
219 | local $SIG{__WARN__} = sigwarn_silencer( |
220 | qr/Ignoring relationship .+ related resultsource .+ is not registered with this schema/ |
221 | ); |
5b9ecfcc |
222 | |
223 | create_schema({ schema => $partial_schema }); |
224 | }; |
225 | |
226 | my @tables = $sqlt_schema->get_tables; |
227 | |
228 | is_deeply ( |
229 | [sort map { $_->name } @tables], |
230 | [qw/cd cd_to_producer producer tags track/], |
231 | 'partial dbic schema parsing ok', |
232 | ); |
233 | |
234 | # the primary key is currently unnamed in sqlt - adding below |
235 | my %constraints_for_table = ( |
236 | producer => [qw/prod_name /], |
237 | tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /], |
238 | track => [qw/track_cd_position track_cd_title track_fk_cd /], |
239 | cd => [qw/cd_artist_title cd_fk_single_track /], |
240 | cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /], |
241 | ); |
242 | |
243 | for my $table (@tables) { |
244 | my $tablename = $table->name; |
245 | my @constraints = $table->get_constraints; |
246 | is_deeply ( |
247 | [ sort map { $_->name } @constraints ], |
248 | |
249 | # the primary key (present on all loaded tables) is currently named '' in sqlt |
250 | # subject to future changes |
251 | [ '', @{$constraints_for_table{$tablename}} ], |
252 | |
253 | "constraints of table '$tablename' ok", |
254 | ); |
255 | } |
256 | }, 'partial schema tests successful'); |
257 | } |
258 | |
0e14d918 |
259 | { |
260 | my $cd_rsrc = $schema->source('CD'); |
261 | $cd_rsrc->name(\'main.cd'); |
262 | |
263 | my $sqlt_schema = create_schema( |
264 | { schema => $schema }, |
265 | args => { ignore_constraint_names => 0, ignore_index_names => 0 } |
266 | ); |
267 | |
268 | foreach my $source_name (qw(CD)) { |
269 | my $table = get_table($sqlt_schema, $schema, $source_name); |
270 | ok( |
271 | !(grep {$_->name =~ m/main\./} $table->get_indices), |
272 | 'indices have periods stripped out' |
273 | ); |
274 | ok( |
275 | !(grep {$_->name =~ m/main\./} $table->get_constraints), |
276 | 'constraints have periods stripped out' |
277 | ); |
278 | } |
279 | } |
280 | |
7f6f5b69 |
281 | done_testing; |
282 | |
0e2c6809 |
283 | sub create_schema { |
206d1995 |
284 | my $args = shift; |
0e2c6809 |
285 | |
206d1995 |
286 | my $additional_sqltargs = $args->{args} || {}; |
0e2c6809 |
287 | |
206d1995 |
288 | my $sqltargs = { |
8273e845 |
289 | add_drop_table => 1, |
206d1995 |
290 | ignore_constraint_names => 1, |
291 | ignore_index_names => 1, |
292 | %{$additional_sqltargs} |
293 | }; |
0e2c6809 |
294 | |
206d1995 |
295 | my $sqlt = SQL::Translator->new( $sqltargs ); |
0e2c6809 |
296 | |
206d1995 |
297 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
a07ae2aa |
298 | return $sqlt->translate( |
299 | $args->{schema} ? ( data => $args->{schema} ) : () |
300 | ) || die $sqlt->error; |
0e2c6809 |
301 | } |
c2b7c5dc |
302 | |
303 | sub get_table { |
304 | my ($sqlt_schema, $schema, $source) = @_; |
305 | |
306 | my $table_name = $schema->source($source)->from; |
307 | $table_name = $$table_name if ref $table_name; |
308 | |
309 | return $sqlt_schema->get_table($table_name); |
310 | } |