ec5e53eb69d10cb6467e53b698d8736021a21875
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIx::Class::Storage::DBI::Sybase::DateTime;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
10
11 plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
12   unless ($dsn && $user);
13
14 plan tests => (16 + 4*2)*2;
15
16 my @storage_types = (
17   'DBI::Sybase',
18   'DBI::Sybase::NoBindVars',
19 );
20 my $schema;
21
22 for my $storage_type (@storage_types) {
23   $schema = DBICTest::Schema->clone;
24
25   unless ($storage_type eq 'DBI::Sybase') { # autodetect
26     $schema->storage_type("::$storage_type");
27   }
28   $schema->connection($dsn, $user, $pass, {AutoCommit => 1});
29
30   $schema->storage->ensure_connected;
31
32   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
33
34   $schema->storage->dbh_do (sub {
35       my ($storage, $dbh) = @_;
36       eval { $dbh->do("DROP TABLE artist") };
37       $dbh->do(<<'SQL');
38 CREATE TABLE artist (
39    artistid INT IDENTITY PRIMARY KEY,
40    name VARCHAR(100),
41    rank INT DEFAULT 13 NOT NULL,
42    charfield CHAR(10) NULL
43 )
44 SQL
45   });
46
47   my %seen_id;
48
49 # so we start unconnected
50   $schema->storage->disconnect;
51
52 # test primary key handling
53   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54   ok($new->artistid > 0, "Auto-PK worked");
55
56   $seen_id{$new->artistid}++;
57
58 # test LIMIT support
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 ## avoid quoting bug with NoBindVars for now
66 #  my $it = $schema->resultset('Artist')->search({artistid => { '>' => 0 }}, {
67
68   my $it = $schema->resultset('Artist')->search({}, {
69     rows => 3,
70     order_by => 'artistid',
71   });
72
73   is( $it->count, 3, "LIMIT count ok" );
74
75   is( $it->next->name, "foo", "iterator->next ok" );
76   $it->next;
77   is( $it->next->name, "Artist 2", "iterator->next ok" );
78   is( $it->next, undef, "next past end of resultset ok" );
79
80 # now try with offset
81   $it = $schema->resultset('Artist')->search({}, {
82     rows => 3,
83     offset => 3,
84     order_by => 'artistid',
85   });
86
87   is( $it->count, 3, "LIMIT with offset count ok" );
88
89   is( $it->next->name, "Artist 3", "iterator->next ok" );
90   $it->next;
91   is( $it->next->name, "Artist 5", "iterator->next ok" );
92   is( $it->next, undef, "next past end of resultset ok" );
93
94   SKIP: {
95     skip 'quoting bug with NoBindVars', 4*2
96         if $storage_type eq 'DBI::Sybase::NoBindVars';
97
98 # Test DateTime inflation with DATETIME
99     my @dt_types = (
100       ['DATETIME', '2004-08-21T14:36:48.080Z'],
101       ['SMALLDATETIME', '2004-08-21T14:36:00.000Z'], # minute precision
102     );
103     
104     for my $dt_type (@dt_types) {
105       my ($type, $sample_dt) = @$dt_type;
106
107       eval { $schema->storage->dbh->do("DROP TABLE track") };
108       $schema->storage->dbh->do(<<"SQL");
109 CREATE TABLE track (
110    trackid INT IDENTITY PRIMARY KEY,
111    cd INT,
112    position INT,
113    last_updated_on $type,
114 )
115 SQL
116       ok(my $dt = DBIx::Class::Storage::DBI::Sybase::DateTime
117         ->parse_datetime($sample_dt));
118
119       my $row;
120       ok( $row = $schema->resultset('Track')->create({
121         last_updated_on => $dt,
122         cd => 1,
123       }));
124       ok( $row = $schema->resultset('Track')
125         ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
126         ->first
127       );
128       is( $row->updated_date, $dt, 'DateTime inflation works' );
129     }
130   }
131 }
132
133 # clean up our mess
134 END {
135   if (my $dbh = eval { $schema->storage->_dbh }) {
136     $dbh->do('DROP TABLE artist');
137     $dbh->do('DROP TABLE track');
138   }
139 }