add examples
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
- update_param_tag is DEPREC...
|
5 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
6 | ||
add examples
|
7 |
# user password database |
8 |
our ($USER, $PASSWORD, $DATABASE) = connect_info(); |
|
9 | ||
10 |
plan skip_all => 'private MySQL test' unless $USER; |
|
11 | ||
12 |
plan 'no_plan'; |
|
13 | ||
- removed DEPRECATED DBIx::C...
|
14 |
require DBIx::Connector; |
15 | ||
add examples
|
16 |
# Function for test name |
cleanup
|
17 |
sub test { print "# $_[0]\n" } |
add examples
|
18 | |
19 |
# Functions for tests |
|
20 |
sub connect_info { |
|
21 |
my $file = 'password.tmp'; |
|
22 |
open my $fh, '<', $file |
|
23 |
or return; |
|
24 |
|
|
25 |
my ($user, $password, $database) = split(/\s/, (<$fh>)[0]); |
|
26 |
|
|
27 |
close $fh; |
|
28 |
|
|
29 |
return ($user, $password, $database); |
|
30 |
} |
|
31 | ||
32 | ||
33 |
# Varialbes for tests |
|
34 |
my $dbi; |
|
35 |
my $dbname; |
|
36 |
my $rows; |
|
EXPERIMETAL fork safety impl...
|
37 |
my $result; |
add examples
|
38 | |
fixed test
|
39 |
{ |
40 |
package DBIx::Custom::MySQL; |
|
41 | ||
42 |
use strict; |
|
43 |
use warnings; |
|
44 | ||
45 |
use base 'DBIx::Custom'; |
|
46 | ||
47 |
__PACKAGE__->attr([qw/database host port/]); |
|
48 | ||
49 |
sub connect { |
|
50 |
my $proto = shift; |
|
51 |
|
|
52 |
# Create a new object |
|
53 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
54 |
|
|
55 |
# Data source |
|
data_source is DEPRECATED! I...
|
56 |
if (!$self->dsn) { |
fixed test
|
57 |
my $database = $self->database; |
58 |
my $host = $self->host; |
|
59 |
my $port = $self->port; |
|
data_source is DEPRECATED! I...
|
60 |
my $dsn = "dbi:mysql:"; |
61 |
$dsn .= "database=$database;" if $database; |
|
62 |
$dsn .= "host=$host;" if $host; |
|
63 |
$dsn .= "port=$port;" if $port; |
|
64 |
$self->dsn($dsn); |
|
fixed test
|
65 |
} |
66 |
|
|
67 |
return $self->SUPER::connect; |
|
68 |
} |
|
69 | ||
70 |
1; |
|
71 |
} |
|
add examples
|
72 | |
73 |
test 'connect'; |
|
74 |
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD, |
|
75 |
database => $DATABASE, host => 'localhost', port => '10000'); |
|
76 |
$dbi->connect; |
|
data_source is DEPRECATED! I...
|
77 |
like($dbi->dsn, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source"); |
cleanup
|
78 |
is(ref $dbi->dbh, 'DBI::db'); |
add examples
|
79 | |
80 |
test 'attributes'; |
|
81 |
$dbi = DBIx::Custom::MySQL->new; |
|
82 |
$dbi->host('a'); |
|
cleanup
|
83 |
is($dbi->host, 'a', "host"); |
add examples
|
84 |
$dbi->port('b'); |
cleanup
|
85 |
is($dbi->port, 'b', "port"); |
add examples
|
86 | |
87 |
test 'limit'; |
|
88 |
$dbi = DBIx::Custom->connect( |
|
data_source is DEPRECATED! I...
|
89 |
dsn => "dbi:mysql:database=$DATABASE", |
add examples
|
90 |
user => $USER, |
91 |
password => $PASSWORD |
|
92 |
); |
|
EXPERIMETAL fork safety impl...
|
93 |
$dbi->delete_all(table => 'table1'); |
add examples
|
94 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
95 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
96 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
97 |
$dbi->query_builder->register_tag_processor( |
|
98 |
limit => sub { |
|
99 |
my ($count, $offset) = @_; |
|
100 |
|
|
101 |
my $s = ''; |
|
102 |
$offset = 0 unless defined $offset; |
|
103 |
$s .= "limit $offset"; |
|
104 |
$s .= ", $count"; |
|
105 |
|
|
106 |
return [$s, []]; |
|
107 |
} |
|
108 |
); |
|
109 |
$rows = $dbi->select( |
|
110 |
table => 'table1', |
|
111 |
where => {key1 => 1}, |
|
112 |
append => "order by key2 {limit 1 0}" |
|
113 |
)->fetch_hash_all; |
|
cleanup
|
114 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
115 |
$rows = $dbi->select( |
116 |
table => 'table1', |
|
117 |
where => {key1 => 1}, |
|
118 |
append => "order by key2 {limit 2 1}" |
|
119 |
)->fetch_hash_all; |
|
cleanup
|
120 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
121 |
$rows = $dbi->select( |
122 |
table => 'table1', |
|
123 |
where => {key1 => 1}, |
|
124 |
append => "order by key2 {limit 1}" |
|
125 |
)->fetch_hash_all; |
|
cleanup
|
126 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
127 |
$dbi->delete_all(table => 'table1'); |
EXPERIMETAL fork safety impl...
|
128 | |
improved type_rule method
|
129 | |
130 |
test 'type_rule'; |
|
131 |
$dbi = DBIx::Custom->connect( |
|
132 |
dsn => "dbi:mysql:database=$DATABASE", |
|
133 |
user => $USER, |
|
134 |
password => $PASSWORD |
|
135 |
); |
|
136 |
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")}; |
|
fixed bug that type_rule fro...
|
137 |
$dbi->each_column( |
138 |
sub { |
|
139 |
my ($self, $table, $column, $column_info) = @_; |
|
140 |
} |
|
141 |
); |
|
142 | ||
improved type_rule method
|
143 |
$dbi->type_rule( |
EXPERIMENTAL type_rule argum...
|
144 |
into1 => { |
added EXPERIMENTAL DBIx::Cus...
|
145 |
date=> sub { |
improved type_rule method
|
146 |
my $date = shift; |
147 |
$date =~ s/aaaaa//g; |
|
148 |
return $date; |
|
149 |
}, |
|
added EXPERIMENTAL DBIx::Cus...
|
150 |
datetime => sub { |
improved type_rule method
|
151 |
my $date = shift; |
152 |
$date =~ s/ccccc//g; |
|
153 |
return $date; |
|
154 |
}, |
|
155 |
}, |
|
EXPERIMENTAL type_rule argum...
|
156 |
from1 => { |
changed type_rule arguments ...
|
157 |
# DATE |
158 |
9 => sub { |
|
159 |
my $date = shift; |
|
160 |
$date .= 'bbbbb'; |
|
161 |
return $date; |
|
162 |
}, |
|
163 |
# DATETIME or TIMPESTANM |
|
164 |
11 => sub { |
|
165 |
my $date = shift; |
|
166 |
$date .= 'ddddd'; |
|
167 |
return $date; |
|
improved type_rule method
|
168 |
} |
169 |
} |
|
170 |
); |
|
171 | ||
172 |
$dbi->insert( |
|
173 |
{ |
|
174 |
date => 'aaaaa2010-aaaaa11-12aaaaa', |
|
175 |
datetime => '2010-11ccccc-12 10:ccccc55:56' |
|
176 |
}, |
|
177 |
table => 'date_test' |
|
178 |
); |
|
179 |
is_deeply( |
|
180 |
$dbi->select(table => 'date_test')->fetch, |
|
181 |
['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd'] |
|
182 |
); |
|
183 | ||
184 |
$dbi->execute("drop table date_test"); |
|
185 | ||
fixed dbh() method bug:wq
|
186 |
test 'dbh'; |
187 |
{ |
|
188 |
my $connector = DBIx::Connector->new( |
|
189 |
"dbi:mysql:database=$DATABASE", |
|
190 |
$USER, |
|
191 |
$PASSWORD, |
|
192 |
DBIx::Custom->new->default_dbi_option |
|
193 |
); |
|
194 | ||
195 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
|
196 |
$dbi->delete_all(table => 'table1'); |
|
197 |
$dbi->do('insert into table1 (key1, key2) values (1, 2)'); |
|
198 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
199 |
|
|
200 |
$dbi = DBIx::Custom->new; |
|
201 |
$dbi->dbh('a'); |
|
202 |
is($dbi->{dbh}, 'a'); |
|
203 |
} |
|
204 | ||
205 |
test 'transaction'; |
|
206 |
test 'dbh'; |
|
207 |
{ |
|
208 |
my $connector = DBIx::Connector->new( |
|
209 |
"dbi:mysql:database=$DATABASE", |
|
210 |
$USER, |
|
211 |
$PASSWORD, |
|
212 |
DBIx::Custom->new->default_dbi_option |
|
213 |
); |
|
214 | ||
215 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
|
216 |
$dbi->delete_all(table => 'table1'); |
|
217 |
|
|
218 |
$dbi->connector->txn(sub { |
|
219 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
220 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
221 |
}); |
|
222 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
223 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
224 | ||
225 |
$dbi->delete_all(table => 'table1'); |
|
226 |
eval { |
|
227 |
$dbi->connector->txn(sub { |
|
228 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
229 |
die "Error"; |
|
230 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
231 |
}); |
|
232 |
}; |
|
233 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
234 |
[]); |
|
235 |
} |
|
236 | ||
EXPERIMETAL fork safety impl...
|
237 |
test 'fork'; |
238 |
{ |
|
- removed EXPERIMENTAL Prefo...
|
239 |
my $connector = DBIx::Connector->new( |
240 |
"dbi:mysql:database=$DATABASE", |
|
241 |
$USER, |
|
242 |
$PASSWORD, |
|
243 |
DBIx::Custom->new->default_dbi_option |
|
EXPERIMETAL fork safety impl...
|
244 |
); |
- removed EXPERIMENTAL Prefo...
|
245 |
|
fixed dbh() method bug:wq
|
246 |
my $dbi = DBIx::Custom->new(connector => $connector); |
EXPERIMETAL fork safety impl...
|
247 |
$dbi->delete_all(table => 'table1'); |
248 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
249 |
die "Can't fork" unless defined (my $pid = fork); |
|
250 | ||
251 |
if ($pid) { |
|
252 |
# Parent |
|
253 |
my $result = $dbi->select(table => 'table1'); |
|
254 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
255 |
} |
|
256 |
else { |
|
257 |
# Child |
|
258 |
my $result = $dbi->select(table => 'table1'); |
|
259 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
260 |
} |
|
261 |
} |
|
262 |