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