branch pushed, removing
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DateTime::Format::Sybase;
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
11
12 plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
13   unless ($dsn && $user);
14
15 plan tests => (26 + 4*2)*2;
16
17 my @storage_types = (
18   'DBI::Sybase',
19   'DBI::Sybase::NoBindVars',
20 );
21 my $schema;
22
23 for my $storage_type (@storage_types) {
24   $schema = DBICTest::Schema->clone;
25
26   unless ($storage_type eq 'DBI::Sybase') { # autodetect
27     $schema->storage_type("::$storage_type");
28   }
29   $schema->connection($dsn, $user, $pass, {AutoCommit => 1});
30
31   $schema->storage->ensure_connected;
32
33   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
34
35   $schema->storage->dbh_do (sub {
36       my ($storage, $dbh) = @_;
37       eval { $dbh->do("DROP TABLE artist") };
38       $dbh->do(<<'SQL');
39 CREATE TABLE artist (
40    artistid INT IDENTITY PRIMARY KEY,
41    name VARCHAR(100),
42    rank INT DEFAULT 13 NOT NULL,
43    charfield CHAR(10) NULL
44 )
45 SQL
46   });
47
48   my %seen_id;
49
50 # so we start unconnected
51   $schema->storage->disconnect;
52
53 # test primary key handling
54   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
55   ok($new->artistid > 0, "Auto-PK worked");
56
57   $seen_id{$new->artistid}++;
58
59   for (1..6) {
60     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
61     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
62     $seen_id{$new->artistid}++;
63   }
64
65 # test simple count
66   is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
67
68 # test LIMIT support
69   my $it = $schema->resultset('Artist')->search({
70     artistid => { '>' => 0 }
71   }, {
72     rows => 3,
73     order_by => 'artistid',
74   });
75
76   is( $it->count, 3, "LIMIT count ok" );
77
78   is( $it->next->name, "foo", "iterator->next ok" );
79   $it->next;
80   is( $it->next->name, "Artist 2", "iterator->next ok" );
81   is( $it->next, undef, "next past end of resultset ok" );
82
83 # now try with offset
84   $it = $schema->resultset('Artist')->search({}, {
85     rows => 3,
86     offset => 3,
87     order_by => 'artistid',
88   });
89
90   is( $it->count, 3, "LIMIT with offset count ok" );
91
92   is( $it->next->name, "Artist 3", "iterator->next ok" );
93   $it->next;
94   is( $it->next->name, "Artist 5", "iterator->next ok" );
95   is( $it->next, undef, "next past end of resultset ok" );
96
97 # now try a grouped count
98   $schema->resultset('Artist')->create({ name => 'Artist 6' })
99     for (1..6);
100
101   $it = $schema->resultset('Artist')->search({}, {
102     group_by => 'name'
103   });
104
105   is( $it->count, 7, 'COUNT of GROUP_BY ok' );
106
107 # Test DateTime inflation with DATETIME
108   my @dt_types = (
109     ['DATETIME', '2004-08-21T14:36:48.080Z'],
110     ['SMALLDATETIME', '2004-08-21T14:36:00.000Z'], # minute precision
111   );
112   
113   for my $dt_type (@dt_types) {
114     my ($type, $sample_dt) = @$dt_type;
115
116     eval { $schema->storage->dbh->do("DROP TABLE track") };
117     $schema->storage->dbh->do(<<"SQL");
118 CREATE TABLE track (
119    trackid INT IDENTITY PRIMARY KEY,
120    cd INT,
121    position INT,
122    last_updated_on $type,
123 )
124 SQL
125     ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
126
127     my $row;
128     ok( $row = $schema->resultset('Track')->create({
129       last_updated_on => $dt,
130       cd => 1,
131     }));
132     ok( $row = $schema->resultset('Track')
133       ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
134       ->first
135     );
136     is( $row->updated_date, $dt, 'DateTime inflation works' );
137   }
138
139 # mostly stole the blob stuff Nniuq wrote for t/73oracle.t
140   my $dbh = $schema->storage->dbh;
141   {
142     local $SIG{__WARN__} = sub {};
143     eval { $dbh->do('DROP TABLE bindtype_test') };
144
145     $dbh->do(qq[
146       CREATE TABLE bindtype_test 
147       (
148         id    INT   PRIMARY KEY,
149         bytea INT   NULL,
150         blob  IMAGE NULL,
151         clob  TEXT  NULL
152       )
153     ],{ RaiseError => 1, PrintError => 1 });
154   }
155
156   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
157   $binstr{'large'} = $binstr{'small'} x 1024;
158
159   my $maxloblen = length $binstr{'large'};
160   note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
161   local $dbh->{'LongReadLen'} = $maxloblen;
162
163   my $rs = $schema->resultset('BindType');
164   my $id = 0;
165
166   foreach my $type (qw(blob clob)) {
167     foreach my $size (qw(small large)) {
168       no warnings 'uninitialized';
169       $id++;
170
171       eval { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) };
172       ok(!$@, "inserted $size $type without dying");
173       diag $@ if $@;
174
175       ok(eval {
176         $rs->search({ id=> $id }, { select => [$type] })->single->$type
177       } eq $binstr{$size}, "verified inserted $size $type" );
178     }
179   }
180 }
181
182 # clean up our mess
183 END {
184   if (my $dbh = eval { $schema->storage->_dbh }) {
185     $dbh->do('DROP TABLE artist');
186     $dbh->do('DROP TABLE track');
187     $dbh->do('DROP TABLE bindtype_test');
188   }
189 }