Explorar el Código

Fix #53 : Actually cover the getResultFieldByName function

George S. Baugh hace 10 años
padre
commit
3494c6a4cc
Se han modificado 5 ficheros con 30 adiciones y 11 borrados
  1. 1 1
      lib/Test/LWP/UserAgent/TestRailMock.pm
  2. 14 8
      lib/TestRail/API.pm
  3. 12 1
      t/TestRail-API-mockOnly.t
  4. 1 0
      t/TestRail-API.t
  5. 2 1
      t/arg_types.t

+ 1 - 1
lib/Test/LWP/UserAgent/TestRailMock.pm

@@ -1207,7 +1207,7 @@ $VAR4 = bless( {
                  'content-type' => 'application/json; charset=utf-8',
                  'server' => 'Apache/2.4.7 (Ubuntu)'
                }, 'HTTP::Headers' );
-$VAR5 = '[]';
+$VAR5 = '[{"display_order":1,"system_name":"custom_step_results","name":"step_results","description":"Step by step results","is_active":1,"type_id":11,"configs":[{"options":{"is_required":0,"format":"markdown","has_actual":1,"has_expected":1},"context":{"project_ids":[5],"is_global":1},"id":"43410543-edaf-44d2-91fc-58a6f9b3f743"},{"options":{"is_required":1,"format":"markdown","has_actual":1,"has_expected":1},"context":{"project_ids":[1],"is_global":1},"id":"0ab86184-0468-40d8-a385-a9b3a1ec41a4"},{"options":{"is_required":0,"format":"markdown","has_actual":1,"has_expected":1},"context":{"project_ids":[10],"is_global":1},"id":"43ebdf1f-c9b9-4b91-a729-5c9f21252f00"}],"id":6,"label":"Step Results"}]';
 $mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));
 
 }

+ 14 - 8
lib/TestRail/API.pm

@@ -1913,15 +1913,21 @@ sub getTestResultFieldByName {
     my ($self,$system_name,$project_id) = @_;
     confess("Object methods must be called by an instance") unless ref($self);
     confess("System name must be string") unless $self->_checkString($system_name);
-    my @candidates = grep {$_->{'name'} eq $system_name} @{$self->getTestResultFields()};
-    return 0 if !scalar(@candidates);
-    if (defined $project_id) {
-        @candidates = grep {
-            $_->{'configs'}->[0]->{'context'}->{'is_global'} ||
-            ( grep {$_ == $project_id} @{ $_->{'configs'}->[0]->{'context'}->{'project_ids'} } )
-        } @candidates;
+    my @candidates = grep { $_->{'name'} eq $system_name} @{$self->getTestResultFields()};
+    return 0 if !scalar(@candidates); #No such name
+    return -1 if ref($candidates[0]) ne 'HASH';
+    return -2 if ref($candidates[0]->{'configs'}) ne 'ARRAY' && !scalar(@{$candidates[0]->{'configs'}}); #bogofilter
+
+    #Give it to the user
+    my $ret = $candidates[0]; #copy/save for later
+    return $ret if !defined($project_id);
+
+    #Filter by project ID
+    foreach my $config (@{$candidates[0]->{'configs'}}) {
+        return $ret if ( grep { $_ == $project_id} @{ $config->{'context'}->{'project_ids'} } )
     }
-    return $candidates[0];
+
+    return -3;
 }
 
 =head2 B<getPossibleTestStatuses()>

+ 12 - 1
t/TestRail-API-mockOnly.t

@@ -3,7 +3,7 @@ use warnings;
 
 #Test things we can only mock, because the API doesn't support them.
 
-use Test::More 'tests' => 5;
+use Test::More 'tests' => 9;
 use TestRail::API;
 use Test::LWP::UserAgent::TestRailMock;
 use Scalar::Util qw{reftype};
@@ -13,6 +13,7 @@ my $tr = TestRail::API->new('http://hokum.bogus','fake','fake',1);
 $tr->{'browser'} = $browser;
 $tr->{'debug'} = 0;
 
+#Have to mock anything requiring configs
 my $project = $tr->getProjectByName('TestProject');
 my $plan    = $tr->getPlanByName($project->{'id'},'HooHaaPlan');
 my $runs = $tr->getChildRuns($plan);
@@ -23,3 +24,13 @@ my $summary = $tr->getPlanSummary($plan->{'id'});
 is($summary->{'plan'},1094,"Plan ID makes it through in summary method");
 is($summary->{'totals'}->{'untested'},4,"Gets total number of tests correctly");
 is($summary->{'percentages'}->{'untested'},'100.00%',"Gets total percentages correctly");
+
+#Also have to mock anything requiring test result fields (all are custom)
+my $projResType = $tr->getTestResultFieldByName('step_results');
+is($projResType->{'id'},6,"Can get result field by name");
+$projResType = $tr->getTestResultFieldByName('step_results',$project->{'id'});
+is($projResType->{'id'},6,"Can get result field by name, AND filter by project ID");
+$projResType = $tr->getTestResultFieldByName('moo_results');
+is($projResType,0,"Bad name returns no result field");
+$projResType = $tr->getTestResultFieldByName('step_results',66669);
+is($projResType,-3,"Bad project returns no result field");

+ 1 - 0
t/TestRail-API.t

@@ -152,6 +152,7 @@ my $resTypes = $tr->getTestResultFields();
 my $statusTypes = $tr->getPossibleTestStatuses();
 ok($resTypes,"Can get test result fields");
 ok($statusTypes,"Can get possible test statuses");
+
 my @status_names = map {$_->{'name'}} @$statusTypes;
 my @status_ids = map {$_->{'id'}} @$statusTypes;
 my @computed_ids = $tr->statusNamesToIds(@status_names);

+ 2 - 1
t/arg_types.t

@@ -2,7 +2,7 @@ use strict;
 use warnings;
 
 use TestRail::API;
-use Test::More 'tests' => 138;
+use Test::More 'tests' => 139;
 use Test::Fatal;
 use Class::Inspector;
 use Test::LWP::UserAgent;
@@ -52,6 +52,7 @@ isnt( exception {$tr->sectionNamesToIds() },undef,'sectionNamesToIds returns err
 isnt( exception {$tr->getTestByID() },undef,'getTestByID returns error when no arguments are passed');
 isnt( exception {$tr->getTestByName() },undef,'getTestByName returns error when no arguments are passed');
 isnt( exception {$tr->getTestResults() },undef,'getTestResults returns error when no arguments are passed');
+isnt( exception {$tr->getTestResultFieldByName() },undef,'getTestResultFieldByName returns error when no arguments are passed');
 isnt( exception {$tr->getTestSuiteByID() },undef,'getTestSuiteByID returns error when no arguments are passed');
 isnt( exception {$tr->getTestSuiteByName() },undef,'getTestSuiteByName returns error when no arguments are passed');
 isnt( exception {$tr->getUserByEmail() },0,'getUserByEmail returns error when no arguments are passed');