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