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