DBIx-Custom / t / dbix-custom-mysql-private.t /
Newer Older
99 lines | 2.41kb
add examples
Yuki Kimoto authored on 2011-01-07
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
# user password database
6
our ($USER, $PASSWORD, $DATABASE) = connect_info();
7

            
8
plan skip_all => 'private MySQL test' unless $USER;
9

            
10
plan 'no_plan';
11

            
12
# Function for test name
13
my $test;
14
sub test {
15
    $test = shift;
16
}
17

            
18

            
19
# Functions for tests
20
sub connect_info {
21
    my $file = 'password.tmp';
22
    open my $fh, '<', $file
23
      or return;
24
    
25
    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
26
    
27
    close $fh;
28
    
29
    return ($user, $password, $database);
30
}
31

            
32

            
33
# Varialbes for tests
34
my $dbi;
35
my $dbname;
36
my $rows;
37

            
38
# Constant varialbes for test
39
my $CREATE_TABLE = {
40
    0 => 'create table table1 (key1 char(255), key2 char(255));'
41
};
42

            
43

            
44
use DBIx::Custom::MySQL;
45

            
46
test 'connect';
47
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
48
                    database => $DATABASE, host => 'localhost', port => '10000');
49
$dbi->connect;
50
like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "$test : created data source");
51
is(ref $dbi->dbh, 'DBI::db', $test);
52

            
53
test 'attributes';
54
$dbi = DBIx::Custom::MySQL->new;
55
$dbi->host('a');
56
is($dbi->host, 'a', "$test: host");
57
$dbi->port('b');
58
is($dbi->port, 'b', "$test: port");
59

            
60
test 'limit';
61
$dbi = DBIx::Custom->connect(
62
    data_source => "dbi:mysql:database=$DATABASE",
63
    user => $USER,
64
    password => $PASSWORD
65
);
66
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
67
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
68
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
69
$dbi->query_builder->register_tag_processor(
70
    limit => sub {
71
        my ($count, $offset) = @_;
72
        
73
        my $s = '';
74
        $offset = 0 unless defined $offset;
75
        $s .= "limit $offset";
76
        $s .= ", $count";
77
        
78
        return [$s, []];
79
    }
80
);
81
$rows = $dbi->select(
82
  table => 'table1',
83
  where => {key1 => 1},
84
  append => "order by key2 {limit 1 0}"
85
)->fetch_hash_all;
86
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
87
$rows = $dbi->select(
88
  table => 'table1',
89
  where => {key1 => 1},
90
  append => "order by key2 {limit 2 1}"
91
)->fetch_hash_all;
92
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test);
93
$rows = $dbi->select(
94
  table => 'table1',
95
  where => {key1 => 1},
96
  append => "order by key2 {limit 1}"
97
)->fetch_hash_all;
98
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
99
$dbi->delete_all(table => 'table1');