ea38461412f8f52b7b0ee04ab6e7d5c2fb6d916d
[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
5 my $vendor = 'mssql';
6
7 sub vendor {
8     shift;
9     $vendor = shift;
10 }
11
12 sub extra { +{
13     create => [
14         qq{
15             CREATE TABLE [${vendor}_loader_test1.dot] (
16                 id INT IDENTITY NOT NULL PRIMARY KEY,
17                 dat VARCHAR(8)
18             )
19         },
20         qq{
21             CREATE TABLE ${vendor}_loader_test2 (
22                 id INT IDENTITY NOT NULL PRIMARY KEY,
23                 dat VARCHAR(100) DEFAULT 'foo',
24                 num NUMERIC DEFAULT 10.89,
25                 anint INT DEFAULT 6,
26                 ts DATETIME DEFAULT getdate()
27             )
28         },
29     ],
30     drop   => [ "[${vendor}_loader_test1.dot]", "${vendor}_loader_test2"  ],
31     count  => 13,
32     run    => sub {
33         my ($schema, $monikers, $classes) = @_;
34
35 # Test that the table above (with '.' in name) gets loaded correctly.
36         my $vendor_titlecased = "\u\L$vendor";
37
38         ok((my $rs = eval {
39             $schema->resultset("${vendor_titlecased}LoaderTest1Dot") }),
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 }, "[${vendor}_loader_test1.dot]",
48             '->table with dot in name has correct name';
49
50 # Test that column defaults are set correctly
51         ok(($rs = eval {
52             $schema->resultset("${vendor_titlecased}LoaderTest2") }),
53             'got a resultset for table with column with default value');
54
55         my $rsrc = $rs->result_source;
56
57         is eval { $rsrc->column_info('dat')->{default_value} }, 'foo',
58             'correct default_value for column with literal string default';
59
60         is eval { $rsrc->column_info('anint')->{default_value} }, 6,
61             'correct default_value for column with literal integer default';
62
63         cmp_ok eval { $rsrc->column_info('num')->{default_value} },
64             '==', 10.89,
65             'correct default_value for column with literal numeric default';
66
67         ok((my $function_default =
68             eval { $rsrc->column_info('ts')->{default_value} }),
69             'got default_value for column with function default');
70
71         is ref($function_default), 'SCALAR',
72             'default_value for function default is a SCALAR ref';
73
74         is eval { $$function_default }, 'getdate()',
75             'default_value for function default is correct';
76
77 # Test that identity columns do not have 'identity' in the data_type, and do
78 # have is_auto_increment.
79         my $identity_col_info = $schema->resultset('LoaderTest10')
80             ->result_source->column_info('id10');
81
82         is $identity_col_info->{data_type}, 'int',
83             q{'INT IDENTITY' column has data_type => 'int'};
84
85         is $identity_col_info->{is_auto_increment}, 1,
86             q{'INT IDENTITY' column has is_auto_increment => 1};
87     },
88 }}
89
90 1;