DBIx-Custom / t / dbix-custom-querybuilder.t /
Newer Older
171 lines | 4.716kb
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;
fixed tests
yuki-kimoto authored on 2010-08-06
60
    my $query = $builder->build_query($data->{source});
cleanup
Yuki Kimoto authored on 2011-01-23
61
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
62
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
fixed tests
yuki-kimoto authored on 2010-08-06
63
}
64

            
65

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

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

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

            
84

            
cleanup
Yuki Kimoto authored on 2011-01-25
85
test "Tag error case";
86
$builder = DBIx::Custom->new->query_builder;
fixed tests
yuki-kimoto authored on 2010-08-06
87

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

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

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

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

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

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

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

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

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

            
119

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

            
130

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

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

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

            
142
eval{$builder->build_query("{in a r}")};
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

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

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

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

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

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

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

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