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