minor MSSQL improvements
[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_def => 'DATETIME DEFAULT getdate()',
28     connect_info => [ ($dbd_sybase_dsn ? {
29             dsn         => $dbd_sybase_dsn,
30             user        => $dbd_sybase_user,
31             password    => $dbd_sybase_password,
32         } : ()),
33         ($odbc_dsn ? {
34             dsn         => $odbc_dsn,
35             user        => $odbc_user,
36             password    => $odbc_password,
37         } : ()),
38     ],
39     data_types => {
40         # http://msdn.microsoft.com/en-us/library/ms187752.aspx
41
42         # numeric types
43         'int identity' => { data_type => 'integer', is_auto_increment => 1 },
44         bigint   => { data_type => 'bigint' },
45         int      => { data_type => 'integer' },
46         integer  => { data_type => 'integer' },
47         smallint => { data_type => 'smallint' },
48         tinyint  => { data_type => 'tinyint' },
49         money       => { data_type => 'money' },
50         smallmoney  => { data_type => 'smallmoney' },
51         bit         => { data_type => 'bit' },
52         real           => { data_type => 'real' },
53         'float(14)'    => { data_type => 'real' },
54         'float(24)'    => { data_type => 'real' },
55         'float(25)'    => { data_type => 'double precision' },
56         'float(53)'    => { data_type => 'double precision' },
57         float          => { data_type => 'double precision' },
58         'double precision'
59                        => { data_type => 'double precision' },
60         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
61         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
62         'dec(6,3)'     => { data_type => 'decimal', size => [6,3] },
63         numeric        => { data_type => 'numeric' },
64         decimal        => { data_type => 'decimal' },
65         dec            => { data_type => 'decimal' },
66
67         # datetime types
68         date     => { data_type => 'date' },
69         datetime => { data_type => 'datetime' },
70         # test rewriting getdate() to current_timestamp
71         'datetime default getdate()'
72                  => { data_type => 'datetime', default_value => \'current_timestamp' },
73         smalldatetime  => { data_type => 'smalldatetime' },
74         time     => { data_type => 'time' },
75         'time(0)'=> { data_type => 'time', size => 0 },
76         'time(1)'=> { data_type => 'time', size => 1 },
77         'time(2)'=> { data_type => 'time', size => 2 },
78         'time(3)'=> { data_type => 'time', size => 3 },
79         'time(4)'=> { data_type => 'time', size => 4 },
80         'time(5)'=> { data_type => 'time', size => 5 },
81         'time(6)'=> { data_type => 'time', size => 6 },
82         'time(7)'=> { data_type => 'time' },
83         datetimeoffset => { data_type => 'datetimeoffset' },
84         'datetimeoffset(0)' => { data_type => 'datetimeoffset', size => 0 },
85         'datetimeoffset(1)' => { data_type => 'datetimeoffset', size => 1 },
86         'datetimeoffset(2)' => { data_type => 'datetimeoffset', size => 2 },
87         'datetimeoffset(3)' => { data_type => 'datetimeoffset', size => 3 },
88         'datetimeoffset(4)' => { data_type => 'datetimeoffset', size => 4 },
89         'datetimeoffset(5)' => { data_type => 'datetimeoffset', size => 5 },
90         'datetimeoffset(6)' => { data_type => 'datetimeoffset', size => 6 },
91         'datetimeoffset(7)' => { data_type => 'datetimeoffset' },
92         datetime2      => { data_type => 'datetime2' },
93         'datetime2(0)' => { data_type => 'datetime2', size => 0 },
94         'datetime2(1)' => { data_type => 'datetime2', size => 1 },
95         'datetime2(2)' => { data_type => 'datetime2', size => 2 },
96         'datetime2(3)' => { data_type => 'datetime2', size => 3 },
97         'datetime2(4)' => { data_type => 'datetime2', size => 4 },
98         'datetime2(5)' => { data_type => 'datetime2', size => 5 },
99         'datetime2(6)' => { data_type => 'datetime2', size => 6 },
100         'datetime2(7)' => { data_type => 'datetime2' },
101
102         # string types
103         char           => { data_type => 'char', size => 1 },
104         'char(2)'      => { data_type => 'char', size => 2 },
105         'varchar(2)'   => { data_type => 'varchar', size => 2 },
106         nchar          => { data_type => 'nchar', size => 1 },
107         'nchar(2)'     => { data_type => 'nchar', size => 2 },
108         'nvarchar(2)'  => { data_type => 'nvarchar', size => 2 },
109
110         # binary types
111         'binary'       => { data_type => 'binary', size => 1 },
112         'binary(2)'    => { data_type => 'binary', size => 2 },
113         'varbinary(2)' => { data_type => 'varbinary', size => 2 },
114
115         # blob types
116         'varchar(max)'   => { data_type => 'text' },
117         text             => { data_type => 'text' },
118         'nvarchar(max)'  => { data_type => 'ntext' },
119         ntext            => { data_type => 'ntext' },
120         'varbinary(max)' => { data_type => 'image' },
121         image            => { data_type => 'image' },
122
123         # other types
124         timestamp        => { data_type => 'timestamp', inflate_datetime => 0 },
125         rowversion       => { data_type => 'rowversion' },
126         uniqueidentifier => { data_type => 'uniqueidentifier' },
127         hierarchyid      => { data_type => 'hierarchyid' },
128         sql_variant      => { data_type => 'sql_variant' },
129         xml              => { data_type => 'xml' },
130     },
131     extra => {
132         create => [
133             q{
134                 CREATE TABLE [mssql_loader_test1.dot] (
135                     id INT IDENTITY NOT NULL PRIMARY KEY,
136                     dat VARCHAR(8)
137                 )
138             },
139             q{
140                 CREATE TABLE mssql_loader_test3 (
141                     id INT IDENTITY NOT NULL PRIMARY KEY
142                 )
143             },
144             q{
145                 CREATE VIEW mssql_loader_test4 AS
146                 SELECT * FROM mssql_loader_test3
147             },
148             # test capitalization of cols in unique constraints and rels
149             q{ SET QUOTED_IDENTIFIER ON },
150             q{ SET ANSI_NULLS ON },
151             q{
152                 CREATE TABLE [MSSQL_Loader_Test5] (
153                     [Id] INT IDENTITY NOT NULL PRIMARY KEY,
154                     [FooCol] INT NOT NULL,
155                     [BarCol] INT NOT NULL,
156                     UNIQUE ([FooCol], [BarCol])
157                 )
158             },
159             q{
160                 CREATE TABLE [MSSQL_Loader_Test6] (
161                     [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
162                 )
163             },
164         ],
165         pre_drop_ddl => [
166             'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
167             'DROP VIEW mssql_loader_test4',
168         ],
169         drop   => [
170             '[mssql_loader_test1.dot]',
171             'mssql_loader_test3',
172             'MSSQL_Loader_Test6',
173             'MSSQL_Loader_Test5',
174         ],
175         count  => 10,
176         run    => sub {
177             my ($schema, $monikers, $classes) = @_;
178
179 # Test that the table above (with '.' in name) gets loaded correctly.
180             ok((my $rs = eval {
181                 $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
182                 'got a resultset for table with dot in name');
183
184             ok((my $from = eval { $rs->result_source->from }),
185                 'got an $rsrc->from for table with dot in name');
186
187             is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
188
189             is eval { $$from }, "[mssql_loader_test1.dot]",
190                 '->table with dot in name has correct name';
191
192 # Test capitalization of columns and unique constraints
193             ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
194                 'got result_source');
195
196             if ($schema->_loader->preserve_case) {
197                 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
198                     'column name case is preserved with case-sensitive collation';
199
200                 my %uniqs = $rsrc->unique_constraints;
201                 delete $uniqs{primary};
202
203                 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
204                     'column name case is preserved in unique constraint with case-sensitive collation');
205             }
206             else {
207                 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
208                     'column names are lowercased for case-insensitive collation';
209
210                 my %uniqs = $rsrc->unique_constraints;
211                 delete $uniqs{primary};
212
213                 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
214                     'columns in unique constraint lowercased for case-insensitive collation');
215             }
216
217             lives_and {
218                 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
219
220                 if ($schema->_loader->preserve_case) {
221                     $five_row->foo_col(1);
222                     $five_row->bar_col(2);
223                 }
224                 else {
225                     $five_row->foocol(1);
226                     $five_row->barcol(2);
227                 }
228                 $five_row->insert;
229
230                 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
231
232                 is $six_row->five->id, 1;
233             } 'relationships for mixed-case tables/columns detected';
234
235 # Test that a bad view (where underlying table is gone) is ignored.
236             my $dbh = $schema->storage->dbh;
237             $dbh->do("DROP TABLE mssql_loader_test3");
238
239             my @warnings;
240             {
241                 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
242                 $schema->rescan;
243             }
244             ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
245                 'bad view ignored');
246
247             throws_ok {
248                 $schema->resultset($monikers->{mssql_loader_test4})
249             } qr/Can't find source/,
250                 'no source registered for bad view';
251         },
252     },
253 );
254
255 if(not ($dbd_sybase_dsn || $odbc_dsn)) {
256     $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');
257 }
258 else {
259     $tester->run_tests();
260 }
261 # vim:et sts=4 sw=4 tw=0: