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