DBIx-Brev
view release on metacpan or search on metacpan
t/DBIx-Brev.t view on Meta::CPAN
$results = [sql_map {$_} q{select a,b from t1 order by a}];
is_deeply($results,$expected,"sql_map simple") or diag explain $results;
$expected = [map $_->[0], @$records]; # [1,3,5]
$results = sql_map {$_} q{select a from t1 order by a};
is_deeply($results,$expected,"sql_map scalarize") or diag explain $results;
}
sub test_sql_query {
my ($expected,$results);
$expected = $records;
$results = sql_query q{select a,b from t1 order by a};
is_deeply($results,$expected,"sql_query scalar context");
$results = [sql_query q{select a,b from t1 order by a}];
is_deeply($results,$expected,"sql_query array context");
$expected = [map $_->[0], @$expected];
$results = sql_query q{select a from t1 order by a};
is_deeply($results,$expected,"sql_query single column");
}
sub test_sql_value {
my $mina = min(map $_->[0], @$records);
my $maxa = max(map $_->[0], @$records);
# test sql_value
my ($min,$max,$count) = sql_value q{select min(a),max(a),count(*) from t1};
is($min, $mina, "sql_value min array context");
is($max, $maxa, "sql_value max array context");
is($count, @$records, "sql_value count array context");
my $min1 = sql_value q{select min(a) from t1};
is($min1, $mina, "sql_value scalar context min");
my $max2 = sql_value q{select max(a),min(a) from t1};
is($max2, $maxa, "sql_value scalar2 context max");
my $a_first = sql_value q{select a from t1 order by a};
is($a_first,$records->[0][0],q{sql_value first})
}
sub test_load_config {
# create config file on the fly
my ($fh,$config_file) = tempfile();
print $fh qq{<database dbm>\ndata_source=dbi:SQLite:dbname=$db_file\n</database>};
close($fh);
# load config using $ENV{DBI_CONF}
$ENV{DBI_CONF} = $config_file;
my %config = DBIx::Brev::load_config();
ok(scalar keys %config,q{load config using $ENV{DBI_CONF}});
# load config using explicit param
my %config1 = DBIx::Brev::load_config($config_file);
is_deeply(\%config,\%config1,"load config using explicit filename");
# load config from HOME
SKIP: {
delete $ENV{DBI_CONF};
my $mswin = $^O eq 'MSWin32';
my $fd = $mswin?q{\\}:q{/};
my ($home) = grep defined && -d, map $ENV{$_}, $mswin? qw(USERPROFILE HOME):'HOME';
skip "was not able to find HOME directory", 1 unless defined($home);
my $home_config = $home.$fd.q{dbi.conf};
my $backup = -f $home_config?grep(!-f,map $home_config.substr(rand(),1),1..10):();
my %config2;
skip "was not able to create/replace $home_config",1 unless eval {
move($home_config,$backup) if $backup;
move($config_file,$home_config);
%config2 = DBIx::Brev::load_config();
unlink $home_config;
move($backup,$home_config) if $backup;
1;
};
is_deeply(\%config2,\%config,q{load config from HOME directory});
};
unlink $config_file;
is($config{database}{dbm}{data_source},"dbi:SQLite:dbname=$db_file",'config file data_source');
}
sub test_fork {
SKIP: {
my $pid = eval {fork()};
skip "can't fork()", 1 unless defined($pid);
if ($pid == 0) {
sql_value("select 1"); # use dbc at fork
exit;
}
wait;
is(sql_value("select 1"),1,"dbc after forked child");
}
}
sub test_without_config {
$DBIx::Brev::use_config = 0;
$DBIx::Brev::use_dbc = 0;
my $dbh = db_use("dbi:SQLite:dbname=$db_file");
#print Dumper($dbh);
ok($dbh,"without config");
#ok($dbh->isa('DBI::db'),"without dbc");
$DBIx::Brev::use_config = 1;
$DBIx::Brev::use_dbc = 1;
}
sub test_without_sqlsplit {
$DBIx::Brev::use_sqlsplit = 0;
my $r = sql_exec("update t1 set a=a+1");
ok($r==3,"without split");
$DBIx::Brev::use_sqlsplit = 1;
}
( run in 0.555 second using v1.01-cache-2.11-cpan-b16cb0d3907 )