« TaskPipe/Tool/Command_ShowJobs.pm
↑ Summary
TaskPipe/Tool/Command__OpenProxies_FetchOpenProxies.pm »
| 1 | package TaskPipe::Tool::Command_TestTask; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use MooseX::ClassAttribute; | ||
| 5 | use TaskPipe::Task::ModuleMap; | ||
| 6 | use TaskPipe::SchemaManager; | ||
| 7 | use Module::Runtime 'require_module'; | ||
| 8 | extends 'TaskPipe::Tool::Command'; | ||
| 9 | with 'MooseX::ConfigCascade'; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | has schema_manager => (is => 'rw', isa => 'TaskPipe::SchemaManager', lazy => 1, default => sub{ TaskPipe::SchemaManager->new( scope => 'project' ) }); | ||
| 14 | |||
| 15 | |||
| 16 | class_has option_specs => (is => 'ro', isa => 'ArrayRef', default => sub{[{ | ||
| 17 | module => __PACKAGE__, | ||
| 18 | is_config => 0 | ||
| 19 | }]}); | ||
| 20 | |||
| 21 | |||
| 22 | sub execute{ | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | my $mod_map = TaskPipe::Task::ModuleMap->new( | ||
| 26 | task_name => $self->name | ||
| 27 | ); | ||
| 28 | |||
| 29 | my $mod_name = $mod_map->load_module; | ||
| 30 | |||
| 31 | my $task = $mod_name->new( | ||
| 32 | test_label => $self->test, | ||
| 33 | gm => $self->job_manager->gm, | ||
| 34 | sm => $self->schema_manager, | ||
| 35 | job_id => $self->run_info->job_id | ||
| 36 | ); | ||
| 37 | |||
| 38 | $task->test; | ||
| 39 | } | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | TaskPipe::Tool::Command_TestTask - command to test an individual TaskPipe task | ||
| 44 | |||
| 45 | =head1 PURPOSE | ||
| 46 | |||
| 47 | Test an individual task by running it against test data | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | C<test task> can be used to run test data against an individual task and check the output. This effectively enables "unit testing" of tasks to make sure they are working correctly before running them as part of a plan. | ||
| 52 | |||
| 53 | A list of test data should be supplied within the task module itself by providing a C<test_pinterp> subroutine. | ||
| 54 | |||
| 55 | =head2 What is C<pinterp> ? | ||
| 56 | |||
| 57 | There are 3 words that are important when discussing the data going into a task. Those words are: | ||
| 58 | |||
| 59 | =over | ||
| 60 | |||
| 61 | =item 1. input(s) | ||
| 62 | |||
| 63 | The inputs are the raw data which the task is provided with. When running a plan, this is the data which is provided by the previous task. To give an example, let's say a previous task provides our task C<Scrape_Example> with this set of data: | ||
| 64 | |||
| 65 | { | ||
| 66 | url => 'http://www.example.com/some-list', | ||
| 67 | headers => { | ||
| 68 | Referer => 'http://www.example.com' | ||
| 69 | }, | ||
| 70 | date => '2018-17-10' | ||
| 71 | } | ||
| 72 | |||
| 73 | This data is the C<input> or C<inputs>. | ||
| 74 | |||
| 75 | =item 2. parameters (or C<params>): | ||
| 76 | |||
| 77 | You specify parameters in your plan. For example: | ||
| 78 | |||
| 79 | task: | ||
| 80 | _name: Scrape_Example | ||
| 81 | url: $this | ||
| 82 | headers: | ||
| 83 | Referer: $this | ||
| 84 | |||
| 85 | This part of the task specification are the parameters: | ||
| 86 | |||
| 87 | url: $this | ||
| 88 | headers: | ||
| 89 | Referer: $this | ||
| 90 | |||
| 91 | The parameters tell TaskPipe I<which part> of the input data to accept and use. | ||
| 92 | |||
| 93 | =item 3. "Interpolated parameters" or C<pinterp> | ||
| 94 | |||
| 95 | The parameters are interpolated using the input data. The result is the C<pinterp>. | ||
| 96 | |||
| 97 | The combination of the parameters and the input data results in the following data being accepted and used in the task: | ||
| 98 | |||
| 99 | url => 'http://www.example.com/some-list' | ||
| 100 | headers => { | ||
| 101 | Referer => 'http://www.example.com' | ||
| 102 | } | ||
| 103 | |||
| 104 | These are the C<pinterp>. Note that in the original set of inputs there was a C<date> input. This is not included in C<pinterp> because we didn't include a C<date> parameter in the plan. So C<inputs> and C<pinterp> are different. | ||
| 105 | |||
| 106 | In fact C<inputs> and C<pinterp> can be really very different, because we can specify that we want to accept data from I<earlier> tasks (e.g. instead of accepting data from the previous task, we accept it from the task previous to the previous task (ie 2 tasks before, instead of one). Consider the following parameters: | ||
| 107 | |||
| 108 | url: $this | ||
| 109 | headers: | ||
| 110 | Referer: $this[1]{url} | ||
| 111 | |||
| 112 | These parameters are telling TaskPipe to take the url from the output named C<url> of the I<previous> task, but take the C<Referer> from the output named C<url> of the task previous to the previous task (2 tasks ago). | ||
| 113 | |||
| 114 | Specifying the C<url> and C<Referer> header like this is a common situation, because this mirrors how web pages progress when a human is clicking around in a web browser: the C<Referer> is always the previous url to the one you are visiting. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head2 Including test data in your Task module | ||
| 119 | |||
| 120 | When testing tasks, we cut to the data that the task is actually accepting - so that means supplying the pinterp directly. Making sure the C<inputs> are correct is a job to consider when we are putting the plan together as a whole. | ||
| 121 | |||
| 122 | To give an example of how testing works, let's say our scraping task C<TaskPipe::Task_Scrape_Example> has a C<test_pinterp> subroutine which looks like | ||
| 123 | |||
| 124 | sub test_pinterp{[{ | ||
| 125 | url => 'http://www.example.com/list-something', | ||
| 126 | headers => { | ||
| 127 | Referer => 'http://www.example.com' | ||
| 128 | } | ||
| 129 | }]} | ||
| 130 | |||
| 131 | so this subroutine returns a list containing one item of test data - the hashref | ||
| 132 | |||
| 133 | { | ||
| 134 | url => 'http://www.example.com/list-something', | ||
| 135 | headers => { | ||
| 136 | Referer => 'http://www.example.com' | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | Let's say our scraping task is in the module C<TaskPipe::Task_Scrape_Example>. That means the name of the task is C<Scrape_Example>. To test our task we would type | ||
| 141 | |||
| 142 | taskpipe test task --name=Scrape_Example --test=0 | ||
| 143 | |||
| 144 | The C<--test=0> parameter tells TaskPipe to use the first item in the C<test_pinterp> list to run the task over. | ||
| 145 | |||
| 146 | If you prefer to name your test data, you can write your C<test_pinterp> subroutine so it returns a hashref: | ||
| 147 | |||
| 148 | sub test_pinterp{{ | ||
| 149 | |||
| 150 | mytest => { | ||
| 151 | url => 'http://www.example.com/list-something', | ||
| 152 | headers => { | ||
| 153 | Referer => 'http://www.example.com' | ||
| 154 | } | ||
| 155 | } | ||
| 156 | }} | ||
| 157 | |||
| 158 | In this example we have one test set of test data named C<mytest> and we can test the task against this data using: | ||
| 159 | |||
| 160 | taskpipe test task --test=mydata | ||
| 161 | |||
| 162 | =head2 Test output | ||
| 163 | |||
| 164 | The results of the test are normally output to a file in your log directory. The filename will be a concatenation of (C<file_prefix> + the task name + the date and time + the C<file_suffix>) where C<file_prefix> and C<file_suffix> come from the project config settings in the C<TaskPipe::Task::TestSettings> section. C<test task> should print the filename it produced to the terminal when you execute the command. | ||
| 165 | |||
| 166 | You can also set C<output> in C<TaskPipe::Task::TestSettings> to C<screen> to echo output to the screen instead of to a file (but potentially a lot of output depending on the task), or C<screen,file> for both. | ||
| 167 | |||
| 168 | =head1 OPTIONS | ||
| 169 | |||
| 170 | =over | ||
| 171 | |||
| 172 | =item name | ||
| 173 | |||
| 174 | The "name" of the task to test. The name of a task is found from the module name via | ||
| 175 | |||
| 176 | TaskPipe::Task_<name> | ||
| 177 | |||
| 178 | or | ||
| 179 | |||
| 180 | MyProject::Task_<name> | ||
| 181 | |||
| 182 | e.g. the task corresponding to module TaskPipe::Task_Record has name="Record" | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | has name => (is => 'rw', isa => 'Str'); | ||
| 187 | |||
| 188 | |||
| 189 | =item test | ||
| 190 | |||
| 191 | The name or index of the test to run. (Specify test_pinterp as a 'HashRef[HashRef]' to use names, or 'ArrayRef[HashRef]' to use indices. | ||
| 192 | |||
| 193 | =back | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | has test => (is => 'rw', isa => 'Str', default => '0'); | ||
| 198 | |||
| 199 | =head1 AUTHOR | ||
| 200 | |||
| 201 | Tom Gracey <tomgracey@gmail.com> | ||
| 202 | |||
| 203 | =head1 COPYRIGHT AND LICENSE | ||
| 204 | |||
| 205 | Copyright (c) Tom Gracey 2018 | ||
| 206 | |||
| 207 | TaskPipe is free software, licensed under | ||
| 208 | |||
| 209 | The GNU Public License Version 3 | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | __PACKAGE__->meta->make_immutable; | ||
| 214 | 1; | ||
| 215 |