Переглянути джерело

Add support for the TestRail get_priorities method

Matthew Spahr 4 роки тому
батько
коміт
1cefd30fa0
4 змінених файлів з 114 додано та 2 видалено
  1. 74 0
      lib/TestRail/API.pm
  2. 10 1
      t/TestRail-API.t
  3. 3 1
      t/arg_types.t
  4. 27 0
      t/lib/Test/LWP/UserAgent/TestRailMock.pm

+ 74 - 0
lib/TestRail/API.pm

@@ -1219,6 +1219,80 @@ sub addCaseField {
     return $self->_doRequest("index.php?/api/v2/add_case_field", 'POST', $options);
 }
 
+=head1 PRIORITY METHODS
+
+=head2 B<getPriorities ()>
+
+Gets possible priorities.
+
+Returns ARRAYREF of priority definition HASHREFs.
+
+    $tr->getPriorities();
+
+=cut
+
+sub getPriorities {
+    state $check = compile(Object);
+    my ($self) = $check->(@_);
+    return clone($self->{'priority_cache'}) if defined($self->{'priority_cache'});
+
+    my $priorities = $self->_doRequest("index.php?/api/v2/get_priorities");
+    return -500 if !$priorities || (reftype($priorities) || 'undef') ne 'ARRAY';
+    $self->{'priority_cache'} = $priorities;
+
+    return clone $priorities;
+}
+
+=head2 B<getPriorityByName (name)>
+
+Gets priority by name.
+
+=over 4
+
+=item STRING C<NAME> - Name of desired priority
+
+=back
+
+Returns priority definition HASHREF.
+Dies if named priority does not exist.
+
+    $tr->getPriorityByName();
+
+=cut
+
+sub getPriorityByName {
+    state $check = compile(Object, Str);
+    my ($self,$name) = $check->(@_);
+
+    my $priorities = $self->getPriorities();
+    return -500 if !$priorities || (reftype($priorities) || 'undef') ne 'ARRAY';
+    foreach my $priority (@$priorities) {
+        return $priority if $priority->{'name'} eq $name;
+    }
+    confess("No such priority '$name'!");
+}
+
+=head2 priorityNamesToIds(names)
+
+Convenience method to translate a list of priority names to TestRail priority IDs.
+
+=over 4
+
+=item ARRAY C<NAMES> - Array of priority names to translate to IDs.
+
+=back
+
+Returns ARRAY of priority IDs in the same order as the priority names passed.
+
+Throws an exception in the case of one (or more) of the names not corresponding to a valid priority.
+
+=cut
+
+sub priorityNamesToIds {
+    my ($self,@names) = @_;
+    return _X_in_my_Y($self,$self->getPriorities(),'id',@names);
+};
+
 =head1 RUN METHODS
 
 =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>

+ 10 - 1
t/TestRail-API.t

@@ -7,7 +7,7 @@ use lib "$FindBin::Bin/lib";
 use TestRail::API;
 use Test::LWP::UserAgent::TestRailMock;
 
-use Test::More tests => 91;
+use Test::More tests => 94;
 use Test::Fatal;
 use Test::Deep;
 use Scalar::Util ();
@@ -72,6 +72,15 @@ is($tr->getCaseTypeByName($type_names[0])->{'id'},$type_ids[0],"Can get case typ
 my @computed_type_ids = $tr->typeNamesToIds(@type_names);
 cmp_deeply(\@computed_type_ids,\@type_ids,"typeNamesToIds returns the correct type IDs in the correct order");
 
+#Test PRIORITY method
+my $priorities = $tr->getPriorities();
+is(ref($priorities),'ARRAY',"getPriorities returns ARRAY of priorities");
+my @priority_names = map {$_->{'name'}} @$priorities;
+my @priority_ids = map {$_->{'id'}} @$priorities;
+is($tr->getPriorityByName($priority_names[0])->{'id'},$priority_ids[0],"Can get case priority by name correctly");
+my @computed_priority_ids = $tr->priorityNamesToIds(@priority_names);
+cmp_deeply(\@computed_priority_ids,\@priority_ids,"priorityNamesToIds returns the correct priority IDs in the correct order");
+
 #Test PROJECT methods
 my $project_name = 'CRUSH ALL HUMANS';
 

+ 3 - 1
t/arg_types.t

@@ -2,7 +2,7 @@ use strict;
 use warnings;
 
 use TestRail::API;
-use Test::More 'tests' => 143;
+use Test::More 'tests' => 145;
 use Test::Fatal;
 use Class::Inspector;
 use Test::LWP::UserAgent;
@@ -16,6 +16,7 @@ $tr->{'browser'}->map_response(qr/.*/, HTTP::Response->new('500', 'ERROR', ['Con
 #No-arg functions
 is( exception { capture { $tr->getCaseTypes()  } },undef,'getCaseTypes returns no error when no arguments are passed');
 is( exception { capture { $tr->getPossibleTestStatuses()  } },undef,'getPossibleTestStatuses returns no error when no arguments are passed');
+is( exception { capture { $tr->getPriorities()  } },undef,'getPriorities returns no error when no arguments are passed');
 is( exception { capture { $tr->getProjects()  } },undef,'getProjects returns no error when no arguments are passed');
 is( exception { capture { $tr->getTestResultFields()  } },undef,'getTestResultFields returns no error when no arguments are passed');
 is( exception { capture { $tr->getUsers()  } },undef,'getUsers returns no error when no arguments are passed');
@@ -43,6 +44,7 @@ isnt( exception { capture { $tr->getMilestoneByID()  } },undef,'getMilestoneByID
 isnt( exception { capture { $tr->getMilestoneByName()  } },undef,'getMilestoneByName returns error when no arguments are passed');
 isnt( exception { capture { $tr->getPlanByID()  } },undef,'getPlanByID returns error when no arguments are passed');
 isnt( exception { capture { $tr->getPlanByName()  } },undef,'getPlanByName returns error when no arguments are passed');
+isnt( exception { capture { $tr->getPriorityByName()  } },undef,'getPriorityByName returns error when no arguments are passed');
 isnt( exception { capture { $tr->getProjectByID()  } },undef,'getProjectByID returns error when no arguments are passed');
 isnt( exception { capture { $tr->getProjectByName()  } },undef,'getProjectByName returns error when no arguments are passed');
 isnt( exception { capture { $tr->getRunByID()  } },undef,'getRunByID returns error when no arguments are passed');

+ 27 - 0
t/lib/Test/LWP/UserAgent/TestRailMock.pm

@@ -2488,6 +2488,33 @@ $mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4,
 
 }
 
+{
+
+$VAR1 = 'index.php?/api/v2/get_priorities';
+$VAR2 = '200';
+$VAR3 = 'OK';
+$VAR4 = bless( {
+                 'connection' => 'close',
+                 'x-powered-by' => 'PHP/5.5.9-1ubuntu4.9',
+                 'client-response-num' => 1,
+                 'date' => 'Tue, 21 Sep 2021 20:59:52 GMT',
+                 'client-peer' => '192.168.122.217:80',
+                 'content-length' => '173',
+                 '::std_case' => {
+                                   'client-date' => 'Client-Date',
+                                   'x-powered-by' => 'X-Powered-By',
+                                   'client-response-num' => 'Client-Response-Num',
+                                   'client-peer' => 'Client-Peer'
+                                 },
+                 'client-date' => 'Thu, 16 Jul 2015 20:59:52 GMT',
+                 'content-type' => 'application/json; charset=utf-8',
+                 'server' => 'Apache/2.4.7 (Ubuntu)'
+               }, 'HTTP::Headers' );
+$VAR5 = '[{"id":1,"is_default":true,"name":"1 - Critical","priority":1,"short_name":"1 - Do"},{"id":4,"is_default":false,"name":"2 - Whatever","priority":2,"short_name":"2 - Don\'t"}]';
+$mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));
+
+}
+
 #Lock Mocks
 
 {