Test suite wide leaktesting
[dbsrgits/DBIx-Class.git] / t / 745db2.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Try::Tiny;
7 use DBIx::Class::Optional::Dependencies ();
8 use lib qw(t/lib);
9 use DBICTest;
10
11 plan 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
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
15
16 #warn "$dsn $user $pass";
17
18 plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
19   unless ($dsn && $user);
20
21 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
22
23 my $dbh = $schema->storage->dbh;
24
25 # test RNO and name_sep detection
26 my $name_sep = $dbh->get_info(41);
27
28 is $schema->storage->sql_maker->name_sep, $name_sep,
29   'name_sep detection';
30
31 my $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
38 is $schema->storage->sql_maker->limit_dialect,
39   ($have_rno ? 'RowNumberOver' : 'FetchFirst'),
40   'limit_dialect detection';
41
42 eval { $dbh->do("DROP TABLE artist") };
43
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);");
45
46 my $ars = $schema->resultset('Artist');
47 is ( $ars->count, 0, 'No rows at first' );
48
49 # test primary key handling
50 my $new = $ars->create({ name => 'foo' });
51 ok($new->artistid, "Auto-PK worked");
52
53 # test explicit key spec
54 $new = $ars->create ({ name => 'bar', artistid => 66 });
55 is($new->artistid, 66, 'Explicit PK worked');
56 $new->discard_changes;
57 is($new->artistid, 66, 'Explicit PK assigned');
58
59 # test populate
60 lives_ok (sub {
61   my @pop;
62   for (1..2) {
63     push @pop, { name => "Artist_$_" };
64   }
65   $ars->populate (\@pop);
66 });
67
68 # test populate with explicit key
69 lives_ok (sub {
70   my @pop;
71   for (1..2) {
72     push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
73   }
74   $ars->populate (\@pop);
75 });
76
77 # count what we did so far
78 is ($ars->count, 6, 'Simple count works');
79
80 # test LIMIT support
81 my $lim = $ars->search( {},
82   {
83     rows => 3,
84     offset => 4,
85     order_by => 'artistid'
86   }
87 );
88 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
89 is( $lim->all, 2, 'Number of ->all objects matches count' );
90
91 # Limit with select-lock
92 TODO: {
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
104 # test iterator
105 $lim->reset;
106 is( $lim->next->artistid, 101, "iterator->next ok" );
107 is( $lim->next->artistid, 102, "iterator->next ok" );
108 is( $lim->next, undef, "next past end of resultset ok" );
109
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 }
129
130 my $test_type_info = {
131     'artistid' => {
132         'data_type' => 'INTEGER',
133         'is_nullable' => 0,
134         'size' => 10
135     },
136     'name' => {
137         'data_type' => 'VARCHAR',
138         'is_nullable' => 1,
139         'size' => 255
140     },
141     'charfield' => {
142         'data_type' => 'CHAR',
143         'is_nullable' => 1,
144         'size' => 10
145     },
146     'rank' => {
147         'data_type' => 'INTEGER',
148         'is_nullable' => 1,
149         'size' => 10
150     },
151 };
152
153
154 my $type_info = $schema->storage->columns_info_for('artist');
155 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
156
157 done_testing;
158
159 # clean up our mess
160 END {
161   my $dbh = eval { $schema->storage->_dbh };
162   $dbh->do("DROP TABLE artist") if $dbh;
163   undef $schema;
164 }