Un-plan test and fix authorship
[dbsrgits/DBIx-Class.git] / t / 72pg.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 {
10   package DBICTest::Schema::Casecheck;
11
12   use strict;
13   use warnings;
14   use base 'DBIx::Class';
15
16   __PACKAGE__->load_components(qw/Core/);
17   __PACKAGE__->table('testschema.casecheck');
18   __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
19   __PACKAGE__->column_info_from_storage(1);
20   __PACKAGE__->set_primary_key('id');
21
22   sub store_column {
23     my ($self, $name, $value) = @_;
24     $value = '#'.$value if($name eq "storecolumn");
25     $self->maybe::next::method($name, $value);
26   }
27 }
28
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
44 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
45
46 plan 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\',\'anothertestschema\'!)'
50     unless ($dsn && $user);
51
52 DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
53 my $schema = DBICTest::Schema->connect($dsn, $user, $pass,);
54
55 # Check that datetime_parser returns correctly before we explicitly connect.
56 SKIP: {
57     eval { require DateTime::Format::Pg };
58     skip "DateTime::Format::Pg required", 2 if $@;
59
60     my $store = ref $schema->storage;
61     is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
62
63     my $parser = $schema->storage->datetime_parser;
64     is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
65 }
66
67 my $dbh = $schema->storage->dbh;
68 $schema->source("Artist")->name("testschema.artist");
69 $schema->source("SequenceTest")->name("testschema.sequence_test");
70 {
71     local $SIG{__WARN__} = sub {};
72     _cleanup ($dbh);
73
74     my $artist_table_def = <<EOS;
75 (
76   artistid serial PRIMARY KEY
77   , name VARCHAR(100)
78   , rank INTEGER NOT NULL DEFAULT '13'
79   , charfield CHAR(10)
80   , arrayfield INTEGER[]
81 )
82 EOS
83     $dbh->do("CREATE SCHEMA testschema;");
84     $dbh->do("CREATE TABLE testschema.artist $artist_table_def;");
85     $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
86     $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
87     $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
88     $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
89     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');
90     ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
91     $dbh->do("CREATE SCHEMA anothertestschema;");
92     $dbh->do("CREATE TABLE anothertestschema.artist $artist_table_def;");
93     $dbh->do("CREATE SCHEMA yetanothertestschema;");
94     $dbh->do("CREATE TABLE yetanothertestschema.artist $artist_table_def;");
95     $dbh->do('set search_path=testschema,public');
96 }
97
98 # store_column is called once for create() for non sequence columns
99
100 ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
101
102 is($storecolumn->storecolumn, '#a'); # was '##a'
103
104
105 # This is in Core now, but it's here just to test that it doesn't break
106 $schema->class('Artist')->load_components('PK::Auto');
107
108 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
109
110 { # test that auto-pk also works with the defined search path by
111   # un-schema-qualifying the table name
112   my $artist_name_save = $schema->source("Artist")->name;
113   $schema->source("Artist")->name("artist");
114
115   my $unq_new;
116   lives_ok {
117       $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
118   } 'insert into unqualified, shadowed table succeeds';
119
120   is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
121
122   #test with anothertestschema
123   $schema->source('Artist')->name('anothertestschema.artist');
124   my $another_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
125   is( $another_new->artistid,1, 'got correct artistid for yetanotherschema');
126
127   #test with yetanothertestschema
128   $schema->source('Artist')->name('yetanothertestschema.artist');
129   my $yetanother_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
130   is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
131   is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
132
133   $schema->source("Artist")->name($artist_name_save);
134 }
135
136 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
137
138 is($new->artistid, 2, "Auto-PK worked");
139
140 $new = $schema->resultset('Artist')->create({ name => 'bar' });
141
142 is($new->artistid, 3, "Auto-PK worked");
143
144
145 my $test_type_info = {
146     'artistid' => {
147         'data_type' => 'integer',
148         'is_nullable' => 0,
149         'size' => 4,
150     },
151     'name' => {
152         'data_type' => 'character varying',
153         'is_nullable' => 1,
154         'size' => 100,
155         'default_value' => undef,
156     },
157     'rank' => {
158         'data_type' => 'integer',
159         'is_nullable' => 0,
160         'size' => 4,
161         'default_value' => 13,
162
163     },
164     'charfield' => {
165         'data_type' => 'character',
166         'is_nullable' => 1,
167         'size' => 10,
168         'default_value' => undef,
169     },
170     'arrayfield' => {
171         'data_type' => 'integer[]',
172         'is_nullable' => 1,
173         'size' => undef,
174         'default_value' => undef,
175     },
176 };
177
178
179 my $type_info = $schema->storage->columns_info_for('testschema.artist');
180 my $artistid_defval = delete $type_info->{artistid}->{default_value};
181 like($artistid_defval,
182      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
183      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
184 is_deeply($type_info, $test_type_info,
185           'columns_info_for - column data types');
186
187 SKIP: {
188   skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
189
190   lives_ok {
191     $schema->resultset('ArrayTest')->create({
192       arrayfield => [1, 2],
193     });
194   } 'inserting arrayref as pg array data';
195
196   lives_ok {
197     $schema->resultset('ArrayTest')->update({
198       arrayfield => [3, 4],
199     });
200   } 'updating arrayref as pg array data';
201
202   $schema->resultset('ArrayTest')->create({
203     arrayfield => [5, 6],
204   });
205
206   my $count;
207   lives_ok {
208     $count = $schema->resultset('ArrayTest')->search({
209       arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #Todo anything less ugly than this?
210     })->count;
211   } 'comparing arrayref to pg array data does not blow up';
212   is($count, 1, 'comparing arrayref to pg array data gives correct result');
213 }
214
215
216 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
217 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
218
219 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
220 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
221
222 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
223 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
224
225 # Test SELECT ... FOR UPDATE
226 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
227 if ($HaveSysSigAction) {
228     Sys::SigAction->import( 'set_sig_handler' );
229 }
230
231 SKIP: {
232     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
233     # create a new schema
234     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
235     $schema2->source("Artist")->name("testschema.artist");
236
237     $schema->txn_do( sub {
238         my $artist = $schema->resultset('Artist')->search(
239             {
240                 artistid => 1
241             },
242             {
243                 for => 'update'
244             }
245         )->first;
246         is($artist->artistid, 1, "select for update returns artistid = 1");
247
248         my $artist_from_schema2;
249         my $error_ok = 0;
250         eval {
251             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
252             alarm(2);
253             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
254             $artist_from_schema2->name('fooey');
255             $artist_from_schema2->update;
256             alarm(0);
257         };
258         if (my $e = $@) {
259             $error_ok = $e =~ /DBICTestTimeout/;
260         }
261
262         # Make sure that an error was raised, and that the update failed
263         ok($error_ok, "update from second schema times out");
264         ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
265     });
266 }
267
268 SKIP: {
269     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
270     # create a new schema
271     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
272     $schema2->source("Artist")->name("testschema.artist");
273
274     $schema->txn_do( sub {
275         my $artist = $schema->resultset('Artist')->search(
276             {
277                 artistid => 1
278             },
279         )->first;
280         is($artist->artistid, 1, "select for update returns artistid = 1");
281
282         my $artist_from_schema2;
283         my $error_ok = 0;
284         eval {
285             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
286             alarm(2);
287             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
288             $artist_from_schema2->name('fooey');
289             $artist_from_schema2->update;
290             alarm(0);
291         };
292         if (my $e = $@) {
293             $error_ok = $e =~ /DBICTestTimeout/;
294         }
295
296         # Make sure that an error was NOT raised, and that the update succeeded
297         ok(! $error_ok, "update from second schema DOES NOT timeout");
298         ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
299     });
300 }
301
302 for (1..5) {
303     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
304     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
305     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
306     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
307 }
308 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
309 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
310
311 sub _cleanup {
312   my $dbh = shift or return;
313
314   for my $stat (
315     'DROP TABLE testschema.artist',
316     'DROP TABLE testschema.casecheck',
317     'DROP TABLE testschema.sequence_test',
318     'DROP TABLE testschema.array_test',
319     'DROP SEQUENCE pkid1_seq',
320     'DROP SEQUENCE pkid2_seq',
321     'DROP SEQUENCE nonpkid_seq',
322     'DROP SCHEMA testschema',
323     'DROP TABLE anothertestschema.artist',
324     'DROP SCHEMA anothertestschema',
325     'DROP TABLE yetanothertestschema.artist',
326     'DROP SCHEMA yetanothertestschema',
327   ) {
328     eval { $dbh->do ($stat) };
329   }
330 }
331
332 done_testing;
333
334 END { _cleanup($dbh) }