DBIx-Custom / t / dbix-custom-querybuilder.t /
Newer Older
175 lines | 4.856kb
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

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
11
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
12

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

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

            
57
for (my $i = 0; $i < @$datas; $i++) {
58
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-01-25
59
    my $builder = DBIx::Custom->new->query_builder;
cleanup
Yuki Kimoto authored on 2011-07-29
60
    $builder->safety_character('\w');
fixed tests
yuki-kimoto authored on 2010-08-06
61
    my $query = $builder->build_query($data->{source});
cleanup
Yuki Kimoto authored on 2011-01-23
62
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
63
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
fixed tests
yuki-kimoto authored on 2010-08-06
64
}
65

            
66

            
cleanup
Yuki Kimoto authored on 2011-01-25
67
test 'Original tag';
68
$builder = DBIx::Custom->new->query_builder;
cleanup
Yuki Kimoto authored on 2011-07-29
69
$builder->safety_character('\w');
fixed tests
yuki-kimoto authored on 2010-08-06
70

            
cleanup
Yuki Kimoto authored on 2011-01-25
71
$ret_val = $builder->register_tag(
fixed tests
yuki-kimoto authored on 2010-08-06
72
    p => sub {
73
        my @args = @_;
74
        
75
        my $expand    = "? $args[0] $args[1]";
76
        my $columns = [2];
77
        return [$expand, $columns];
78
    }
79
);
80

            
81
$query = $builder->build_query("{p a b}");
cleanup
Yuki Kimoto authored on 2011-01-25
82
is($query->{sql}, "? a b;", "register_tag sql");
83
is_deeply($query->{columns}, [2], "register_tag columns");
fixed tests
yuki-kimoto authored on 2010-08-06
84
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
85

            
86

            
cleanup
Yuki Kimoto authored on 2011-01-25
87
test "Tag error case";
88
$builder = DBIx::Custom->new->query_builder;
cleanup
Yuki Kimoto authored on 2011-07-29
89
$builder->safety_character('\w');
fixed tests
yuki-kimoto authored on 2010-08-06
90

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

            
94
eval{$builder->build_query("{a }")};
select, update, and delete w...
Yuki Kimoto authored on 2011-04-25
95
like($@, qr/\QTag "a" is not registered/, "tag not exist");
fixed tests
yuki-kimoto authored on 2010-08-06
96

            
cleanup
Yuki Kimoto authored on 2011-01-25
97
$builder->register_tag({
fixed tests
yuki-kimoto authored on 2010-08-06
98
    q => 'string'
99
});
100

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
104
$builder->register_tag({
fixed tests
yuki-kimoto authored on 2010-08-06
105
   r => sub {} 
106
});
107

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

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

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

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

            
122

            
123
test 'General error case';
cleanup
Yuki Kimoto authored on 2011-01-25
124
$builder = DBIx::Custom->new->query_builder;
cleanup
Yuki Kimoto authored on 2011-07-29
125
$builder->safety_character('\w');
cleanup
Yuki Kimoto authored on 2011-01-25
126
$builder->register_tag(
fixed tests
yuki-kimoto authored on 2010-08-06
127
    a => sub {
128
        return ["? ? ?", ['']];
129
    }
130
);
131
eval{$builder->build_query("{a}")};
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
132
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
fixed tests
yuki-kimoto authored on 2010-08-06
133

            
134

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

            
139
eval{$builder->build_query("{in }")};
cleanup
Yuki Kimoto authored on 2011-01-23
140
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
141

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

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

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

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

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

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

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

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

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
175
=cut