Newer Older
236 lines | 9.06kb
packing
yuki-kimoto authored on 2009-11-12
1
use strict;
2
use warnings;
3

            
4
use Test::More 'no_plan';
5

            
6
use DBIx::Custom::SQL::Template;
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 $sql_tmpl;
17
my $query;
18
my $ret_val;
19
my $clone;
20

            
21
test "Various template pattern";
22
$datas = [
23
    # Basic tests
24
    {   name            => 'placeholder basic',
25
        tmpl            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
26
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
27
        key_infos_expected   => [
28
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['k1']]},
29
            {original_key => 'k2', table => '', column => 'k2', access_keys => [['k2']]},
30
            {original_key => 'k3', table => '', column => 'k3', access_keys => [['k3']]},
31
            {original_key => 'k4', table => '', column => 'k4', access_keys => [['k4']]},
32
            {original_key => 'k5', table => '', column => 'k5', access_keys => [['k5']]},
33
            {original_key => 'k6', table => '', column => 'k6', access_keys => [['k6']]},
34
            {original_key => 'k7', table => '', column => 'k7', access_keys => [['k7']]},
35
            {original_key => 'k8', table => '', column => 'k8', access_keys => [['k8']]},
36
        ],
37
    },
38
    {
39
        name            => 'placeholder in',
40
        tmpl            => "{in k1 3};",
41
        sql_expected    => "k1 in (?, ?, ?);",
42
        key_infos_expected   => [
43
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['k1', [0]]]},
44
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['k1', [1]]]},
45
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['k1', [2]]]},
46
        ],
47
    },
48
    {
49
        name            => 'insert',
50
        tmpl            => "{insert k1 k2 k3}",
51
        sql_expected    => "(k1, k2, k3) values (?, ?, ?);",
52
        key_infos_expected   => [
53
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['#insert', 'k1'], ['k1']]},
54
            {original_key => 'k2', table => '', column => 'k2', access_keys => [['#insert', 'k2'], ['k2']]},
55
            {original_key => 'k3', table => '', column => 'k3', access_keys => [['#insert', 'k3'], ['k3']]},
56
        ],
57
    },
58
    {
59
        name            => 'update',
60
        tmpl            => "{update k1 k2 k3}",
61
        sql_expected    => "set k1 = ?, k2 = ?, k3 = ?;",
62
        key_infos_expected   => [
63
            {original_key => 'k1', table => '', column => 'k1', access_keys => [['#update', 'k1'], ['k1']]},
64
            {original_key => 'k2', table => '', column => 'k2', access_keys => [['#update', 'k2'], ['k2']]},
65
            {original_key => 'k3', table => '', column => 'k3', access_keys => [['#update', 'k3'], ['k3']]},
66
        ],
67
    },
68
    
69
    # Table name
70
    {
71
        name            => 'placeholder with table name',
72
        tmpl            => "{= a.k1} {= a.k2}",
73
        sql_expected    => "a.k1 = ? a.k2 = ?;",
74
        key_infos_expected  => [
75
            {original_key => 'a.k1', table => 'a', column => 'k1', access_keys => [['a.k1'], ['a', 'k1']]},
76
            {original_key => 'a.k2', table => 'a', column => 'k2', access_keys => [['a.k2'], ['a', 'k2']]},
77
        ],
78
    },
79
    {   
80
        name            => 'placeholder in with table name',
81
        tmpl            => "{in a.k1 2} {in b.k2 2}",
82
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
83
        key_infos_expected  => [
84
            {original_key => 'a.k1', table => 'a', column => 'k1', access_keys => [['a.k1', [0]], ['a', 'k1', [0]]]},
85
            {original_key => 'a.k1', table => 'a', column => 'k1', access_keys => [['a.k1', [1]], ['a', 'k1', [1]]]},
86
            {original_key => 'b.k2', table => 'b', column => 'k2', access_keys => [['b.k2', [0]], ['b', 'k2', [0]]]},
87
            {original_key => 'b.k2', table => 'b', column => 'k2', access_keys => [['b.k2', [1]], ['b', 'k2', [1]]]},
88
        ],
89
    },
90
    {
91
        name            => 'insert with table name',
92
        tmpl            => "{insert a.k1 b.k2}",
93
        sql_expected    => "(k1, k2) values (?, ?);",
94
        key_infos_expected  => [
95
            {original_key => 'a.k1', table => 'a', column => 'k1', access_keys => [['#insert', 'a.k1'], ['#insert', 'a', 'k1'], ['a.k1'], ['a', 'k1']]},
96
            {original_key => 'b.k2', table => 'b', column => 'k2', access_keys => [['#insert', 'b.k2'], ['#insert', 'b', 'k2'], ['b.k2'], ['b', 'k2']]},
97
        ],
98
    },
99
    {
100
        name            => 'update with table name',
101
        tmpl            => "{update a.k1 b.k2}",
102
        sql_expected    => "set k1 = ?, k2 = ?;",
103
        key_infos_expected  => [
104
            {original_key => 'a.k1', table => 'a', column => 'k1', access_keys => [['#update', 'a.k1'], ['#update', 'a', 'k1'], ['a.k1'], ['a', 'k1']]},
105
            {original_key => 'b.k2', table => 'b', column => 'k2', access_keys => [['#update', 'b.k2'], ['#update', 'b', 'k2'], ['b.k2'], ['b', 'k2']]},
106
        ],
107
    },
108
    {
109
        name            => 'not contain tag',
110
        tmpl            => "aaa",
111
        sql_expected    => "aaa;",
112
        key_infos_expected  => [],
113
    }
114
];
115

            
116
for (my $i = 0; $i < @$datas; $i++) {
117
    my $data = $datas->[$i];
118
    my $sql_tmpl = DBIx::Custom::SQL::Template->new;
119
    my $query = $sql_tmpl->create_query($data->{tmpl});
120
    is($query->{sql}, $data->{sql_expected}, "$test : $data->{name} : sql");
121
    is_deeply($query->{key_infos}, $data->{key_infos_expected}, "$test : $data->{name} : key_infos");
122
}
123

            
124

            
125
test 'Original tag processor';
126
$sql_tmpl = DBIx::Custom::SQL::Template->new;
127

            
128
$ret_val = $sql_tmpl->add_tag_processor(
129
    p => sub {
130
        my ($tag_name, $args) = @_;
131
        
132
        my $expand    = "$tag_name ? $args->[0] $args->[1]";
133
        my $key_infos = [2];
134
        return ($expand, $key_infos);
135
    }
136
);
137

            
138
$query = $sql_tmpl->create_query("{p a b}");
139
is($query->{sql}, "p ? a b;", "$test : add_tag_processor sql");
140
is_deeply($query->{key_infos}, [2], "$test : add_tag_processor key_infos");
141
isa_ok($ret_val, 'DBIx::Custom::SQL::Template');
142

            
143

            
144
test "Tag processor error case";
145
$sql_tmpl = DBIx::Custom::SQL::Template->new;
146

            
147

            
148
eval{$sql_tmpl->create_query("{a }")};
149
like($@, qr/Tag '{a }' in SQL template is not exist/, "$test : tag_processor not exist");
150

            
151
$sql_tmpl->add_tag_processor({
152
    q => 'string'
153
});
154

            
155
eval{$sql_tmpl->create_query("{q}", {})};
156
like($@, qr/Tag processor 'q' must be code reference/, "$test : tag_processor not code ref");
157

            
158
$sql_tmpl->add_tag_processor({
159
   r => sub {} 
160
});
161

            
162
eval{$sql_tmpl->create_query("{r}")};
163
like($@, qr/\QTag processor 'r' must return (\E\$expand\Q, \E\$key_infos\Q)/, "$test : tag processor return noting");
164

            
165
$sql_tmpl->add_tag_processor({
166
   s => sub { return ("a", "")} 
167
});
168

            
169
eval{$sql_tmpl->create_query("{s}")};
170
like($@, qr/\QTag processor 's' must return (\E\$expand\Q, \E\$key_infos\Q)/, "$test : tag processor return not array key_infos");
171

            
172
$sql_tmpl->add_tag_processor(
173
    t => sub {return ("a", [])}
174
);
175

            
176
eval{$sql_tmpl->create_query("{t ???}")};
177
like($@, qr/Tag '{t }' arguments cannot contain '?'/, "$test : cannot contain '?' in tag argument");
178

            
179

            
180
test 'General error case';
181
$sql_tmpl = DBIx::Custom::SQL::Template->new;
182
$sql_tmpl->add_tag_processor(
183
    a => sub {
184
        return ("? ? ?", [[],[]]);
185
    }
186
);
187
eval{$sql_tmpl->create_query("{a}")};
188
like($@, qr/Placeholder count in SQL created by tag processor 'a' must be same as key informations count/, "$test placeholder count is invalid");
189

            
190

            
191
test 'Default tag processor Error case';
192
eval{$sql_tmpl->create_query("{= }")};
193
like($@, qr/You must be pass key as argument to tag '{= }'/, "$test : basic '=' : key not exist");
194

            
195
eval{$sql_tmpl->create_query("{in }")};
196
like($@, qr/You must be pass key as first argument of tag '{in }'/, "$test : in : key not exist");
197

            
198
eval{$sql_tmpl->create_query("{in a}")};
199
like($@, qr/\QYou must be pass placeholder count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$placeholder_count\Q}/,
200
     "$test : in : key not exist");
201

            
202
eval{$sql_tmpl->create_query("{in a r}")};
203
like($@, qr/\QYou must be pass placeholder count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$placeholder_count\Q}/,
204
     "$test : in : key not exist");
205

            
206

            
207
test 'Clone';
208
$sql_tmpl = DBIx::Custom::SQL::Template->new;
209
$sql_tmpl
210
  ->tag_start('[')
211
  ->tag_end(']')
212
  ->tag_syntax('syntax')
213
  ->tag_processors({a => 1, b => 2});
214

            
215
$clone = $sql_tmpl->clone;
216
is($clone->tag_start, $sql_tmpl->tag_start, "$test : tag_start");
217
is($clone->tag_end, $sql_tmpl->tag_end, "$test : tag_end");
218
is($clone->tag_syntax, $sql_tmpl->tag_syntax, "$test : tag_syntax");
219

            
220
is_deeply( scalar $clone->tag_processors, scalar $sql_tmpl->tag_processors,
221
          "$test : tag_processors deep clone");
222

            
223
isnt($clone->tag_processors, $sql_tmpl->tag_processors, 
224
     "$test : tag_processors reference not copy");
225

            
226
$sql_tmpl->tag_processors(undef);
227

            
228
$clone = $sql_tmpl->clone;
229
is_deeply(scalar $clone->tag_processors, {}, "$test tag_processor undef copy");
230

            
231

            
232

            
233
__END__
234

            
235

            
236