Ver código fonte

Fix #68 - Use _X_in_my_Y to dedup code

George S. Baugh 10 anos atrás
pai
commit
244aedad77
6 arquivos alterados com 31 adições e 44 exclusões
  1. 2 0
      Changes
  2. 1 1
      lib/Test/Rail/Parser.pm
  3. 21 33
      lib/TestRail/API.pm
  4. 4 4
      lib/TestRail/Utils/Find.pm
  5. 2 4
      t/TestRail-API.t
  6. 1 2
      t/arg_types.t

+ 2 - 0
Changes

@@ -5,6 +5,8 @@ Revision history for Perl module TestRail::API
     - Update TestRail::API::getCaseByName to take filter hashref too
     - Update getRunSummary and getPlanSummary to use 'labels' rather than system names
     - Add TestRail::API::statusNamesToLabels to ease interaction with above method updates
+    - Change TestRail::API::translateConfigNamesToIds to accept ARRAY arguments
+    - make above function and TestRail::API::sectionNamesToIds return values in correct order
 
 0.030 2015-07-31 TEODESIAN
     - Fix testrail-tests, was calling function in incorrect namespace

+ 1 - 1
lib/Test/Rail/Parser.pm

@@ -188,7 +188,7 @@ sub new {
     my ($run,$plan,$config_ids);
 
     #check if configs passed are defined for project.  If we can't get all the IDs, something's hinky
-    $config_ids = $tr->translateConfigNamesToIds($tropts->{'project_id'},$tropts->{'configs'});
+    @$config_ids = $tr->translateConfigNamesToIds($tropts->{'project_id'},@{$tropts->{'configs'}});
     confess("Could not retrieve list of valid configurations for your project.") unless (reftype($config_ids) || 'undef') eq 'ARRAY';
     my @bogus_configs = grep {!defined($_)} @$config_ids;
     my $num_bogus = scalar(@bogus_configs);

+ 21 - 33
lib/TestRail/API.pm

@@ -767,16 +767,9 @@ Throws an exception in the case of one (or more) of the names not corresponding
 =cut
 
 sub sectionNamesToIds {
-    state $check = compile(Object, Int, Int, slurpy ArrayRef[Str]);
-    my ($self,$project_id,$suite_id,$names) = $check->(@_);
-
-    confess("At least one section name must be provided") if !scalar(@$names);
-
-    my $sections = $self->getSections($project_id,$suite_id);
-    confess("Invalid project/suite ($project_id,$suite_id) provided.") unless (reftype($sections) || 'undef') eq 'ARRAY';
-    my @ret = grep {defined $_} map {my $section = $_; my @list = grep {$section->{'name'} eq $_} @$names; scalar(@list) ? $section->{'id'} : undef} @$sections;
-    confess("One or more user names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
-    return @ret;
+    my ($self,$project_id,$suite_id,@names) = @_;
+    my $sections = $self->getSections($project_id,$suite_id) or confess("Could not find sections in provided project/suite.");
+    return _X_in_my_Y($self,$sections,'id',@names);
 }
 
 =head1 CASE METHODS
@@ -1992,7 +1985,7 @@ Throws an exception in the case of one (or more) of the names not corresponding
 
 sub statusNamesToIds {
     my ($self,@names) = @_;
-    return _statusNamesToX($self,'id',@names);
+    return _X_in_my_Y($self,$self->getPossibleTestStatuses(),'id',@names);
 };
 
 =head2 statusNamesToLabels(names)
@@ -2014,28 +2007,28 @@ Throws an exception in the case of one (or more) of the names not corresponding
 
 sub statusNamesToLabels {
     my ($self,@names) = @_;
-    return _statusNamesToX($self,'label',@names);
+    return _X_in_my_Y($self,$self->getPossibleTestStatuses(),'label',@names);
 };
 
-#Reduce code duplication
-sub _statusNamesToX {
-    state $check = compile(Object, Str, slurpy ArrayRef[Str]);
-    my ($self,$key,$names) = $check->(@_);
-    confess("No status names passed!") unless scalar(@$names);
+# Reduce code duplication with internal methods?
+# It's more likely than you think
+# Free PC check @ cpan.org
+sub _X_in_my_Y {
+    state $check = compile(Object, ArrayRef, Str, slurpy ArrayRef[Str]);
+    my ($self,$search_arr,$key,$names) = $check->(@_);
 
-    my $statuses = $self->getPossibleTestStatuses();
     my @ret;
     foreach my $name (@$names) {
-        foreach my $status (@$statuses) {
-            if ($status->{'name'} eq $name) {
-                push @ret, $status->{$key};
+        foreach my $member (@$search_arr) {
+            if ($member->{'name'} eq $name) {
+                push @ret, $member->{$key};
                 last;
             }
         }
     }
-    confess("One or more status names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
+    confess("One or more names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
     return @ret;
-};
+}
 
 =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
 
@@ -2226,23 +2219,18 @@ Transforms a list of configuration names into a list of config IDs.
 
 =item INTEGER C<PROJECT_ID> - Relevant project ID for configs.
 
-=item ARRAYREF C<CONFIGS> - Array ref of config names
+=item ARRAY C<CONFIGS> - Array of config names
 
 =back
 
-Returns ARRAYREF of configuration names, with undef values for unknown configuration names.
+Returns ARRAY of configuration names, with undef values for unknown configuration names.
 
 =cut
 
 sub translateConfigNamesToIds {
-    state $check = compile(Object, Int, ArrayRef[Str]);
-    my ($self,$project_id,$configs) = $check->(@_);
-
-    return [] if !scalar(@$configs);
-    my $existing_configs = $self->getConfigurations($project_id);
-    return map {undef} @$configs if (reftype($existing_configs) || 'undef') ne 'ARRAY';
-    my @ret = map {my $name = $_; my @candidates = grep { $name eq $_->{'name'} } @$existing_configs; scalar(@candidates) ? $candidates[0]->{'id'} : undef } @$configs;
-    return \@ret;
+    my ($self,$project_id,@names) = @_;
+    my $configs = $self->getConfigurations($project_id) or confess("Could not determine configurations in provided project.");
+    return _X_in_my_Y($self,$configs,'id',@names);
 }
 
 =head1 STATIC METHODS

+ 4 - 4
lib/TestRail/Utils/Find.pm

@@ -42,18 +42,18 @@ sub findRuns {
     my ($opts,$tr) = @_;
     confess("TestRail handle must be provided as argument 2") unless blessed($tr) eq 'TestRail::API';
 
-    my ($status_ids);
+    my ($status_labels);
 
     #Process statuses
     if ($opts->{'statuses'}) {
-        @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}});
+        @$status_labels = $tr->statusNamesToLabels(@{$opts->{'statuses'}});
     }
 
     my $project = $tr->getProjectByName($opts->{'project'});
     confess("No such project '$opts->{project}'.\n") if !$project;
 
     my $pconfigs = [];
-    $pconfigs = $tr->translateConfigNamesToIds($project->{'id'},$opts->{configs}) if $opts->{'configs'};
+    @$pconfigs = $tr->translateConfigNamesToIds($project->{'id'},@{$opts->{configs}}) if $opts->{'configs'};
 
     my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
     $runs = $tr->getRuns($project->{'id'}) if (!$opts->{'configs'}); # If configs are passed, global runs are not in consideration.
@@ -81,7 +81,7 @@ sub findRuns {
     if ($opts->{'statuses'}) {
         @$runs =  $tr->getRunSummary(@$runs);
         @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
-        foreach my $status (@{$opts->{'statuses'}}) {
+        foreach my $status (@$status_labels) {
             @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it.  Otherwise forget it.
         }
     }

+ 2 - 4
t/TestRail-API.t

@@ -214,10 +214,8 @@ if ($is_arr) {
     @config_names = map {$_->{'name'}} @$configs;
     @config_ids = map {$_->{'id'}} @$configs;
 }
-my $t_config_ids = $tr->translateConfigNamesToIds($new_project->{'id'},\@config_names);
-@config_ids = sort(@config_ids);
-@$t_config_ids = sort(@$t_config_ids);
-is_deeply(\@config_ids,$t_config_ids, "Can correctly translate Project names to IDs");
+my @t_config_ids = $tr->translateConfigNamesToIds($new_project->{'id'},@config_names);
+is_deeply(\@config_ids,\@t_config_ids, "Can correctly translate Project names to IDs, and they are correctly sorted");
 
 ############################################################
 # TestRail arbitrarily limits many calls to 250 result sets.

+ 1 - 2
t/arg_types.t

@@ -2,7 +2,7 @@ use strict;
 use warnings;
 
 use TestRail::API;
-use Test::More 'tests' => 144;
+use Test::More 'tests' => 143;
 use Test::Fatal;
 use Class::Inspector;
 use Test::LWP::UserAgent;
@@ -147,7 +147,6 @@ is(exception {$tr->getTestByName(1,'poo')}, undef,'getTestByName with 2 args ret
 is(exception {$tr->getTestSuiteByName(1,'zap')}, undef,'getTestSuiteByName with 2 args returns no error');
 is(exception {$tr->createCase(1,'whee')}, undef,'createCase with 2 args returns no error');
 is(exception {$tr->getChildRunByName({},'whee')},undef,'getChildRunByName returns no error when 2 arguments passed');
-is(exception {$tr->translateConfigNamesToIds(1,[1,2,3])}, undef,'translateConfigNamesToIds returns no error when 2 arguments passed');
 is(exception {$tr->bulkAddResults(1,[])}, undef,'bulkAddResults returns no error when 2 arguments passed');
 is(exception {$tr->getCases(1,2)}, undef,'getCases with 2 args returns error');