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