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