added Next version
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 | ||
6 |
use FindBin; |
|
7 |
use DBIx::Custom::Next; |
|
8 | ||
9 |
my $dbi; |
|
10 |
my $dsn; |
|
11 |
my $args; |
|
12 |
my $user = 'dbix_custom'; |
|
13 |
my $password = 'dbix_custom'; |
|
14 |
my $database = 'dbix_custom'; |
|
15 | ||
16 |
$dsn = "dbi:mysql:database=$database"; |
|
17 |
$args = {dsn => $dsn, user => $user, password => $password,}; |
|
18 | ||
19 |
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql2.run" |
|
20 |
&& eval { $dbi = DBIx::Custom::Next->connect($args); 1 }; |
|
21 |
plan 'no_plan'; |
|
22 | ||
23 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
|
24 | ||
25 |
require DBIx::Connector; |
|
26 | ||
27 |
# Function for test name |
|
28 |
sub test { print "# $_[0]\n" } |
|
29 | ||
30 |
# Varialbes for tests |
|
31 |
my $dbname; |
|
32 |
my $row; |
|
33 |
my $rows; |
|
34 |
my $result; |
|
35 |
my $model; |
|
36 | ||
37 |
test 'connect'; |
|
38 |
eval { |
|
39 |
$dbi = DBIx::Custom::Next->connect( |
|
40 |
dsn => "dbi:mysql:database=$database;host=localhost;port=10000", |
|
41 |
user => $user, |
|
42 |
password => $password |
|
43 |
); |
|
44 |
}; |
|
45 |
ok(!$@); |
|
46 | ||
47 |
eval { $dbi->do('drop table table1') }; |
|
48 |
$dbi->do('create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB'); |
|
49 | ||
50 |
test 'update_or_insert'; |
|
51 |
$dbi->delete_all(table => 'table1'); |
|
52 |
$dbi->update_or_insert( |
|
53 |
{key2 => 2}, |
|
54 |
table => 'table1', |
|
55 |
id => 1, |
|
56 |
primary_key => 'key1', |
|
57 |
option => { |
|
58 |
select => {append => 'for update'}, |
|
59 |
insert => {append => ' #'}, |
|
60 |
update => {append => ' #'} |
|
61 |
} |
|
62 |
); |
|
63 | ||
64 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
65 |
is_deeply($row, {key1 => 1, key2 => 2}, "basic"); |
|
66 | ||
67 |
$dbi->update_or_insert( |
|
68 |
{key2 => 3}, |
|
69 |
table => 'table1', |
|
70 |
id => 1, |
|
71 |
primary_key => 'key1', |
|
72 |
option => { |
|
73 |
select => {append => 'for update'}, |
|
74 |
insert => {append => ' #'}, |
|
75 |
update => {append => ' #'} |
|
76 |
} |
|
77 |
); |
|
78 | ||
79 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
80 |
is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
|
81 | ||
82 |
$dbi->delete_all(table => 'table1'); |
|
83 |
$model = $dbi->create_model( |
|
84 |
table => 'table1', |
|
85 |
primary_key => 'key1', |
|
86 |
); |
|
87 |
$model->update_or_insert( |
|
88 |
{key2 => 2}, |
|
89 |
id => 1, |
|
90 |
option => { |
|
91 |
select => {append => 'for update'}, |
|
92 |
insert => {append => ' #'}, |
|
93 |
update => {append => ' #'} |
|
94 |
} |
|
95 |
); |
|
96 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
97 |
is_deeply($row, {key1 => 1, key2 => 2}, "basic"); |
|
98 |
$model->update_or_insert( |
|
99 |
{key2 => 3}, |
|
100 |
id => 1, |
|
101 |
option => { |
|
102 |
select => {append => 'for update'}, |
|
103 |
insert => {append => ' #'}, |
|
104 |
update => {append => ' #'} |
|
105 |
} |
|
106 |
); |
|
107 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
108 |
is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
|
109 | ||
110 |
# Test memory leaks |
|
111 |
for (1 .. 300) { |
|
112 |
$dbi = DBIx::Custom::Next->connect( |
|
113 |
dsn => "dbi:mysql:database=$database;host=localhost;port=10000", |
|
114 |
user => $user, |
|
115 |
password => $password |
|
116 |
); |
|
117 |
$dbi->create_model(table => 'table1'); |
|
118 |
$dbi->create_model(table => 'table2'); |
|
119 |
} |
|
120 | ||
121 |
test 'dbh'; |
|
122 |
{ |
|
123 |
my $connector = DBIx::Connector->new( |
|
124 |
"dbi:mysql:database=$database", |
|
125 |
$user, |
|
126 |
$password, |
|
127 |
DBIx::Custom::Next->new->default_option |
|
128 |
); |
|
129 | ||
130 |
my $dbi = DBIx::Custom::Next->connect(connector => $connector); |
|
131 |
$dbi->delete_all(table => 'table1'); |
|
132 |
$dbi->do('insert into table1 (key1, key2) values (1, 2)'); |
|
133 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
134 |
|
|
135 |
$dbi = DBIx::Custom::Next->new; |
|
136 |
$dbi->dbh('a'); |
|
137 |
is($dbi->{dbh}, 'a'); |
|
138 |
} |
|
139 | ||
140 |
test 'transaction'; |
|
141 |
test 'dbh'; |
|
142 |
{ |
|
143 |
my $connector = DBIx::Connector->new( |
|
144 |
"dbi:mysql:database=$database", |
|
145 |
$user, |
|
146 |
$password, |
|
147 |
DBIx::Custom::Next->new->default_option |
|
148 |
); |
|
149 | ||
150 |
my $dbi = DBIx::Custom::Next->connect(connector => $connector); |
|
151 |
$dbi->delete_all(table => 'table1'); |
|
152 |
|
|
153 |
$dbi->connector->txn(sub { |
|
154 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
155 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
156 |
}); |
|
157 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
158 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
159 | ||
160 |
$dbi->delete_all(table => 'table1'); |
|
161 |
eval { |
|
162 |
$dbi->connector->txn(sub { |
|
163 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
164 |
die "Error"; |
|
165 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
166 |
}); |
|
167 |
}; |
|
168 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
169 |
[]); |
|
170 |
} |
|
171 | ||
172 |
use DBIx::Custom::Next; |
|
173 |
use Scalar::Util 'blessed'; |
|
174 |
{ |
|
175 |
my $dbi = DBIx::Custom::Next->connect( |
|
176 |
user => $user, |
|
177 |
password => $password, |
|
178 |
dsn => "dbi:mysql:dbname=$database" |
|
179 |
); |
|
180 |
$dbi->connect; |
|
181 |
|
|
182 |
ok(blessed $dbi->dbh); |
|
183 |
can_ok($dbi->dbh, qw/prepare/); |
|
184 |
ok($dbi->dbh->{AutoCommit}); |
|
185 |
ok(!$dbi->dbh->{mysql_enable_utf8}); |
|
186 |
} |
|
187 | ||
188 |
{ |
|
189 |
my $dbi = DBIx::Custom::Next->connect( |
|
190 |
user => $user, |
|
191 |
password => $password, |
|
192 |
dsn => "dbi:mysql:dbname=$database", |
|
193 |
option => {AutoCommit => 0, mysql_enable_utf8 => 1} |
|
194 |
); |
|
195 |
$dbi->connect; |
|
196 |
ok(!$dbi->dbh->{AutoCommit}); |
|
197 |
#ok($dbi->dbh->{mysql_enable_utf8}); |
|
198 |
} |
|
199 | ||
200 |
test 'fork'; |
|
201 |
{ |
|
202 |
my $connector = DBIx::Connector->new( |
|
203 |
"dbi:mysql:database=$database", |
|
204 |
$user, |
|
205 |
$password, |
|
206 |
DBIx::Custom::Next->new->default_option |
|
207 |
); |
|
208 |
|
|
209 |
my $dbi = DBIx::Custom::Next->new(connector => $connector); |
|
210 |
$dbi->delete_all(table => 'table1'); |
|
211 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
212 |
die "Can't fork" unless defined (my $pid = fork); |
|
213 | ||
214 |
if ($pid) { |
|
215 |
# Parent |
|
216 |
my $result = $dbi->select(table => 'table1'); |
|
217 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
218 |
} |
|
219 |
else { |
|
220 |
# Child |
|
221 |
my $result = $dbi->select(table => 'table1'); |
|
222 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
223 |
} |
|
224 |
} |
|
225 |