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