better pod thanks to ribasushi
[dbsrgits/DBIx-Class-Historic.git] / t / 99dbic_sqlt_parser.t
CommitLineData
0e2c6809 1use strict;
2use warnings;
206d1995 3
0e2c6809 4use Test::More;
8fd10683 5use Test::Exception;
0e2c6809 6use lib qw(t/lib);
7use DBICTest;
06e15b8e 8use DBICTest::Schema;
9use Scalar::Util ();
0e2c6809 10
11BEGIN {
b2b2e7fd 12 require DBIx::Class::Storage::DBI;
7f6f5b69 13 plan skip_all =>
b2b2e7fd 14 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
15 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
0e2c6809 16}
17
06e15b8e 18# Test for SQLT-related leaks
19{
20 my $s = DBICTest::Schema->clone;
a7acf09f 21 create_schema ({ schema => $s });
06e15b8e 22 Scalar::Util::weaken ($s);
23
24 ok (!$s, 'Schema not leaked');
25}
26
27
0e2c6809 28my $schema = DBICTest->init_schema();
0fc7cd47 29# Dummy was yanked out by the sqlt hook test
8e8a8d91 30# CustomSql tests the horrific/deprecated ->name(\$sql) hack
e0cd97a4 31# YearXXXXCDs are views
8e8a8d91 32#
33my @sources = grep
d6c322f8 34 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
8e8a8d91 35 $schema->sources
36;
0fc7cd47 37
0e2c6809 38{
206d1995 39 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
0e2c6809 40
620df7e8 41 foreach my $source_name (@sources) {
42 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 43
206d1995 44 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
45 my @indices = $table->get_indices;
620df7e8 46
206d1995 47 my $index_count = scalar(@indices);
620df7e8 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);
3eaae0f2 53 my $pk_test = join("\x00", $source->primary_columns);
54 my $idx_test = join("\x00", $index->fields);
c1092055 55 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
620df7e8 56 }
206d1995 57 }
0e2c6809 58}
59
60{
206d1995 61 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
0e2c6809 62
206d1995 63 foreach my $source (@sources) {
64 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 65
206d1995 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);
2581038c 69 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
206d1995 70 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
71 }
0e2c6809 72}
73
74{
206d1995 75 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
0e2c6809 76
206d1995 77 foreach my $source (@sources) {
78 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 79
206d1995 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 }
0e2c6809 84}
85
8f1617e2 86{
ab7e74aa 87 {
88 package # hide from PAUSE
89 DBICTest::Schema::NoViewDefinition;
90
91 use base qw/DBICTest::BaseResult/;
8f1617e2 92
ab7e74aa 93 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
94 __PACKAGE__->table('noviewdefinition');
8f1617e2 95
ab7e74aa 96 1;
8f1617e2 97 }
98
ab7e74aa 99 my $schema_invalid_view = $schema->clone;
100 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 101
8fd10683 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';
8f1617e2 105}
106
a7f4b74c 107lives_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
7f6f5b69 125done_testing;
126
0e2c6809 127sub create_schema {
206d1995 128 my $args = shift;
0e2c6809 129
206d1995 130 my $schema = $args->{schema};
131 my $additional_sqltargs = $args->{args} || {};
0e2c6809 132
206d1995 133 my $sqltargs = {
134 add_drop_table => 1,
135 ignore_constraint_names => 1,
136 ignore_index_names => 1,
137 %{$additional_sqltargs}
138 };
0e2c6809 139
206d1995 140 my $sqlt = SQL::Translator->new( $sqltargs );
0e2c6809 141
206d1995 142 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
143 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 144}
c2b7c5dc 145
146sub 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}