support for Sybase SQL Anywhere through ODBC
[dbsrgits/DBIx-Class.git] / t / 749sybase_asa.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
9 # tests stolen from 748informix.t
10
11 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SYBASE_ASA_${_}" }      qw/DSN USER PASS/};
12 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SYBASE_ASA_ODBC_${_}" } qw/DSN USER PASS/};
13
14 plan skip_all => <<"EOF" unless $dsn || $dsn2;
15 Set $ENV{DBICTEST_SYBASE_ASA_DSN} and/or $ENV{DBICTEST_SYBASE_ASA_ODBC_DSN},
16 _USER and _PASS to run these tests
17 EOF
18
19 my @info = (
20   [ $dsn,  $user,  $pass  ],
21   [ $dsn2, $user2, $pass2 ],
22 );
23
24 my @handles_to_clean;
25
26 foreach my $info (@info) {
27   my ($dsn, $user, $pass) = @$info;
28
29   next unless $dsn;
30
31   my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
32
33   my $dbh = $schema->storage->dbh;
34
35   push @handles_to_clean, $dbh;
36
37   eval { $dbh->do("DROP TABLE artist") };
38
39   $dbh->do(<<EOF);
40   CREATE TABLE artist (
41     artistid INT IDENTITY PRIMARY KEY,
42     name VARCHAR(255) NULL,
43     charfield CHAR(10) NULL,
44     rank INT DEFAULT 13
45   )
46 EOF
47
48   my $ars = $schema->resultset('Artist');
49   is ( $ars->count, 0, 'No rows at first' );
50
51 # test primary key handling
52   my $new = $ars->create({ name => 'foo' });
53   ok($new->artistid, "Auto-PK worked");
54
55 # test explicit key spec
56   $new = $ars->create ({ name => 'bar', artistid => 66 });
57   is($new->artistid, 66, 'Explicit PK worked');
58   $new->discard_changes;
59   is($new->artistid, 66, 'Explicit PK assigned');
60
61 # test populate
62   lives_ok (sub {
63     my @pop;
64     for (1..2) {
65       push @pop, { name => "Artist_$_" };
66     }
67     $ars->populate (\@pop);
68   });
69
70 # test populate with explicit key
71   lives_ok (sub {
72     my @pop;
73     for (1..2) {
74       push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
75     }
76     $ars->populate (\@pop);
77   });
78
79 # count what we did so far
80   is ($ars->count, 6, 'Simple count works');
81
82 # test LIMIT support
83   my $lim = $ars->search( {},
84     {
85       rows => 3,
86       offset => 4,
87       order_by => 'artistid'
88     }
89   );
90   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
91   is( $lim->all, 2, 'Number of ->all objects matches count' );
92
93 # test iterator
94   $lim->reset;
95   is( $lim->next->artistid, 101, "iterator->next ok" );
96   is( $lim->next->artistid, 102, "iterator->next ok" );
97   is( $lim->next, undef, "next past end of resultset ok" );
98
99 # test empty insert
100   {
101     local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
102
103     lives_ok { $ars->create({}) }
104       'empty insert works';
105   }
106
107 # test blobs (stolen from 73oracle.t)
108   eval { $dbh->do('DROP TABLE bindtype_test') };
109   $dbh->do(qq[
110   CREATE TABLE bindtype_test
111   (
112     id    INT          NOT NULL PRIMARY KEY,
113     bytea INT          NULL,
114     blob  LONG BINARY  NULL,
115     clob  LONG VARCHAR NULL
116   )
117   ],{ RaiseError => 1, PrintError => 1 });
118
119   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
120   $binstr{'large'} = $binstr{'small'} x 1024;
121
122   my $maxloblen = length $binstr{'large'};
123   local $dbh->{'LongReadLen'} = $maxloblen;
124
125   my $rs = $schema->resultset('BindType');
126   my $id = 0;
127
128   foreach my $type (qw( blob clob )) {
129     foreach my $size (qw( small large )) {
130       $id++;
131
132 # turn off horrendous binary DBIC_TRACE output
133       local $schema->storage->{debug} = 0;
134
135       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
136       "inserted $size $type without dying";
137
138       ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
139     }
140   }
141 }
142
143 done_testing;
144
145 # clean up our mess
146 END {
147   foreach my $dbh (@handles_to_clean) {
148     eval { $dbh->do("DROP TABLE $_") } for qw/artist bindtype_test/;
149   }
150 }