Informix: write out highest precision datetime (until we can parse the datetime preci...
[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_def => 'DATETIME DEFAULT getdate()',
3a89a69f 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 ],
deedd576 39 data_types => {
81ade4d9 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 => 'float', size => 24 },
53 'float(14)' => { data_type => 'float', size => 24 },
54 'float(24)' => { data_type => 'float', size => 24 },
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' },
8a64178e 70 # test rewriting getdate() to CURRENT_TIMESTAMP
81ade4d9 71 'datetime DEFAULT getdate()'
8a64178e 72 => { data_type => 'datetime', default_value => \'CURRENT_TIMESTAMP' },
81ade4d9 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(2)' => { data_type => 'binary', size => 2 },
112 'varbinary(2)' => { data_type => 'varbinary', size => 2 },
113
114 # blob types
115 'varchar(max)' => { data_type => 'text' },
116 text => { data_type => 'text' },
117 'nvarchar(max)' => { data_type => 'ntext' },
118 ntext => { data_type => 'ntext' },
119 'varbinary(max)' => { data_type => 'image' },
120 image => { data_type => 'image' },
121
122 # other types
123 timestamp => { data_type => 'timestamp', inflate_datetime => 0 },
124 uniqueidentifier => { data_type => 'uniqueidentifier' },
125 hierarchyid => { data_type => 'hierarchyid' },
126 sql_variant => { data_type => 'sql_variant' },
127 xml => { data_type => 'xml' },
deedd576 128 },
3a89a69f 129 extra => {
130 create => [
131 q{
132 CREATE TABLE [mssql_loader_test1.dot] (
133 id INT IDENTITY NOT NULL PRIMARY KEY,
134 dat VARCHAR(8)
135 )
136 },
137 q{
138 CREATE TABLE mssql_loader_test3 (
139 id INT IDENTITY NOT NULL PRIMARY KEY
140 )
141 },
142 q{
143 CREATE VIEW mssql_loader_test4 AS
144 SELECT * FROM mssql_loader_test3
145 },
020f3c3a 146 # test capitalization of cols in unique constraints and rels
ccce0e82 147 q{ SET QUOTED_IDENTIFIER ON },
148 q{ SET ANSI_NULLS ON },
deedd576 149 q{
020f3c3a 150 CREATE TABLE [MSSQL_Loader_Test5] (
151 [Id] INT IDENTITY NOT NULL PRIMARY KEY,
ccce0e82 152 [FooCol] INT NOT NULL,
153 [BarCol] INT NOT NULL,
154 UNIQUE ([FooCol], [BarCol])
deedd576 155 )
156 },
020f3c3a 157 q{
158 CREATE TABLE [MSSQL_Loader_Test6] (
159 [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
160 )
161 },
3a89a69f 162 ],
163 pre_drop_ddl => [
164 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
165 'DROP VIEW mssql_loader_test4',
166 ],
167 drop => [
168 '[mssql_loader_test1.dot]',
deedd576 169 'mssql_loader_test3',
060f5ecd 170 'MSSQL_Loader_Test6',
171 'MSSQL_Loader_Test5',
3a89a69f 172 ],
bfb43060 173 count => 10,
3a89a69f 174 run => sub {
175 my ($schema, $monikers, $classes) = @_;
176
177# Test that the table above (with '.' in name) gets loaded correctly.
178 ok((my $rs = eval {
179 $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
180 'got a resultset for table with dot in name');
181
182 ok((my $from = eval { $rs->result_source->from }),
183 'got an $rsrc->from for table with dot in name');
184
185 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
186
187 is eval { $$from }, "[mssql_loader_test1.dot]",
188 '->table with dot in name has correct name';
189
deedd576 190# Test capitalization of columns and unique constraints
191 ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
192 'got result_source');
193
bc1cb85e 194 if ($schema->_loader->preserve_case) {
060f5ecd 195 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
196 'column name case is preserved with case-sensitive collation';
020f3c3a 197
060f5ecd 198 my %uniqs = $rsrc->unique_constraints;
199 delete $uniqs{primary};
3a89a69f 200
060f5ecd 201 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
202 'column name case is preserved in unique constraint with case-sensitive collation');
203 }
204 else {
205 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
206 'column names are lowercased for case-insensitive collation';
207
208 my %uniqs = $rsrc->unique_constraints;
209 delete $uniqs{primary};
3a89a69f 210
060f5ecd 211 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
212 'columns in unique constraint lowercased for case-insensitive collation');
213 }
3a89a69f 214
020f3c3a 215 lives_and {
060f5ecd 216 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
217 $five_row->foocol(1);
218 $five_row->barcol(2);
219 $five_row->insert;
220
020f3c3a 221 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
222
223 is $six_row->five->id, 1;
224 } 'relationships for mixed-case tables/columns detected';
225
3a89a69f 226# Test that a bad view (where underlying table is gone) is ignored.
227 my $dbh = $schema->storage->dbh;
228 $dbh->do("DROP TABLE mssql_loader_test3");
229
230 my @warnings;
231 {
232 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
233 $schema->rescan;
234 }
235 ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
236 'bad view ignored');
237
238 throws_ok {
239 $schema->resultset($monikers->{mssql_loader_test4})
240 } qr/Can't find source/,
241 'no source registered for bad view';
242 },
243 },
046e344c 244);
245
3a89a69f 246if(not ($dbd_sybase_dsn || $odbc_dsn)) {
247 $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 248}
249else {
250 $tester->run_tests();
251}
060f5ecd 252# vim:et sts=4 sw=4 tw=0: