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