Newer Older
60 lines | 1.589kb
packing
yuki-kimoto authored on 2009-11-12
1
use Test::More 'no_plan';
2
use strict;
3
use warnings;
4
use utf8;
5

            
6
use DBIx::Custom::SQLite;
7

            
8
# Function for test name
9
my $test;
10
sub test {
11
    $test = shift;
12
}
13

            
14
# Constant varialbes for test
15
my $CREATE_TABLE = {
16
    0 => 'create table table1 (key1 char(255), key2 char(255));',
17
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
18
    2 => 'create table table2 (key1 char(255), key3 char(255));'
19
};
20

            
21

            
22
# Variables for tests
23
my $dbi;
24
my $ret_val;
25
my $rows;
26
my $db_file;
27

            
28
test 'connect_memory';
29
$dbi = DBIx::Custom::SQLite->new;
30
$dbi->connect_memory;
31
$ret_val = $dbi->do($CREATE_TABLE->{0});
32
ok(defined $ret_val, $test);
33
$dbi->insert('table1', {key1 => 'あ', key2 => 2});
34
$rows = $dbi->select('table1', {key1 => 'あ'})->fetch_all_hash;
35
is_deeply($rows, [{key1 => 'あ', key2 => 2}], "$test : select rows");
36

            
37
test 'connect_memory error';
38
eval{$dbi->connect_memory};
39
like($@, qr/Already connected/, "$test : already connected");
40

            
41
test 'reconnect_memory';
42
$dbi = DBIx::Custom::SQLite->new;
43
$dbi->reconnect_memory;
44
$ret_val = $dbi->do($CREATE_TABLE->{0});
45
ok(defined $ret_val, "$test : connect first");
46
$dbi->reconnect_memory;
47
$ret_val = $dbi->do($CREATE_TABLE->{2});
48
ok(defined $ret_val, "$test : connect first");
49

            
50
test 'connect';
51
$db_file  = 't/test.db';
52
unlink $db_file if -f $db_file;
53
$dbi = DBIx::Custom::SQLite->new(database => $db_file);
54
$dbi->connect;
55
ok(-f $db_file, "$test : database file");
56
$ret_val = $dbi->do($CREATE_TABLE->{0});
57
ok(defined $ret_val, "$test : database");
58
$dbi->disconnect;
59
unlink $db_file if -f $db_file;
60