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