>table(\"foo") now works
[dbsrgits/DBIx-Class-Historic.git] / t / 99dbic_sqlt_parser.t
CommitLineData
0e2c6809 1#!/usr/bin/perl
2use strict;
3use warnings;
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
1dac6833 8
0e2c6809 9BEGIN {
1b840063 10 eval "use SQL::Translator 0.09003;";
1dac6833 11 if ($@) {
1b840063 12 plan skip_all => 'needs SQL::Translator 0.09003 for testing';
1dac6833 13 }
0e2c6809 14}
15
16my $schema = DBICTest->init_schema();
0fc7cd47 17# Dummy was yanked out by the sqlt hook test
18# YearXXXXCDs are views
c2b7c5dc 19
0fc7cd47 20my @sources = grep { $_ ne 'Dummy' && $_ !~ /^Year\d{4}CDs$/ }
21 $schema->sources;
22
181c0934 23plan tests => ( @sources * 3);
0e2c6809 24
25{
26 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
27
181c0934 28 foreach my $source (@sources) {
c2b7c5dc 29 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 30
31 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
32 my @indices = $table->get_indices;
33 my $index_count = scalar(@indices);
2581038c 34 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
0e2c6809 35 is($index_count, $fk_count, "correct number of indices for $source with no args");
36 }
37}
38
39{
40 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
41
181c0934 42 foreach my $source (@sources) {
c2b7c5dc 43 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 44
45 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
46 my @indices = $table->get_indices;
47 my $index_count = scalar(@indices);
2581038c 48 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
0e2c6809 49 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
50 }
51}
52
53{
54 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
55
181c0934 56 foreach my $source (@sources) {
c2b7c5dc 57 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 58
59 my @indices = $table->get_indices;
60 my $index_count = scalar(@indices);
61 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
62 }
63}
64
65sub create_schema {
66 my $args = shift;
67
68 my $schema = $args->{schema};
69 my $additional_sqltargs = $args->{args} || {};
70
71 my $sqltargs = {
72 add_drop_table => 1,
73 ignore_constraint_names => 1,
74 ignore_index_names => 1,
75 %{$additional_sqltargs}
76 };
77
78 my $sqlt = SQL::Translator->new( $sqltargs );
79
80 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
81 return $sqlt->translate({ data => $schema }) or die $sqlt->error;
82}
c2b7c5dc 83
84sub get_table {
85 my ($sqlt_schema, $schema, $source) = @_;
86
87 my $table_name = $schema->source($source)->from;
88 $table_name = $$table_name if ref $table_name;
89
90 return $sqlt_schema->get_table($table_name);
91}