Ver código fonte

Work on #118: Cache the list of tests in runs when doing getTestByName

George S. Baugh 8 anos atrás
pai
commit
01c8155aec
2 arquivos alterados com 14 adições e 1 exclusões
  1. 1 0
      Changes
  2. 13 1
      lib/TestRail/API.pm

+ 1 - 0
Changes

@@ -4,6 +4,7 @@ Revision history for Perl module TestRail::API
     - Fix issue where follow_post_redirect could not be passed to constructor
     - Add getRunResults, getRunResultsPaginated Methods in TestRail::API
     - Cache connections by default for speedups
+    - Cache getTests() for getTestByName()
 
 0.038 2017-01-23 TEODESIAN
     - Optimize TestRail::Utils::Find::getResults and testrail-results

+ 13 - 1
lib/TestRail/API.pm

@@ -1982,6 +1982,11 @@ sub getTests {
     $query_string = '&status_id='.join(',',@$status_ids) if defined($status_ids) && scalar(@$status_ids);
     my $results = $self->_doRequest("index.php?/api/v2/get_tests/$run_id$query_string");
     @$results = grep {my $aid = $_->{'assignedto_id'}; grep {defined($aid) && $aid == $_} @$assignedto_ids} @$results if defined($assignedto_ids) && scalar(@$assignedto_ids);
+
+    #Cache stuff for getTestByName
+    $self->{tests_cache} //= {};
+    $self->{tests_cache}->{$run_id} = $results;
+
     return $results;
 }
 
@@ -1989,6 +1994,10 @@ sub getTests {
 
 Gets specified test by name.
 
+This is done by getting the list of all tests in the run and then picking out the relevant test.
+As such, for efficiency the list of tests is cached.
+The cache may be refreshed, or restricted by running getTests (with optional restrictions, such as assignedto_ids, etc).
+
 =over 4
 
 =item INTEGER C<RUN ID> - ID of parent run.
@@ -2007,7 +2016,10 @@ sub getTestByName {
     state $check = compile(Object, Int, Str);
     my ($self,$run_id,$name) = $check->(@_);
 
-    my $tests = $self->getTests($run_id);
+    $self->{tests_cache} //= {};
+    my $tests = $self->{tests_cache}->{$run_id};
+
+    $tests = $self->getTests($run_id) if !$tests;
     return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
     foreach my $test (@$tests) {
         return $test if $test->{'title'} eq $name;