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