DBIx-Custom / t / tmp / dbix-custom-sqlite.t /
Newer Older
79 lines | 2.174kb
Simplify key search
yuki-kimoto authored on 2010-02-11
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5

            
6
BEGIN {
7
    eval { require DBD::SQLite; 1 }
8
        or plan skip_all => 'DBD::SQLite required';
9
    eval { DBD::SQLite->VERSION >= 1.25 }
10
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
11

            
12
    plan 'no_plan';
13
    use_ok('DBIx::Custom::SQLite');
14
}
15

            
16
# Function for test name
17
my $test;
18
sub test {
19
    $test = shift;
20
}
21

            
22
# Constant varialbes for test
23
my $CREATE_TABLE = {
24
    0 => 'create table table1 (key1 char(255), key2 char(255));',
25
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
26
    2 => 'create table table2 (key1 char(255), key3 char(255));'
27
};
28

            
29

            
30
# Variables for tests
31
my $dbi;
32
my $ret_val;
33
my $rows;
34
my $db_file;
35
my $id;
36

            
37
test 'connect_memory';
38
$dbi = DBIx::Custom::SQLite->new;
39
$dbi->connect_memory;
40
$ret_val = $dbi->do($CREATE_TABLE->{0});
41
ok(defined $ret_val, $test);
42
$dbi->utf8_filter_on;
43
$dbi->insert('table1', {key1 => 'あ', key2 => 2});
44
$rows = $dbi->select('table1', {key1 => 'あ'})->fetch_hash_all;
45
is_deeply($rows, [{key1 => 'あ', key2 => 2}], "$test : select rows");
46

            
47
test 'connect_memory error';
48
eval{$dbi->connect_memory};
49
like($@, qr/Already connected/, "$test : already connected");
50

            
51
test 'reconnect_memory';
52
$dbi = DBIx::Custom::SQLite->new;
53
$dbi->reconnect_memory;
54
$ret_val = $dbi->do($CREATE_TABLE->{0});
55
ok(defined $ret_val, "$test : connect first");
56
$dbi->reconnect_memory;
57
$ret_val = $dbi->do($CREATE_TABLE->{2});
58
ok(defined $ret_val, "$test : connect first");
59

            
60
test 'connect';
61
$db_file  = 't/test.db';
62
unlink $db_file if -f $db_file;
63
$dbi = DBIx::Custom::SQLite->new(database => $db_file);
64
$dbi->connect;
65
ok(-f $db_file, "$test : database file");
66
$ret_val = $dbi->do($CREATE_TABLE->{0});
67
ok(defined $ret_val, "$test : database");
68
$dbi->disconnect;
69
unlink $db_file if -f $db_file;
70

            
71
test 'last_insert_rowid';
72
$dbi = DBIx::Custom::SQLite->new;
73
$dbi->connect_memory;
74
$ret_val = $dbi->do($CREATE_TABLE->{0});
75
$dbi->insert('table1', {key1 => 1, key2 => 2});
76
is($dbi->last_insert_rowid, 1, "$test: first");
77
$dbi->insert('table1', {key1 => 1, key2 => 2});
78
is($dbi->last_insert_rowid, 2, "$test: second");
79
$dbi->disconnect;