make set_from_related handle undef
[dbsrgits/DBIx-Class.git] / t / run / 13oracle.tl
1 sub run_tests {
2 my $schema = shift;
3
4 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
5
6 plan skip_all, 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
7   'Warning: This test drops and creates tables called \'artist\', \'cd\' and \'track\''
8   unless ($dsn && $user && $pass);
9
10 plan tests => 6;
11
12 DBICTest::Schema->compose_connection('OraTest' => $dsn, $user, $pass);
13
14 my $dbh = OraTest->schema->storage->dbh;
15
16 eval {
17   $dbh->do("DROP SEQUENCE artist_seq");
18   $dbh->do("DROP TABLE artist");
19   $dbh->do("DROP TABLE cd");
20   $dbh->do("DROP TABLE track");
21 };
22 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
23 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255))");
24 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
25 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255))");
26
27 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
28 $dbh->do(qq{
29   CREATE OR REPLACE TRIGGER artist_insert_trg
30   BEFORE INSERT ON artist
31   FOR EACH ROW
32   BEGIN
33     IF :new.artistid IS NULL THEN
34       SELECT artist_seq.nextval
35       INTO :new.artistid
36       FROM DUAL;
37     END IF;
38   END;
39 });
40
41 OraTest::Artist->load_components('PK::Auto');
42 OraTest::CD->load_components('PK::Auto::Oracle');
43 OraTest::Track->load_components('PK::Auto::Oracle');
44
45 # test primary key handling
46 my $new = OraTest::Artist->create({ name => 'foo' });
47 is($new->artistid, 1, "Oracle Auto-PK worked");
48
49 # test join with row count ambiguity
50 my $cd = OraTest::CD->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
51 my $track = OraTest::Track->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
52 my $tjoin = OraTest::Track->search({ 'me.title' => 'Track1'},
53         { join => 'cd',
54           rows => 2 }
55 );
56
57 is($tjoin->next->title, 'Track1', "ambiguous column ok");
58
59 # check count distinct with multiple columns
60 my $other_track = OraTest::Track->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
61 my $tcount = OraTest::Track->search(
62     {},
63     {
64         select => [{count => {distinct => ['position', 'title']}}],
65         as => ['count']
66     }
67   );
68
69 is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
70
71 # test LIMIT support
72 for (1..6) {
73     OraTest::Artist->create({ name => 'Artist ' . $_ });
74 }
75 my $it = OraTest::Artist->search( {},
76     { rows => 3,
77       offset => 2,
78       order_by => 'artistid' }
79 );
80 is( $it->count, 3, "LIMIT count ok" );
81 is( $it->next->name, "Artist 2", "iterator->next ok" );
82 $it->next;
83 $it->next;
84 is( $it->next, undef, "next past end of resultset ok" );
85
86 # clean up our mess
87 $dbh->do("DROP SEQUENCE artist_seq");
88 $dbh->do("DROP TABLE artist");
89 $dbh->do("DROP TABLE cd");
90 $dbh->do("DROP TABLE track");
91
92 }
93
94 1;