Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
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 | |
c3af542a |
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 | |
89add887 |
28 | } |
29 | |
9ba627e3 |
30 | { |
31 | package DBICTest::Schema::ArrayTest; |
32 | |
33 | use strict; |
34 | use warnings; |
35 | use base 'DBIx::Class'; |
36 | |
37 | __PACKAGE__->load_components(qw/Core/); |
38 | __PACKAGE__->table('testschema.array_test'); |
39 | __PACKAGE__->add_columns(qw/id arrayfield/); |
40 | __PACKAGE__->column_info_from_storage(1); |
41 | __PACKAGE__->set_primary_key('id'); |
42 | |
43 | } |
44 | |
0567538f |
45 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; |
46 | |
9ba627e3 |
47 | plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '. |
48 | '(note: This test drops and creates tables called \'artist\', \'casecheck\', \'array_test\' and \'sequence_test\''. |
49 | ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''. |
50 | ' as well as following schemas: \'testschema\'!)' |
64d5427c |
51 | unless ($dsn && $user); |
0567538f |
52 | |
0567538f |
53 | |
c3af542a |
54 | plan tests => 39; |
9ba627e3 |
55 | |
56 | DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' ); |
34a9e8a0 |
57 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
0567538f |
58 | |
114780ee |
59 | # Check that datetime_parser returns correctly before we explicitly connect. |
60 | SKIP: { |
61 | eval { require DateTime::Format::Pg }; |
62 | skip "DateTime::Format::Pg required", 2 if $@; |
63 | |
64 | my $store = ref $schema->storage; |
65 | is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage'); |
66 | |
67 | my $parser = $schema->storage->datetime_parser; |
68 | is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected'); |
69 | } |
70 | |
3ff5b740 |
71 | my $dbh = $schema->storage->dbh; |
72 | $schema->source("Artist")->name("testschema.artist"); |
39b8d119 |
73 | $schema->source("SequenceTest")->name("testschema.sequence_test"); |
bb452615 |
74 | { |
75 | local $SIG{__WARN__} = sub {}; |
76 | $dbh->do("CREATE SCHEMA testschema;"); |
9ba627e3 |
77 | $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10), arrayfield INTEGER[]);"); |
bb452615 |
78 | $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));"); |
79 | $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0"); |
80 | $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0"); |
81 | $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0"); |
c3af542a |
82 | 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 |
83 | ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table'); |
bb452615 |
84 | } |
0567538f |
85 | |
c3af542a |
86 | # store_column is called once for create() for non sequence columns |
87 | |
88 | ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'})); |
89 | |
90 | is($storecolumn->storecolumn, '#a'); # was '##a' |
91 | |
92 | |
3ff5b740 |
93 | # This is in Core now, but it's here just to test that it doesn't break |
94 | $schema->class('Artist')->load_components('PK::Auto'); |
0567538f |
95 | |
3ff5b740 |
96 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
0567538f |
97 | |
b6b65a3e |
98 | is($new->artistid, 1, "Auto-PK worked"); |
99 | |
3ff5b740 |
100 | $new = $schema->resultset('Artist')->create({ name => 'bar' }); |
b6b65a3e |
101 | |
102 | is($new->artistid, 2, "Auto-PK worked"); |
103 | |
a953d8d9 |
104 | my $test_type_info = { |
105 | 'artistid' => { |
103e3e03 |
106 | 'data_type' => 'integer', |
107 | 'is_nullable' => 0, |
fc22fbac |
108 | 'size' => 4, |
a953d8d9 |
109 | }, |
110 | 'name' => { |
103e3e03 |
111 | 'data_type' => 'character varying', |
112 | 'is_nullable' => 1, |
ae515736 |
113 | 'size' => 100, |
fc22fbac |
114 | 'default_value' => undef, |
103e3e03 |
115 | }, |
85df19d7 |
116 | 'rank' => { |
117 | 'data_type' => 'integer', |
118 | 'is_nullable' => 0, |
119 | 'size' => 4, |
120 | 'default_value' => 13, |
121 | |
122 | }, |
103e3e03 |
123 | 'charfield' => { |
124 | 'data_type' => 'character', |
a953d8d9 |
125 | 'is_nullable' => 1, |
fc22fbac |
126 | 'size' => 10, |
127 | 'default_value' => undef, |
103e3e03 |
128 | }, |
9ba627e3 |
129 | 'arrayfield' => { |
130 | 'data_type' => 'integer[]', |
131 | 'is_nullable' => 1, |
132 | 'size' => undef, |
133 | 'default_value' => undef, |
134 | }, |
a953d8d9 |
135 | }; |
136 | |
fc22fbac |
137 | |
3ff5b740 |
138 | my $type_info = $schema->storage->columns_info_for('testschema.artist'); |
fc22fbac |
139 | my $artistid_defval = delete $type_info->{artistid}->{default_value}; |
140 | like($artistid_defval, |
4d272ce5 |
141 | qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/, |
fc22fbac |
142 | 'columns_info_for - sequence matches Pg get_autoinc_seq expectations'); |
143 | is_deeply($type_info, $test_type_info, |
144 | 'columns_info_for - column data types'); |
a953d8d9 |
145 | |
20ea616f |
146 | { |
9ba627e3 |
147 | lives_ok { |
148 | $schema->resultset('ArrayTest')->create({ |
149 | arrayfield => [1, 2], |
150 | }); |
151 | } 'inserting arrayref as pg array data'; |
152 | |
153 | lives_ok { |
154 | $schema->resultset('ArrayTest')->update({ |
155 | arrayfield => [3, 4], |
156 | }); |
157 | } 'updating arrayref as pg array data'; |
c224117b |
158 | |
159 | $schema->resultset('ArrayTest')->create({ |
160 | arrayfield => [5, 6], |
161 | }); |
162 | |
163 | my $count; |
164 | lives_ok { |
165 | $count = $schema->resultset('ArrayTest')->search({ |
6ffb5be5 |
166 | arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #TODO anything less ugly than this? |
c224117b |
167 | })->count; |
168 | } 'comparing arrayref to pg array data does not blow up'; |
169 | is($count, 1, 'comparing arrayref to pg array data gives correct result'); |
9ba627e3 |
170 | } |
171 | |
172 | |
3ff5b740 |
173 | my $name_info = $schema->source('Casecheck')->column_info( 'name' ); |
ae515736 |
174 | is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" ); |
175 | |
3ff5b740 |
176 | my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' ); |
ae515736 |
177 | is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" ); |
178 | |
3ff5b740 |
179 | my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' ); |
ae515736 |
180 | is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" ); |
181 | |
95ba7ee4 |
182 | # Test SELECT ... FOR UPDATE |
183 | my $HaveSysSigAction = eval "require Sys::SigAction" && !$@; |
184 | if ($HaveSysSigAction) { |
185 | Sys::SigAction->import( 'set_sig_handler' ); |
186 | } |
187 | |
188 | SKIP: { |
189 | skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction; |
190 | # create a new schema |
191 | my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass); |
192 | $schema2->source("Artist")->name("testschema.artist"); |
193 | |
194 | $schema->txn_do( sub { |
195 | my $artist = $schema->resultset('Artist')->search( |
196 | { |
197 | artistid => 1 |
198 | }, |
199 | { |
200 | for => 'update' |
201 | } |
202 | )->first; |
203 | is($artist->artistid, 1, "select for update returns artistid = 1"); |
204 | |
205 | my $artist_from_schema2; |
206 | my $error_ok = 0; |
207 | eval { |
208 | my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } ); |
209 | alarm(2); |
210 | $artist_from_schema2 = $schema2->resultset('Artist')->find(1); |
211 | $artist_from_schema2->name('fooey'); |
212 | $artist_from_schema2->update; |
213 | alarm(0); |
214 | }; |
215 | if (my $e = $@) { |
216 | $error_ok = $e =~ /DBICTestTimeout/; |
217 | } |
218 | |
219 | # Make sure that an error was raised, and that the update failed |
220 | ok($error_ok, "update from second schema times out"); |
221 | ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema"); |
222 | }); |
223 | } |
224 | |
225 | SKIP: { |
226 | skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction; |
227 | # create a new schema |
228 | my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass); |
229 | $schema2->source("Artist")->name("testschema.artist"); |
230 | |
231 | $schema->txn_do( sub { |
232 | my $artist = $schema->resultset('Artist')->search( |
233 | { |
234 | artistid => 1 |
235 | }, |
236 | )->first; |
237 | is($artist->artistid, 1, "select for update returns artistid = 1"); |
238 | |
239 | my $artist_from_schema2; |
240 | my $error_ok = 0; |
241 | eval { |
242 | my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } ); |
243 | alarm(2); |
244 | $artist_from_schema2 = $schema2->resultset('Artist')->find(1); |
245 | $artist_from_schema2->name('fooey'); |
246 | $artist_from_schema2->update; |
247 | alarm(0); |
248 | }; |
249 | if (my $e = $@) { |
250 | $error_ok = $e =~ /DBICTestTimeout/; |
251 | } |
252 | |
253 | # Make sure that an error was NOT raised, and that the update succeeded |
254 | ok(! $error_ok, "update from second schema DOES NOT timeout"); |
255 | ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema"); |
256 | }); |
257 | } |
258 | |
d093d3eb |
259 | for (1..5) { |
39b8d119 |
260 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' }); |
261 | is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key"); |
262 | is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key"); |
263 | is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key"); |
d093d3eb |
264 | } |
265 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 }); |
266 | is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually"); |
267 | |
d6feb60f |
268 | |
3ff5b740 |
269 | END { |
270 | if($dbh) { |
271 | $dbh->do("DROP TABLE testschema.artist;"); |
272 | $dbh->do("DROP TABLE testschema.casecheck;"); |
39b8d119 |
273 | $dbh->do("DROP TABLE testschema.sequence_test;"); |
9ba627e3 |
274 | $dbh->do("DROP TABLE testschema.array_test;"); |
39b8d119 |
275 | $dbh->do("DROP SEQUENCE pkid1_seq"); |
276 | $dbh->do("DROP SEQUENCE pkid2_seq"); |
277 | $dbh->do("DROP SEQUENCE nonpkid_seq"); |
3ff5b740 |
278 | $dbh->do("DROP SCHEMA testschema;"); |
279 | } |
280 | } |
0567538f |
281 | |