Final count tests
[dbsrgits/DBIx-Class-Historic.git] / t / 73oracle.t
CommitLineData
cb464582 1{
2 package # hide from PAUSE
3 DBICTest::Schema::ArtistFQN;
4
5 use base 'DBIx::Class::Core';
6
7 __PACKAGE__->table(
8 defined $ENV{DBICTEST_ORA_USER}
9 ? $ENV{DBICTEST_ORA_USER} . '.artist'
10 : 'artist'
11 );
12 __PACKAGE__->add_columns(
13 'artistid' => {
14 data_type => 'integer',
15 is_auto_increment => 1,
16 },
17 'name' => {
18 data_type => 'varchar',
19 size => 100,
20 is_nullable => 1,
21 },
22 );
23 __PACKAGE__->set_primary_key('artistid');
24
25 1;
26}
27
70350518 28use strict;
29use warnings;
30
31use Test::More;
32use lib qw(t/lib);
33use DBICTest;
0567538f 34
35my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
36
70350518 37plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
39b8d119 38 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
39 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
0567538f 40 unless ($dsn && $user && $pass);
41
f12f0d97 42plan tests => 26;
0567538f 43
cb464582 44DBICTest::Schema->load_classes('ArtistFQN');
3ff5b740 45my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
0567538f 46
3ff5b740 47my $dbh = $schema->storage->dbh;
0567538f 48
49eval {
50 $dbh->do("DROP SEQUENCE artist_seq");
39b8d119 51 $dbh->do("DROP SEQUENCE pkid1_seq");
52 $dbh->do("DROP SEQUENCE pkid2_seq");
53 $dbh->do("DROP SEQUENCE nonpkid_seq");
0567538f 54 $dbh->do("DROP TABLE artist");
39b8d119 55 $dbh->do("DROP TABLE sequence_test");
2660b14e 56 $dbh->do("DROP TABLE cd");
57 $dbh->do("DROP TABLE track");
0567538f 58};
59$dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
39b8d119 60$dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
61$dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
62$dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
7c4ead2d 63$dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
39b8d119 64$dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
2660b14e 65$dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
e8e971f2 66$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
2660b14e 67
0567538f 68$dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
39b8d119 69$dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
0567538f 70$dbh->do(qq{
71 CREATE OR REPLACE TRIGGER artist_insert_trg
72 BEFORE INSERT ON artist
73 FOR EACH ROW
74 BEGIN
75 IF :new.artistid IS NULL THEN
76 SELECT artist_seq.nextval
77 INTO :new.artistid
78 FROM DUAL;
79 END IF;
80 END;
81});
82
3ff5b740 83# This is in Core now, but it's here just to test that it doesn't break
84$schema->class('Artist')->load_components('PK::Auto');
85# These are compat shims for PK::Auto...
86$schema->class('CD')->load_components('PK::Auto::Oracle');
87$schema->class('Track')->load_components('PK::Auto::Oracle');
0567538f 88
89# test primary key handling
3ff5b740 90my $new = $schema->resultset('Artist')->create({ name => 'foo' });
c8f4b52b 91is($new->artistid, 1, "Oracle Auto-PK worked");
0567538f 92
cb464582 93# test again with fully-qualified table name
94$new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
95is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
96
2660b14e 97# test join with row count ambiguity
3ff5b740 98my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
99my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
100my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
2660b14e 101 { join => 'cd',
102 rows => 2 }
103);
104
105is($tjoin->next->title, 'Track1', "ambiguous column ok");
106
286f32b3 107# check count distinct with multiple columns
3ff5b740 108my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
11d68671 109
3ff5b740 110my $tcount = $schema->resultset('Track')->search(
11d68671 111 {},
112 {
113 select => [ qw/position title/ ],
114 distinct => 1,
115 }
116);
f12f0d97 117is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 118
119$tcount = $schema->resultset('Track')->search(
120 {},
121 {
122 columns => [ qw/position title/ ],
123 distinct => 1,
124 }
125);
f12f0d97 126is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 127
11d68671 128$tcount = $schema->resultset('Track')->search(
129 {},
130 {
131 group_by => [ qw/position title/ ]
132 }
133);
f12f0d97 134is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 135
0567538f 136# test LIMIT support
137for (1..6) {
3ff5b740 138 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
0567538f 139}
3ff5b740 140my $it = $schema->resultset('Artist')->search( {},
0567538f 141 { rows => 3,
cb464582 142 offset => 3,
0567538f 143 order_by => 'artistid' }
144);
145is( $it->count, 3, "LIMIT count ok" );
146is( $it->next->name, "Artist 2", "iterator->next ok" );
147$it->next;
148$it->next;
149is( $it->next, undef, "next past end of resultset ok" );
150
e8e971f2 151{
152 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
153 my @results = $rs->all;
154 is( scalar @results, 1, "Group by with limit OK" );
155}
156
ccd6f984 157# test auto increment using sequences WITHOUT triggers
39b8d119 158for (1..5) {
159 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
160 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
161 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
162 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
163}
164my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
165is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
ccd6f984 166
0567538f 167# clean up our mess
3ff5b740 168END {
fe0d48d3 169 if($schema && ($dbh = $schema->storage->dbh)) {
3ff5b740 170 $dbh->do("DROP SEQUENCE artist_seq");
39b8d119 171 $dbh->do("DROP SEQUENCE pkid1_seq");
172 $dbh->do("DROP SEQUENCE pkid2_seq");
173 $dbh->do("DROP SEQUENCE nonpkid_seq");
3ff5b740 174 $dbh->do("DROP TABLE artist");
39b8d119 175 $dbh->do("DROP TABLE sequence_test");
3ff5b740 176 $dbh->do("DROP TABLE cd");
177 $dbh->do("DROP TABLE track");
178 }
179}
0567538f 180