fixed sql mistakes from hierarchical query tests
[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
835cdc8d 69# test iterator
88d20956 70$lim->reset;
71is( $lim->next->artistid, 101, "iterator->next ok" );
72is( $lim->next->artistid, 102, "iterator->next ok" );
73is( $lim->next, undef, "next past end of resultset ok" );
25aa8311 74
843f8ecd 75
76my $test_type_info = {
77 'artistid' => {
78 'data_type' => 'INTEGER',
79 'is_nullable' => 0,
fc22fbac 80 'size' => 10
843f8ecd 81 },
82 'name' => {
83 'data_type' => 'VARCHAR',
84 'is_nullable' => 1,
85 'size' => 255
86 },
87 'charfield' => {
fc22fbac 88 'data_type' => 'CHAR',
843f8ecd 89 'is_nullable' => 1,
835cdc8d 90 'size' => 10
843f8ecd 91 },
70c29d32 92 'rank' => {
93 'data_type' => 'INTEGER',
94 'is_nullable' => 1,
835cdc8d 95 'size' => 10
70c29d32 96 },
843f8ecd 97};
98
99
3ff5b740 100my $type_info = $schema->storage->columns_info_for('artist');
843f8ecd 101is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
102
88d20956 103done_testing;
104
843f8ecd 105# clean up our mess
3ff5b740 106END {
3a4c1d89 107 my $dbh = eval { $schema->storage->_dbh };
3ff5b740 108 $dbh->do("DROP TABLE artist") if $dbh;
109}