mirror of
https://github.com/DBD-SQLite/DBD-SQLite
synced 2025-06-07 14:19:10 -04:00
DBD-SQLite: made svn:eol-style property to native
This commit is contained in:
parent
1e3d1171a8
commit
1d6d87ffc1
2 changed files with 233 additions and 233 deletions
312
t/36_hooks.t
312
t/36_hooks.t
|
@ -1,156 +1,156 @@
|
||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
BEGIN {
|
BEGIN {
|
||||||
$| = 1;
|
$| = 1;
|
||||||
$^W = 1;
|
$^W = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
use t::lib::Test qw/connect_ok @CALL_FUNCS/;
|
use t::lib::Test qw/connect_ok @CALL_FUNCS/;
|
||||||
use Test::More;
|
use Test::More;
|
||||||
use Test::NoWarnings qw/had_no_warnings clear_warnings/;
|
use Test::NoWarnings qw/had_no_warnings clear_warnings/;
|
||||||
|
|
||||||
use DBD::SQLite;
|
use DBD::SQLite;
|
||||||
|
|
||||||
plan tests => 24 * @CALL_FUNCS + 1;
|
plan tests => 24 * @CALL_FUNCS + 1;
|
||||||
|
|
||||||
# hooks : just count the commits / rollbacks / updates
|
# hooks : just count the commits / rollbacks / updates
|
||||||
my ($n_commits, $n_rollbacks, $n_updates, @update_args);
|
my ($n_commits, $n_rollbacks, $n_updates, @update_args);
|
||||||
sub commit_hook { $n_commits += 1; return 0; }
|
sub commit_hook { $n_commits += 1; return 0; }
|
||||||
sub rollback_hook { $n_rollbacks += 1; return 0; }
|
sub rollback_hook { $n_rollbacks += 1; return 0; }
|
||||||
sub update_hook { $n_updates += 1;
|
sub update_hook { $n_updates += 1;
|
||||||
@update_args = @_; }
|
@update_args = @_; }
|
||||||
|
|
||||||
my $sql_count_rows = "SELECT COUNT(foo) FROM hook_test";
|
my $sql_count_rows = "SELECT COUNT(foo) FROM hook_test";
|
||||||
|
|
||||||
foreach my $call_func (@CALL_FUNCS) {
|
foreach my $call_func (@CALL_FUNCS) {
|
||||||
|
|
||||||
# connect
|
# connect
|
||||||
my $dbh = connect_ok( RaiseError => 1 );
|
my $dbh = connect_ok( RaiseError => 1 );
|
||||||
$dbh->do( 'CREATE TEMP TABLE hook_test ( foo )' );
|
$dbh->do( 'CREATE TEMP TABLE hook_test ( foo )' );
|
||||||
|
|
||||||
# register the hooks
|
# register the hooks
|
||||||
my $previous_commit_hook = $dbh->$call_func(\&commit_hook,
|
my $previous_commit_hook = $dbh->$call_func(\&commit_hook,
|
||||||
"commit_hook");
|
"commit_hook");
|
||||||
my $previous_rollback_hook = $dbh->$call_func(\&rollback_hook,
|
my $previous_rollback_hook = $dbh->$call_func(\&rollback_hook,
|
||||||
"rollback_hook");
|
"rollback_hook");
|
||||||
my $previous_update_hook = $dbh->$call_func(\&update_hook,
|
my $previous_update_hook = $dbh->$call_func(\&update_hook,
|
||||||
"update_hook");
|
"update_hook");
|
||||||
ok(!$previous_commit_hook, "initial commit hook was undef");
|
ok(!$previous_commit_hook, "initial commit hook was undef");
|
||||||
ok(!$previous_rollback_hook, "initial rollback hook was undef");
|
ok(!$previous_rollback_hook, "initial rollback hook was undef");
|
||||||
ok(!$previous_update_hook, "initial update hook was undef");
|
ok(!$previous_update_hook, "initial update hook was undef");
|
||||||
|
|
||||||
# a couple of transactions
|
# a couple of transactions
|
||||||
do_transaction($dbh) for 1..3;
|
do_transaction($dbh) for 1..3;
|
||||||
|
|
||||||
# commit hook should have been called three times
|
# commit hook should have been called three times
|
||||||
is($n_commits, 3, "3 commits");
|
is($n_commits, 3, "3 commits");
|
||||||
|
|
||||||
# update hook should have been called 30 times
|
# update hook should have been called 30 times
|
||||||
is($n_updates, 30, "30 updates");
|
is($n_updates, 30, "30 updates");
|
||||||
|
|
||||||
# check args transmitted to update hook;
|
# check args transmitted to update hook;
|
||||||
is($update_args[0], DBD::SQLite::INSERT, 'update hook arg 0: INSERT');
|
is($update_args[0], DBD::SQLite::INSERT, 'update hook arg 0: INSERT');
|
||||||
is($update_args[1], 'temp', 'update hook arg 1: database');
|
is($update_args[1], 'temp', 'update hook arg 1: database');
|
||||||
is($update_args[2], 'hook_test', 'update hook arg 2: table');
|
is($update_args[2], 'hook_test', 'update hook arg 2: table');
|
||||||
ok($update_args[3], 'update hook arg 3: rowid');
|
ok($update_args[3], 'update hook arg 3: rowid');
|
||||||
|
|
||||||
# unregister the commit and update hooks, check if previous hooks are returned
|
# unregister the commit and update hooks, check if previous hooks are returned
|
||||||
$previous_commit_hook = $dbh->$call_func(undef, "commit_hook");
|
$previous_commit_hook = $dbh->$call_func(undef, "commit_hook");
|
||||||
ok($previous_commit_hook eq \&commit_hook,
|
ok($previous_commit_hook eq \&commit_hook,
|
||||||
"previous commit hook correctly returned");
|
"previous commit hook correctly returned");
|
||||||
$previous_update_hook = $dbh->$call_func(undef, "update_hook");
|
$previous_update_hook = $dbh->$call_func(undef, "update_hook");
|
||||||
ok($previous_update_hook eq \&update_hook,
|
ok($previous_update_hook eq \&update_hook,
|
||||||
"previous update hook correctly returned");
|
"previous update hook correctly returned");
|
||||||
|
|
||||||
# some more transactions .. commit and update hook should not be called
|
# some more transactions .. commit and update hook should not be called
|
||||||
$n_commits = 0;
|
$n_commits = 0;
|
||||||
$n_updates = 0;
|
$n_updates = 0;
|
||||||
do_transaction($dbh) for 1..3;
|
do_transaction($dbh) for 1..3;
|
||||||
is($n_commits, 0, "commit hook unregistered");
|
is($n_commits, 0, "commit hook unregistered");
|
||||||
is($n_updates, 0, "update hook unregistered");
|
is($n_updates, 0, "update hook unregistered");
|
||||||
|
|
||||||
# check here explicitly for warnings, before we clear them
|
# check here explicitly for warnings, before we clear them
|
||||||
had_no_warnings();
|
had_no_warnings();
|
||||||
|
|
||||||
# remember how many rows we had so far
|
# remember how many rows we had so far
|
||||||
my ($n_rows) = $dbh->selectrow_array($sql_count_rows);
|
my ($n_rows) = $dbh->selectrow_array($sql_count_rows);
|
||||||
|
|
||||||
# a commit hook that rejects the transaction
|
# a commit hook that rejects the transaction
|
||||||
$dbh->$call_func(sub {return 1}, "commit_hook");
|
$dbh->$call_func(sub {return 1}, "commit_hook");
|
||||||
eval {do_transaction($dbh)}; # in eval() because of RaiseError
|
eval {do_transaction($dbh)}; # in eval() because of RaiseError
|
||||||
ok ($@, "transaction was rejected: $@" );
|
ok ($@, "transaction was rejected: $@" );
|
||||||
|
|
||||||
# no explicit rollback, because SQLite already did it
|
# no explicit rollback, because SQLite already did it
|
||||||
# eval {$dbh->rollback;};
|
# eval {$dbh->rollback;};
|
||||||
# ok (!$@, "rollback OK $@");
|
# ok (!$@, "rollback OK $@");
|
||||||
|
|
||||||
# rollback hook should have been called
|
# rollback hook should have been called
|
||||||
is($n_rollbacks, 1, "1 rollback");
|
is($n_rollbacks, 1, "1 rollback");
|
||||||
|
|
||||||
# unregister the rollback hook, check if previous hook is returned
|
# unregister the rollback hook, check if previous hook is returned
|
||||||
$previous_rollback_hook = $dbh->$call_func(undef, "rollback_hook");
|
$previous_rollback_hook = $dbh->$call_func(undef, "rollback_hook");
|
||||||
ok($previous_rollback_hook eq \&rollback_hook,
|
ok($previous_rollback_hook eq \&rollback_hook,
|
||||||
"previous hook correctly returned");
|
"previous hook correctly returned");
|
||||||
|
|
||||||
# try transaction again .. rollback hook should not be called
|
# try transaction again .. rollback hook should not be called
|
||||||
$n_rollbacks = 0;
|
$n_rollbacks = 0;
|
||||||
eval {do_transaction($dbh)};
|
eval {do_transaction($dbh)};
|
||||||
is($n_rollbacks, 0, "rollback hook unregistered");
|
is($n_rollbacks, 0, "rollback hook unregistered");
|
||||||
|
|
||||||
# check that the rollbacks did really occur
|
# check that the rollbacks did really occur
|
||||||
my ($n_rows_after) = $dbh->selectrow_array($sql_count_rows);
|
my ($n_rows_after) = $dbh->selectrow_array($sql_count_rows);
|
||||||
is($n_rows, $n_rows_after, "no rows added" );
|
is($n_rows, $n_rows_after, "no rows added" );
|
||||||
|
|
||||||
# unregister commit hook, register an authorizer that forbids delete ops
|
# unregister commit hook, register an authorizer that forbids delete ops
|
||||||
$dbh->$call_func(undef, "commit_hook");
|
$dbh->$call_func(undef, "commit_hook");
|
||||||
my @authorizer_args;
|
my @authorizer_args;
|
||||||
my $authorizer = sub {
|
my $authorizer = sub {
|
||||||
@authorizer_args = @_;
|
@authorizer_args = @_;
|
||||||
my $action_code = shift;
|
my $action_code = shift;
|
||||||
my $retval = $action_code == DBD::SQLite::DELETE ? DBD::SQLite::DENY
|
my $retval = $action_code == DBD::SQLite::DELETE ? DBD::SQLite::DENY
|
||||||
: DBD::SQLite::OK;
|
: DBD::SQLite::OK;
|
||||||
return $retval;
|
return $retval;
|
||||||
};
|
};
|
||||||
unless ($^O =~ /MSWin32|freebsd/) {
|
unless ($^O =~ /MSWin32|freebsd/) {
|
||||||
# FIXME: this line may cause segfalut
|
# FIXME: this line may cause segfalut
|
||||||
$dbh->$call_func($authorizer, "set_authorizer");
|
$dbh->$call_func($authorizer, "set_authorizer");
|
||||||
}
|
}
|
||||||
|
|
||||||
# try an insert (should be authorized) and check authorizer args
|
# try an insert (should be authorized) and check authorizer args
|
||||||
$dbh->do("INSERT INTO hook_test VALUES ('auth_test')");
|
$dbh->do("INSERT INTO hook_test VALUES ('auth_test')");
|
||||||
is_deeply(\@authorizer_args,
|
is_deeply(\@authorizer_args,
|
||||||
[DBD::SQLite::INSERT, 'hook_test', undef, 'temp', undef],
|
[DBD::SQLite::INSERT, 'hook_test', undef, 'temp', undef],
|
||||||
"args to authorizer (INSERT)");
|
"args to authorizer (INSERT)");
|
||||||
|
|
||||||
# try a delete (should be unauthorized)
|
# try a delete (should be unauthorized)
|
||||||
eval {$dbh->do("DELETE FROM hook_test WHERE foo = 'auth_test'")};
|
eval {$dbh->do("DELETE FROM hook_test WHERE foo = 'auth_test'")};
|
||||||
ok($@, "delete was rejected with message $@");
|
ok($@, "delete was rejected with message $@");
|
||||||
is_deeply(\@authorizer_args,
|
is_deeply(\@authorizer_args,
|
||||||
[DBD::SQLite::DELETE, 'hook_test', undef, 'temp', undef],
|
[DBD::SQLite::DELETE, 'hook_test', undef, 'temp', undef],
|
||||||
"args to authorizer (DELETE)");
|
"args to authorizer (DELETE)");
|
||||||
|
|
||||||
|
|
||||||
# unregister the authorizer ... now DELETE should be authorized
|
# unregister the authorizer ... now DELETE should be authorized
|
||||||
$dbh->$call_func(undef, "set_authorizer");
|
$dbh->$call_func(undef, "set_authorizer");
|
||||||
eval {$dbh->do("DELETE FROM hook_test WHERE foo = 'auth_test'")};
|
eval {$dbh->do("DELETE FROM hook_test WHERE foo = 'auth_test'")};
|
||||||
ok(!$@, "delete was accepted");
|
ok(!$@, "delete was accepted");
|
||||||
|
|
||||||
|
|
||||||
# sqlite3 did warn in tests above, so avoid complains from Test::Warnings
|
# sqlite3 did warn in tests above, so avoid complains from Test::Warnings
|
||||||
# (would be better to turn off warnings from sqlite3, but I didn't find
|
# (would be better to turn off warnings from sqlite3, but I didn't find
|
||||||
# any way to do that)
|
# any way to do that)
|
||||||
clear_warnings();
|
clear_warnings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub do_transaction {
|
sub do_transaction {
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
|
|
||||||
$dbh->begin_work;
|
$dbh->begin_work;
|
||||||
for my $count (1 .. 10) {
|
for my $count (1 .. 10) {
|
||||||
my $rand = rand;
|
my $rand = rand;
|
||||||
$dbh->do( "INSERT INTO hook_test(foo) VALUES ( $rand )" );
|
$dbh->do( "INSERT INTO hook_test(foo) VALUES ( $rand )" );
|
||||||
}
|
}
|
||||||
$dbh->commit;
|
$dbh->commit;
|
||||||
}
|
}
|
||||||
|
|
154
t/37_regexp.t
154
t/37_regexp.t
|
@ -1,77 +1,77 @@
|
||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
BEGIN {
|
BEGIN {
|
||||||
$| = 1;
|
$| = 1;
|
||||||
$^W = 1;
|
$^W = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
use t::lib::Test qw/connect_ok @CALL_FUNCS/;
|
use t::lib::Test qw/connect_ok @CALL_FUNCS/;
|
||||||
use Test::More;
|
use Test::More;
|
||||||
|
|
||||||
my @words = qw{
|
my @words = qw{
|
||||||
berger Bergère bergère Bergere
|
berger Bergère bergère Bergere
|
||||||
HOT hôte
|
HOT hôte
|
||||||
hétéroclite hétaïre hêtre héraut
|
hétéroclite hétaïre hêtre héraut
|
||||||
HAT hâter
|
HAT hâter
|
||||||
fétu fête fève ferme
|
fétu fête fève ferme
|
||||||
};
|
};
|
||||||
my @regexes = qw( ^b\\w+ (?i:^b\\w+) );
|
my @regexes = qw( ^b\\w+ (?i:^b\\w+) );
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
if ($] < 5.008005) {
|
if ($] < 5.008005) {
|
||||||
plan skip_all => 'Unicode is not supported before 5.8.5'
|
plan skip_all => 'Unicode is not supported before 5.8.5'
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
plan tests => 2 * (1 + 2 * @regexes) * @CALL_FUNCS + 1 ;
|
plan tests => 2 * (1 + 2 * @regexes) * @CALL_FUNCS + 1 ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
use Test::NoWarnings;
|
use Test::NoWarnings;
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
# Sadly perl for windows (and probably sqlite, too) may hang
|
# Sadly perl for windows (and probably sqlite, too) may hang
|
||||||
# if the system locale doesn't support european languages.
|
# if the system locale doesn't support european languages.
|
||||||
# en-us should be a safe default. if it doesn't work, use 'C'.
|
# en-us should be a safe default. if it doesn't work, use 'C'.
|
||||||
if ( $^O eq 'MSWin32') {
|
if ( $^O eq 'MSWin32') {
|
||||||
use POSIX 'locale_h';
|
use POSIX 'locale_h';
|
||||||
setlocale(LC_COLLATE, 'en-us');
|
setlocale(LC_COLLATE, 'en-us');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
use locale;
|
use locale;
|
||||||
|
|
||||||
use DBD::SQLite;
|
use DBD::SQLite;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
foreach my $call_func (@CALL_FUNCS) {
|
foreach my $call_func (@CALL_FUNCS) {
|
||||||
|
|
||||||
for my $use_unicode (0, 1) {
|
for my $use_unicode (0, 1) {
|
||||||
|
|
||||||
# connect
|
# connect
|
||||||
my $dbh = connect_ok( RaiseError => 1, unicode => $use_unicode );
|
my $dbh = connect_ok( RaiseError => 1, unicode => $use_unicode );
|
||||||
|
|
||||||
# populate test data
|
# populate test data
|
||||||
my @vals = @words;
|
my @vals = @words;
|
||||||
if ($use_unicode) {
|
if ($use_unicode) {
|
||||||
utf8::upgrade($_) foreach @vals;
|
utf8::upgrade($_) foreach @vals;
|
||||||
}
|
}
|
||||||
|
|
||||||
$dbh->do( 'CREATE TEMP TABLE regexp_test ( txt )' );
|
$dbh->do( 'CREATE TEMP TABLE regexp_test ( txt )' );
|
||||||
$dbh->do( "INSERT INTO regexp_test VALUES ( '$_' )" ) foreach @vals;
|
$dbh->do( "INSERT INTO regexp_test VALUES ( '$_' )" ) foreach @vals;
|
||||||
|
|
||||||
foreach my $regex (@regexes) {
|
foreach my $regex (@regexes) {
|
||||||
my @perl_match = grep {/$regex/} @vals;
|
my @perl_match = grep {/$regex/} @vals;
|
||||||
my $sql = "SELECT txt from regexp_test WHERE txt REGEXP '$regex' "
|
my $sql = "SELECT txt from regexp_test WHERE txt REGEXP '$regex' "
|
||||||
. "COLLATE perllocale";
|
. "COLLATE perllocale";
|
||||||
my $db_match = $dbh->selectcol_arrayref($sql);
|
my $db_match = $dbh->selectcol_arrayref($sql);
|
||||||
|
|
||||||
is_deeply(\@perl_match, $db_match, "REGEXP '$regex'");
|
is_deeply(\@perl_match, $db_match, "REGEXP '$regex'");
|
||||||
|
|
||||||
my @perl_antimatch = grep {!/$regex/} @vals;
|
my @perl_antimatch = grep {!/$regex/} @vals;
|
||||||
$sql =~ s/REGEXP/NOT REGEXP/;
|
$sql =~ s/REGEXP/NOT REGEXP/;
|
||||||
my $db_antimatch = $dbh->selectcol_arrayref($sql);
|
my $db_antimatch = $dbh->selectcol_arrayref($sql);
|
||||||
is_deeply(\@perl_antimatch, $db_antimatch, "NOT REGEXP '$regex'");
|
is_deeply(\@perl_antimatch, $db_antimatch, "NOT REGEXP '$regex'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue