add test for bad view in mssql
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_mssql_extra_tests.pm
1 package dbixcsl_mssql_extra_tests;
2
3 use Test::More;
4 use Test::Exception;
5
6 my $vendor = 'mssql';
7
8 sub vendor {
9     shift;
10     $vendor = shift;
11 }
12
13 # for cleanup in END
14 my $saved_dbh;
15
16 sub extra { +{
17     create => [
18         qq{
19             CREATE TABLE [${vendor}_loader_test1.dot] (
20                 id INT IDENTITY NOT NULL PRIMARY KEY,
21                 dat VARCHAR(8)
22             )
23         },
24         qq{
25             CREATE TABLE ${vendor}_loader_test2 (
26                 id INT IDENTITY NOT NULL PRIMARY KEY,
27                 dat VARCHAR(100) DEFAULT 'foo',
28                 num NUMERIC DEFAULT 10.89,
29                 anint INT DEFAULT 6,
30                 ts DATETIME DEFAULT getdate()
31             )
32         },
33         qq{
34             CREATE TABLE ${vendor}_loader_test3 (
35                 id INT IDENTITY NOT NULL PRIMARY KEY
36             )
37         },
38         qq{
39             CREATE VIEW ${vendor}_loader_test4 AS
40             SELECT * FROM ${vendor}_loader_test3
41         },
42     ],
43     drop   => [
44         "[${vendor}_loader_test1.dot]",
45         "${vendor}_loader_test2",
46         "${vendor}_loader_test3"
47     ],
48     count  => 15,
49     run    => sub {
50         my ($schema, $monikers, $classes) = @_;
51
52 # Test that the table above (with '.' in name) gets loaded correctly.
53         my $vendor_titlecased = "\u\L$vendor";
54
55         ok((my $rs = eval {
56             $schema->resultset("${vendor_titlecased}LoaderTest1Dot") }),
57             'got a resultset for table with dot in name');
58
59         ok((my $from = eval { $rs->result_source->from }),
60             'got an $rsrc->from for table with dot in name');
61
62         is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
63
64         is eval { $$from }, "[${vendor}_loader_test1.dot]",
65             '->table with dot in name has correct name';
66
67 # Test that column defaults are set correctly
68         ok(($rs = eval {
69             $schema->resultset("${vendor_titlecased}LoaderTest2") }),
70             'got a resultset for table with column with default value');
71
72         my $rsrc = $rs->result_source;
73
74         is eval { $rsrc->column_info('dat')->{default_value} }, 'foo',
75             'correct default_value for column with literal string default';
76
77         is eval { $rsrc->column_info('anint')->{default_value} }, 6,
78             'correct default_value for column with literal integer default';
79
80         cmp_ok eval { $rsrc->column_info('num')->{default_value} },
81             '==', 10.89,
82             'correct default_value for column with literal numeric default';
83
84         ok((my $function_default =
85             eval { $rsrc->column_info('ts')->{default_value} }),
86             'got default_value for column with function default');
87
88         is ref($function_default), 'SCALAR',
89             'default_value for function default is a SCALAR ref';
90
91         is eval { $$function_default }, 'getdate()',
92             'default_value for function default is correct';
93
94 # Test that identity columns do not have 'identity' in the data_type, and do
95 # have is_auto_increment.
96         my $identity_col_info = $schema->resultset('LoaderTest10')
97             ->result_source->column_info('id10');
98
99         is $identity_col_info->{data_type}, 'int',
100             q{'INT IDENTITY' column has data_type => 'int'};
101
102         is $identity_col_info->{is_auto_increment}, 1,
103             q{'INT IDENTITY' column has is_auto_increment => 1};
104
105 # Test that a bad view (where underlying table is gone) is ignored.
106         $saved_dbh = $schema->storage->dbh;
107         $saved_dbh->do("DROP TABLE ${vendor}_loader_test3");
108
109         my @warnings;
110         {
111             local $SIG{__WARN__} = sub { push @warnings, $_[0] };
112             $schema->rescan;
113         }
114         ok ((grep /^Bad table or view '${vendor}_loader_test4'/, @warnings),
115             'bad view ignored');
116
117         throws_ok {
118             $schema->resultset("${vendor_titlecased}LoaderTest4")
119         } qr/Can't find source/,
120             'no source registered for bad view';
121     },
122 }}
123
124 # Clean up the bad view, table will be cleaned up in drops
125 END {
126     local $@;
127     eval {
128         $saved_dbh->do($_) for (
129 "CREATE TABLE ${vendor}_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)",
130 "DROP VIEW ${vendor}_loader_test4"
131         );
132     };
133 }
134
135 1;