Test for the real leak reason
[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;
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;
21 my $sqlt_s = create_schema ({ schema => $s });
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
206d1995 41 foreach my $source (@sources) {
42 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 43
206d1995 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);
2581038c 47 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
206d1995 48 is($index_count, $fk_count, "correct number of indices for $source with no args");
49 }
0e2c6809 50}
51
52{
206d1995 53 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
0e2c6809 54
206d1995 55 foreach my $source (@sources) {
56 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 57
206d1995 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);
2581038c 61 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
206d1995 62 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
63 }
0e2c6809 64}
65
66{
206d1995 67 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
0e2c6809 68
206d1995 69 foreach my $source (@sources) {
70 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 71
206d1995 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 }
0e2c6809 76}
77
8f1617e2 78{
ab7e74aa 79 {
80 package # hide from PAUSE
81 DBICTest::Schema::NoViewDefinition;
82
83 use base qw/DBICTest::BaseResult/;
8f1617e2 84
ab7e74aa 85 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
86 __PACKAGE__->table('noviewdefinition');
8f1617e2 87
ab7e74aa 88 1;
8f1617e2 89 }
90
ab7e74aa 91 my $schema_invalid_view = $schema->clone;
92 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 93
8fd10683 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';
8f1617e2 97}
98
a7f4b74c 99lives_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
7f6f5b69 117done_testing;
118
0e2c6809 119sub create_schema {
206d1995 120 my $args = shift;
0e2c6809 121
206d1995 122 my $schema = $args->{schema};
123 my $additional_sqltargs = $args->{args} || {};
0e2c6809 124
206d1995 125 my $sqltargs = {
126 add_drop_table => 1,
127 ignore_constraint_names => 1,
128 ignore_index_names => 1,
129 %{$additional_sqltargs}
130 };
0e2c6809 131
206d1995 132 my $sqlt = SQL::Translator->new( $sqltargs );
0e2c6809 133
206d1995 134 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
135 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 136}
c2b7c5dc 137
138sub 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}