multi-dsn support for common tests
[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     => 'getdate()',
28     default_function_def => 'DATETIME DEFAULT getdate()',
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     ],
40     extra => {
41         create => [
42             q{
43                 CREATE TABLE [mssql_loader_test1.dot] (
44                     id INT IDENTITY NOT NULL PRIMARY KEY,
45                     dat VARCHAR(8)
46                 )
47             },
48             q{
49                 CREATE TABLE mssql_loader_test3 (
50                     id INT IDENTITY NOT NULL PRIMARY KEY
51                 )
52             },
53             q{
54                 CREATE VIEW mssql_loader_test4 AS
55                 SELECT * FROM mssql_loader_test3
56             },
57         ],
58         pre_drop_ddl => [
59             'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
60             'DROP VIEW mssql_loader_test4',
61         ],
62         drop   => [
63             '[mssql_loader_test1.dot]',
64             'mssql_loader_test3'
65         ],
66         count  => 8,
67         run    => sub {
68             my ($schema, $monikers, $classes) = @_;
69
70 # Test that the table above (with '.' in name) gets loaded correctly.
71             ok((my $rs = eval {
72                 $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
73                 'got a resultset for table with dot in name');
74
75             ok((my $from = eval { $rs->result_source->from }),
76                 'got an $rsrc->from for table with dot in name');
77
78             is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
79
80             is eval { $$from }, "[mssql_loader_test1.dot]",
81                 '->table with dot in name has correct name';
82
83 # Test that identity columns do not have 'identity' in the data_type, and do
84 # have is_auto_increment.
85             my $identity_col_info = $schema->resultset($monikers->{loader_test10})
86                 ->result_source->column_info('id10');
87
88             is $identity_col_info->{data_type}, 'int',
89                 q{'INT IDENTITY' column has data_type => 'int'};
90
91             is $identity_col_info->{is_auto_increment}, 1,
92                 q{'INT IDENTITY' column has is_auto_increment => 1};
93
94 # Test that a bad view (where underlying table is gone) is ignored.
95             my $dbh = $schema->storage->dbh;
96             $dbh->do("DROP TABLE mssql_loader_test3");
97
98             my @warnings;
99             {
100                 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
101                 $schema->rescan;
102             }
103             ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
104                 'bad view ignored');
105
106             throws_ok {
107                 $schema->resultset($monikers->{mssql_loader_test4})
108             } qr/Can't find source/,
109                 'no source registered for bad view';
110         },
111     },
112 );
113
114 if(not ($dbd_sybase_dsn || $odbc_dsn)) {
115     $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');
116 }
117 else {
118     $tester->run_tests();
119 }