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