add examples
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
# user password database |
|
6 |
our ($USER, $PASSWORD, $DATABASE) = connect_info(); |
|
7 | ||
8 |
plan skip_all => 'private MySQL test' unless $USER; |
|
9 | ||
10 |
plan 'no_plan'; |
|
11 | ||
12 |
# Function for test name |
|
cleanup
|
13 |
sub test { print "# $_[0]\n" } |
add examples
|
14 | |
15 |
# Functions for tests |
|
16 |
sub connect_info { |
|
17 |
my $file = 'password.tmp'; |
|
18 |
open my $fh, '<', $file |
|
19 |
or return; |
|
20 |
|
|
21 |
my ($user, $password, $database) = split(/\s/, (<$fh>)[0]); |
|
22 |
|
|
23 |
close $fh; |
|
24 |
|
|
25 |
return ($user, $password, $database); |
|
26 |
} |
|
27 | ||
28 | ||
29 |
# Varialbes for tests |
|
30 |
my $dbi; |
|
31 |
my $dbname; |
|
32 |
my $rows; |
|
EXPERIMETAL fork safety impl...
|
33 |
my $result; |
add examples
|
34 | |
35 |
# Constant varialbes for test |
|
36 |
my $CREATE_TABLE = { |
|
37 |
0 => 'create table table1 (key1 char(255), key2 char(255));' |
|
38 |
}; |
|
39 | ||
40 | ||
41 |
use DBIx::Custom::MySQL; |
|
42 | ||
43 |
test 'connect'; |
|
44 |
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD, |
|
45 |
database => $DATABASE, host => 'localhost', port => '10000'); |
|
46 |
$dbi->connect; |
|
cleanup
|
47 |
like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source"); |
48 |
is(ref $dbi->dbh, 'DBI::db'); |
|
add examples
|
49 | |
50 |
test 'attributes'; |
|
51 |
$dbi = DBIx::Custom::MySQL->new; |
|
52 |
$dbi->host('a'); |
|
cleanup
|
53 |
is($dbi->host, 'a', "host"); |
add examples
|
54 |
$dbi->port('b'); |
cleanup
|
55 |
is($dbi->port, 'b', "port"); |
add examples
|
56 | |
57 |
test 'limit'; |
|
58 |
$dbi = DBIx::Custom->connect( |
|
59 |
data_source => "dbi:mysql:database=$DATABASE", |
|
60 |
user => $USER, |
|
61 |
password => $PASSWORD |
|
62 |
); |
|
EXPERIMETAL fork safety impl...
|
63 |
$dbi->delete_all(table => 'table1'); |
add examples
|
64 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
65 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
66 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
67 |
$dbi->query_builder->register_tag_processor( |
|
68 |
limit => sub { |
|
69 |
my ($count, $offset) = @_; |
|
70 |
|
|
71 |
my $s = ''; |
|
72 |
$offset = 0 unless defined $offset; |
|
73 |
$s .= "limit $offset"; |
|
74 |
$s .= ", $count"; |
|
75 |
|
|
76 |
return [$s, []]; |
|
77 |
} |
|
78 |
); |
|
79 |
$rows = $dbi->select( |
|
80 |
table => 'table1', |
|
81 |
where => {key1 => 1}, |
|
82 |
append => "order by key2 {limit 1 0}" |
|
83 |
)->fetch_hash_all; |
|
cleanup
|
84 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
85 |
$rows = $dbi->select( |
86 |
table => 'table1', |
|
87 |
where => {key1 => 1}, |
|
88 |
append => "order by key2 {limit 2 1}" |
|
89 |
)->fetch_hash_all; |
|
cleanup
|
90 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
91 |
$rows = $dbi->select( |
92 |
table => 'table1', |
|
93 |
where => {key1 => 1}, |
|
94 |
append => "order by key2 {limit 1}" |
|
95 |
)->fetch_hash_all; |
|
cleanup
|
96 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
97 |
$dbi->delete_all(table => 'table1'); |
EXPERIMETAL fork safety impl...
|
98 | |
99 |
test 'fork'; |
|
100 |
{ |
|
101 |
$dbi = DBIx::Custom->connect( |
|
102 |
data_source => "dbi:mysql:database=$DATABASE", |
|
103 |
user => $USER, |
|
104 |
password => $PASSWORD |
|
105 |
); |
|
106 |
$dbi->delete_all(table => 'table1'); |
|
107 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
108 |
die "Can't fork" unless defined (my $pid = fork); |
|
109 | ||
110 |
if ($pid) { |
|
111 |
# Parent |
|
112 |
my $result = $dbi->select(table => 'table1'); |
|
113 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
114 |
} |
|
115 |
else { |
|
116 |
# Child |
|
117 |
my $result = $dbi->select(table => 'table1'); |
|
118 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
119 |
} |
|
120 |
} |
|
121 | ||
122 |
test 'fork in transaction'; |
|
123 |
{ |
|
124 |
$dbi = DBIx::Custom->connect( |
|
125 |
data_source => "dbi:mysql:database=$DATABASE", |
|
126 |
user => $USER, |
|
127 |
password => $PASSWORD |
|
128 |
); |
|
129 |
|
|
130 |
$dbi->begin_work; |
|
131 |
die "Can't fork" unless defined (my $pid = fork); |
|
132 |
|
|
133 |
if ($pid) { |
|
134 |
# Parent |
|
135 |
} |
|
136 |
else { |
|
137 |
# Child |
|
138 |
eval {$dbi->select(table => 'table1') }; |
|
139 |
die "Not OK" unless $@ =~ /transaction/; |
|
140 |
} |
|
141 |
} |