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