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