ソースを参照

Fix #73 - Add ability to pass arbitrary filters to getCases

George S. Baugh 10 年 前
コミット
f1c0b3ba5f
6 ファイル変更55 行追加25 行削除
  1. 4 0
      Changes
  2. 1 1
      lib/Test/Rail/Parser.pm
  3. 32 13
      lib/TestRail/API.pm
  4. 11 3
      t/TestRail-API.t
  5. 5 6
      t/arg_types.t
  6. 2 2
      t/server_dead.t

+ 4 - 0
Changes

@@ -1,5 +1,9 @@
 Revision history for Perl module TestRail::API
 
+0.031 2015-08-14 TEODESIAN
+    - Update getCases to use testRail 4.0 filters, change filter args to HASHREF
+    - Update TestRail::API::getCaseByName to take filter hashref too
+
 0.030 2015-07-31 TEODESIAN
     - Fix testrail-tests, was calling function in incorrect namespace
     - Fix testrail-bulk-mark-results, was not including library

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

@@ -231,7 +231,7 @@ sub new {
             confess("Sections passed to spawn must be ARRAYREF") unless (reftype($tropts->{'sections'}) || 'undef') eq 'ARRAY';
             @{$tropts->{'sections'}} = $tr->sectionNamesToIds($tropts->{'project_id'},$tropts->{'spawn'},@{$tropts->{'sections'}});
             foreach my $section (@{$tropts->{'sections'}}) {
-                my $section_cases = $tr->getCases($tropts->{'project_id'},$tropts->{'spawn'},$section);
+                my $section_cases = $tr->getCases($tropts->{'project_id'},$tropts->{'spawn'},{ 'section_id' => $section });
                 push(@$cases,@$section_cases) if (reftype($section_cases) || 'undef') eq 'ARRAY';
             }
         }

+ 32 - 13
lib/TestRail/API.pm

@@ -921,7 +921,7 @@ sub deleteCase {
     return $result;
 }
 
-=head2 B<getCases (project_id,suite_id,section_id)>
+=head2 B<getCases (project_id,suite_id,filters)>
 
 Gets cases for provided section.
 
@@ -931,26 +931,45 @@ Gets cases for provided section.
 
 =item INTEGER C<SUITE ID> - ID of parent suite.
 
-=item INTEGER C<SECTION ID> - ID of parent section
+=item HASHREF C<FILTERS> (optional) - HASHREF describing parameters to filter cases by.
 
 =back
 
+See:
+
+    L<http://docs.gurock.com/testrail-api2/reference-cases#get_cases>
+
+for details as to the allowable filter keys.
+
+If the section ID is omitted, all cases for the suite will be returned.
 Returns ARRAYREF of test case definition HASHREFs.
 
-    $tr->getCases(1,2,3);
+    $tr->getCases(1,2, {'section_id' => 3} );
 
 =cut
 
 sub getCases {
-    state $check = compile(Object, Int, Int, Int);
-    my ($self,$project_id,$suite_id,$section_id) = $check->(@_);
+    state $check = compile(Object, Int, Int, Optional[Maybe[HashRef]]);
+    my ($self,$project_id,$suite_id,$filters) = $check->(@_);
 
     my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
-    $url .= "&section_id=$section_id" if $section_id;
+
+    my @valid_keys = qw{section_id created_after created_before created_by milestone_id priority_id type_id updated_after updated_before updated_by};
+
+    # Add in filters
+    foreach my $filter (keys(%$filters)) {
+        confess("Invalid filter key '$filter' passed") unless grep {$_ eq $filter} @valid_keys;
+        if (ref $filters->{$filter} eq 'ARRAY') {
+            $url .= "&$filter=".join(',',$filters->{$filter});
+        } else {
+            $url .= "&$filter=".$filters->{$filter} if defined($filters->{$filter});
+        }
+    }
+
     return $self->_doRequest($url);
 }
 
-=head2 B<getCaseByName (project_id,suite_id,section_id,name)>
+=head2 B<getCaseByName (project_id,suite_id,name,filters)>
 
 Gets case by name.
 
@@ -960,23 +979,23 @@ Gets case by name.
 
 =item INTEGER C<SUITE ID> - ID of parent suite.
 
-=item INTEGER C<SECTION ID> - ID of parent section.
+=item STRING C<NAME> - Name of desired test case.
 
-=item STRING <NAME> - Name of desired test case.
+=item HASHREF C<FILTERS> - Filter dictionary acceptable to getCases.
 
 =back
 
 Returns test case definition HASHREF.
 
-    $tr->getCaseByName(1,2,3,'nugs');
+    $tr->getCaseByName(1,2,'nugs', {'section_id' => 3});
 
 =cut
 
 sub getCaseByName {
-    state $check = compile(Object, Int, Int, Int, Str);
-    my ($self,$project_id,$suite_id,$section_id,$name) = $check->(@_);
+    state $check = compile(Object, Int, Int, Str, Optional[Maybe[HashRef]]);
+    my ($self,$project_id,$suite_id,$name,$filters) = $check->(@_);
 
-    my $cases = $self->getCases($project_id,$suite_id,$section_id);
+    my $cases = $self->getCases($project_id,$suite_id,$filters);
     return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
     foreach my $case (@$cases) {
         return $case if $case->{'title'} eq $name;

+ 11 - 3
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 => 75;
+use Test::More tests => 76;
 use Test::Fatal;
 use Test::Deep;
 use Scalar::Util ();
@@ -101,10 +101,18 @@ my $case_name = 'STROGGIFY POPULATION CENTERS';
 my $new_case = $tr->createCase($new_section->{'id'},$case_name);
 is($new_case->{'title'},$case_name,"Can create new test case");
 
-ok($tr->getCases($new_project->{'id'},$new_suite->{'id'},$new_section->{'id'}),"Can get case listing");
-is($tr->getCaseByName($new_project->{'id'}, $new_suite->{'id'}, $new_section->{'id'}, $case_name)->{'title'},$case_name,"Can get case by name");
+my $case_filters = {
+    'section_id' => $new_section->{'id'}
+};
+
+ok($tr->getCases($new_project->{'id'},$new_suite->{'id'},$case_filters),"Can get case listing");
+is($tr->getCaseByName($new_project->{'id'}, $new_suite->{'id'},  $case_name, $case_filters)->{'title'},$case_name,"Can get case by name");
 is($tr->getCaseByID($new_case->{'id'})->{'id'},$new_case->{'id'},"Can get case by ID");
 
+#Negative case
+$case_filters->{'hokum'} = 'bogus';
+isnt(exception {$tr->getCases($new_project->{'id'},$new_suite->{'id'},$case_filters)},undef,"Passing bogus filter croaks");
+
 #Test RUN methods
 my $run_name = 'SEND T-1000 INFILTRATION UNITS BACK IN TIME';
 my $new_run = $tr->createRun($new_project->{'id'},$new_suite->{'id'},$run_name,"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE");

+ 5 - 6
t/arg_types.t

@@ -149,11 +149,11 @@ is(exception {$tr->createCase(1,'whee')}, undef,'createCase with 2 args returns
 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');
 
 isnt(exception {$tr->createRun(1,1)}, undef,'createRun with 2 args returns error');
 isnt(exception {$tr->createSection(1,1)}, undef,'createSection with 2 args returns error');
 isnt(exception {$tr->getCaseByName(1,1)}, undef,'getCaseByName with 2 args returns error');
-isnt(exception {$tr->getCases(1,2)}, undef,'getCases with 2 args returns error');
 isnt(exception {$tr->getSectionByName(1,1)}, undef,'getSectionByName with 2 args returns error');
 isnt(exception {$tr->sectionNamesToIds(1,1) },undef,'sectionNamesToIds returns error when 2 arguments are passed');
 isnt( exception {$tr->createRunInPlan(1,1) },undef,'createRunInPlan returns error when 2 arguments passed');
@@ -161,11 +161,10 @@ isnt( exception {$tr->createRunInPlan(1,1) },undef,'createRunInPlan returns erro
 #3 arg functions
 is(exception {$tr->createRun(1,1,'whee')}, undef,'createRun with 3 args returns no error');
 is(exception {$tr->createSection(1,1,'whee')}, undef,'createSection with 3 args returns no error');
-is(exception {$tr->getCases(1,2,3)}, undef,'getCases with 3 args returns no error');
+is(exception {$tr->getCases(1,2,{})}, undef,'getCases with 3 args returns no error');
 is(exception {$tr->getSectionByName(1,1,'zip')}, undef,'getSectionByName with 3 args returns no error');
-is( exception {$tr->createRunInPlan(1,1,'nugs') },undef,'createRunInPlan with 3 args returns no error');
-
-isnt(exception {$tr->getCaseByName(1,1,1)}, undef,'getCaseByName with 3 args returns error');
+is(exception {$tr->createRunInPlan(1,1,'nugs') },undef,'createRunInPlan with 3 args returns no error');
+is(exception {$tr->getCaseByName(1,1,'whee')}, undef,'getCaseByName with 3 args returns no error');
 
 #4 arg functions
-is(exception {$tr->getCaseByName(1,1,1,'hug')}, undef,'getCaseByName with 4 args returns no error');
+is(exception {$tr->getCaseByName(1,1,'hug', {})}, undef,'getCaseByName with 4 args returns no error');

+ 2 - 2
t/server_dead.t

@@ -36,10 +36,10 @@ is($tr->deleteRun(1),-500,'deleteRun returns error');
 is($tr->deleteSection(1),-500,'deleteSection returns error');
 is($tr->deleteTestSuite(1),-500,'deleteTestSuite returns error');
 is($tr->getCaseByID(1),-500,'getCaseByID returns error');
-is($tr->getCaseByName(1,1,1,'hug'),-500,'getCaseByName returns error');
+is($tr->getCaseByName(1,1,'hug'),-500,'getCaseByName returns error');
 is($tr->getCaseTypeByName('zap'),-500,'getCaseTypeByName returns error');
 is($tr->getCaseTypes(),-500,'getCaseTypes returns error');
-is($tr->getCases(1,2,3),-500,'getCases returns error');
+is($tr->getCases(1,2),-500,'getCases returns error');
 is($tr->getMilestoneByID(1),-500,'getMilestoneByID returns error');
 is($tr->getMilestoneByName(1,'hug'),-500,'getMilestoneByName returns error');
 is($tr->getMilestones(1),-500,'getMilestones returns error');