fixed 73oracle.t
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
9
10 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
11   'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
12   ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
13   unless ($dsn && $user && $pass);
14
15 plan tests => 23;
16
17 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
18
19 my $dbh = $schema->storage->dbh;
20
21 eval {
22   $dbh->do("DROP SEQUENCE artist_seq");
23   $dbh->do("DROP SEQUENCE pkid1_seq");
24   $dbh->do("DROP SEQUENCE pkid2_seq");
25   $dbh->do("DROP SEQUENCE nonpkid_seq");
26   $dbh->do("DROP TABLE artist");
27   $dbh->do("DROP TABLE sequence_test");
28   $dbh->do("DROP TABLE cd");
29   $dbh->do("DROP TABLE track");
30 };
31 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
32 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
33 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
34 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
35 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38))");
36 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
37 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
38 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
39
40 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
41 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
42 $dbh->do(qq{
43   CREATE OR REPLACE TRIGGER artist_insert_trg
44   BEFORE INSERT ON artist
45   FOR EACH ROW
46   BEGIN
47     IF :new.artistid IS NULL THEN
48       SELECT artist_seq.nextval
49       INTO :new.artistid
50       FROM DUAL;
51     END IF;
52   END;
53 });
54
55 # This is in Core now, but it's here just to test that it doesn't break
56 $schema->class('Artist')->load_components('PK::Auto');
57 # These are compat shims for PK::Auto...
58 $schema->class('CD')->load_components('PK::Auto::Oracle');
59 $schema->class('Track')->load_components('PK::Auto::Oracle');
60
61 # test primary key handling
62 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
63 is($new->artistid, 1, "Oracle Auto-PK worked");
64
65 # test join with row count ambiguity
66 my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
67 my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
68 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
69         { join => 'cd',
70           rows => 2 }
71 );
72
73 is($tjoin->next->title, 'Track1', "ambiguous column ok");
74
75 # check count distinct with multiple columns
76 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
77 my $tcount = $schema->resultset('Track')->search(
78     {},
79     {
80         select => [{count => {distinct => ['position', 'title']}}],
81         as => ['count']
82     }
83   );
84
85 is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
86
87 # test LIMIT support
88 for (1..6) {
89     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
90 }
91 my $it = $schema->resultset('Artist')->search( {},
92     { rows => 3,
93       offset => 2,
94       order_by => 'artistid' }
95 );
96 is( $it->count, 3, "LIMIT count ok" );
97 is( $it->next->name, "Artist 2", "iterator->next ok" );
98 $it->next;
99 $it->next;
100 is( $it->next, undef, "next past end of resultset ok" );
101
102 {
103   my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
104   my @results = $rs->all;
105   is( scalar @results, 1, "Group by with limit OK" );
106 }
107
108 # test auto increment using sequences WITHOUT triggers
109 for (1..5) {
110     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
111     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
112     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
113     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
114 }
115 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
116 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
117
118 # clean up our mess
119 END {
120     if($dbh) {
121         $dbh->do("DROP SEQUENCE artist_seq");
122         $dbh->do("DROP SEQUENCE pkid1_seq");
123         $dbh->do("DROP SEQUENCE pkid2_seq");
124         $dbh->do("DROP SEQUENCE nonpkid_seq");
125         $dbh->do("DROP TABLE artist");
126         $dbh->do("DROP TABLE sequence_test");
127         $dbh->do("DROP TABLE cd");
128         $dbh->do("DROP TABLE track");
129     }
130 }
131