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