DBIx-Custom / t / dbix-custom-mysql-private.t /
Newer Older
95 lines | 2.348kb
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
cleanup
Yuki Kimoto authored on 2011-01-23
13
sub test { print "# $_[0]\n" }
add examples
Yuki Kimoto authored on 2011-01-07
14

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

            
28

            
29
# Varialbes for tests
30
my $dbi;
31
my $dbname;
32
my $rows;
33

            
34
# Constant varialbes for test
35
my $CREATE_TABLE = {
36
    0 => 'create table table1 (key1 char(255), key2 char(255));'
37
};
38

            
39

            
40
use DBIx::Custom::MySQL;
41

            
42
test 'connect';
43
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
44
                    database => $DATABASE, host => 'localhost', port => '10000');
45
$dbi->connect;
cleanup
Yuki Kimoto authored on 2011-01-23
46
like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source");
47
is(ref $dbi->dbh, 'DBI::db');
add examples
Yuki Kimoto authored on 2011-01-07
48

            
49
test 'attributes';
50
$dbi = DBIx::Custom::MySQL->new;
51
$dbi->host('a');
cleanup
Yuki Kimoto authored on 2011-01-23
52
is($dbi->host, 'a', "host");
add examples
Yuki Kimoto authored on 2011-01-07
53
$dbi->port('b');
cleanup
Yuki Kimoto authored on 2011-01-23
54
is($dbi->port, 'b', "port");
add examples
Yuki Kimoto authored on 2011-01-07
55

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