Fix RT#58554 (make sure FOR X is always the last part of a select)
[dbsrgits/DBIx-Class.git] / t / 745db2.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
10
11 #warn "$dsn $user $pass";
12
13 plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
14   unless ($dsn && $user);
15
16 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
17
18 my $dbh = $schema->storage->dbh;
19
20 eval { $dbh->do("DROP TABLE artist") };
21
22 $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);");
23
24 my $ars = $schema->resultset('Artist');
25 is ( $ars->count, 0, 'No rows at first' );
26
27 # test primary key handling
28 my $new = $ars->create({ name => 'foo' });
29 ok($new->artistid, "Auto-PK worked");
30
31 # test explicit key spec
32 $new = $ars->create ({ name => 'bar', artistid => 66 });
33 is($new->artistid, 66, 'Explicit PK worked');
34 $new->discard_changes;
35 is($new->artistid, 66, 'Explicit PK assigned');
36
37 # test populate
38 lives_ok (sub {
39   my @pop;
40   for (1..2) {
41     push @pop, { name => "Artist_$_" };
42   }
43   $ars->populate (\@pop);
44 });
45
46 # test populate with explicit key
47 lives_ok (sub {
48   my @pop;
49   for (1..2) {
50     push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
51   }
52   $ars->populate (\@pop);
53 });
54
55 # count what we did so far
56 is ($ars->count, 6, 'Simple count works');
57
58 # test LIMIT support
59 my $lim = $ars->search( {},
60   {
61     rows => 3,
62     offset => 4,
63     order_by => 'artistid'
64   }
65 );
66 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
67 is( $lim->all, 2, 'Number of ->all objects matches count' );
68
69 # Limit with select-lock
70 TODO: {
71   local $TODO = "Seems we can't SELECT ... FOR ... on subqueries";
72   lives_ok {
73     $schema->txn_do (sub {
74       isa_ok (
75         $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
76         'DBICTest::Schema::Artist',
77       );
78     });
79   } 'Limited FOR UPDATE select works';
80 }
81
82 # test iterator
83 $lim->reset;
84 is( $lim->next->artistid, 101, "iterator->next ok" );
85 is( $lim->next->artistid, 102, "iterator->next ok" );
86 is( $lim->next, undef, "next past end of resultset ok" );
87
88
89 my $test_type_info = {
90     'artistid' => {
91         'data_type' => 'INTEGER',
92         'is_nullable' => 0,
93         'size' => 10
94     },
95     'name' => {
96         'data_type' => 'VARCHAR',
97         'is_nullable' => 1,
98         'size' => 255
99     },
100     'charfield' => {
101         'data_type' => 'CHAR',
102         'is_nullable' => 1,
103         'size' => 10
104     },
105     'rank' => {
106         'data_type' => 'INTEGER',
107         'is_nullable' => 1,
108         'size' => 10
109     },
110 };
111
112
113 my $type_info = $schema->storage->columns_info_for('artist');
114 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
115
116 done_testing;
117
118 # clean up our mess
119 END {
120     my $dbh = eval { $schema->storage->_dbh };
121     $dbh->do("DROP TABLE artist") if $dbh;
122 }