better size introspection for Sybase
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 15sybase_common.t
1 use strict;
2 use lib qw(t/lib);
3 use dbixcsl_common_tests;
4 use Test::More;
5 use Test::Exception;
6 use List::MoreUtils 'apply';
7
8 my $dsn      = $ENV{DBICTEST_SYBASE_DSN} || '';
9 my $user     = $ENV{DBICTEST_SYBASE_USER} || '';
10 my $password = $ENV{DBICTEST_SYBASE_PASS} || '';
11
12 my $tester = dbixcsl_common_tests->new(
13     vendor      => 'sybase',
14     auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
15     dsn         => $dsn,
16     user        => $user,
17     password    => $password,
18     extra       => {
19         create  => [
20             q{
21                 CREATE TABLE sybase_loader_test1 (
22                     id INTEGER IDENTITY NOT NULL PRIMARY KEY,
23                     ts timestamp,
24                     charfield VARCHAR(10) DEFAULT 'foo',
25                     computed_dt AS getdate()
26                 )
27             },
28 # Test data types, see http://ispirer.com/wiki/sqlways/sybase/data-types
29 # XXX handle FLOAT(P) at some point
30             q{
31                 CREATE TABLE sybase_loader_test2 (
32                     id INTEGER IDENTITY NOT NULL PRIMARY KEY,
33                     a_text TEXT,
34                     a_unitext UNITEXT,
35                     an_image IMAGE,
36                     a_bigint BIGINT,
37                     an_int INT,
38                     an_integer INTEGER,
39                     a_smallint SMALLINT,
40                     a_tinyint TINYINT,
41                     a_real REAL,
42                     a_double_precision DOUBLE PRECISION,
43                     a_date DATE,
44                     a_time TIME,
45                     a_datetime DATETIME,
46                     a_smalldatetime SMALLDATETIME,
47                     a_money MONEY,
48                     a_smallmoney SMALLMONEY,
49                     a_timestamp timestamp,
50                     a_bit BIT,
51                     a_char_with_precision CHAR(2),
52                     an_nchar_with_precision NCHAR(2),
53                     a_unichar_with_precision UNICHAR(2),
54                     a_varchar_with_precision VARCHAR(2),
55                     an_nvarchar_with_precision NVARCHAR(2),
56                     a_univarchar_with_precision UNIVARCHAR(2),
57                     a_float FLOAT,
58                     a_binary_with_precision BINARY(2),
59                     a_varbinary_with_precision VARBINARY(2),
60                     the_numeric NUMERIC(6,3),
61                     the_decimal DECIMAL(6,3)
62                 )
63             },
64         ],
65         drop  => [ qw/ sybase_loader_test1 sybase_loader_test2 / ],
66         count => 38,
67         run   => sub {
68             my ($schema, $monikers, $classes) = @_;
69
70             my $rs = $schema->resultset($monikers->{sybase_loader_test1});
71             my $rsrc = $rs->result_source;
72
73             is $rsrc->column_info('id')->{data_type},
74                 'int',
75                 'INTEGER IDENTITY data_type is correct';
76
77             is $rsrc->column_info('id')->{is_auto_increment},
78                 1,
79                 'INTEGER IDENTITY is_auto_increment => 1';
80
81             is $rsrc->column_info('ts')->{data_type},
82                'timestamp',
83                'timestamps have the correct data_type';
84
85             is $rsrc->column_info('charfield')->{data_type},
86                 'varchar',
87                 'VARCHAR has correct data_type';
88
89             is $rsrc->column_info('charfield')->{default_value},
90                 'foo',
91                 'constant DEFAULT is correct';
92
93             is $rsrc->column_info('charfield')->{size},
94                 10,
95                 'VARCHAR(10) has correct size';
96
97             ok ((exists $rsrc->column_info('computed_dt')->{data_type}
98               && (not defined $rsrc->column_info('computed_dt')->{data_type})),
99                 'data_type for computed column exists and is undef')
100             or diag "Data type is: ",
101                 $rsrc->column_info('computed_dt')->{data_type}
102             ;
103
104             my $computed_dt_default =
105                 $rsrc->column_info('computed_dt')->{default_value};
106
107             ok ((ref $computed_dt_default eq 'SCALAR'),
108                 'default_value for computed column is a scalar ref')
109             or diag "default_value is: ", $computed_dt_default
110             ;
111
112             eval { is $$computed_dt_default,
113                 'getdate()',
114                 'default_value for computed column is correct' };
115
116             $rsrc = $schema->resultset($monikers->{sybase_loader_test2})
117                 ->result_source;
118             my @type_columns = grep /^a/, $rsrc->columns;
119             my @without_precision = grep !/_with_precision\z/, @type_columns;
120             my @with_precision    = grep  /_with_precision\z/, @type_columns;
121             my %with_precision;
122             @with_precision{
123                 apply { s/_with_precision\z// } @with_precision
124             } = ();
125
126             for my $col (@without_precision) {
127                 my ($data_type) = $col =~ /^an?_(.*)/;
128                 $data_type =~ s/_/ /g;
129
130                 ok((not exists $rsrc->column_info($col)->{size}),
131                     "$data_type " .
132                     (exists $with_precision{$col} ? 'without precision ' : '') .
133                     "has no 'size' column_info")
134                 or diag "size is ".$rsrc->column_info($col)->{size}."\n";
135             }
136
137             for my $col (@with_precision) {
138                 my ($data_type) = $col =~ /^an?_(.*)_with_precision\z/;
139                 ($data_type = uc $data_type) =~ s/_/ /g;
140                 my $size = $rsrc->column_info($col)->{size};
141
142                 is $size, 2,
143                   "$data_type with precision has a correct 'size' column_info";
144             }
145
146             is_deeply $rsrc->column_info('the_numeric')->{size}, [6,3],
147                 'size for NUMERIC(precision, scale) is correct';
148
149             is_deeply $rsrc->column_info('the_decimal')->{size}, [6,3],
150                 'size for DECIMAL(precision, scale) is correct';
151
152         },
153     },
154 );
155
156 if( !$dsn || !$user ) {
157     $tester->skip_tests('You need to set the DBICTEST_SYBASE_DSN, _USER, and _PASS environment variables');
158 }
159 else {
160     $tester->run_tests();
161 }