DBIx-Custom / t / dbix-custom-querybuilder.t /
Newer Older
142 lines | 4.177kb
fixed tests
yuki-kimoto authored on 2010-08-06
1
use strict;
2
use warnings;
3

            
4
use Test::More 'no_plan';
5

            
6
use DBIx::Custom::QueryBuilder;
7

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

            
14
# Variable for test
15
my $datas;
16
my $builder;
17
my $query;
18
my $ret_val;
19

            
20
test "Various source pattern";
21
$datas = [
22
    # Basic tests
23
    {   name            => 'placeholder basic',
24
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
25
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
26
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
27
    },
28
    {
29
        name            => 'placeholder in',
30
        source            => "{in k1 3};",
31
        sql_expected    => "k1 in (?, ?, ?);",
32
        columns_expected   => [qw/k1 k1 k1/]
33
    },
34
    
35
    # Table name
36
    {
37
        name            => 'placeholder with table name',
38
        source            => "{= a.k1} {= a.k2}",
39
        sql_expected    => "a.k1 = ? a.k2 = ?;",
40
        columns_expected  => [qw/a.k1 a.k2/]
41
    },
42
    {   
43
        name            => 'placeholder in with table name',
44
        source            => "{in a.k1 2} {in b.k2 2}",
45
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
46
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
47
    },
48
    {
49
        name            => 'not contain tag',
50
        source            => "aaa",
51
        sql_expected    => "aaa;",
52
        columns_expected  => [],
53
    }
54
];
55

            
56
for (my $i = 0; $i < @$datas; $i++) {
57
    my $data = $datas->[$i];
58
    my $builder = DBIx::Custom::QueryBuilder->new;
59
    my $query = $builder->build_query($data->{source});
60
    is($query->{sql}, $data->{sql_expected}, "$test : $data->{name} : sql");
61
    is_deeply($query->{columns}, $data->{columns_expected}, "$test : $data->{name} : columns");
62
}
63

            
64

            
65
test 'Original tag processor';
66
$builder = DBIx::Custom::QueryBuilder->new;
67

            
68
$ret_val = $builder->register_tag_processor(
69
    p => sub {
70
        my @args = @_;
71
        
72
        my $expand    = "? $args[0] $args[1]";
73
        my $columns = [2];
74
        return [$expand, $columns];
75
    }
76
);
77

            
78
$query = $builder->build_query("{p a b}");
79
is($query->{sql}, "? a b;", "$test : register_tag_processor sql");
80
is_deeply($query->{columns}, [2], "$test : register_tag_processor columns");
81
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
82

            
83

            
84
test "Tag processor error case";
85
$builder = DBIx::Custom::QueryBuilder->new;
86

            
87

            
88
eval{$builder->build_query("{a }")};
89
like($@, qr/\QTag "a" in "{a }" is not registered/, "$test : tag_processor not exist");
90

            
91
$builder->register_tag_processor({
92
    q => 'string'
93
});
94

            
95
eval{$builder->build_query("{q}", {})};
96
like($@, qr/Tag processor "q" must be sub reference/, "$test : tag_processor not code ref");
97

            
98
$builder->register_tag_processor({
99
   r => sub {} 
100
});
101

            
102
eval{$builder->build_query("{r}")};
103
like($@, qr/\QTag processor "r" must return [STRING, ARRAY_REFERENCE]/, "$test : tag processor return noting");
104

            
105
$builder->register_tag_processor({
106
   s => sub { return ["a", ""]} 
107
});
108

            
109
eval{$builder->build_query("{s}")};
110
like($@, qr/\QTag processor "s" must return [STRING, ARRAY_REFERENCE]/, "$test : tag processor return not array columns");
111

            
112
$builder->register_tag_processor(
113
    t => sub {return ["a", []]}
114
);
115

            
116

            
117
test 'General error case';
118
$builder = DBIx::Custom::QueryBuilder->new;
119
$builder->register_tag_processor(
120
    a => sub {
121
        return ["? ? ?", ['']];
122
    }
123
);
124
eval{$builder->build_query("{a}")};
125
like($@, qr/\QPlaceholder count in "? ? ?" must be same as column count 1/, "$test : placeholder count is invalid");
126

            
127

            
128
test 'Default tag processor Error case';
129
eval{$builder->build_query("{= }")};
130
like($@, qr/Column name must be specified in tag "{= }"/, "$test : basic '=' : key not exist");
131

            
132
eval{$builder->build_query("{in }")};
133
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "$test : in : key not exist");
134

            
135
eval{$builder->build_query("{in a}")};
136
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
137
     "$test : in : key not exist");
138

            
139
eval{$builder->build_query("{in a r}")};
140
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
141
     "$test : in : key not exist");
142