lib/CMS/Drupal/Modules/MembershipEntity/Test.pm

filter: Statementstmt 100% 105 / 105 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 98.5% 139 / 141 CC 20 SCAR 16.9
CC
20
Cov
98%
build_test_data
12.0
build_test_db
5.0
__ANON__
2.0
CRAP 5.4
« lib/CMS/Drupal/Modules/MembershipEntity.pm ↑ Summary lib/CMS/Drupal/Modules/MembershipEntity/Membership.pm »
1 package CMS::Drupal::Modules::MembershipEntity::Test;
2 8 $CMS::Drupal::Modules::MembershipEntity::Test::VERSION = '0.96';
3 # ABSTRACT: Exports some helper routines for testing
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use base "Exporter::Tiny";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 our @EXPORT = qw/ build_test_db
10 build_and_validate_test_db
11 build_test_data /;
12  
13 8 ▶use Carp qw/ croak confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Test::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use File::Slurper qw/ read_text read_lines /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub build_and_validate_test_db {
22  
23 ## Reads the test data from .dat files and
24 ## builds an in-memory SQLite database
25  
26 ## This package expects to be used in a program
27 ## running in the test directory ...
28
29 1 ▶ my $drupal = shift;
Subroutine build_and_validate_test_db: called CC=1 SCAR=0.0
Pod documented
30 1 my $dbh;
31  
32 subtest 'Built the test database' => sub {
33
34 1 ▶ plan tests => 5;
Subroutine __ANON__: called CC=2 SCAR=6.9
35 1 $dbh = $drupal->dbh( database => ':memory:',
36 driver => 'SQLite' );
37  
38 1 isa_ok( $dbh, 'DBI::db', 'Got a valid $dbh' );
39  
40 subtest 'Created the test database tables.' => sub {
41 1 ▶ plan tests => 4;
Subroutine __ANON__: called CC=2 SCAR=6.9
42 1 for (split( /\n{2,}/, read_text("$FindBin::Bin/data/test_db.sql") )) {
43 4 my $rv = $dbh->do($_);
44 4 isnt( $rv, undef, 'Added a table to the test database' );
45 }
46 1 };
47  
48 #########
49 # Populate the test database
50
51 # First we have to have a default type
52 1 my $add_type = qq/
53 INSERT INTO membership_entity_type (type, label, weight, description, data, status, module)
54 VALUES (?, ?, ?, ?, ?, ?, ?)
55 /;
56
57 1 ▶ my @fields = split(',', read_text("$FindBin::Bin/data/test_types.dat")) or croak; # This file must have only ONE line
Branch: 1 branch
BranchTrueFalseTotal
my @fields = split(/,/, read_text("$FindBin::Bin/data/test_types.dat"), 0) or croak() 0 1 1
58
59 1 my $add_type_rv = $dbh->do( $add_type, {}, @fields, undef );
60 1 cmp_ok( $add_type_rv, '>', 0, 'Populate the membership_entity_type table with a default type' );
61
62 ## Now add Memberships from the data file
63 1 my $add_mem = qq/
64 INSERT INTO membership_entity (mid, member_id, type, uid, status, created, changed)
65 VALUES ( ?, ?, ?, ?, ?, ?, ?)
66 /;
67
68 test 'Populate the membership_entity table with test data' => sub {
69 1 ▶ for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
70 29 my @fields = split(',');
71 29 my $add_mem_rv = $dbh->do( $add_mem, {}, @fields );
72 29 cmp_ok( $add_mem_rv, '>', 0, "Added a Membership for mid $fields[0]" );
73 }
74 1 };
75
76 ## Now add Membership Terms from the data file
77 1 my $add_term = qq/
78 INSERT INTO membership_entity_term(id, mid, status, term, modifiers, start, end )
79 VALUES (?, ?, ?, ?, ?, ?, ?)
80 /;
81
82 test 'Populate the membership_entity_term table with test data' => sub {
83 1 ▶ for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 33 my @fields = split(',');
85 33 my $add_term_rv = $dbh->do( $add_term, {}, @fields );
86 33 cmp_ok( $add_term_rv, '>', 0, "Added a Term for $fields[0]" );
87 }
88 1 };
89 1 }; # done building the test DB
90  
91 1 $dbh->sqlite_backup_to_file("$FindBin::Bin/data/.DB.sqlite");
92  
93 1 return $dbh;
94 }
95  
96 sub build_test_db {
97  
98 ## No testing here!
99  
100 ## Reads the test data from .dat files and
101 ## builds an in-memory SQLite database
102  
103  
104 7 ▶ my $drupal = shift;
Subroutine build_test_db: called CC=5 SCAR=16.1
Pod documented
105 7 my $dbh = $drupal->dbh( database => ':memory:',
106 driver => 'SQLite' );
107  
108 7 for (split( /\n{2,}/, read_text("$FindBin::Bin/data/test_db.sql") )) {
109 28 my $rv = $dbh->do($_);
110 }
111  
112 # First we have to have a default type
113 7 my $add_type = qq/
114 INSERT INTO membership_entity_type (type, label, weight, description, data, status, module)
115 VALUES (?, ?, ?, ?, ?, ?, ?)
116 /;
117  
118 7 ▶ my @fields = split(',', read_text("$FindBin::Bin/data/test_types.dat")) or croak; # This file must have only ONE line
Branch: 1 branch
BranchTrueFalseTotal
my @fields = split(/,/, read_text("$FindBin::Bin/data/test_types.dat"), 0) or croak() 0 7 7
119  
120 7 my $add_type_rv = $dbh->do( $add_type, {}, @fields, undef );
121  
122 ## Now add Memberships from the data file
123 7 my $add_mem = qq/
124 INSERT INTO membership_entity (mid, member_id, type, uid, status, created, changed)
125 VALUES ( ?, ?, ?, ?, ?, ?, ?)
126 /;
127  
128 7 for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
129 203 my @fields = split(',');
130 203 my $add_mem_rv = $dbh->do( $add_mem, {}, @fields );
131 }
132  
133 ## Now add Membership Terms from the data file
134 7 my $add_term = qq/
135 INSERT INTO membership_entity_term(id, mid, status, term, modifiers, start, end )
136 VALUES (?, ?, ?, ?, ?, ?, ?)
137 /;
138  
139 7 for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
140 231 my @fields = split(',');
141 231 my $add_term_rv = $dbh->do( $add_term, {}, @fields );
142 }
143  
144 7 $dbh->sqlite_backup_to_file( "$FindBin::Bin/data/.DB.sqlite" );
145  
146 7 return $dbh;
147 }
148  
149 ############################
150  
151 sub build_test_data {
152  
153 6 ▶ my @mids = @_;
Subroutine build_test_data: called CC=12 SCAR=24.8
Pod documented
154 6 my %include;
155 6 for( @mids ) {
156 20 $include{ $_ }++;
157 }
158
159 6 my %membs;
160 my %terms;
161  
162 6 for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
163 174 my @fields = split(',');
164 174 ▶ if (scalar @mids > 0) { next unless exists $include{ $fields[0] }; }
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @mids > 0) 116 58 174
unless exists $include{$fields[0]} 97 19 116
165 77 $membs{ $fields[0] } = { 'mid' => $fields[0],
166 'member_id' => $fields[1],
167 'type' => $fields[2],
168 'uid' => $fields[3],
169 'status' => $fields[4],
170 'created' => $fields[5],
171 'changed' => $fields[6] };
172 }
173  
174 6 my %term_count;
175  
176 6 for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
177 198 my @fields = split(',');
178 198 ▶ if (scalar @mids > 0) { next unless exists $include{ $fields[1] } };
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @mids > 0) 132 66 198
unless exists $include{$fields[1]} 111 21 132
179 87 $term_count{ $fields[1] }++;
180 87 for (5..6) {
181 174 my @datetime = reverse (split /[-| |:]/, $fields[ $_ ]);
182 174 $datetime[4]--;
183 174 $fields[ $_ ] = timelocal( @datetime );
184 }
185 $terms{ $fields[0] } = bless(
186 { 'tid' => $fields[0],
187 'mid' => $fields[1],
188 'status' => $fields[2],
189 'term' => $fields[3],
190 'modifiers' => $fields[4],
191 'start' => $fields[5],
192 'end' => $fields[6],
193 87 'array_position' => $term_count{ $fields[1] } },
194 'CMS::Drupal::Modules::MembershipEntity::Term' );
195 }
196  
197 6 ▶ while ( my ($tid, $term) = each %terms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %terms) 87 6 93
198 87 $membs{ $term->{'mid'} }->{'terms'}->{ $tid } = $term;
199 }
200  
201 6 foreach my $mem ( keys %membs ) {
202 77 $membs{ $mem } = bless( $membs{ $mem }, 'CMS::Drupal::Modules::MembershipEntity::Membership' );
203 }
204  
205 6 ▶ return (scalar keys %membs == 1) ? @membs{ keys %membs } : \%membs;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %membs == 1 ? : 1 5 6
206 }
207  
208 8 1; # End package
209  
210 __END__
211  
212 =pod
213  
214 =encoding UTF-8
215  
216 =head1 NAME
217  
218 CMS::Drupal::Modules::MembershipEntity::Test - Exports some helper routines for testing
219  
220 =head1 VERSION
221  
222 version 0.96
223  
224 =head1 SYNOPSIS
225  
226 use Test::More;
227 use CMS::Drupal;
228 use CMS::Drupal::Modules::MembershipEntity::Test qw/ build_test_db build_test_data/;
229  
230 my $drupal = CMS::Drupal->new;
231  
232 my $dbh = build_test_db( $drupal );
233  
234 my $ME = CMS::Drupal::Modules::MembershipEntity->new( dbh => $dbh );
235  
236 my $hashref = $ME->fetch_memberships;
237 my $cmp_data = build_test_data;
238  
239 # or:
240
241 my $hashref = $ME->fetch_memberships('1234', '5678');
242 my $cmp_data = build_test_data('1234', '5678');
243  
244 is_deeply($hashref, $cmp_data, 'Data matches');
245  
246 =head1 DESCRIPTION
247  
248 Use this module when testing the CMS::Drupal::Modules::MembershipEntity modules.
249  
250 =head1 METHODS
251  
252 =head2 build_and_validate_test_db
253  
254 This method returns a database handle ($dbh) that is connected to an in-memory SQLite database.
255 The database is built by this method using data files that must be contained in the same directory that
256 the calling script lives in.
257  
258 The method takes one argument, which must be a $drupal object. This is because it calls $drupal->dbh()
259 to generate its database handle, which, because we are using SQLite, contains the database inside the
260 handle itself.
261  
262 The files are:
263  
264 =over 4
265  
266 =item *
267  
268 B<test_db.sql>
269  
270 =item *
271  
272 B<test_types.dat>
273  
274 =item *
275  
276 B<test_memberships.dat>
277  
278 =item *
279  
280 B<test_terms.dat>
281  
282 =back
283  
284 Note that this method uses Test::More and Test::Group itself to report success/failures in building
285 the test database. So in your scipt that calls this method you should add one additional test to
286 your plan.
287  
288 =head2 build_test_db
289  
290 This method does the same as the previous one except it does not run tests itself, in other words
291 it builds the test db and the test data silently.
292  
293 =head2 build_test_data
294  
295 This method returns a data structure containing the Memberships as they would be returned by
296 CMS::Drupal::Modules::MembershipEntity::fetch_memberships(). It creates the data structure
297 by parsing the same files that were used to build in test database.
298  
299 The method takes an optional single argument, which is an array containing a list of B<mid>s.
300 Only the Memberships associated with the B<mid>s provided will be included in the data
301 returned.
302  
303 $cmp_data = build_test_data('1234', '5678');
304  
305 The data structure is a hashref of hashrefs (Membership objects, indexed by mid, containing
306 among their attributes an array of hashrefs (Membership Term objects) ...
307  
308 '4086' => bless( {
309 'mid' => '4086',
310 'uid' => '12305',
311 'status' => '1',
312 'member_id' => '01252',
313 'terms' => {
314 '4088' => bless( {
315 'mid' => '4086',
316 'array_position' => 2,
317 'status' => '1',
318 'modifiers' => 'a:0:{}',
319 'end' => 1448611200,
320 'start' => 1354089600,
321 'term' => 'import',
322 'tid' => '4088'
323 }, 'CMS::Drupal::Modules::MembershipEntity::Term' ),
324 '3920' => bless( {
325 'mid' => '4086',
326 'array_position' => 1,
327 'status' => '0',
328 'modifiers' => 'a:0:{}',
329 'end' => 1403247600,
330 'start' => 1308639600,
331 'term' => 'import',
332 'tid' => '3920'
333 }, 'CMS::Drupal::Modules::MembershipEntity::Term' )
334 },
335 'type' => 'membership',
336 'created' => '1354086000',
337 'changed' => '1400604379'
338 }, 'CMS::Drupal::Modules::MembershipEntity::Membership' ),
339  
340 =head1 SEE ALSO
341  
342 =over 4
343  
344 =item *
345  
346 L<CMS::Drupal::Modules::MembershipEntity::Membership|CMS::Drupal::Modules::MembershipEntity::Membership>
347  
348 =item *
349  
350 L<CMS::Drupal::Modules::MembershipEntity::Term|CMS::Drupal::Modules::MembershipEntity::Term>
351  
352 =item *
353  
354 L<CMS::Drupal::Modules::MembershipEntity|CMS::Drupal::Modules::MembershipEntity>
355  
356 =item *
357  
358 L<CMS::Drupal|CMS::Drupal>
359  
360 =back
361  
362 =head1 AUTHOR
363  
364 Nick Tonkin <tonkin@cpan.org>
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 This software is copyright (c) 2015 by Nick Tonkin.
369  
370 This is free software; you can redistribute it and/or modify it under
371 the same terms as the Perl 5 programming language system itself.
372  
373 =cut
« lib/CMS/Drupal/Modules/MembershipEntity.pm ↑ Summary lib/CMS/Drupal/Modules/MembershipEntity/Membership.pm »