check database driver dependencies for tests
[dbsrgits/DBIx-Class.git] / t / 745db2.t
CommitLineData
70350518 1use strict;
835cdc8d 2use warnings;
70350518 3
4use Test::More;
88d20956 5use Test::Exception;
96eacdb7 6use Try::Tiny;
199fbc45 7use DBIx::Class::Optional::Dependencies ();
70350518 8use lib qw(t/lib);
9use DBICTest;
843f8ecd 10
199fbc45 11plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_db2')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_db2');
13
843f8ecd 14my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
15
16#warn "$dsn $user $pass";
17
58d387fe 18plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
843f8ecd 19 unless ($dsn && $user);
20
3ff5b740 21my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
843f8ecd 22
3ff5b740 23my $dbh = $schema->storage->dbh;
843f8ecd 24
96eacdb7 25# test RNO and name_sep detection
26my $name_sep = $dbh->get_info(41);
27
28is $schema->storage->sql_maker->name_sep, $name_sep,
29 'name_sep detection';
30
31my $have_rno = try {
32 $dbh->selectrow_array(
33"SELECT row_number() OVER (ORDER BY 1) FROM sysibm${name_sep}sysdummy1"
34 );
35 1;
36};
37
38is $schema->storage->sql_maker->limit_dialect,
39 ($have_rno ? 'RowNumberOver' : 'FetchFirst'),
40 'limit_dialect detection';
41
88c00130 42eval { $dbh->do("DROP TABLE artist") };
843f8ecd 43
4e88dfc4 44$dbh->do("CREATE TABLE artist (artistid INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
843f8ecd 45
25aa8311 46my $ars = $schema->resultset('Artist');
88d20956 47is ( $ars->count, 0, 'No rows at first' );
25aa8311 48
835cdc8d 49# test primary key handling
25aa8311 50my $new = $ars->create({ name => 'foo' });
843f8ecd 51ok($new->artistid, "Auto-PK worked");
52
835cdc8d 53# test explicit key spec
88d20956 54$new = $ars->create ({ name => 'bar', artistid => 66 });
55is($new->artistid, 66, 'Explicit PK worked');
56$new->discard_changes;
57is($new->artistid, 66, 'Explicit PK assigned');
58
835cdc8d 59# test populate
88d20956 60lives_ok (sub {
61 my @pop;
62 for (1..2) {
63 push @pop, { name => "Artist_$_" };
64 }
65 $ars->populate (\@pop);
66});
67
835cdc8d 68# test populate with explicit key
88d20956 69lives_ok (sub {
70 my @pop;
71 for (1..2) {
72 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
73 }
74 $ars->populate (\@pop);
75});
835cdc8d 76
77# count what we did so far
88d20956 78is ($ars->count, 6, 'Simple count works');
79
835cdc8d 80# test LIMIT support
88d20956 81my $lim = $ars->search( {},
25aa8311 82 {
83 rows => 3,
88d20956 84 offset => 4,
25aa8311 85 order_by => 'artistid'
86 }
843f8ecd 87);
835cdc8d 88is( $lim->count, 2, 'ROWS+OFFSET count ok' );
88d20956 89is( $lim->all, 2, 'Number of ->all objects matches count' );
25aa8311 90
e5372da4 91# Limit with select-lock
92TODO: {
93 local $TODO = "Seems we can't SELECT ... FOR ... on subqueries";
94 lives_ok {
95 $schema->txn_do (sub {
96 isa_ok (
97 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
98 'DBICTest::Schema::Artist',
99 );
100 });
101 } 'Limited FOR UPDATE select works';
102}
103
835cdc8d 104# test iterator
88d20956 105$lim->reset;
106is( $lim->next->artistid, 101, "iterator->next ok" );
107is( $lim->next->artistid, 102, "iterator->next ok" );
108is( $lim->next, undef, "next past end of resultset ok" );
25aa8311 109
96eacdb7 110# test FetchFirst limit dialect syntax
111{
112 local $schema->storage->sql_maker->{limit_dialect} = 'FetchFirst';
113
114 my $lim = $ars->search({}, {
115 rows => 3,
116 offset => 2,
117 order_by => 'artistid',
118 });
119
120 is $lim->count, 3, 'fetch first limit count ok';
121
122 is $lim->all, 3, 'fetch first number of ->all objects matches count';
123
124 is $lim->next->artistid, 3, 'iterator->next ok';
125 is $lim->next->artistid, 66, 'iterator->next ok';
126 is $lim->next->artistid, 101, 'iterator->next ok';
127 is $lim->next, undef, 'iterator->next past end of resultset ok';
128}
843f8ecd 129
130my $test_type_info = {
131 'artistid' => {
132 'data_type' => 'INTEGER',
133 'is_nullable' => 0,
fc22fbac 134 'size' => 10
843f8ecd 135 },
136 'name' => {
137 'data_type' => 'VARCHAR',
138 'is_nullable' => 1,
139 'size' => 255
140 },
141 'charfield' => {
fc22fbac 142 'data_type' => 'CHAR',
843f8ecd 143 'is_nullable' => 1,
835cdc8d 144 'size' => 10
843f8ecd 145 },
70c29d32 146 'rank' => {
147 'data_type' => 'INTEGER',
148 'is_nullable' => 1,
835cdc8d 149 'size' => 10
70c29d32 150 },
843f8ecd 151};
152
153
3ff5b740 154my $type_info = $schema->storage->columns_info_for('artist');
843f8ecd 155is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
156
88d20956 157done_testing;
158
843f8ecd 159# clean up our mess
3ff5b740 160END {
3a4c1d89 161 my $dbh = eval { $schema->storage->_dbh };
3ff5b740 162 $dbh->do("DROP TABLE artist") if $dbh;
163}