1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
#!perl
## Test arrays
use 5.006;
use strict;
use warnings;
use Test::More;
use Data::Dumper;
use DBI ':sql_types';
use DBD::Pg ':pg_types';
use lib 't','.';
require 'dbdpg_test_setup.pl';
select(($|=1,select(STDERR),$|=1)[1]);
my $dbh = connect_database();
if (! defined $dbh) {
plan skip_all => 'Connection to database failed, cannot continue testing';
}
plan tests => 245;
isnt ($dbh, undef, 'Connect to database for array testing');
my ($sth,$result,$t);
my $pgversion = $dbh->{pg_server_version};
if ($pgversion >= 80100) {
$dbh->do('SET escape_string_warning = false');
}
my $SQL = q{DELETE FROM dbd_pg_test WHERE pname = 'Array Testing'};
my $cleararray = $dbh->prepare($SQL);
$SQL = q{INSERT INTO dbd_pg_test(id,pname,testarray) VALUES (99,'Array Testing',?)};
my $addarray = $dbh->prepare($SQL);
$SQL = q{SELECT testarray FROM dbd_pg_test WHERE pname= 'Array Testing'};
my $getarray = $dbh->prepare($SQL);
my $array_tests =
q![]
{}
Empty array
['']
{""}
Empty array
[['']]
{{""}}
Empty array with two levels
[[['']]]
{{{""}}}
Empty array with three levels
[[''],['']]
{{""},{""}}
Two empty arrays
[[[''],[''],['']]]
{{{""},{""},{""}}}
Three empty arrays at second level
[[],[[]]]
ERROR: must be of equal size
Unbalanced empty arrays
{}
ERROR: Cannot bind a reference
Bare hashref
[{}]
ERROR: only scalars and other arrays
Hashref at top level
[1,2,{3,4},5]
ERROR: only scalars and other arrays
Hidden hashref
[[1,2],[3]]
ERROR: must be of equal size
Unbalanced array
[[1,2],[3,4,5]]
ERROR: must be of equal size
Unbalanced array
[[1,2],[]]
ERROR: must be of equal size
Unbalanced array
[[],[3]]
ERROR: must be of equal size
Unbalanced array
[123]
{123} quote: {"123"}
Simple 1-D numeric array
['abc']
{abc} quote: {"abc"}
Simple 1-D text array
['a','b,c']
{a,"b,c"} quote: {"a","b,c"}
Text array with commas and quotes
['a','b,}']
{a,"b,}"} quote: {"a","b,}"}
Text array with commas, escaped closing brace
['a','b,]']
{a,"b,]"} quote: {"a","b,]"}
Text array with commas, escaped closing bracket
[1,2]
{1,2} quote: {"1","2"}
Simple 1-D numeric array
[[1]]
{{1}} quote: {{"1"}}
Simple 2-D numeric array
[[1,2]]
{{1,2}} quote: {{"1","2"}}
Simple 2-D numeric array
[[[1]]]
{{{1}}} quote: {{{"1"}}}
Simple 3-D numeric array
[[["alpha",2],[23,"pop"]]]
{{{alpha,2},{23,pop}}} quote: {{{"alpha","2"},{"23","pop"}}}
3-D mixed array
[[[1,2,3],[4,5,"6"],["seven","8","9"]]]
{{{1,2,3},{4,5,6},{seven,8,9}}} quote: {{{"1","2","3"},{"4","5","6"},{"seven","8","9"}}}
3-D mixed array
[q{O'RLY?}]
{O'RLY?} quote: {"O'RLY?"}
Simple single quote
[q{O"RLY?}]
{"O\"RLY?"}
Simple double quote
[[q{O"RLY?}],[q|'Ya' - "really"|],[123]]
{{"O\"RLY?"},{"'Ya' - \"really\""},{123}} quote: {{"O\"RLY?"},{"'Ya' - \"really\""},{"123"}}
Many quotes
["Test\\\\nRun"]
{"Test\\\\nRun"} quote: {"Test\\\\\\nRun"}
Simple backslash
[["Test\\\\nRun","Quite \"so\""],["back\\\\\\\\slashes are a \"pa\\\\in\"",123] ]
{{"Test\\\\nRun","Quite \"so\""},{"back\\\\\\\\\\\\slashes are a \"pa\\\\in\"",123}} quote: {{"Test\\\\\\nRun","Quite \"so\""},{"back\\\\\\\\\\\\slashes are a \"pa\\\\\\in\"","123"}}
Escape party
[undef]
{NULL}
NEED 80200: Simple undef test
[[undef]]
{{NULL}}
NEED 80200: Simple undef test
[[1,2],[undef,3],["four",undef],[undef,undef]]
{{1,2},{NULL,3},{four,NULL},{NULL,NULL}} quote: {{"1","2"},{NULL,"3"},{"four",NULL},{NULL,NULL}}
NEED 80200: Multiple undef test
!;
## Note: We silently allow things like this: [[[]],[]]
$dbh->{pg_expand_array} = 0;
for my $test (split /\n\n/ => $array_tests) {
next unless $test =~ /\w/;
my ($input,$expected,$msg) = split /\n/ => $test;
my $qexpected = $expected;
if ($expected =~ s/\s*quote:\s*(.+)//) {
$qexpected = $1;
}
if ($msg =~ s/NEED (\d+):\s*//) {
my $ver = $1;
if ($pgversion < $ver) {
SKIP: {
skip ('Cannot test NULL arrays unless version 8.2 or better', 4);
}
next;
}
}
$t="Correct array inserted: $msg : $input";
$cleararray->execute();
eval {
$addarray->execute(eval $input);
};
if ($expected =~ /error:\s+(.+)/i) {
like ($@, qr{$1}, "Array failed : $msg : $input");
like ($@, qr{$1}, "Array failed : $msg : $input");
}
else {
is ($@, q{}, "Array worked : $msg : $input");
$getarray->execute();
$result = $getarray->fetchall_arrayref()->[0][0];
is ($result, $expected, $t);
}
$t="Array quote worked : $msg : $input";
eval {
$result = $dbh->quote(eval $input );
};
if ($qexpected =~ /error:\s+(.+)/i) {
my $errmsg = $1;
$errmsg =~ s/bind/quote/;
like ($@, qr{$errmsg}, "Array quote failed : $msg : $input");
like ($@, qr{$errmsg}, "Array quote failed : $msg : $input");
}
else {
is ($@, q{}, $t);
$t="Correct array quote: $msg : $input";
is ($result, $qexpected, $t);
}
}
## Same thing, but expand the arrays
$dbh->{pg_expand_array} = 1;
for my $test (split /\n\n/ => $array_tests) {
next unless $test =~ /\w/;
my ($input,$expected,$msg) = split /\n/ => $test;
my $qexpected = $expected;
if ($expected =~ s/\s*quote:\s*(.+)//) {
$qexpected = $1;
}
if ($msg =~ s/NEED (\d+):\s*//) {
my $ver = $1;
if ($pgversion < $ver) {
SKIP: {
skip ('Cannot test NULL arrays unless version 8.2 or better', 2);
}
next;
}
}
$t="Array worked : $msg : $input";
$cleararray->execute();
eval {
$addarray->execute(eval $input);
};
if ($expected =~ /error:\s+(.+)/i) {
like ($@, qr{$1}, "Array failed : $msg : $input");
like ($@, qr{$1}, "Array failed : $msg : $input");
}
else {
is ($@, q{}, $t);
$t="Correct array inserted: $msg : $input";
$getarray->execute();
$result = $getarray->fetchall_arrayref()->[0][0];
$qexpected =~ s/{}/{''}/;
$qexpected =~ y/{}/[]/;
$qexpected =~ s/NULL/undef/g;
$qexpected =~ s/\\\\n/\\n/g;
$qexpected =~ s/\\\\"/\\"/g;
$qexpected =~ s/\\\\i/\\i/g;
if ($msg =~ /closing brace/) {
$qexpected =~ s/]"/}"/;
}
$expected = eval $qexpected;
is_deeply ($result, $expected, $t);
}
if ($msg =~ /STOP/) {
warn "Exiting for DEBUGGING. Result is:\n";
warn Dumper $result;
cleanup_database($dbh,'test');
$dbh->disconnect;
exit;
}
}
$cleararray->execute();
## Pure string to array conversion testing
my $array_tests_out =
q!1
[1]
Simple test of single array element
1,2
[1,2]
Simple test of multiple array elements
1,2,3
[1,2,3]
Simple test of multiple array elements
'a','b'
['a','b']
Array with text items
0.1,2.4
[0.1,2.4]
Array with numeric items
'My"lrd','b','c'
['My"lrd','b','c']
Array with escaped items
[1]
[[1]]
Multi-level integer array
[[1,2]]
[[[1,2]]]
Multi-level integer array
[[1],[2]]
[[[1],[2]]]
Multi-level integer array
[[1],[2],[3]]
[[[1],[2],[3]]]
Multi-level integer array
[[[1]],[[2]],[[3]]]
[[[[1]],[[2]],[[3]]]]
Multi-level integer array
'abc',NULL
['abc',undef]
NEED 80200: Array with a null
['abc','NULL',NULL,NULL,123::text]
[['abc','NULL',undef,undef,'123']]
NEED 80200: Array with many nulls and a quoted int
['abc','']
[['abc','']]
Final item is empty
1,NULL
[1,undef]
NEED 80200: Last item is NULL
NULL
[undef]
NEED 80200: Only item is NULL
NULL,NULL
[undef,undef]
NEED 80200: Two NULL items only
NULL,NULL,NULL
[undef,undef,undef]
NEED 80200: Three NULL items only
[123,NULL,456]
[[123,undef,456]]
NEED 80200: Middle item is NULL
NULL,'abc'
[undef,'abc']
NEED 80200: First item is NULL
'a','NULL'
['a',"NULL"]
Fake NULL is text
[[[[[1,2,3]]]]]
[[[[[[1,2,3]]]]]]
Deep nesting
[[[[[1],[2],[3]]]]]
[[[[[[1],[2],[3]]]]]]
Deep nesting
[[[[[1]]],[[[2]]],[[[3]]]]]
[[[[[[1]]],[[[2]]],[[[3]]]]]]
Deep nesting
[[[[[1]],[[2]],[[3]]]]]
[[[[[[1]],[[2]],[[3]]]]]]
Deep nesting
1::bool
['t']
Test of boolean type
1::bool,0::bool,'true'::boolean
['t','f','t']
Test of boolean types
1::oid
[1]
Test of oid type - should not quote
1::text
['1']
Text number should quote
1,2,3
[1,2,3]
Unspecified int should not quote
1::int
[1]
Integer number should quote
'(1,2),(4,5)'::box,'(5,3),(4,5)'
['(4,5),(1,2)','(5,5),(4,3)']
Type 'box' works
!;
$Data::Dumper::Indent = 0;
for my $test (split /\n\n/ => $array_tests_out) {
next unless $test =~ /\w/;
my ($input,$expected,$msg) = split /\n/ => $test;
my $qexpected = $expected;
if ($expected =~ s/\s*quote:\s*(.+)//) {
$qexpected = $1;
}
if ($msg =~ s/NEED (\d+):\s*//) {
my $ver = $1;
if ($pgversion < $ver) {
SKIP: {
skip ('Cannot test NULL arrays unless version 8.2 or better', 1);
}
next;
}
}
if ($pgversion < 80200) {
if ($input =~ /SKIP/ or $test =~ /Fake NULL|boolean/) {
SKIP: {
skip ('Cannot test some array items on pre-8.2 servers', 1);
}
next;
}
}
$t="Array test $msg : $input";
$SQL = qq{SELECT ARRAY[$input]};
my $result = '';
eval {
$result = $dbh->selectall_arrayref($SQL)->[0][0];
};
if ($result =~ /error:\s+(.+)/i) {
like ($@, qr{$1}, "Array failed : $msg : $input");
}
else {
$expected = eval $expected;
## is_deeply does not handle type differences
is ( (Dumper $result), (Dumper $expected), $t);
}
}
## Check utf-8 in and out of the database
SKIP: {
eval { require Encode; };
skip ('Encode module is needed for unicode tests', 14) if $@;
$t='String should be UTF-8';
local $dbh->{pg_enable_utf8} = 1;
my $utf8_str = chr(0x100).'dam'; # LATIN CAPITAL LETTER A WITH MACRON
ok (Encode::is_utf8( $utf8_str ), $t);
$t='quote() handles utf8';
my $quoted = $dbh->quote($utf8_str);
is ($quoted, qq{'$utf8_str'}, $t);
$t='Quoted string should be UTF-8';
ok (Encode::is_utf8( $quoted ), $t);
$t='quote() handles utf8 inside array';
$quoted = $dbh->quote([$utf8_str, $utf8_str]);
is ($quoted, qq!{"$utf8_str","$utf8_str"}!, $t);
$t='Quoted array of strings should be UTF-8';
ok (Encode::is_utf8( $quoted ), $t);
## Workaround for client encodings such as SJIS
my $old_encoding = $dbh->selectall_arrayref('SHOW client_encoding')->[0][0];
if ($old_encoding ne 'UTF8') {
$dbh->do(q{SET NAMES 'UTF8'});
}
$t='Inserting utf-8 into an array via quoted do() works';
$dbh->do('DELETE FROM dbd_pg_test');
$SQL = qq{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, '$quoted', 'one')};
eval {
$dbh->do($SQL);
};
is ($@, q{}, $t);
$t='Retreiving an array containing utf-8 works';
$SQL = q{SELECT id, testarray, val FROM dbd_pg_test WHERE id = 1};
$sth = $dbh->prepare($SQL);
$sth->execute();
$result = $sth->fetchall_arrayref()->[0];
my $expected = [1,[$utf8_str,$utf8_str],'one'];
is_deeply ($result, $expected, $t);
$t='Selected string should be UTF-8';
ok (Encode::is_utf8( $result->[1][0] ), $t);
$t='Selected string should be UTF-8';
ok (Encode::is_utf8( $result->[1][1] ), $t);
$t='Inserting utf-8 into an array via prepare and arrayref works';
$dbh->do('DELETE FROM dbd_pg_test');
$SQL = q{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (?, ?, 'one')};
$sth = $dbh->prepare($SQL);
eval {
$sth->execute(1,['Bob',$utf8_str]);
};
is ($@, q{}, $t);
$t='Retreiving an array containing utf-8 works';
$SQL = q{SELECT id, testarray, val FROM dbd_pg_test WHERE id = 1};
$sth = $dbh->prepare($SQL);
$sth->execute();
$result = $sth->fetchall_arrayref()->[0];
$expected = [1,['Bob',$utf8_str],'one'];
is_deeply ($result, $expected, $t);
$t='Selected ASCII string should not be UTF-8';
ok (!Encode::is_utf8( $result->[1][0] ), $t);
$t='Selected string should be UTF-8';
ok (Encode::is_utf8( $result->[1][1] ), $t);
$t='Non utf-8 inside an array is not return as utf-8';
$dbh->do('DELETE FROM dbd_pg_test');
$SQL = q{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, '{"noutfhere"}', 'one')};
$dbh->do($SQL);
$SQL = q{SELECT testarray FROM dbd_pg_test WHERE id = 1};
$sth = $dbh->prepare($SQL);
$sth->execute();
$result = $sth->fetchall_arrayref()->[0][0];
ok (!Encode::is_utf8($result), $t);
$sth->finish();
}
cleanup_database($dbh,'test');
$dbh->disconnect;
|