| 1 | =pod | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | ETL::Pipeline::Output::UnitTest - Output destination for unit tests | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use ETL::Pipeline; | ||||||||||
| 10 | ETL::Pipeline->new( { | ||||||||||
| 11 | input => ['UnitTest'], | ||||||||||
| 12 | mapping => {First => 'Header1', Second => 'Header2'}, | ||||||||||
| 13 | output => ['UnitTest'] | ||||||||||
| 14 | } )->process; | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | B<ETL::Pipeline::Output::UnitTest> is an output destination used by the unit | ||||||||||
| 19 | tests. It proves that the L<ETL::Pipeline::Output> role works. You should not | ||||||||||
| 20 | use this destination in production code - only unit tests. | ||||||||||
| 21 | |||||||||||
| 22 | The I<data> is stored in memory. The class provides methods to access the saved | ||||||||||
| 23 | records. | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | package ETL::Pipeline::Output::UnitTest; | ||||||||||
| 28 | |||||||||||
| 29 | 8 | ▶ | use 5.014000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 8 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 8 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 8 | our $VERSION = '3.00'; | |||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | =head1 METHODS & ATTRIBUTES | ||||||||||
| 40 | |||||||||||
| 41 | =head2 Arguments for L<ETL::Pipeline/output> | ||||||||||
| 42 | |||||||||||
| 43 | None - there's no configuration for this destination. It's meant to be quick and | ||||||||||
| 44 | light for unit testing. | ||||||||||
| 45 | |||||||||||
| 46 | =head2 Attributes | ||||||||||
| 47 | |||||||||||
| 48 | =head3 records | ||||||||||
| 49 | |||||||||||
| 50 | In B<ETL::Pipeline::Output::UnitTest>, a record is a hash reference. | ||||||||||
| 51 | B<records> stores a list of record (hash references). The list survives after | ||||||||||
| 52 | calling L</finish>. This allows you to check the results of a test after | ||||||||||
| 53 | calling L<ETL::Pipeline/process>. | ||||||||||
| 54 | |||||||||||
| 55 | The list is cleared after calling L</configure>. | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | has 'records' => ( | ||||||||||
| 60 | 61 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 8 | handles => { | |||||||||
| 62 | _add_record => 'push', | ||||||||||
| 63 | all_records => 'elements', | ||||||||||
| 64 | _reset => 'clear', | ||||||||||
| 65 | get_record => 'get', | ||||||||||
| 66 | number_of_records => 'count', | ||||||||||
| 67 | }, | ||||||||||
| 68 | is => 'ro', | ||||||||||
| 69 | isa => 'ArrayRef[HashRef[Any]]', | ||||||||||
| 70 | traits => [qw/Array/], | ||||||||||
| 71 | ); | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | =head2 Methods | ||||||||||
| 75 | |||||||||||
| 76 | =head3 all_records | ||||||||||
| 77 | |||||||||||
| 78 | Returns a list of all the records. It dereferences L</records>. | ||||||||||
| 79 | |||||||||||
| 80 | =cut | ||||||||||
| 81 | |||||||||||
| 82 | # This method is defined by the "records" attribute. | ||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | =head3 close | ||||||||||
| 86 | |||||||||||
| 87 | Prevents further storage of records. L</write> will throw a fatal exception if | ||||||||||
| 88 | it is called after B<close>. This helps ensure that everything runs in the | ||||||||||
| 89 | proper order. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 51 | ▶ | sub close { shift->_state( "already called 'closed'" ); } | ||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | =head3 get_record | ||||||||||
| 97 | |||||||||||
| 98 | Returns a single record from storage. Useful to check the values and make sure | ||||||||||
| 99 | things were added in the correct order. It returns the ame hash reference passed | ||||||||||
| 100 | into L</write>. | ||||||||||
| 101 | |||||||||||
| 102 | =head3 number_of_records | ||||||||||
| 103 | |||||||||||
| 104 | Returns the count of records currently in storage. | ||||||||||
| 105 | |||||||||||
| 106 | =cut | ||||||||||
| 107 | |||||||||||
| 108 | # These methods are defined by the "records" attribute. | ||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | =head3 open | ||||||||||
| 112 | |||||||||||
| 113 | Allows storage of records. L</write> will throw a fatal exception if it is | ||||||||||
| 114 | called before B<open>. This helps ensure that everything runs in the proper | ||||||||||
| 115 | order. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | 53 | ▶ | sub open { shift->_state( 'processing records' ); } | ||||||||
|
Subroutine
open: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | =head3 write | ||||||||||
| 123 | |||||||||||
| 124 | Add the current record into the L</records> attribute. Unit tests can then | ||||||||||
| 125 | check what was saved, to make sure the pipeline completed. | ||||||||||
| 126 | |||||||||||
| 127 | =cut | ||||||||||
| 128 | |||||||||||
| 129 | 117 | ▶ | sub write { | ||||||||
|
Subroutine
write: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 130 | 117 | my ($self, $etl, $record) = @_; | |||||||||
| 131 | |||||||||||
| 132 | 117 | ▶ | if ($self->_state eq 'processing records') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 117 | $self->_add_record( $record ); | |||||||||
| 134 | } else { | ||||||||||
| 135 | 0 | croak sprintf "'write' failed because ETL::Pipeline %s", $self->_state; | |||||||||
| 136 | } | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | #------------------------------------------------------------------------------- | ||||||||||
| 141 | # Internal methods and attributes | ||||||||||
| 142 | |||||||||||
| 143 | # Object state. It lets "write" verify that everything has happened in the | ||||||||||
| 144 | # correct order. | ||||||||||
| 145 | 8 | has '_state' => ( | |||||||||
| 146 | default => "hasn't called 'open' yet", | ||||||||||
| 147 | is => 'rw', | ||||||||||
| 148 | isa => 'Str', | ||||||||||
| 149 | ); | ||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | =head1 SEE ALSO | ||||||||||
| 153 | |||||||||||
| 154 | L<ETL::Pipeline>, L<ETL::Pipeline::Output>, L<ETL::Pipeline::Input::UnitTest> | ||||||||||
| 155 | |||||||||||
| 156 | =cut | ||||||||||
| 157 | |||||||||||
| 158 | 8 | with 'ETL::Pipeline::Output'; | |||||||||
| 159 | |||||||||||
| 160 | |||||||||||
| 161 | =head1 AUTHOR | ||||||||||
| 162 | |||||||||||
| 163 | Robert Wohlfarth <robert.j.wohlfarth@vumc.org> | ||||||||||
| 164 | |||||||||||
| 165 | =head1 LICENSE | ||||||||||
| 166 | |||||||||||
| 167 | Copyright 2021 (c) Vanderbilt University | ||||||||||
| 168 | |||||||||||
| 169 | This program is free software; you can redistribute it and/or modify it under | ||||||||||
| 170 | the same terms as Perl itself. | ||||||||||
| 171 | |||||||||||
| 172 | =cut | ||||||||||
| 173 | |||||||||||
| 174 | 8 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 175 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||