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

            
4
use Test::More 'no_plan';
5

            
cleanup
Yuki Kimoto authored on 2011-01-25
6
use DBIx::Custom;
fixed tests
yuki-kimoto authored on 2010-08-06
7

            
8
# Function for test name
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
9
sub test{ print "# $_[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];
cleanup
Yuki Kimoto authored on 2011-01-25
57
    my $builder = DBIx::Custom->new->query_builder;
fixed tests
yuki-kimoto authored on 2010-08-06
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

            
cleanup
Yuki Kimoto authored on 2011-01-25
64
test 'Original tag';
65
$builder = DBIx::Custom->new->query_builder;
fixed tests
yuki-kimoto authored on 2010-08-06
66

            
cleanup
Yuki Kimoto authored on 2011-01-25
67
$ret_val = $builder->register_tag(
fixed tests
yuki-kimoto authored on 2010-08-06
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-25
78
is($query->{sql}, "? a b;", "register_tag sql");
79
is_deeply($query->{columns}, [2], "register_tag columns");
fixed tests
yuki-kimoto authored on 2010-08-06
80
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
81

            
82

            
cleanup
Yuki Kimoto authored on 2011-01-25
83
test "Tag error case";
84
$builder = DBIx::Custom->new->query_builder;
fixed tests
yuki-kimoto authored on 2010-08-06
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 }")};
select, update, and delete w...
Yuki Kimoto authored on 2011-04-25
90
like($@, qr/\QTag "a" is not registered/, "tag not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
91

            
cleanup
Yuki Kimoto authored on 2011-01-25
92
$builder->register_tag({
fixed tests
yuki-kimoto authored on 2010-08-06
93
    q => 'string'
94
});
95

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
99
$builder->register_tag({
fixed tests
yuki-kimoto authored on 2010-08-06
100
   r => sub {} 
101
});
102

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
106
$builder->register_tag({
fixed tests
yuki-kimoto authored on 2010-08-06
107
   s => sub { return ["a", ""]} 
108
});
109

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
113
$builder->register_tag(
fixed tests
yuki-kimoto authored on 2010-08-06
114
    t => sub {return ["a", []]}
115
);
116

            
117

            
118
test 'General error case';
cleanup
Yuki Kimoto authored on 2011-01-25
119
$builder = DBIx::Custom->new->query_builder;
120
$builder->register_tag(
fixed tests
yuki-kimoto authored on 2010-08-06
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

            
cleanup
Yuki Kimoto authored on 2011-01-25
129
test 'Default tag Error case';
fixed tests
yuki-kimoto authored on 2010-08-06
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