e4767f9cbd70f33e79972de82b0c7a1fe456b57c
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_mssql_extra_tests.pm
1 package dbixcsl_mssql_extra_tests;
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7
8 # for cleanup in END
9 my $storage;
10
11 sub extra { +{
12     create => [
13         q{
14             CREATE TABLE [mssql_loader_test1.dot] (
15                 id INT IDENTITY NOT NULL PRIMARY KEY,
16                 dat VARCHAR(8)
17             )
18         },
19         q{
20             CREATE TABLE mssql_loader_test3 (
21                 id INT IDENTITY NOT NULL PRIMARY KEY
22             )
23         },
24         q{
25             CREATE VIEW mssql_loader_test4 AS
26             SELECT * FROM mssql_loader_test3
27         },
28     ],
29     drop   => [
30         "[mssql_loader_test1.dot]",
31         "mssql_loader_test3"
32     ],
33     count  => 8,
34     run    => sub {
35         my ($schema, $monikers, $classes) = @_;
36
37 # Test that the table above (with '.' in name) gets loaded correctly.
38         ok((my $rs = eval {
39             $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }),
40             'got a resultset for table with dot in name');
41
42         ok((my $from = eval { $rs->result_source->from }),
43             'got an $rsrc->from for table with dot in name');
44
45         is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
46
47         is eval { $$from }, "[mssql_loader_test1.dot]",
48             '->table with dot in name has correct name';
49
50 # Test that identity columns do not have 'identity' in the data_type, and do
51 # have is_auto_increment.
52         my $identity_col_info = $schema->resultset($monikers->{loader_test10})
53             ->result_source->column_info('id10');
54
55         is $identity_col_info->{data_type}, 'int',
56             q{'INT IDENTITY' column has data_type => 'int'};
57
58         is $identity_col_info->{is_auto_increment}, 1,
59             q{'INT IDENTITY' column has is_auto_increment => 1};
60
61 # Test that a bad view (where underlying table is gone) is ignored.
62         $storage = $schema->storage;
63
64         my $dbh = $storage->dbh;
65         $dbh->do("DROP TABLE mssql_loader_test3");
66
67         my @warnings;
68         {
69             local $SIG{__WARN__} = sub { push @warnings, $_[0] };
70             $schema->rescan;
71         }
72         ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings),
73             'bad view ignored');
74
75         throws_ok {
76             $schema->resultset($monikers->{mssql_loader_test4})
77         } qr/Can't find source/,
78             'no source registered for bad view';
79     },
80 }}
81
82 # Clean up the bad view
83 END {
84     local $@;
85     eval {
86         my $dbh = $storage->dbh;
87         $dbh->do($_) for (
88 "CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)",
89 "DROP VIEW mssql_loader_test4",
90 "DROP TABLE mssql_loader_test3",
91         );
92     };
93 }
94
95 1;