release 0.05000
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 15sybase_common.t
CommitLineData
fe67d343 1use strict;
2use lib qw(t/lib);
3use dbixcsl_common_tests;
804c115d 4use Test::More;
2d1dc6de 5use Test::Exception;
6ecee584 6use List::MoreUtils 'apply';
f9f65ded 7
fe67d343 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(
7cb9244f 13 vendor => 'sybase',
fe67d343 14 auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
15 dsn => $dsn,
16 user => $user,
17 password => $password,
804c115d 18 extra => {
19 create => [
20 q{
21 CREATE TABLE sybase_loader_test1 (
22 id INTEGER IDENTITY NOT NULL PRIMARY KEY,
de82711a 23 ts timestamp,
24 charfield VARCHAR(10) DEFAULT 'foo',
25 computed_dt AS getdate()
804c115d 26 )
27 },
6ecee584 28# Test data types, see http://ispirer.com/wiki/sqlways/sybase/data-types
29# XXX handle FLOAT(P) at some point
ca7feebf 30# ( http://msdn.microsoft.com/en-us/library/aa258876(SQL.80).aspx )
6ecee584 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 },
804c115d 65 ],
6ecee584 66 drop => [ qw/ sybase_loader_test1 sybase_loader_test2 / ],
67 count => 38,
804c115d 68 run => sub {
69 my ($schema, $monikers, $classes) = @_;
70
71 my $rs = $schema->resultset($monikers->{sybase_loader_test1});
de82711a 72 my $rsrc = $rs->result_source;
73
74 is $rsrc->column_info('id')->{data_type},
2fb9a4b3 75 'int',
de82711a 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';
804c115d 81
2fb9a4b3 82 is $rsrc->column_info('ts')->{data_type},
83 'timestamp',
84 'timestamps have the correct data_type';
de82711a 85
86 is $rsrc->column_info('charfield')->{data_type},
87 'varchar',
88 'VARCHAR has correct data_type';
89
2fb9a4b3 90 is $rsrc->column_info('charfield')->{default_value},
91 'foo',
92 'constant DEFAULT is correct';
de82711a 93
94 is $rsrc->column_info('charfield')->{size},
95 10,
96 'VARCHAR(10) has correct size';
97
db4d62ad 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 ;
2d1dc6de 104
2fb9a4b3 105 my $computed_dt_default =
106 $rsrc->column_info('computed_dt')->{default_value};
2d1dc6de 107
2fb9a4b3 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 ;
2d1dc6de 112
2fb9a4b3 113 eval { is $$computed_dt_default,
114 'getdate()',
115 'default_value for computed column is correct' };
6ecee584 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
804c115d 153 },
154 },
fe67d343 155);
156
157if( !$dsn || !$user ) {
158 $tester->skip_tests('You need to set the DBICTEST_SYBASE_DSN, _USER, and _PASS environment variables');
159}
160else {
161 $tester->run_tests();
162}