| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This is a test for correctly handling UTF-8 content |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use charnames ":full"; |
| 8 |
|
| 9 |
use DBI; |
| 10 |
use Text::CSV_XS; |
| 11 |
use Encode qw( encode ); |
| 12 |
|
| 13 |
use Test::More; |
| 14 |
|
| 15 |
BEGIN { use_ok ("DBI") } |
| 16 |
do "./t/lib.pl"; |
| 17 |
|
| 18 |
ok (my $dbh = Connect ({ f_ext => ".csv/r", f_schema => undef }), "connect"); |
| 19 |
|
| 20 |
ok (my $tbl1 = FindNewTable ($dbh), "find new test table"); |
| 21 |
ok (my $tbl2 = FindNewTable ($dbh), "find new test table"); |
| 22 |
|
| 23 |
my @data = ( |
| 24 |
"The \N{SNOWMAN} is melting", |
| 25 |
"U2 should \N{SKULL AND CROSSBONES}", |
| 26 |
"I \N{BLACK HEART SUIT} my wife", |
| 27 |
"Unicode makes me \N{WHITE SMILING FACE}", |
| 28 |
); |
| 29 |
ok ("Creating table with UTF-8 content"); |
| 30 |
foreach my $tbl ($tbl1, $tbl2) { |
| 31 |
ok (my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\n" }), "New csv"); |
| 32 |
ok (open (my $fh, ">:utf8", |
| 33 |
File::Spec->catfile (DbDir (), "$tbl.csv")), "Open CSV"); |
| 34 |
ok ($csv->print ($fh, [ "id", "str" ]), "CSV print header"); |
| 35 |
ok ($csv->print ($fh, [ $_, $data[$_ - 1] ]), "CSV row $_") for 1 .. scalar @data; |
| 36 |
ok (close ($fh), "close"); |
| 37 |
} |
| 38 |
|
| 39 |
{ $dbh->{f_encoding} = undef; |
| 40 |
|
| 41 |
my $row; |
| 42 |
|
| 43 |
ok (my $sth = $dbh->prepare ("select * from $tbl1"), "prepare"); |
| 44 |
ok ($sth->execute, "execute"); |
| 45 |
foreach my $i (1 .. scalar @data) { |
| 46 |
ok ($row = $sth->fetch, "fetch $i"); |
| 47 |
my $v = $data[$i - 1]; |
| 48 |
utf8::is_utf8 ($v) or $v = encode ("utf8", $v); |
| 49 |
is_deeply ($row, [ $i , $v ], "unencoded content $i"); |
| 50 |
} |
| 51 |
ok ($sth->finish, "finish"); |
| 52 |
undef $sth; |
| 53 |
} |
| 54 |
|
| 55 |
{ $dbh->{f_encoding} = "utf8"; |
| 56 |
|
| 57 |
my $row; |
| 58 |
|
| 59 |
ok (my $sth = $dbh->prepare ("select * from $tbl2"), "prepare"); |
| 60 |
ok ($sth->execute, "execute"); |
| 61 |
foreach my $i (1 .. scalar @data) { |
| 62 |
ok ($row = $sth->fetch, "fetch $i"); |
| 63 |
my $v = $data[$i - 1]; |
| 64 |
ok (utf8::is_utf8 ($v), "is encoded"); |
| 65 |
is_deeply ($row, [ $i , $v ], "encoded content $i"); |
| 66 |
} |
| 67 |
ok ($sth->finish, "finish"); |
| 68 |
undef $sth; |
| 69 |
} |
| 70 |
|
| 71 |
ok ($dbh->do ("drop table $tbl1"), "drop table"); |
| 72 |
ok ($dbh->do ("drop table $tbl2"), "drop table"); |
| 73 |
ok ($dbh->disconnect, "disconnect"); |
| 74 |
|
| 75 |
done_testing (); |