DBIx-Custom / t / common.t /
Newer Older
127 lines | 3.664kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
4
use DBIx::Custom;
5

            
6
my $dbi;
7

            
8
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
9
  unless $ENV{DBIX_CUSTOM_TEST_RUN}
10
    && eval { $dbi = DBIx::Custom->connect; 1 };
11

            
12
plan 'no_plan';
13

            
cleanup test
Yuki Kimoto authored on 2011-08-08
14
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
15
sub test { print "# $_[0]\n" }
16

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
17
# Constant
18
my $create_table1 = $dbi->create_table1;
19

            
20
# Variable
cleanup test
Yuki Kimoto authored on 2011-08-08
21
# Variables
22
my $builder;
23
my $datas;
24
my $sth;
25
my $source;
26
my @sources;
27
my $select_source;
28
my $insert_source;
29
my $update_source;
30
my $param;
31
my $params;
32
my $sql;
33
my $result;
34
my $row;
35
my @rows;
36
my $rows;
37
my $query;
38
my @queries;
39
my $select_query;
40
my $insert_query;
41
my $update_query;
42
my $ret_val;
43
my $infos;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
44
my $model;
cleanup test
Yuki Kimoto authored on 2011-08-08
45
my $model2;
46
my $where;
47
my $update_param;
48
my $insert_param;
49
my $join;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
50

            
51
# Drop table
52
eval { $dbi->execute('drop table table1') };
53

            
54
# Create table
55
$dbi->execute($create_table1);
56
$model = $dbi->create_model(table => 'table1');
57
$model->insert({key1 => 1, key2 => 2});
58
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
59

            
cleanup test
Yuki Kimoto authored on 2011-08-08
60
test 'DBIx::Custom::Result test';
61
$dbi->delete_all(table => 'table1');
62
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
63
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
64
$source = "select key1, key2 from table1";
65
$query = $dbi->create_query($source);
66
$result = $dbi->execute($query);
67

            
68
@rows = ();
69
while (my $row = $result->fetch) {
70
    push @rows, [@$row];
71
}
72
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
73

            
74
$result = $dbi->execute($query);
75
@rows = ();
76
while (my $row = $result->fetch_hash) {
77
    push @rows, {%$row};
78
}
79
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
80

            
81
$result = $dbi->execute($query);
82
$rows = $result->fetch_all;
83
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
84

            
85
$result = $dbi->execute($query);
86
$rows = $result->fetch_hash_all;
87
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
88

            
89
test 'Insert query return value';
90
$source = "insert into table1 {insert_param key1 key2}";
91
$query = $dbi->execute($source, {}, query => 1);
92
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
93
ok($ret_val);
94

            
95
test 'Direct query';
96
$dbi->delete_all(table => 'table1');
97
$insert_source = "insert into table1 {insert_param key1 key2}";
98
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
99
$result = $dbi->execute('select * from table1;');
100
$rows = $result->all;
101
is_deeply($rows, [{key1 => 1, key2 => 2}]);
102

            
103
test 'Filter basic';
104
$dbi->delete_all(table => 'table1');
105
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
106
                    three_times => sub { $_[0] * 3});
107

            
108
$insert_source  = "insert into table1 {insert_param key1 key2};";
109
$insert_query = $dbi->execute($insert_source, {}, query => 1);
110
$insert_query->filter({key1 => 'twice'});
111
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
112
$result = $dbi->execute('select * from table1;');
113
$rows = $result->filter({key2 => 'three_times'})->all;
114
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
115

            
116
test 'Filter in';
117
$dbi->delete_all(table => 'table1');
118
$insert_source  = "insert into table1 {insert_param key1 key2};";
119
$insert_query = $dbi->execute($insert_source, {}, query => 1);
120
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
121
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
122
$select_query = $dbi->execute($select_source,{}, query => 1);
123
$select_query->filter({'table1.key1' => 'twice'});
124
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
125
$rows = $result->all;
126
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
127