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