File Coverage

blib/lib/CMS/Drupal/Modules/MembershipEntity/Test.pm
Criterion Covered Total %
statement 102 102 100.0
branch 12 14 85.7
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 131 133 98.5


line stmt bran cond sub pod time code
1             package CMS::Drupal::Modules::MembershipEntity::Test;
2             $CMS::Drupal::Modules::MembershipEntity::Test::VERSION = '0.96';
3             # ABSTRACT: Exports some helper routines for testing
4              
5 8     8   6175 use strict;
  8         18  
  8         182  
6 8     8   40 use warnings;
  8         17  
  8         232  
7              
8 8     8   37 use base "Exporter::Tiny";
  8         14  
  8         870  
9             our @EXPORT = qw/ build_test_db
10             build_and_validate_test_db
11             build_test_data /;
12              
13 8     8   37 use Carp qw/ croak confess /;
  8         15  
  8         446  
14 8     8   39 use Test::More;
  8         19  
  8         84  
15 8     8   3347 use Test::Group;
  8         23124  
  8         600  
16 8     8   5586 use File::Slurper qw/ read_text read_lines /;
  8         1945009  
  8         702  
17 8     8   4842 use FindBin;
  8         6936  
  8         343  
18 8     8   9308 use DBD::SQLite;
  8         105676  
  8         238  
19 8     8   55 use Time::Local;
  8         15  
  8         10061  
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     1 1 4628 my $drupal = shift;
30 1         3 my $dbh;
31              
32             subtest 'Built the test database' => sub {
33            
34 1     1   523 plan tests => 5;
35 1         180 $dbh = $drupal->dbh( database => ':memory:',
36             driver => 'SQLite' );
37              
38 1         2703512 isa_ok( $dbh, 'DBI::db', 'Got a valid $dbh' );
39              
40             subtest 'Created the test database tables.' => sub {
41 1         855 plan tests => 4;
42 1         237 for (split( /\n{2,}/, read_text("$FindBin::Bin/data/test_db.sql") )) {
43 4         275686 my $rv = $dbh->do($_);
44 4         2774 isnt( $rv, undef, 'Added a table to the test database' );
45             }
46 1         991 };
47              
48             #########
49             # Populate the test database
50            
51             # First we have to have a default type
52 1         1181 my $add_type = qq/
53             INSERT INTO membership_entity_type (type, label, weight, description, data, status, module)
54             VALUES (?, ?, ?, ?, ?, ?, ?)
55             /;
56            
57 1 50       12 my @fields = split(',', read_text("$FindBin::Bin/data/test_types.dat")) or croak; # This file must have only ONE line
58            
59 1         6594 my $add_type_rv = $dbh->do( $add_type, {}, @fields, undef );
60 1         650 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         1056 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         221 for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
70 29         436818 my @fields = split(',');
71 29         243 my $add_mem_rv = $dbh->do( $add_mem, {}, @fields );
72 29         5052 cmp_ok( $add_mem_rv, '>', 0, "Added a Membership for mid $fields[0]" );
73             }
74 1         19 };
75            
76             ## Now add Membership Terms from the data file
77 1         3624 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         240 for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
84 33         181683 my @fields = split(',');
85 33         285 my $add_term_rv = $dbh->do( $add_term, {}, @fields );
86 33         5938 cmp_ok( $add_term_rv, '>', 0, "Added a Term for $fields[0]" );
87             }
88 1         57 };
89 1         6 }; # done building the test DB
90              
91 1         33348566 $dbh->sqlite_backup_to_file("$FindBin::Bin/data/.DB.sqlite");
92              
93 1         20 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     7 1 410 my $drupal = shift;
105 7         53 my $dbh = $drupal->dbh( database => ':memory:',
106             driver => 'SQLite' );
107              
108 7         130459 for (split( /\n{2,}/, read_text("$FindBin::Bin/data/test_db.sql") )) {
109 28         8449 my $rv = $dbh->do($_);
110             }
111              
112             # First we have to have a default type
113 7         1653 my $add_type = qq/
114             INSERT INTO membership_entity_type (type, label, weight, description, data, status, module)
115             VALUES (?, ?, ?, ?, ?, ?, ?)
116             /;
117              
118 7 50       48 my @fields = split(',', read_text("$FindBin::Bin/data/test_types.dat")) or croak; # This file must have only ONE line
119              
120 7         777 my $add_type_rv = $dbh->do( $add_type, {}, @fields, undef );
121              
122             ## Now add Memberships from the data file
123 7         831 my $add_mem = qq/
124             INSERT INTO membership_entity (mid, member_id, type, uid, status, created, changed)
125             VALUES ( ?, ?, ?, ?, ?, ?, ?)
126             /;
127              
128 7         51 for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
129 203         20437 my @fields = split(',');
130 203         938 my $add_mem_rv = $dbh->do( $add_mem, {}, @fields );
131             }
132              
133             ## Now add Membership Terms from the data file
134 7         709 my $add_term = qq/
135             INSERT INTO membership_entity_term(id, mid, status, term, modifiers, start, end )
136             VALUES (?, ?, ?, ?, ?, ?, ?)
137             /;
138              
139 7         43 for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
140 231         22986 my @fields = split(',');
141 231         945 my $add_term_rv = $dbh->do( $add_term, {}, @fields );
142             }
143              
144 7         27004045 $dbh->sqlite_backup_to_file( "$FindBin::Bin/data/.DB.sqlite" );
145              
146 7         183 return $dbh;
147             }
148              
149             ############################
150              
151             sub build_test_data {
152              
153 6     6 1 11042 my @mids = @_;
154 6         15 my %include;
155 6         22 for( @mids ) {
156 20         104 $include{ $_ }++;
157             }
158            
159 6         11 my %membs;
160             my %terms;
161              
162 6         46 for ( read_lines("$FindBin::Bin/data/test_memberships.dat" ) ) {
163 174         1701 my @fields = split(',');
164 174 100       367 if (scalar @mids > 0) { next unless exists $include{ $fields[0] }; }
  116 100       404  
165 77         1631 $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         22 my %term_count;
175              
176 6         30 for ( read_lines("$FindBin::Bin/data/test_terms.dat" ) ) {
177 198         1401 my @fields = split(',');
178 198 100       407 if (scalar @mids > 0) { next unless exists $include{ $fields[1] } };
  132 100       413  
179 87         174 $term_count{ $fields[1] }++;
180 87         147 for (5..6) {
181 174         4559 my @datetime = reverse (split /[-| |:]/, $fields[ $_ ]);
182 174         306 $datetime[4]--;
183 174         398 $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         4317 'array_position' => $term_count{ $fields[1] } },
194             'CMS::Drupal::Modules::MembershipEntity::Term' );
195             }
196              
197 6         53 while ( my ($tid, $term) = each %terms ) {
198 87         422 $membs{ $term->{'mid'} }->{'terms'}->{ $tid } = $term;
199             }
200              
201 6         38 foreach my $mem ( keys %membs ) {
202 77         186 $membs{ $mem } = bless( $membs{ $mem }, 'CMS::Drupal::Modules::MembershipEntity::Membership' );
203             }
204              
205 6 100       59 return (scalar keys %membs == 1) ? @membs{ keys %membs } : \%membs;
206             }
207              
208             1; # End package
209              
210             __END__