Skip to content

Commit

Permalink
fix: obs search reqs w/ spatial params should not return cached data (#…
Browse files Browse the repository at this point in the history
…457)

* fix: add more spatial params to obs search cache key

* fix: spatial params should not generate a cache key at all

* Helps to run all tests
  • Loading branch information
kueda authored Sep 15, 2024
1 parent 7f169fd commit ed2e7e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
8 changes: 2 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,12 @@ const util = class util {
if ( queryDup.order_by === "" ) {
delete queryDup.order_by;
}
if ( _.isEmpty( queryDup.lat ) && _.isEmpty( queryDup.lat ) ) {
delete queryDup.lat;
delete queryDup.lng;
delete queryDup.radius;
}

// only cacheable params are present, so generate a cache key
if ( _.isEmpty( queryDup ) && _.isEmpty( reqInatDup ) ) {
fileCacheKey = `${prefix}`;
_.each( cacheableParams, ( v, k ) => {
if ( v && !( k === "perPage" && options.ignorePagination ) ) {
if ( v !== null && v !== undefined && !( k === "perPage" && options.ignorePagination ) ) {
fileCacheKey += `-${k}-${v}`;
}
} );
Expand Down
25 changes: 22 additions & 3 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,19 @@ describe( "util", ( ) => {
} ) ).to.eq( "ObservationsController.search-returnBounds-true" );
} );

it( "allows queries with place_id to be cached for obs search", ( ) => {
function expectParamInCacheKey( paramKey, paramVal, paramCacheKey ) {
const req = {
query: {
place_id: 1
[paramKey]: paramVal
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search-placeID-1" );
} ) ).to.eq( `ObservationsController.search-${paramCacheKey}-${paramVal}` );
}

it( "allows queries with place_id to be cached for obs search", ( ) => {
expectParamInCacheKey( "place_id", 1, "placeID" );
} );

it( "does not allow queries with place_id to be cached for obs search when logged in", ( ) => {
Expand All @@ -203,6 +207,21 @@ describe( "util", ( ) => {
} ) ).to.be.null;
} );

function expectParamNotToGenerateCacheKey( paramKey, paramValue ) {
const req = { query: { [paramKey]: paramValue } };
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
}

it( "should not generate a key if lat in params", ( ) => expectParamNotToGenerateCacheKey( "lat", 1 ) );
it( "should not generate a key if lng in params", ( ) => expectParamNotToGenerateCacheKey( "lng", 1 ) );
it( "should not generate a key if radius in params", ( ) => expectParamNotToGenerateCacheKey( "radius", 1 ) );
it( "should not generate a key if swlat in params", ( ) => expectParamNotToGenerateCacheKey( "swlat", 1 ) );
it( "should not generate a key if swlng in params", ( ) => expectParamNotToGenerateCacheKey( "swlng", 1 ) );
it( "should not generate a key if nelat in params", ( ) => expectParamNotToGenerateCacheKey( "nelat", 1 ) );
it( "should not generate a key if nelng in params", ( ) => expectParamNotToGenerateCacheKey( "nelng", 1 ) );

it( "includes locale in cache key for obs search by default", ( ) => {
const req = {
query: {
Expand Down

0 comments on commit ed2e7e4

Please sign in to comment.