don't use eq_set in test
[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_name (@sources) {
42     my $table = get_table($sqlt_schema, $schema, $source_name);
43
44     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
45     my @indices = $table->get_indices;
46
47     my $index_count = scalar(@indices);
48     $index_count++ if ($source_name eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
49     is($index_count, $fk_count, "correct number of indices for $source_name with no args");
50     
51     for my $index (@indices) {
52         my $source = $schema->source($source_name);
53         my $pk_test = join("\x00", sort $source->primary_columns);
54         my $idx_test = join("\x00", sort $index->fields);
55         isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
56     }
57   }
58 }
59
60
61   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
62
63   foreach my $source (@sources) {
64     my $table = get_table($sqlt_schema, $schema, $source);
65
66     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
67     my @indices = $table->get_indices;
68     my $index_count = scalar(@indices);
69     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
70     is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
71   }
72 }
73
74
75   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
76
77   foreach my $source (@sources) {
78     my $table = get_table($sqlt_schema, $schema, $source);
79
80     my @indices = $table->get_indices;
81     my $index_count = scalar(@indices);
82     is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
83   }
84 }
85
86
87     {
88         package # hide from PAUSE
89             DBICTest::Schema::NoViewDefinition;
90
91         use base qw/DBICTest::BaseResult/;
92
93         __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
94         __PACKAGE__->table('noviewdefinition');
95
96         1;
97     }
98
99     my $schema_invalid_view = $schema->clone;
100     $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
101
102     throws_ok { create_schema({ schema => $schema_invalid_view }) }
103         qr/view noviewdefinition is missing a view_definition/,
104         'parser detects views with a view_definition';
105 }
106
107 lives_ok (sub {
108   my $sqlt_schema = create_schema ({
109     schema => $schema,
110     args => {
111       parser_args => {
112         sources => ['CD']
113       },
114     },
115   });
116
117   is_deeply (
118     [$sqlt_schema->get_tables ],
119     ['cd'],
120     'sources limitng with relationships works',
121   );
122
123 });
124
125 done_testing;
126
127 sub create_schema {
128   my $args = shift;
129
130   my $schema = $args->{schema};
131   my $additional_sqltargs = $args->{args} || {};
132
133   my $sqltargs = {
134     add_drop_table => 1, 
135     ignore_constraint_names => 1,
136     ignore_index_names => 1,
137     %{$additional_sqltargs}
138   };
139
140   my $sqlt = SQL::Translator->new( $sqltargs );
141
142   $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
143   return $sqlt->translate({ data => $schema }) || die $sqlt->error;
144 }
145
146 sub get_table {
147     my ($sqlt_schema, $schema, $source) = @_;
148
149     my $table_name = $schema->source($source)->from;
150     $table_name    = $$table_name if ref $table_name;
151
152     return $sqlt_schema->get_table($table_name);
153 }