add test for bad view in mssql
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_mssql_extra_tests.pm
CommitLineData
b1e43108 1package dbixcsl_mssql_extra_tests;
2
3use Test::More;
d073740e 4use Test::Exception;
b1e43108 5
7cb9244f 6my $vendor = 'mssql';
7
8sub vendor {
9 shift;
10 $vendor = shift;
11}
12
d073740e 13# for cleanup in END
14my $saved_dbh;
15
b1e43108 16sub extra { +{
17 create => [
18 qq{
7cb9244f 19 CREATE TABLE [${vendor}_loader_test1.dot] (
b1e43108 20 id INT IDENTITY NOT NULL PRIMARY KEY,
21 dat VARCHAR(8)
22 )
23 },
5c6fb0a1 24 qq{
25 CREATE TABLE ${vendor}_loader_test2 (
26 id INT IDENTITY NOT NULL PRIMARY KEY,
27 dat VARCHAR(100) DEFAULT 'foo',
1f625792 28 num NUMERIC DEFAULT 10.89,
29 anint INT DEFAULT 6,
5c6fb0a1 30 ts DATETIME DEFAULT getdate()
31 )
32 },
d073740e 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"
b1e43108 47 ],
d073740e 48 count => 15,
b1e43108 49 run => sub {
50 my ($schema, $monikers, $classes) = @_;
51
d89bca78 52# Test that the table above (with '.' in name) gets loaded correctly.
7cb9244f 53 my $vendor_titlecased = "\u\L$vendor";
54
55 ok((my $rs = eval {
56 $schema->resultset("${vendor_titlecased}LoaderTest1Dot") }),
5c6fb0a1 57 'got a resultset for table with dot in name');
b1e43108 58
59 ok((my $from = eval { $rs->result_source->from }),
5c6fb0a1 60 'got an $rsrc->from for table with dot in name');
b1e43108 61
5c6fb0a1 62 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
b1e43108 63
7cb9244f 64 is eval { $$from }, "[${vendor}_loader_test1.dot]",
5c6fb0a1 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',
1f625792 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';
5c6fb0a1 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';
d89bca78 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};
d073740e 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';
b1e43108 121 },
122}}
123
d073740e 124# Clean up the bad view, table will be cleaned up in drops
125END {
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
b1e43108 1351;