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