Commit | Line | Data |
0e2c6809 |
1 | use strict; |
2 | use warnings; |
206d1995 |
3 | |
0e2c6809 |
4 | use Test::More; |
8fd10683 |
5 | use Test::Exception; |
06e15b8e |
6 | use Scalar::Util (); |
0e2c6809 |
7 | |
8 | BEGIN { |
2527233b |
9 | require DBIx::Class; |
7f6f5b69 |
10 | plan skip_all => |
2527233b |
11 | 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') |
12 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy') |
0e2c6809 |
13 | } |
14 | |
68de9438 |
15 | use lib qw(t/lib); |
16 | use DBICTest; |
17 | use DBICTest::Schema; |
18 | |
06e15b8e |
19 | # Test for SQLT-related leaks |
20 | { |
21 | my $s = DBICTest::Schema->clone; |
02730621 |
22 | my $sqlt_schema = create_schema ({ schema => $s }); |
06e15b8e |
23 | Scalar::Util::weaken ($s); |
24 | |
25 | ok (!$s, 'Schema not leaked'); |
02730621 |
26 | |
27 | isa_ok ($sqlt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced'); |
06e15b8e |
28 | } |
29 | |
02730621 |
30 | # make sure classname-style works |
31 | lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') }; |
32 | |
06e15b8e |
33 | |
0e2c6809 |
34 | my $schema = DBICTest->init_schema(); |
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 | # |
39 | my @sources = grep |
d6c322f8 |
40 | { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x } |
8e8a8d91 |
41 | $schema->sources |
42 | ; |
0fc7cd47 |
43 | |
f0ac764e |
44 | my $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 |
120 | lives_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 | |
7f6f5b69 |
138 | done_testing; |
139 | |
0e2c6809 |
140 | sub create_schema { |
206d1995 |
141 | my $args = shift; |
0e2c6809 |
142 | |
206d1995 |
143 | my $schema = $args->{schema}; |
144 | my $additional_sqltargs = $args->{args} || {}; |
0e2c6809 |
145 | |
206d1995 |
146 | my $sqltargs = { |
147 | add_drop_table => 1, |
148 | ignore_constraint_names => 1, |
149 | ignore_index_names => 1, |
150 | %{$additional_sqltargs} |
151 | }; |
0e2c6809 |
152 | |
206d1995 |
153 | my $sqlt = SQL::Translator->new( $sqltargs ); |
0e2c6809 |
154 | |
206d1995 |
155 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
156 | return $sqlt->translate({ data => $schema }) || die $sqlt->error; |
0e2c6809 |
157 | } |
c2b7c5dc |
158 | |
159 | sub get_table { |
160 | my ($sqlt_schema, $schema, $source) = @_; |
161 | |
162 | my $table_name = $schema->source($source)->from; |
163 | $table_name = $$table_name if ref $table_name; |
164 | |
165 | return $sqlt_schema->get_table($table_name); |
166 | } |