test column name lowercasing in MSSQL
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 16mssql_common.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5
6 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
7 BEGIN {
8   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
9     unshift @INC, $_ for split /:/, $lib_dirs;
10   }
11 }
12
13 use lib qw(t/lib);
14 use dbixcsl_common_tests;
15
16 my $dbd_sybase_dsn      = $ENV{DBICTEST_MSSQL_DSN} || '';
17 my $dbd_sybase_user     = $ENV{DBICTEST_MSSQL_USER} || '';
18 my $dbd_sybase_password = $ENV{DBICTEST_MSSQL_PASS} || '';
19
20 my $odbc_dsn      = $ENV{DBICTEST_MSSQL_ODBC_DSN} || '';
21 my $odbc_user     = $ENV{DBICTEST_MSSQL_ODBC_USER} || '';
22 my $odbc_password = $ENV{DBICTEST_MSSQL_ODBC_PASS} || '';
23
24 my $tester = dbixcsl_common_tests->new(
25     vendor      => 'mssql',
26     auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
27     default_function     => 'getdate()',
28     default_function_def => 'DATETIME DEFAULT getdate()',
29     connect_info => [ ($dbd_sybase_dsn ? {
30             dsn         => $dbd_sybase_dsn,
31             user        => $dbd_sybase_user,
32             password    => $dbd_sybase_password,
33         } : ()),
34         ($odbc_dsn ? {
35             dsn         => $odbc_dsn,
36             user        => $odbc_user,
37             password    => $odbc_password,
38         } : ()),
39     ],
40     data_types => {
41         'int identity' => { data_type => 'int', is_auto_increment => 1 },
42     },
43     extra => {
44         create => [
45             q{
46                 CREATE TABLE [mssql_loader_test1.dot] (
47                     id INT IDENTITY NOT NULL PRIMARY KEY,
48                     dat VARCHAR(8)
49                 )
50             },
51             q{
52                 CREATE TABLE mssql_loader_test3 (
53                     id INT IDENTITY NOT NULL PRIMARY KEY
54                 )
55             },
56             q{
57                 CREATE VIEW mssql_loader_test4 AS
58                 SELECT * FROM mssql_loader_test3
59             },
60             # test capitalization of cols in unique constraints
61             q{
62                 CREATE TABLE mssql_loader_test5 (
63                     id INT IDENTITY NOT NULL PRIMARY KEY,
64                     FooCol INT NOT NULL,
65                     BarCol INT NOT NULL,
66                     UNIQUE (FooCol, BarCol)
67                 )
68             },
69         ],
70         pre_drop_ddl => [
71             'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
72             'DROP VIEW mssql_loader_test4',
73         ],
74         drop   => [
75             '[mssql_loader_test1.dot]',
76             'mssql_loader_test3',
77             'mssql_loader_test5',
78         ],
79         count  => 9,
80         run    => sub {
81             my ($schema, $monikers, $classes) = @_;
82
83 # Test that the table above (with '.' in name) gets loaded correctly.
84             ok((my $rs = eval {
85                 $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
86                 'got a resultset for table with dot in name');
87
88             ok((my $from = eval { $rs->result_source->from }),
89                 'got an $rsrc->from for table with dot in name');
90
91             is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
92
93             is eval { $$from }, "[mssql_loader_test1.dot]",
94                 '->table with dot in name has correct name';
95
96 # Test capitalization of columns and unique constraints
97             ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
98                 'got result_source');
99
100             is $rsrc->column_info('foocol')->{data_type}, 'int',
101                 'column names are lowercased';
102
103             my %uniqs = $rsrc->unique_constraints;
104             delete $uniqs{primary};
105
106             is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
107                 'columns in unique constraint lowercased');
108
109 # Test that a bad view (where underlying table is gone) is ignored.
110             my $dbh = $schema->storage->dbh;
111             $dbh->do("DROP TABLE mssql_loader_test3");
112
113             my @warnings;
114             {
115                 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
116                 $schema->rescan;
117             }
118             ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
119                 'bad view ignored');
120
121             throws_ok {
122                 $schema->resultset($monikers->{mssql_loader_test4})
123             } qr/Can't find source/,
124                 'no source registered for bad view';
125         },
126     },
127 );
128
129 if(not ($dbd_sybase_dsn || $odbc_dsn)) {
130     $tester->skip_tests('You need to set the DBICTEST_MSSQL_DSN, _USER and _PASS and/or the DBICTEST_MSSQL_ODBC_DSN, _USER and _PASS environment variables');
131 }
132 else {
133     $tester->run_tests();
134 }