[DBD::Pg] Add some UTF-8 tests. Fix typo in dbdimp.c
dbdpg-commits-gENoVmVU/[email protected] Sun, 1 Dec 2013 23:46:17 -0500
| Newsgroups | gmane.comp.db.postgresql.dbdpg.cvs |
|---|---|
| Message-ID | <[email protected]> |
Committed by Greg Sabino Mullane <greg-O9fzpki4YnJWk0Htik3J/[email protected]> Add some UTF-8 tests. Fix typo in dbdimp.c --- dbdimp.c | 2 +- t/30unicode.t | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/dbdimp.c b/dbdimp.c index 0e3f1e7..b3eee86 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -3966,7 +3966,7 @@ int pg_db_getcopydata (SV * dbh, SV * dataline, int async) if (copystatus > 0) { sv_setpv(dataline, tempbuf); if (imp_dbh->pg_utf8_flag) - sv_utf8_upgrade(dataline) + sv_utf8_upgrade(dataline); TRACE_PQFREEMEM; PQfreemem(tempbuf); } diff --git a/t/30unicode.t b/t/30unicode.t index 650e410..fc1406a 100644 --- a/t/30unicode.t +++ b/t/30unicode.t @@ -7,6 +7,8 @@ use 5.006; use strict; use warnings; +use utf8; +use Encode; use Test::More; use lib 't','.'; require 'dbdpg_test_setup.pl'; @@ -17,7 +19,6 @@ my $dbh = connect_database(); if (! $dbh) { plan skip_all => 'Connection to database failed, cannot continue testing'; } -plan tests => 1; isnt ($dbh, undef, 'Connect to database for unicode testing'); @@ -25,5 +26,51 @@ my $pgversion = $dbh->{pg_server_version}; my $t; +my $name = 'Émilie du Châtelet'; +utf8::encode($name); + +my $SQL = 'SELECT ?::text'; +my $sth = $dbh->prepare($SQL); +$sth->execute($name); +my $result = $sth->fetchall_arrayref->[0][0]; +$t = 'Fetching UTF-8 string from the database returns proper string'; +is ($result, $name, $t); +$t = 'Fetching UTF-8 string from the database returns string with UTF-8 flag on'; +ok (utf8::is_utf8($result), $t); + +$dbh->{pg_enable_utf8} = 0; +$sth->execute($name); +$result = $sth->fetchall_arrayref->[0][0]; +$t = 'Fetching UTF-8 string from the database returns proper string (pg_enable_utf8=0)'; +is ($result, $name, $t); +$t = 'Fetching UTF-8 string from the database returns string with UTF-8 flag off (pg_enable_utf8=0)'; +ok (!utf8::is_utf8($result), $t); + + +$name = 'Ada Lovelace'; +utf8::encode($name); + +$dbh->{pg_enable_utf8} = -1; +$SQL = 'SELECT ?::text'; +$sth = $dbh->prepare($SQL); +$sth->execute($name); +$result = $sth->fetchall_arrayref->[0][0]; +$t = 'Fetching ASCII string from the database returns proper string'; +is ($result, $name, $t); +$t = 'Fetching ASCII string from the database returns string with UTF-8 flag on'; +ok (utf8::is_utf8($result), $t); + +$dbh->{pg_enable_utf8} = 0; +$sth->execute($name); +$result = $sth->fetchall_arrayref->[0][0]; +$t = 'Fetching ASCII string from the database returns proper string (pg_enable_utf8=0)'; +is ($result, $name, $t); +$t = 'Fetching ASCII string from the database returns string with UTF-8 flag off (pg_enable_utf8=0)'; +ok (!utf8::is_utf8($result), $t); + + cleanup_database($dbh,'test'); $dbh->disconnect(); + +done_testing(); + -- 1.8.4