Commit 47db4300 authored by Xavier Guimard's avatar Xavier Guimard

Prepare UTF-8 tests

parent f1d3e947
......@@ -68,4 +68,7 @@ WriteMakefile(
)
: ()
),
clean => {
FILES => 't/lmConf*'
}
);
......@@ -32,7 +32,6 @@ sub store {
$self->logError;
return UNKNOWN_ERROR;
}
eval { $self->_dbh->do("COMMIT"); };
return $cfgNum;
}
......
......@@ -10,13 +10,16 @@ our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI);
sub store {
my ( $self, $fields ) = @_;
my $cfgNum = $fields->{cfgNum};
$self->{noQuotes} = 1;
$fields = $self->serialize($fields);
my $req;
my $lastCfg = $self->lastCfg;
if ( $lastCfg == $fields->{cfgNum} ) {
$self->_dbh->begin_work;
eval { $self->_dbh->do("BEGIN") };
if ( $lastCfg == $cfgNum ) {
$req = $self->_dbh->prepare(
"UPDATE $self->{dbiTable} SET field=?, value=? WHERE cfgNum=? AND field=?"
);
......@@ -31,30 +34,28 @@ sub store {
$self->logError;
return UNKNOWN_ERROR;
}
$self->_dbh->{AutoCommit} = 0;
while ( my ( $k, $v ) = each %$fields ) {
my @execValues;
if ( $lastCfg == $fields->{cfgNum} ) {
@execValues = ( $k, $v, $fields->{cfgNum}, $k );
if ( $lastCfg == $cfgNum ) {
@execValues = ( $k, $v, $cfgNum, $k );
}
else { @execValues = ( $fields->{cfgNum}, $k, $v ); }
else { @execValues = ( $cfgNum, $k, $v ); }
unless ( $req->execute(@execValues) ) {
$self->logError;
$self->_dbh->do("ROLLBACK");
$self->_dbh->{AutoCommit} = 1;
return UNKNOWN_ERROR;
}
}
$self->_dbh->do("COMMIT");
$self->_dbh->{AutoCommit} = 1;
return $fields->{cfgNum};
eval { $self->_dbh->do("COMMIT") };
return $cfgNum;
}
sub load {
my ( $self, $cfgNum, $fields ) = @_;
$fields = $fields ? join( ",", @$fields ) : '*';
my $sth =
$self->_dbh->prepare( "SELECT cfgNum,field,value from "
$self->_dbh
->prepare( "SELECT cfgNum,field,value from "
. $self->{dbiTable}
. " WHERE cfgNum=?" );
$sth->execute($cfgNum);
......
......@@ -24,7 +24,7 @@ sub prereq {
'"dbiChain" is required in *DBI configuration type';
return 0;
}
print STDERR __PACKAGE__ . 'Warning: "dbiUser" parameter is not set'
$Lemonldap::NG::Common::Conf::msg .= 'Warning: "dbiUser" parameter is not set'
unless ( $self->{dbiUser} );
$self->{dbiTable} ||= "lmConfig";
1;
......
......@@ -5,8 +5,13 @@
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More tests => 3;
BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
use strict;
use Test::More;
BEGIN {
use_ok('DBI');
use_ok('Lemonldap::NG::Common::Conf');
}
#########################
......@@ -15,15 +20,64 @@ BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
my $h;
@ARGV = ("help=groups");
ok(
$h = new Lemonldap::NG::Common::Conf(
{
type => 'CDBI',
dbiChain => "DBI:mysql:database=lemonldap-ng",
dbiUser => 'lemonldap-ng',
}
)
);
ok( $h->can('_dbh') );
unlink 't/lmConf.sql';
my $count = 2;
eval { require DBD::SQLite };
unless ($@) {
require DBI;
my $dbh = DBI->connect( "dbi:SQLite:dbname=t/lmConf.sql", '', '', {AutoCommit=>1} );
$dbh->{sqlite_unicode} = 1;
ok(
$dbh->do(
' CREATE TABLE lmConfig ( cfgNum int not null primary key, data longblob)'
),
'Database created'
);
undef $dbh;
$count++;
ok(
$h = new Lemonldap::NG::Common::Conf(
{
type => 'CDBI',
dbiChain => "DBI:SQLite:dbname=t/lmConf.sql",
dbiUser => '',
dbiPassword => '',
}
),
'CDBI object'
);
ok( $h->can('_dbh'), 'Driver is build' );
$count += 2;
my @test = (
# simple ascii
{ cfgNum => 1, test => 'ascii' },
# utf-8
{ cfgNum => 2, test => 'Русский' },
# compatible utf8/latin-1 char but with different codes
{ cfgNum => 3, test => 'éà' }
);
for ( my $i = 0 ; $i < @test ; $i++ ) {
ok( $h->store( $test[$i] ) == $i+1, "Test $i is stored" )
or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
$count++;
my $cfg;
ok( $cfg = $h->load($i+1), "Test $i can be read" )
or print STDERR $Lemonldap::NG::Common::Conf::msg;
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" );
$count += 2;
}
}
unlink 't/lmConf.sql';
done_testing($count);
......@@ -5,8 +5,13 @@
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More tests => 3;
BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
use strict;
use Test::More;
BEGIN {
use_ok('DBI');
use_ok('Lemonldap::NG::Common::Conf');
}
#########################
......@@ -15,15 +20,64 @@ BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
my $h;
@ARGV = ("help=groups");
ok(
$h = new Lemonldap::NG::Common::Conf(
{
type => 'RDBI',
dbiChain => "DBI:mysql:database=lemonldap-ng",
dbiUser => 'lemonldap-ng',
}
)
);
ok( $h->can('_dbh') );
unlink 't/lmConf.sql';
my $count = 2;
eval { require DBD::SQLite };
unless ($@) {
require DBI;
my $dbh = DBI->connect( "dbi:SQLite:dbname=t/lmConf.sql", '', '', {AutoCommit=>1} );
$dbh->{sqlite_unicode} = 1;
ok(
$dbh->do(
" CREATE TABLE lmConfig ( cfgNum int not null, field varchar(255) NOT NULL DEFAULT '', value longblob, PRIMARY KEY (cfgNum,field))"
),
'Database created'
);
undef $dbh;
$count++;
ok(
$h = new Lemonldap::NG::Common::Conf(
{
type => 'RDBI',
dbiChain => "DBI:SQLite:dbname=t/lmConf.sql",
dbiUser => '',
dbiPassword => '',
}
),
'RDBI object'
);
ok( $h->can('_dbh'), 'Driver is build' );
$count += 2;
my @test = (
# simple ascii
{ cfgNum => 1, test => 'ascii' },
# utf-8
{ cfgNum => 2, test => 'Русский' },
# compatible utf8/latin-1 char but with different codes
{ cfgNum => 3, test => 'éà' }
);
for ( my $i = 0 ; $i < @test ; $i++ ) {
ok( $h->store( $test[$i] ) == $i+1, "Test $i is stored" )
or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
$count++;
my $cfg;
ok( $cfg = $h->load($i+1), "Test $i can be read" )
or print STDERR $Lemonldap::NG::Common::Conf::msg;
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" );
$count += 2;
}
}
unlink 't/lmConf.sql';
done_testing($count);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment