ClickHouse/Test.pm

filter: Statementstmt 48.6% 52 / 107 Branchbran 18.1% 4 / 22 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 65.0% 13 / 20 Podpod 100% 5 / 5 totaltotal 43.3% 75 / 173 CC 20 SCAR 22.6
CC
20
Cov
40%
init_schema
30.0
_database
20.0
END
14.1
CRAP 9.6
« ClickHouse/Introspect/Util.pm ↑ Summary ClickHouse/Introspect/Columns.pm »
1 package DBIO::ClickHouse::Test;
2 # ABSTRACT: ClickHouse-specific test utilities for DBIO
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.900007';
8  
9 23 ▶use DBIO::ClickHouse::Test::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use DBIO::ClickHouse::Deploy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use DBIO::ClickHouse::DDL::Emit qw(create_database drop_database);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use DBIO::Util qw(mask_connect_secrets);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Test databases created by init_schema, dropped in END.
17 23 our @CLEANUP;
18  
19  
20 23 ▶sub import {
Subroutine import: called CC=6 SCAR=18.3
21 23 my $self = shift;
22 23 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 20 23
23 3 my $caller = caller;
24 3 for my $exp (@_) {
25 3 ▶ if ($exp eq ':DiffSQL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp eq ':DiffSQL') { } 3 0 3
elsif ($exp eq ':GlobalLock') { } 0 0 0
26 3 require DBIO::SQLMaker;
27 3 require SQL::Abstract::Test;
28 3 for (qw(is_same_sql_bind is_same_sql is_same_bind)) {
29 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 *{"${caller}::$_"} = \&{"SQL::Abstract::Test::$_"};
31 }
32 }
33 elsif ($exp eq ':GlobalLock') {
34 # no-op, parallel to DBIO::SQLite::Test / DBIO::DuckDB::Test
35 }
36 else {
37 0 croak "Unknown export $exp requested from $self";
38 }
39 }
40 }
41 }
42  
43 # The keys init_schema understands itself. They must not leak into the DBI
44 # connect attributes: DBD::MariaDB rejects an attribute name it does not know,
45 # so a stray no_populate => 1 would fail the connection rather than the deploy.
46 23 my %CONTROL_ARG = map { $_ => 1 } qw(
47 no_deploy no_populate schema_class
48 );
49  
50  
51 0 ▶sub _database {
Subroutine _database: not called CC=4 SCAR=30.0
52 0 my $self = shift;
53 0 my %args = @_;
54  
55 0 delete @args{ grep { $CONTROL_ARG{$_} } keys %args };
56  
57 my $dsn = $ENV{DBIO_TEST_CLICKHOUSE_DSN}
58 0 ▶ or croak
Branch: 1 branch
BranchTrueFalseTotal
my $dsn = $ENV{"DBIO_TEST_CLICKHOUSE_DSN"} or croak("DBIO::ClickHouse::Test requires DBIO_TEST_CLICKHOUSE_DSN: ClickHouse has no embedded/in-memory mode, so a running ClickHouse server (MySQL wire protocol, port 9004) is needed. Set DBIO_TEST_CLICKHOUSE_DSN (plus optional _DBUSER / _DBPASS), e.g. dbi:MariaDB:database=default;host=127.0.0.1;port=9004") 0 0 0
59 'DBIO::ClickHouse::Test requires DBIO_TEST_CLICKHOUSE_DSN: '
60 . 'ClickHouse has no embedded/in-memory mode, so a running ClickHouse '
61 . 'server (MySQL wire protocol, port 9004) is needed. Set '
62 . 'DBIO_TEST_CLICKHOUSE_DSN (plus optional _DBUSER / _DBPASS), e.g. '
63 . q{dbi:MariaDB:database=default;host=127.0.0.1;port=9004};
64  
65 0 my %opts = ( AutoCommit => 1, RaiseError => 1, PrintError => 0, %args );
66  
67 # DBD::mysql hands back character data as bytes unless the connection asks it
68 # to decode (DBD::MariaDB decodes unconditionally), and the fixture and its
69 # assertions assume decoded Unicode -- the same contract both transports are
70 # documented with. mysql_enable_utf8mb4 is a mysql-only attribute; it must
71 # not leak into a MariaDB connection, which rejects attributes it does not
72 # know. See docs/adr/0001-mysql-wire-protocol-transport.md.
73 0 ▶ $opts{mysql_enable_utf8mb4} = 1 if $dsn =~ /^dbi:mysql:/i;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ /^dbi:mysql:/i 0 0 0
74  
75 return (
76 $dsn,
77 0 ▶ (map { $ENV{"DBIO_TEST_CLICKHOUSE_${_}"} // '' } qw/DBUSER DBPASS/),
Condition0%0/2
$ENV{"DBIO_TEST_CLICKHOUSE_$_"} // ""
l!l
00
MC/DC0%0/1
$ENV{"DBIO_TEST_CLICKHOUSE_$_"} // ""
$ENV{"DBIO_TEST_CLICKHOUSE_$_"}
Truth table0%0/2
A
A= $ENV{"DBIO_TEST_CLICKHOUSE_$_"}
Aresult
11
00
78 \%opts,
79 );
80 }
81  
82  
83 0 ▶sub test_database { 'dbio_ch_test_' . $$ }
Subroutine test_database: not called CC=1 SCAR=6.9
Pod documented
84  
85  
86 0 ▶sub test_connect_info {
Subroutine test_connect_info: not called CC=1 SCAR=6.9
Pod documented
87 0 my $self = shift;
88 0 my ($dsn, $user, $pass, $opts) = $self->_database(@_);
89 0 return ($self->_dsn_for_database($dsn, $self->test_database), $user, $pass, $opts);
90 }
91  
92  
93 0 ▶sub init_schema {
Subroutine init_schema: not called CC=5 SCAR=34.0
Pod documented
94 0 my $self = shift;
95 0 my %args = @_;
96  
97 0 my ($admin_dsn, $user, $pass) = $self->_database(%args);
98 0 my ($dsn, undef, undef, $opts) = $self->test_connect_info(%args);
99  
100 0 my $database = $self->test_database;
101 0 my $admin = $self->_admin_dbh($admin_dsn, $user, $pass);
102  
103 # Drop first: a same-PID leftover from an aborted run would otherwise turn
104 # every CREATE TABLE below into "table already exists".
105 0 $admin->do(drop_database(name => $database, if_exists => 1));
106 0 $admin->do(create_database(name => $database));
107  
108 # Registered on the statement after the CREATE, not after the connect: from
109 # here on there is a database that has to be dropped no matter what fails.
110 0 my $cleanup = _register_cleanup($admin, $database);
111  
112 0 ▶ my $schema_class = $args{schema_class} // 'DBIO::ClickHouse::Test::Schema';
Condition0%0/2
$args{"schema_class"} // "DBIO::ClickHouse::Test::Schema"
l!l
00
MC/DC0%0/1
$args{"schema_class"} // "DBIO::ClickHouse::Test::Schema"
$args{"schema_class"}
Truth table0%0/2
A
A= $args{"schema_class"}
Aresult
11
00
113 0 my $schema = $schema_class->clone;
114 0 $schema->storage_type('+DBIO::ClickHouse::Storage');
115 0 $schema = $schema->connect($dsn, $user, $pass, $opts);
116  
117 # So the END block can close the connection before dropping what it points at.
118 0 $cleanup->{schema} = $schema;
119  
120 0 ▶ unless ($args{no_deploy}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_deploy"}) 0 0 0
121 0 DBIO::ClickHouse::Deploy->new(schema => $schema)->install;
122 $self->populate_schema($schema)
123 0 ▶ unless $args{no_populate} || $args{schema_class};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_populate"} or $args{"schema_class"} 0 0 0
Condition0%0/3
$args{"no_populate"} or $args{"schema_class"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"no_populate"} or $args{"schema_class"}
$args{"no_populate"} $args{"schema_class"}
Truth table0%0/3
A or B
A= $args{"no_populate"}B= $args{"schema_class"}
ABresult
1X1
011
000
124 }
125  
126 0 return $schema;
127 }
128  
129  
130 0 ▶sub populate_schema {
Subroutine populate_schema: not called CC=1 SCAR=6.9
Pod documented
131 0 my ($self, $schema) = @_;
132  
133 0 $schema->resultset('Snapshot')->populate([
134 { id => 1, label => 'alpha', version => 1 },
135 { id => 2, label => 'beta', version => 1 },
136 { id => 3, label => 'gamma', version => 1 }
137 ]);
138  
139 0 $schema->resultset('Hit')->populate([
140 { url => '/a', hits => 1, snapshot => 1, ts => '2024-01-01 00:00:00.000' },
141 { url => '/b', hits => 10, snapshot => 1, ts => '2024-01-02 00:00:00.000' },
142 { url => '/c', hits => 100, snapshot => 2, ts => '2024-02-01 00:00:00.000' },
143 { url => '/d', hits => 150, snapshot => 2, ts => '2024-02-02 00:00:00.000' },
144 { url => '/e', hits => 200, snapshot => 3, ts => '2024-03-01 00:00:00.000' },
145 { url => '/f', hits => 250, snapshot => 3, ts => undef }
146 ]);
147  
148 0 $schema->resultset('Reading')->populate([
149 { sensor => 'a', ts => '2024-01-01 06:00:00.000', day => '2024-01-01', value => 1.5 },
150 { sensor => 'a', ts => '2024-02-01 06:00:00.000', day => '2024-02-01', value => 2.5 },
151 { sensor => 'b', ts => '2024-01-01 06:00:00.000', day => '2024-01-01', value => 3.5 },
152 { sensor => 'b', ts => '2024-02-01 06:00:00.000', day => '2024-02-01', value => 4.5 }
153 ]);
154  
155 0 $schema->resultset('Unsorted')->populate([
156 { foo => 1, bar => 'one' },
157 { foo => 2, bar => 'two' }
158 ]);
159  
160 0 $schema->resultset('Probe')->populate([ $self->probe_row ]);
161 }
162  
163  
164 1 ▶sub probe_row {
Subroutine probe_row: called CC=1 SCAR=0.0
Pod documented
165 return {
166 1 id => 1,
167 i8 => -128,
168 i16 => -32768,
169 i64 => '-9223372036854775808',
170 u32 => 4294967295,
171 u64 => '18446744073709551615',
172  
173 f32 => 1.5,
174 f64 => 2.25,
175 dec => '12345678.90',
176 amount => '999999999999999.1234',
177  
178 s_var => 'varchar value',
179 s_text => 'text value',
180 s_blob => 'blob value',
181 s_json => '{"a":1}',
182  
183 s_fixed => 'ab',
184 s_low => 'low',
185 s_low_null => undef,
186  
187 flag => 1,
188 guid => '11111111-2222-4333-8444-555555555555',
189 ip => '10.0.0.1',
190  
191 d_wide => '1900-01-01',
192 d_narrow => '2024-05-06',
193 ts_ms => '2024-05-06 07:08:09.123',
194 ts_sec => '2024-05-06 07:08:09',
195 ts_utc => '2024-05-06 07:08:09.456',
196 t_str => '07:08:09',
197  
198 state => q{it's},
199  
200 tags => \q{['x', 'y']},
201 nums => \q{[1, 2, 3]},
202  
203 note => undef,
204 title => 'given',
205 counter => 7
206 };
207 }
208  
209 # --- cleanup -----------------------------------------------------------------
210  
211 # The admin handle is a SECOND connection, to the database named in the DSN,
212 # and it is kept alive on purpose: the END block needs a handle that is not
213 # pointed at the database it is about to drop, and reconnecting at END time
214 # (during interpreter shutdown, possibly after a die) is exactly where a
215 # cleanup silently fails to happen.
216 1 ▶sub _admin_dbh {
Subroutine _admin_dbh: called CC=4 SCAR=16.6
217 1 my ($self, $dsn, $user, $pass) = @_;
218 # DBI's own connect error carries the DSN (which may hold a password=) and
219 # the driver's text may repeat the password: nothing leaves unmasked (k39).
220 1 my $dbh = eval {
221 1 DBI->connect($dsn, $user, $pass,
222 { RaiseError => 1, PrintError => 0, AutoCommit => 1 });
223 };
224 1 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 1 0 1
225 1 ▶ my $error = $@ || "Cannot connect to $dsn: " . ( $DBI::errstr // '' );
Condition20%1/5
$@ || "Cannot connect to ${dsn}: " . ($DBI::errstr // '')
l!l&&r!l&&!r
100
$DBI::errstr // ""
l!l
00
MC/DC0%0/2
$@ || "Cannot connect to ${dsn}: " . ($DBI::errstr // '')
$@ "Cannot connect to ${dsn}: " . ($DBI::errstr // '')
MC/DC0%0/1
$DBI::errstr // ""
$DBI::errstr
Truth table33%1/3
A || B
A= $@B= "Cannot connect to ${dsn}: " . ($DBI::errstr // '')
ABresult
1X1
011
000
Truth table0%0/2
A
A= $DBI::errstr
Aresult
11
00
226 1 croak mask_connect_secrets("$error", [ $dsn, $user, $pass ]);
227 }
228 0 return $dbh;
229 }
230  
231 0 ▶sub _dsn_for_database {
Subroutine _dsn_for_database: not called CC=2 SCAR=17.9
232 0 my ($self, $dsn, $database) = @_;
233  
234 0 ▶ return "$dsn;database=$database" unless $dsn =~ /(?:database|dbname)=/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn =~ /(?:database|dbname)=/i 0 0 0
235  
236 0 $dsn =~ s/(database|dbname)=[^;]*/$1=$database/i;
237 0 return $dsn;
238 }
239  
240 0 ▶sub _register_cleanup {
Subroutine _register_cleanup: not called CC=1 SCAR=6.9
241 0 my ($dbh, $database) = @_;
242 0 my $job = {
243 dbh => $dbh,
244 sql => drop_database(name => $database, if_exists => 1)
245 };
246 0 push @CLEANUP, $job;
247 0 return $job;
248 }
249  
250 # Loud on failure, per the family rule: a cleanup that quietly did not happen
251 # leaves a database on a shared server and makes the NEXT run's failure look
252 # like a code bug.
253 23 ▶END {
Subroutine END: called CC=4 SCAR=26.4
254 23 for my $job (reverse @CLEANUP) {
255 0 ▶ if (my $schema = $job->{schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $job->{"schema"}) 0 0 0
256 0 eval { $schema->storage->disconnect; 1 };
257 }
258 0 ▶ next if eval { $job->{dbh}->do($job->{sql}); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $job->{"dbh"}->do($job->{"sql"}); 1 } } 0 0 0
259 0 warn "DBIO::ClickHouse::Test cleanup failed: $job->{sql}: $@";
260 }
261 23 @CLEANUP = ();
262 }
263  
264  
265 23 1;
266  
267 __END__
« ClickHouse/Introspect/Util.pm ↑ Summary ClickHouse/Introspect/Columns.pm »