DBIx-Custom / t / dbix-custom-sql-template.t /
Newer Older
177 lines | 5.269kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use strict;
2
use warnings;
3

            
4
use Test::More 'no_plan';
5

            
add all tests
yuki-kimoto authored on 2010-05-01
6
use DBIx::Custom::SQLTemplate;
packaging one directory
yuki-kimoto authored on 2009-11-16
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 ?;",
add all tests
yuki-kimoto authored on 2010-05-01
27
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
packaging one directory
yuki-kimoto authored on 2009-11-16
28
    },
29
    {
30
        name            => 'placeholder in',
31
        tmpl            => "{in k1 3};",
32
        sql_expected    => "k1 in (?, ?, ?);",
add all tests
yuki-kimoto authored on 2010-05-01
33
        columns_expected   => [qw/k1 k1 k1/]
packaging one directory
yuki-kimoto authored on 2009-11-16
34
    },
35
    
36
    # Table name
37
    {
38
        name            => 'placeholder with table name',
39
        tmpl            => "{= a.k1} {= a.k2}",
40
        sql_expected    => "a.k1 = ? a.k2 = ?;",
add all tests
yuki-kimoto authored on 2010-05-01
41
        columns_expected  => [qw/a.k1 a.k2/]
packaging one directory
yuki-kimoto authored on 2009-11-16
42
    },
43
    {   
44
        name            => 'placeholder in with table name',
45
        tmpl            => "{in a.k1 2} {in b.k2 2}",
46
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
add all tests
yuki-kimoto authored on 2010-05-01
47
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
packaging one directory
yuki-kimoto authored on 2009-11-16
48
    },
49
    {
50
        name            => 'not contain tag',
51
        tmpl            => "aaa",
52
        sql_expected    => "aaa;",
add all tests
yuki-kimoto authored on 2010-05-01
53
        columns_expected  => [],
packaging one directory
yuki-kimoto authored on 2009-11-16
54
    }
55
];
56

            
57
for (my $i = 0; $i < @$datas; $i++) {
58
    my $data = $datas->[$i];
add all tests
yuki-kimoto authored on 2010-05-01
59
    my $sql_tmpl = DBIx::Custom::SQLTemplate->new;
packaging one directory
yuki-kimoto authored on 2009-11-16
60
    my $query = $sql_tmpl->create_query($data->{tmpl});
61
    is($query->{sql}, $data->{sql_expected}, "$test : $data->{name} : sql");
add all tests
yuki-kimoto authored on 2010-05-01
62
    is_deeply($query->{columns}, $data->{columns_expected}, "$test : $data->{name} : columns");
packaging one directory
yuki-kimoto authored on 2009-11-16
63
}
64

            
65

            
66
test 'Original tag processor';
add all tests
yuki-kimoto authored on 2010-05-01
67
$sql_tmpl = DBIx::Custom::SQLTemplate->new;
packaging one directory
yuki-kimoto authored on 2009-11-16
68

            
some changed
yuki-kimoto authored on 2010-05-02
69
$ret_val = $sql_tmpl->register_tag_processor(
packaging one directory
yuki-kimoto authored on 2009-11-16
70
    p => sub {
71
        my ($tag_name, $args) = @_;
72
        
73
        my $expand    = "$tag_name ? $args->[0] $args->[1]";
add all tests
yuki-kimoto authored on 2010-05-01
74
        my $columns = [2];
75
        return ($expand, $columns);
packaging one directory
yuki-kimoto authored on 2009-11-16
76
    }
77
);
78

            
79
$query = $sql_tmpl->create_query("{p a b}");
some changed
yuki-kimoto authored on 2010-05-02
80
is($query->{sql}, "p ? a b;", "$test : register_tag_processor sql");
81
is_deeply($query->{columns}, [2], "$test : register_tag_processor columns");
add all tests
yuki-kimoto authored on 2010-05-01
82
isa_ok($ret_val, 'DBIx::Custom::SQLTemplate');
packaging one directory
yuki-kimoto authored on 2009-11-16
83

            
84

            
85
test "Tag processor error case";
add all tests
yuki-kimoto authored on 2010-05-01
86
$sql_tmpl = DBIx::Custom::SQLTemplate->new;
packaging one directory
yuki-kimoto authored on 2009-11-16
87

            
88

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

            
some changed
yuki-kimoto authored on 2010-05-02
92
$sql_tmpl->register_tag_processor({
packaging one directory
yuki-kimoto authored on 2009-11-16
93
    q => 'string'
94
});
95

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

            
some changed
yuki-kimoto authored on 2010-05-02
99
$sql_tmpl->register_tag_processor({
packaging one directory
yuki-kimoto authored on 2009-11-16
100
   r => sub {} 
101
});
102

            
103
eval{$sql_tmpl->create_query("{r}")};
add all tests
yuki-kimoto authored on 2010-05-01
104
like($@, qr/\QTag processor 'r' must return (\E\$expand\Q, \E\$columns\Q)/, "$test : tag processor return noting");
packaging one directory
yuki-kimoto authored on 2009-11-16
105

            
some changed
yuki-kimoto authored on 2010-05-02
106
$sql_tmpl->register_tag_processor({
packaging one directory
yuki-kimoto authored on 2009-11-16
107
   s => sub { return ("a", "")} 
108
});
109

            
110
eval{$sql_tmpl->create_query("{s}")};
add all tests
yuki-kimoto authored on 2010-05-01
111
like($@, qr/\QTag processor 's' must return (\E\$expand\Q, \E\$columns\Q)/, "$test : tag processor return not array columns");
packaging one directory
yuki-kimoto authored on 2009-11-16
112

            
some changed
yuki-kimoto authored on 2010-05-02
113
$sql_tmpl->register_tag_processor(
packaging one directory
yuki-kimoto authored on 2009-11-16
114
    t => sub {return ("a", [])}
115
);
116

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

            
120

            
121
test 'General error case';
add all tests
yuki-kimoto authored on 2010-05-01
122
$sql_tmpl = DBIx::Custom::SQLTemplate->new;
some changed
yuki-kimoto authored on 2010-05-02
123
$sql_tmpl->register_tag_processor(
packaging one directory
yuki-kimoto authored on 2009-11-16
124
    a => sub {
125
        return ("? ? ?", [[],[]]);
126
    }
127
);
128
eval{$sql_tmpl->create_query("{a}")};
129
like($@, qr/Placeholder count in SQL created by tag processor 'a' must be same as key informations count/, "$test placeholder count is invalid");
130

            
131

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

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

            
139
eval{$sql_tmpl->create_query("{in a}")};
add all tests
yuki-kimoto authored on 2010-05-01
140
like($@, qr/\QYou must be pass value count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$count\Q}/,
packaging one directory
yuki-kimoto authored on 2009-11-16
141
     "$test : in : key not exist");
142

            
143
eval{$sql_tmpl->create_query("{in a r}")};
add all tests
yuki-kimoto authored on 2010-05-01
144
like($@, qr/\QYou must be pass value count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$count\Q}/,
packaging one directory
yuki-kimoto authored on 2009-11-16
145
     "$test : in : key not exist");
146

            
147

            
148
test 'Clone';
add all tests
yuki-kimoto authored on 2010-05-01
149
$sql_tmpl = DBIx::Custom::SQLTemplate->new;
packaging one directory
yuki-kimoto authored on 2009-11-16
150
$sql_tmpl
151
  ->tag_start('[')
152
  ->tag_end(']')
153
  ->tag_syntax('syntax')
154
  ->tag_processors({a => 1, b => 2});
155

            
156
$clone = $sql_tmpl->clone;
157
is($clone->tag_start, $sql_tmpl->tag_start, "$test : tag_start");
158
is($clone->tag_end, $sql_tmpl->tag_end, "$test : tag_end");
159
is($clone->tag_syntax, $sql_tmpl->tag_syntax, "$test : tag_syntax");
160

            
161
is_deeply( scalar $clone->tag_processors, scalar $sql_tmpl->tag_processors,
162
          "$test : tag_processors deep clone");
163

            
164
isnt($clone->tag_processors, $sql_tmpl->tag_processors, 
165
     "$test : tag_processors reference not copy");
166

            
167
$sql_tmpl->tag_processors(undef);
168

            
169
$clone = $sql_tmpl->clone;
170
is_deeply(scalar $clone->tag_processors, {}, "$test tag_processor undef copy");
171

            
172

            
173

            
174
__END__
175

            
176

            
177