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'; |
|
- removed EXPERIMENTAL Prefo...
|
100 |
use DBIx::Connector; |
EXPERIMETAL fork safety impl...
|
101 |
{ |
- removed EXPERIMENTAL Prefo...
|
102 |
my $connector = DBIx::Connector->new( |
103 |
"dbi:mysql:database=$DATABASE", |
|
104 |
$USER, |
|
105 |
$PASSWORD, |
|
106 |
DBIx::Custom->new->default_dbi_option |
|
EXPERIMETAL fork safety impl...
|
107 |
); |
- removed EXPERIMENTAL Prefo...
|
108 |
|
109 |
$dbi = DBIx::Custom->new(connector => $connector); |
|
EXPERIMETAL fork safety impl...
|
110 |
$dbi->delete_all(table => 'table1'); |
111 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
112 |
die "Can't fork" unless defined (my $pid = fork); |
|
113 | ||
114 |
if ($pid) { |
|
115 |
# Parent |
|
116 |
my $result = $dbi->select(table => 'table1'); |
|
117 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
118 |
} |
|
119 |
else { |
|
120 |
# Child |
|
121 |
my $result = $dbi->select(table => 'table1'); |
|
122 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
123 |
} |
|
124 |
} |
|
125 |