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