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