Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
CommitLineData
0e2c6809 1use strict;
2use warnings;
206d1995 3
0e2c6809 4use Test::More;
8fd10683 5use Test::Exception;
06e15b8e 6use Scalar::Util ();
0e2c6809 7
4bea1fe7 8use lib qw(t/lib);
9use DBICTest;
10
0e2c6809 11BEGIN {
2527233b 12 require DBIx::Class;
7f6f5b69 13 plan skip_all =>
2527233b 14 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
0e2c6809 16}
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 }),
24 create_schema ({ args => { parser_args => { 'DBIx::Class::Schema' => $s } } }),
25 create_schema ({ args => { parser_args => { 'DBIx::Schema' => $s } } }),
26 create_schema ({ args => { parser_args => { package => $s } } }),
27 );
28
06e15b8e 29 Scalar::Util::weaken ($s);
30
31 ok (!$s, 'Schema not leaked');
02730621 32
48775dd1 33 isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
34 for @schemas;
06e15b8e 35}
36
02730621 37# make sure classname-style works
38lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
39
06e15b8e 40
6ddb4ac0 41my $schema = DBICTest->init_schema( no_deploy => 1 );
42
0fc7cd47 43# Dummy was yanked out by the sqlt hook test
8e8a8d91 44# CustomSql tests the horrific/deprecated ->name(\$sql) hack
e0cd97a4 45# YearXXXXCDs are views
8e8a8d91 46#
47my @sources = grep
d6c322f8 48 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
8e8a8d91 49 $schema->sources
50;
0fc7cd47 51
f0ac764e 52my $idx_exceptions = {
53 'Artwork' => -1,
54 'ForceForeign' => -1,
55 'LinerNotes' => -1,
827a808f 56 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
f0ac764e 57};
58
c49ff507 59{
206d1995 60 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
0e2c6809 61
620df7e8 62 foreach my $source_name (@sources) {
63 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 64
206d1995 65 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
f0ac764e 66 $fk_count += $idx_exceptions->{$source_name} || 0;
206d1995 67 my @indices = $table->get_indices;
620df7e8 68
206d1995 69 my $index_count = scalar(@indices);
620df7e8 70 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
c49ff507 71
620df7e8 72 for my $index (@indices) {
73 my $source = $schema->source($source_name);
3eaae0f2 74 my $pk_test = join("\x00", $source->primary_columns);
75 my $idx_test = join("\x00", $index->fields);
c1092055 76 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
620df7e8 77 }
206d1995 78 }
0e2c6809 79}
80
c49ff507 81{
206d1995 82 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
0e2c6809 83
f0ac764e 84 foreach my $source_name (@sources) {
85 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 86
206d1995 87 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
f0ac764e 88 $fk_count += $idx_exceptions->{$source_name} || 0;
206d1995 89 my @indices = $table->get_indices;
90 my $index_count = scalar(@indices);
f0ac764e 91 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
206d1995 92 }
0e2c6809 93}
94
c49ff507 95{
206d1995 96 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
0e2c6809 97
206d1995 98 foreach my $source (@sources) {
99 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 100
206d1995 101 my @indices = $table->get_indices;
102 my $index_count = scalar(@indices);
103 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
104 }
0e2c6809 105}
106
c49ff507 107{
ab7e74aa 108 {
109 package # hide from PAUSE
110 DBICTest::Schema::NoViewDefinition;
111
112 use base qw/DBICTest::BaseResult/;
8f1617e2 113
ab7e74aa 114 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
115 __PACKAGE__->table('noviewdefinition');
8f1617e2 116
ab7e74aa 117 1;
8f1617e2 118 }
119
ab7e74aa 120 my $schema_invalid_view = $schema->clone;
121 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 122
8fd10683 123 throws_ok { create_schema({ schema => $schema_invalid_view }) }
124 qr/view noviewdefinition is missing a view_definition/,
125 'parser detects views with a view_definition';
8f1617e2 126}
127
a7f4b74c 128lives_ok (sub {
129 my $sqlt_schema = create_schema ({
130 schema => $schema,
131 args => {
132 parser_args => {
133 sources => ['CD']
134 },
135 },
136 });
137
138 is_deeply (
139 [$sqlt_schema->get_tables ],
140 ['cd'],
141 'sources limitng with relationships works',
142 );
143
144});
145
5b9ecfcc 146{
147 package DBICTest::PartialSchema;
148
149 use base qw/DBIx::Class::Schema/;
150
151 __PACKAGE__->load_classes(
152 { 'DBICTest::Schema' => [qw/
153 CD
154 Track
155 Tag
156 Producer
157 CD_to_Producer
158 /]}
159 );
160}
161
162{
163 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
164
165 lives_ok (sub {
166 my $sqlt_schema = do {
167
168 local $SIG{__WARN__} = sub {
169 warn @_
170 unless $_[0] =~ /Ignoring relationship .+ related resultsource .+ is not registered with this schema/
171 };
172
173 create_schema({ schema => $partial_schema });
174 };
175
176 my @tables = $sqlt_schema->get_tables;
177
178 is_deeply (
179 [sort map { $_->name } @tables],
180 [qw/cd cd_to_producer producer tags track/],
181 'partial dbic schema parsing ok',
182 );
183
184 # the primary key is currently unnamed in sqlt - adding below
185 my %constraints_for_table = (
186 producer => [qw/prod_name /],
187 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
188 track => [qw/track_cd_position track_cd_title track_fk_cd /],
189 cd => [qw/cd_artist_title cd_fk_single_track /],
190 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
191 );
192
193 for my $table (@tables) {
194 my $tablename = $table->name;
195 my @constraints = $table->get_constraints;
196 is_deeply (
197 [ sort map { $_->name } @constraints ],
198
199 # the primary key (present on all loaded tables) is currently named '' in sqlt
200 # subject to future changes
201 [ '', @{$constraints_for_table{$tablename}} ],
202
203 "constraints of table '$tablename' ok",
204 );
205 }
206 }, 'partial schema tests successful');
207}
208
7f6f5b69 209done_testing;
210
0e2c6809 211sub create_schema {
206d1995 212 my $args = shift;
0e2c6809 213
206d1995 214 my $schema = $args->{schema};
215 my $additional_sqltargs = $args->{args} || {};
0e2c6809 216
206d1995 217 my $sqltargs = {
8273e845 218 add_drop_table => 1,
206d1995 219 ignore_constraint_names => 1,
220 ignore_index_names => 1,
221 %{$additional_sqltargs}
222 };
0e2c6809 223
206d1995 224 my $sqlt = SQL::Translator->new( $sqltargs );
0e2c6809 225
206d1995 226 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
227 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 228}
c2b7c5dc 229
230sub get_table {
231 my ($sqlt_schema, $schema, $source) = @_;
232
233 my $table_name = $schema->source($source)->from;
234 $table_name = $$table_name if ref $table_name;
235
236 return $sqlt_schema->get_table($table_name);
237}