revert previous revision
[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/);
19   __PACKAGE__->column_info_from_storage(1);
20   __PACKAGE__->set_primary_key('id');
21
22 }
23
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
39 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
40
41 plan 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\'!)'
45     unless ($dsn && $user);
46
47
48 plan tests => 37;
49
50 DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
51 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
52
53 # Check that datetime_parser returns correctly before we explicitly connect.
54 SKIP: {
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
65 my $dbh = $schema->storage->dbh;
66 $schema->source("Artist")->name("testschema.artist");
67 $schema->source("SequenceTest")->name("testschema.sequence_test");
68 {
69     local $SIG{__WARN__} = sub {};
70     $dbh->do("CREATE SCHEMA testschema;");
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[]);");
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');
77     ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
78 }
79
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');
82
83 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
84
85 is($new->artistid, 1, "Auto-PK worked");
86
87 $new = $schema->resultset('Artist')->create({ name => 'bar' });
88
89 is($new->artistid, 2, "Auto-PK worked");
90
91 my $test_type_info = {
92     'artistid' => {
93         'data_type' => 'integer',
94         'is_nullable' => 0,
95         'size' => 4,
96     },
97     'name' => {
98         'data_type' => 'character varying',
99         'is_nullable' => 1,
100         'size' => 100,
101         'default_value' => undef,
102     },
103     'rank' => {
104         'data_type' => 'integer',
105         'is_nullable' => 0,
106         'size' => 4,
107         'default_value' => 13,
108
109     },
110     'charfield' => {
111         'data_type' => 'character',
112         'is_nullable' => 1,
113         'size' => 10,
114         'default_value' => undef,
115     },
116     'arrayfield' => {
117         'data_type' => 'integer[]',
118         'is_nullable' => 1,
119         'size' => undef,
120         'default_value' => undef,
121     },
122 };
123
124
125 my $type_info = $schema->storage->columns_info_for('testschema.artist');
126 my $artistid_defval = delete $type_info->{artistid}->{default_value};
127 like($artistid_defval,
128      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
129      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
130 is_deeply($type_info, $test_type_info,
131           'columns_info_for - column data types');
132
133 SKIP: {
134   skip "SQL::Abstract < 1.49 does not pass through arrayrefs", 4
135     if $SQL::Abstract::VERSION < 1.49;
136
137   lives_ok {
138     $schema->resultset('ArrayTest')->create({
139       arrayfield => [1, 2],
140     });
141   } 'inserting arrayref as pg array data';
142
143   lives_ok {
144     $schema->resultset('ArrayTest')->update({
145       arrayfield => [3, 4],
146     });
147   } 'updating arrayref as pg array data';
148
149   $schema->resultset('ArrayTest')->create({
150     arrayfield => [5, 6],
151   });
152
153   my $count;
154   lives_ok {
155     $count = $schema->resultset('ArrayTest')->search({
156       arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #TODO anything less ugly than this?
157     })->count;
158   } 'comparing arrayref to pg array data does not blow up';
159   is($count, 1, 'comparing arrayref to pg array data gives correct result');
160 }
161
162
163 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
164 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
165
166 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
167 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
168
169 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
170 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
171
172 # Test SELECT ... FOR UPDATE
173 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
174 if ($HaveSysSigAction) {
175     Sys::SigAction->import( 'set_sig_handler' );
176 }
177
178 SKIP: {
179     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
180     # create a new schema
181     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
182     $schema2->source("Artist")->name("testschema.artist");
183
184     $schema->txn_do( sub {
185         my $artist = $schema->resultset('Artist')->search(
186             {
187                 artistid => 1
188             },
189             {
190                 for => 'update'
191             }
192         )->first;
193         is($artist->artistid, 1, "select for update returns artistid = 1");
194
195         my $artist_from_schema2;
196         my $error_ok = 0;
197         eval {
198             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
199             alarm(2);
200             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
201             $artist_from_schema2->name('fooey');
202             $artist_from_schema2->update;
203             alarm(0);
204         };
205         if (my $e = $@) {
206             $error_ok = $e =~ /DBICTestTimeout/;
207         }
208
209         # Make sure that an error was raised, and that the update failed
210         ok($error_ok, "update from second schema times out");
211         ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
212     });
213 }
214
215 SKIP: {
216     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
217     # create a new schema
218     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
219     $schema2->source("Artist")->name("testschema.artist");
220
221     $schema->txn_do( sub {
222         my $artist = $schema->resultset('Artist')->search(
223             {
224                 artistid => 1
225             },
226         )->first;
227         is($artist->artistid, 1, "select for update returns artistid = 1");
228
229         my $artist_from_schema2;
230         my $error_ok = 0;
231         eval {
232             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
233             alarm(2);
234             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
235             $artist_from_schema2->name('fooey');
236             $artist_from_schema2->update;
237             alarm(0);
238         };
239         if (my $e = $@) {
240             $error_ok = $e =~ /DBICTestTimeout/;
241         }
242
243         # Make sure that an error was NOT raised, and that the update succeeded
244         ok(! $error_ok, "update from second schema DOES NOT timeout");
245         ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
246     });
247 }
248
249 SKIP: {
250   skip "Oracle Auto-PK tests are broken", 16;
251
252   # test auto increment using sequences WITHOUT triggers
253   for (1..5) {
254     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
255     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
256     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
257     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
258   }
259   my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
260   is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
261 }
262
263 END {
264     if($dbh) {
265         $dbh->do("DROP TABLE testschema.artist;");
266         $dbh->do("DROP TABLE testschema.casecheck;");
267         $dbh->do("DROP TABLE testschema.sequence_test;");
268         $dbh->do("DROP TABLE testschema.array_test;");
269         $dbh->do("DROP SEQUENCE pkid1_seq");
270         $dbh->do("DROP SEQUENCE pkid2_seq");
271         $dbh->do("DROP SEQUENCE nonpkid_seq");
272         $dbh->do("DROP SCHEMA testschema;");
273     }
274 }
275