DBIx-Custom / t / dbix-custom-querybuilder.t /
Newer Older
169 lines | 4.884kb
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
cleanup
Yuki Kimoto authored on 2011-01-23
9
sub test{ "# $_[0]\n" }
fixed tests
yuki-kimoto authored on 2010-08-06
10

            
11
# Variable for test
12
my $datas;
13
my $builder;
14
my $query;
15
my $ret_val;
added tests
yuki-kimoto authored on 2010-08-12
16
my $source;
17
my $tree;
fixed tests
yuki-kimoto authored on 2010-08-06
18

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

            
55
for (my $i = 0; $i < @$datas; $i++) {
56
    my $data = $datas->[$i];
57
    my $builder = DBIx::Custom::QueryBuilder->new;
58
    my $query = $builder->build_query($data->{source});
cleanup
Yuki Kimoto authored on 2011-01-23
59
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
60
    is_deeply($query->{columns}, $data->{columns_expected}, "$data->{name} : columns");
fixed tests
yuki-kimoto authored on 2010-08-06
61
}
62

            
63

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

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

            
77
$query = $builder->build_query("{p a b}");
cleanup
Yuki Kimoto authored on 2011-01-23
78
is($query->{sql}, "? a b;", "register_tag_processor sql");
79
is_deeply($query->{columns}, [2], "register_tag_processor columns");
fixed tests
yuki-kimoto authored on 2010-08-06
80
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
81

            
82

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

            
add tests
yuki-kimoto authored on 2010-08-10
86
eval{$builder->build_query('{? }')};
cleanup
Yuki Kimoto authored on 2011-01-23
87
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
fixed tests
yuki-kimoto authored on 2010-08-06
88

            
89
eval{$builder->build_query("{a }")};
cleanup
Yuki Kimoto authored on 2011-01-23
90
like($@, qr/\QTag "a" in "{a }" is not registered/, "tag_processor not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
91

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

            
96
eval{$builder->build_query("{q}", {})};
cleanup
Yuki Kimoto authored on 2011-01-23
97
like($@, qr/Tag processor "q" must be sub reference/, "tag_processor not code ref");
fixed tests
yuki-kimoto authored on 2010-08-06
98

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

            
103
eval{$builder->build_query("{r}")};
cleanup
Yuki Kimoto authored on 2011-01-23
104
like($@, qr/\QTag processor "r" must return [STRING, ARRAY_REFERENCE]/, "tag processor return noting");
fixed tests
yuki-kimoto authored on 2010-08-06
105

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

            
110
eval{$builder->build_query("{s}")};
cleanup
Yuki Kimoto authored on 2011-01-23
111
like($@, qr/\QTag processor "s" must return [STRING, ARRAY_REFERENCE]/, "tag processor return not array columns");
fixed tests
yuki-kimoto authored on 2010-08-06
112

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

            
117

            
118
test 'General error case';
119
$builder = DBIx::Custom::QueryBuilder->new;
120
$builder->register_tag_processor(
121
    a => sub {
122
        return ["? ? ?", ['']];
123
    }
124
);
125
eval{$builder->build_query("{a}")};
cleanup
Yuki Kimoto authored on 2011-01-23
126
like($@, qr/\QPlaceholder count in "? ? ?" must be same as column count 1/, "placeholder count is invalid");
fixed tests
yuki-kimoto authored on 2010-08-06
127

            
128

            
129
test 'Default tag processor Error case';
130
eval{$builder->build_query("{= }")};
cleanup
Yuki Kimoto authored on 2011-01-23
131
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
132

            
133
eval{$builder->build_query("{in }")};
cleanup
Yuki Kimoto authored on 2011-01-23
134
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
135

            
136
eval{$builder->build_query("{in a}")};
137
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
cleanup
Yuki Kimoto authored on 2011-01-23
138
     "in : key not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
139

            
140
eval{$builder->build_query("{in a r}")};
141
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
cleanup
Yuki Kimoto authored on 2011-01-23
142
     "in : key not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
143

            
added tests
yuki-kimoto authored on 2010-08-12
144
test 'variouse source';
145
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
146
$query = $builder->build_query($source);
cleanup
Yuki Kimoto authored on 2011-01-23
147
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
148

            
added tests
yuki-kimoto authored on 2010-08-12
149
$source = "abc;";
150
$query = $builder->build_query($source);
cleanup
Yuki Kimoto authored on 2011-01-23
151
is($query->sql, 'abc;', "basic : 2");
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
152

            
added tests
yuki-kimoto authored on 2010-08-12
153
$source = "{= a}";
154
$query = $builder->build_query($source);
cleanup
Yuki Kimoto authored on 2011-01-23
155
is($query->sql, 'a = ?;', "only tag");
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
156

            
157
$source = "000;";
158
$query = $builder->build_query($source);
cleanup
Yuki Kimoto authored on 2011-01-23
159
is($query->sql, '000;', "contain 0 value");
added tests
yuki-kimoto authored on 2010-08-12
160

            
161
$source = "a {= b} }";
162
eval{$builder->build_query($source)};
cleanup
Yuki Kimoto authored on 2011-01-23
163
like($@, qr/unexpected "}"/, "error : 1");
added tests
yuki-kimoto authored on 2010-08-12
164

            
165
$source = "a {= {}";
166
eval{$builder->build_query($source)};
cleanup
Yuki Kimoto authored on 2011-01-23
167
like($@, qr/unexpected "{"/, "error : 2");
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
168

            
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
169