File: utf8.t

package info (click to toggle)
libdbd-sybase-perl 1.24-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ansic: 5,629; perl: 2,216; makefile: 4
file content (180 lines) | stat: -rw-r--r-- 4,747 bytes parent folder | download
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
#!perl
#
# $Id: utf8.t,v 1.5 2011/10/02 15:01:50 mpeppler Exp $

use lib 't';
use _test;

use strict;

use Test::More;

BEGIN {
    plan skip_all => 'This test requires Perl 5.8+'
        unless $] >= 5.008;

}

use DBI;
use DBD::Sybase;
use Encode ();

binmode( $_, 'utf8' )
    for map { Test::Builder->new->$_() }
    qw( output failure_output todo_output );

use vars qw($Pwd $Uid $Srv $Db);

( $Uid, $Pwd, $Srv, $Db ) = _test::get_info();

my $dbh = DBI->connect(
    "dbi:Sybase:$Srv;database=$Db;charset=utf8", $Uid, $Pwd,
    { PrintError => 1 }
);
$dbh->{syb_enable_utf8} = 1;


# Don't run this test on MS-SQL ("Unknown") servers...
unless ($dbh->{syb_server_version} ne 'Unknown' && $dbh->{syb_server_version} ne 'MS-SQL' && $dbh->{syb_server_version} ge '15' && $dbh->{syb_enable_utf8}) {
    plan skip_all => 'This test requires ASE 15 or later, and OpenClient 15.x or later';
}

plan tests => 11;

$dbh->do("create table #utf8test (uv univarchar(510), ut unitext)");


my $ascii = 'Some text';
# This is a byte string rather than a character string - this means that when using this
# to compare with the output from the DB we get a failure, even though the strings appear
# to be the same. So the string needs to be converted to UTF8 characters via Encode::decode()
# for use in the test. To simplify I've commented this out and use the second sample string
# instead.
my $utf8t = 'पट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट्टपट';
#my $utf8 = Encode::decode('UTF-8', $utf8t);
my $utf8 = "\x{263A} - smiley1 - \x{263B} - smiley2" x 15;

{
    my $quoted = $dbh->quote($ascii);
    $dbh->do("insert into #utf8test (uv, ut) values ($quoted, $quoted)");

    my $rows = $dbh->selectall_arrayref(
        "select * from #utf8test",
        { Slice => {} }
    );

    is_deeply(
        $rows,
        [
            {
                uv => $ascii,
                ut => $ascii,
            }
        ],
        "got expected row back from #utf8test"
    );

    ok(
        !Encode::is_utf8( $rows->[0]{uv} ),
        'uv column was returned with utf8 flag off'
    );

    ok(
        !Encode::is_utf8( $rows->[0]{ut} ),
        'ut column was returned with utf8 flag off'
    );
}

{
    $dbh->do("delete from #utf8test");

    my $quoted = $dbh->quote($utf8);
    $dbh->do("insert into #utf8test (uv, ut) values ($quoted, $quoted)");

    my $rows = $dbh->selectall_arrayref(
        "select * from #utf8test",
        { Slice => {} }
    );

    is_deeply(
        $rows,
        [
            {
                uv => $utf8,
                ut => $utf8,
            }
        ],
        "got expected row back from #utf8test"
    );

    ok(
        Encode::is_utf8( $rows->[0]{uv} ),
        'uv column was returned with utf8 flag on'
    );

    ok(
        Encode::is_utf8( $rows->[0]{ut} ),
        'ut column was returned with utf8 flag on'
    );
}

$dbh->{syb_enable_utf8} = 0;

{
    my $rows = $dbh->selectall_arrayref(
        "select * from #utf8test",
        { Slice => {} }
    );

    ok(
        !Encode::is_utf8( $rows->[0]{uv} ),
        'uv column was returned with utf8 flag off (syb_enable_utf8 was false)'
    );

    ok(
        !Encode::is_utf8( $rows->[0]{ut} ),
        'ut column was returned with utf8 flag off (syb_enable_utf8 was false)'
    );
}

{
    my $dbh2 = DBI->connect(
        "dbi:Sybase:$Srv;database=$Db;charset=utf8",
        $Uid, $Pwd, {
            PrintError      => 1,
            syb_enable_utf8 => 1
        }
    );

    $dbh2->do("create table #utf8test (uv univarchar(250), ut unitext)");

    my $quoted = $dbh->quote($utf8);
    $dbh2->do("insert into #utf8test (uv, ut) values ($quoted, $quoted)");

    my $rows = $dbh2->selectall_arrayref(
        "select * from #utf8test",
        { Slice => {} }
    );

    is_deeply(
        $rows,
        [
            {
                uv => substr($utf8, 0, 250),
                ut => $utf8,
            }
        ],
        "got expected row back from #utf8test"
    );

    ok(
        Encode::is_utf8( $rows->[0]{uv} ),
        'uv column was returned with utf8 flag on (syb_enable_utf8 passed to connect)'
    );

    ok(
        Encode::is_utf8( $rows->[0]{ut} ),
        'ut column was returned with utf8 flag on (syb_enable_utf8 passed to connect)'
    );
}