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