And document the last changes
[dbsrgits/DBIx-Class.git] / t / 72pg.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
d6feb60f 7use DBICTest::Stats;
70350518 8
89add887 9{
10 package DBICTest::Schema::Casecheck;
11
12 use strict;
13 use warnings;
14 use base 'DBIx::Class';
15
3ff5b740 16 __PACKAGE__->load_components(qw/Core/);
89add887 17 __PACKAGE__->table('casecheck');
18 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
d9916234 19 __PACKAGE__->column_info_from_storage(1);
89add887 20 __PACKAGE__->set_primary_key('id');
21
22}
23
0567538f 24my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
25
26#warn "$dsn $user $pass";
27
70350518 28plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
ae515736 29 . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
0567538f 30
ddf66ced 31plan tests => 34;
0567538f 32
89add887 33DBICTest::Schema->load_classes( 'Casecheck' );
d6feb60f 34my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { auto_savepoint => 1});
0567538f 35
114780ee 36# Check that datetime_parser returns correctly before we explicitly connect.
37SKIP: {
38 eval { require DateTime::Format::Pg };
39 skip "DateTime::Format::Pg required", 2 if $@;
40
41 my $store = ref $schema->storage;
42 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
43
44 my $parser = $schema->storage->datetime_parser;
45 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
46}
47
3ff5b740 48my $dbh = $schema->storage->dbh;
d6feb60f 49my $stats = new DBICTest::Stats();
50$schema->storage->debugobj($stats);
51$schema->storage->debug(1);
52
3ff5b740 53$schema->source("Artist")->name("testschema.artist");
39b8d119 54$schema->source("SequenceTest")->name("testschema.sequence_test");
4d272ce5 55$dbh->do("CREATE SCHEMA testschema;");
ae515736 56$dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
39b8d119 57$dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
58$dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
59$dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
60$dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
ae515736 61ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
0567538f 62
3ff5b740 63# This is in Core now, but it's here just to test that it doesn't break
64$schema->class('Artist')->load_components('PK::Auto');
0567538f 65
3ff5b740 66my $new = $schema->resultset('Artist')->create({ name => 'foo' });
0567538f 67
b6b65a3e 68is($new->artistid, 1, "Auto-PK worked");
69
3ff5b740 70$new = $schema->resultset('Artist')->create({ name => 'bar' });
b6b65a3e 71
72is($new->artistid, 2, "Auto-PK worked");
73
a953d8d9 74my $test_type_info = {
75 'artistid' => {
103e3e03 76 'data_type' => 'integer',
77 'is_nullable' => 0,
fc22fbac 78 'size' => 4,
a953d8d9 79 },
80 'name' => {
103e3e03 81 'data_type' => 'character varying',
82 'is_nullable' => 1,
ae515736 83 'size' => 100,
fc22fbac 84 'default_value' => undef,
103e3e03 85 },
86 'charfield' => {
87 'data_type' => 'character',
a953d8d9 88 'is_nullable' => 1,
fc22fbac 89 'size' => 10,
90 'default_value' => undef,
103e3e03 91 },
a953d8d9 92};
93
fc22fbac 94
3ff5b740 95my $type_info = $schema->storage->columns_info_for('testschema.artist');
fc22fbac 96my $artistid_defval = delete $type_info->{artistid}->{default_value};
97like($artistid_defval,
4d272ce5 98 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 99 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
100is_deeply($type_info, $test_type_info,
101 'columns_info_for - column data types');
a953d8d9 102
3ff5b740 103my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 104is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
105
3ff5b740 106my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 107is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
108
3ff5b740 109my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 110is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
111
95ba7ee4 112# Test SELECT ... FOR UPDATE
113my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
114if ($HaveSysSigAction) {
115 Sys::SigAction->import( 'set_sig_handler' );
116}
117
118SKIP: {
119 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
120 # create a new schema
121 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
122 $schema2->source("Artist")->name("testschema.artist");
123
124 $schema->txn_do( sub {
125 my $artist = $schema->resultset('Artist')->search(
126 {
127 artistid => 1
128 },
129 {
130 for => 'update'
131 }
132 )->first;
133 is($artist->artistid, 1, "select for update returns artistid = 1");
134
135 my $artist_from_schema2;
136 my $error_ok = 0;
137 eval {
138 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
139 alarm(2);
140 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
141 $artist_from_schema2->name('fooey');
142 $artist_from_schema2->update;
143 alarm(0);
144 };
145 if (my $e = $@) {
146 $error_ok = $e =~ /DBICTestTimeout/;
147 }
148
149 # Make sure that an error was raised, and that the update failed
150 ok($error_ok, "update from second schema times out");
151 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
152 });
153}
154
155SKIP: {
156 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
157 # create a new schema
158 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
159 $schema2->source("Artist")->name("testschema.artist");
160
161 $schema->txn_do( sub {
162 my $artist = $schema->resultset('Artist')->search(
163 {
164 artistid => 1
165 },
166 )->first;
167 is($artist->artistid, 1, "select for update returns artistid = 1");
168
169 my $artist_from_schema2;
170 my $error_ok = 0;
171 eval {
172 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
173 alarm(2);
174 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
175 $artist_from_schema2->name('fooey');
176 $artist_from_schema2->update;
177 alarm(0);
178 };
179 if (my $e = $@) {
180 $error_ok = $e =~ /DBICTestTimeout/;
181 }
182
183 # Make sure that an error was NOT raised, and that the update succeeded
184 ok(! $error_ok, "update from second schema DOES NOT timeout");
185 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
186 });
187}
188
d6feb60f 189SKIP: {
190 skip "Oracle Auto-PK tests are broken", 16;
191 # test auto increment using sequences WITHOUT triggers
192
193 for (1..5) {
39b8d119 194 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
195 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
196 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
197 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
d6feb60f 198 }
199 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
200 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
39b8d119 201}
d6feb60f 202
ddf66ced 203$schema->txn_begin;
d6feb60f 204
205my $arty = $schema->resultset('Artist')->find(1);
206
ddf66ced 207my $name = $arty->name;
d6feb60f 208
ddf66ced 209$schema->storage->_svp_begin ("mysavepoint");
d6feb60f 210
211$arty->update({ name => 'Jheephizzy' });
212
ddf66ced 213$arty->discard_changes;
d6feb60f 214
ddf66ced 215cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
d6feb60f 216
ddf66ced 217$schema->storage->_svp_rollback ("mysavepoint");
d6feb60f 218
219$arty->discard_changes;
220
ddf66ced 221cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
d6feb60f 222
ddf66ced 223$schema->txn_commit;
39b8d119 224
3ff5b740 225END {
226 if($dbh) {
227 $dbh->do("DROP TABLE testschema.artist;");
228 $dbh->do("DROP TABLE testschema.casecheck;");
39b8d119 229 $dbh->do("DROP TABLE testschema.sequence_test;");
230 $dbh->do("DROP SEQUENCE pkid1_seq");
231 $dbh->do("DROP SEQUENCE pkid2_seq");
232 $dbh->do("DROP SEQUENCE nonpkid_seq");
3ff5b740 233 $dbh->do("DROP SCHEMA testschema;");
234 }
235}
0567538f 236