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