Changelog for Changes to support multiple step schema version updates
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBICTest::Schema;
9 use Scalar::Util ();
10
11 BEGIN {
12   require DBIx::Class::Storage::DBI;
13   plan skip_all =>
14       'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
15     if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
16 }
17
18 # Test for SQLT-related leaks
19 {
20   my $s = DBICTest::Schema->clone;
21   create_schema ({ schema => $s });
22   Scalar::Util::weaken ($s);
23
24   ok (!$s, 'Schema not leaked');
25 }
26
27
28 my $schema = DBICTest->init_schema();
29 # Dummy was yanked out by the sqlt hook test
30 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
31 # YearXXXXCDs are views
32 #
33 my @sources = grep
34   { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
35   $schema->sources
36 ;
37
38
39   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
40
41   foreach my $source (@sources) {
42     my $table = get_table($sqlt_schema, $schema, $source);
43
44     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
45     my @indices = $table->get_indices;
46     my $index_count = scalar(@indices);
47     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
48     is($index_count, $fk_count, "correct number of indices for $source with no args");
49   }
50 }
51
52
53   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
54
55   foreach my $source (@sources) {
56     my $table = get_table($sqlt_schema, $schema, $source);
57
58     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
59     my @indices = $table->get_indices;
60     my $index_count = scalar(@indices);
61     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
62     is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
63   }
64 }
65
66
67   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
68
69   foreach my $source (@sources) {
70     my $table = get_table($sqlt_schema, $schema, $source);
71
72     my @indices = $table->get_indices;
73     my $index_count = scalar(@indices);
74     is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
75   }
76 }
77
78
79     {
80         package # hide from PAUSE
81             DBICTest::Schema::NoViewDefinition;
82
83         use base qw/DBICTest::BaseResult/;
84
85         __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
86         __PACKAGE__->table('noviewdefinition');
87
88         1;
89     }
90
91     my $schema_invalid_view = $schema->clone;
92     $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
93
94     throws_ok { create_schema({ schema => $schema_invalid_view }) }
95         qr/view noviewdefinition is missing a view_definition/,
96         'parser detects views with a view_definition';
97 }
98
99 lives_ok (sub {
100   my $sqlt_schema = create_schema ({
101     schema => $schema,
102     args => {
103       parser_args => {
104         sources => ['CD']
105       },
106     },
107   });
108
109   is_deeply (
110     [$sqlt_schema->get_tables ],
111     ['cd'],
112     'sources limitng with relationships works',
113   );
114
115 });
116
117 done_testing;
118
119 sub create_schema {
120   my $args = shift;
121
122   my $schema = $args->{schema};
123   my $additional_sqltargs = $args->{args} || {};
124
125   my $sqltargs = {
126     add_drop_table => 1, 
127     ignore_constraint_names => 1,
128     ignore_index_names => 1,
129     %{$additional_sqltargs}
130   };
131
132   my $sqlt = SQL::Translator->new( $sqltargs );
133
134   $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
135   return $sqlt->translate({ data => $schema }) || die $sqlt->error;
136 }
137
138 sub get_table {
139     my ($sqlt_schema, $schema, $source) = @_;
140
141     my $table_name = $schema->source($source)->from;
142     $table_name    = $$table_name if ref $table_name;
143
144     return $sqlt_schema->get_table($table_name);
145 }