Chronicle/Mock.pm

filter: Statementstmt 72.4% 42 / 58 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.4% 13 / 17 Podpod 100% 1 / 1 totaltotal 71.7% 56 / 78 CC 2 SCAR 5.3
CC
2
Cov
70%
testdb
6.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.7
« Chronicle/Writer.pm ↑ Summary Chronicle/Subscriber.pm »
1 package Data::Chronicle::Mock;
2  
3 =head1 NAME
4  
5 Data::Chronicle::Mock - Mocking utility to test chronicle based scenarios
6  
7 =cut
8  
9 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use DBIx::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Test::PostgreSQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Test::Mock::Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Data::Chronicle::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Data::Chronicle::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '0.21'; ## VERSION
20  
21 {
22 # We need to store the Test::PostgreSQL handle somewhere to prevent
23 # premature destruction of the database.
24 package Data::Chronicle::Mock::Connector; ## no critic
25 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶ use parent qw/DBIx::Connector/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶ sub testdb {
Subroutine testdb: not called CC=2 SCAR=17.9
30 0 my $self = shift;
31 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
32 0 $self->{' p g s q l '} = shift;
33 }
34 0 return $self->{' p g s q l '};
35 }
36 }
37  
38 # This is to resolve a compatibility issue between Test::Mock::Redis (where mget returns an array)
39 # and RedisDB (where mget returns an arrayref).
40 2 ▶use Test::MockModule;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 my $mocked_mock_redis = Test::MockModule->new('Test::Mock::Redis');
42 0 ▶$mocked_mock_redis->mock('mget', sub { return [$mocked_mock_redis->original('mget')->(@_)] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 =head3 C<< my $ch = get_mocked_chronicle(); >>
45  
46 Creates a simulated chronicle connected to a temporary storage.
47  
48 =cut
49  
50 1 ▶sub get_mocked_chronicle {
Subroutine get_mocked_chronicle: called CC=1 SCAR=4.7
Pod documented
51 1 my $redis = Test::Mock::Redis->new(server => 'whatever');
52  
53 1 my $pgsql = Test::PostgreSQL->new();
54 0 my $dbic = Data::Chronicle::Mock::Connector->new($pgsql->dsn);
55 0 $dbic->testdb($pgsql);
56 0 $dbic->mode('ping');
57 0 my $stmt = qq(CREATE TABLE chronicle (
58 id bigserial,
59 timestamp TIMESTAMP DEFAULT NOW(),
60 category VARCHAR(255),
61 name VARCHAR(255),
62 value TEXT,
63 PRIMARY KEY(id),
64 CONSTRAINT search_index UNIQUE(category,name,timestamp)
65 ););
66  
67 {
68 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 ▶ $dbic->run(sub { $_->do($stmt) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 }
71  
72 0 my $chronicle_r = Data::Chronicle::Reader->new(
73 cache_reader => $redis,
74 dbic => $dbic
75 );
76  
77 0 my $chronicle_w = Data::Chronicle::Writer->new(
78 cache_writer => $redis,
79 dbic => $dbic,
80 ttl => 86400
81 );
82  
83 0 return ($chronicle_r, $chronicle_w);
84 }
85  
86 2 1;
« Chronicle/Writer.pm ↑ Summary Chronicle/Subscriber.pm »