v5 backcompat tests
[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 => 'getdate()',
28 default_function_def => 'DATETIME DEFAULT getdate()',
3a89a69f 29 connect_info => [ ($dbd_sybase_dsn ? {
30 dsn => $dbd_sybase_dsn,
31 user => $dbd_sybase_user,
32 password => $dbd_sybase_password,
33 } : ()),
34 ($odbc_dsn ? {
35 dsn => $odbc_dsn,
36 user => $odbc_user,
37 password => $odbc_password,
38 } : ()),
39 ],
deedd576 40 data_types => {
41 'int identity' => { data_type => 'int', is_auto_increment => 1 },
42 },
3a89a69f 43 extra => {
44 create => [
45 q{
46 CREATE TABLE [mssql_loader_test1.dot] (
47 id INT IDENTITY NOT NULL PRIMARY KEY,
48 dat VARCHAR(8)
49 )
50 },
51 q{
52 CREATE TABLE mssql_loader_test3 (
53 id INT IDENTITY NOT NULL PRIMARY KEY
54 )
55 },
56 q{
57 CREATE VIEW mssql_loader_test4 AS
58 SELECT * FROM mssql_loader_test3
59 },
020f3c3a 60 # test capitalization of cols in unique constraints and rels
ccce0e82 61 q{ SET QUOTED_IDENTIFIER ON },
62 q{ SET ANSI_NULLS ON },
deedd576 63 q{
020f3c3a 64 CREATE TABLE [MSSQL_Loader_Test5] (
65 [Id] INT IDENTITY NOT NULL PRIMARY KEY,
ccce0e82 66 [FooCol] INT NOT NULL,
67 [BarCol] INT NOT NULL,
68 UNIQUE ([FooCol], [BarCol])
deedd576 69 )
70 },
020f3c3a 71 q{
72 CREATE TABLE [MSSQL_Loader_Test6] (
73 [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
74 )
75 },
3a89a69f 76 ],
77 pre_drop_ddl => [
78 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
79 'DROP VIEW mssql_loader_test4',
80 ],
81 drop => [
82 '[mssql_loader_test1.dot]',
deedd576 83 'mssql_loader_test3',
060f5ecd 84 'MSSQL_Loader_Test6',
85 'MSSQL_Loader_Test5',
3a89a69f 86 ],
bfb43060 87 count => 10,
3a89a69f 88 run => sub {
89 my ($schema, $monikers, $classes) = @_;
90
91# Test that the table above (with '.' in name) gets loaded correctly.
92 ok((my $rs = eval {
93 $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
94 'got a resultset for table with dot in name');
95
96 ok((my $from = eval { $rs->result_source->from }),
97 'got an $rsrc->from for table with dot in name');
98
99 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
100
101 is eval { $$from }, "[mssql_loader_test1.dot]",
102 '->table with dot in name has correct name';
103
deedd576 104# Test capitalization of columns and unique constraints
105 ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
106 'got result_source');
107
060f5ecd 108 if ($schema->_loader->_is_case_sensitive) {
109 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
110 'column name case is preserved with case-sensitive collation';
020f3c3a 111
060f5ecd 112 my %uniqs = $rsrc->unique_constraints;
113 delete $uniqs{primary};
3a89a69f 114
060f5ecd 115 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
116 'column name case is preserved in unique constraint with case-sensitive collation');
117 }
118 else {
119 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
120 'column names are lowercased for case-insensitive collation';
121
122 my %uniqs = $rsrc->unique_constraints;
123 delete $uniqs{primary};
3a89a69f 124
060f5ecd 125 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
126 'columns in unique constraint lowercased for case-insensitive collation');
127 }
3a89a69f 128
020f3c3a 129 lives_and {
060f5ecd 130 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
131 $five_row->foocol(1);
132 $five_row->barcol(2);
133 $five_row->insert;
134
020f3c3a 135 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
136
137 is $six_row->five->id, 1;
138 } 'relationships for mixed-case tables/columns detected';
139
3a89a69f 140# Test that a bad view (where underlying table is gone) is ignored.
141 my $dbh = $schema->storage->dbh;
142 $dbh->do("DROP TABLE mssql_loader_test3");
143
144 my @warnings;
145 {
146 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
147 $schema->rescan;
148 }
149 ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
150 'bad view ignored');
151
152 throws_ok {
153 $schema->resultset($monikers->{mssql_loader_test4})
154 } qr/Can't find source/,
155 'no source registered for bad view';
156 },
157 },
046e344c 158);
159
3a89a69f 160if(not ($dbd_sybase_dsn || $odbc_dsn)) {
161 $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 162}
163else {
164 $tester->run_tests();
165}
060f5ecd 166# vim:et sts=4 sw=4 tw=0: