Skip to content

Commit

Permalink
fix: add tests for sortedSetFetchByScore, fix minor issue with 0 min/…
Browse files Browse the repository at this point in the history
…max scores (#328)

* fix: add tests for sortedSetFetchByScore, fix minor issue with 0 min/max scores

Adds tests for sortedSetFetchByScore.  Fixes a minor issue where
if min/max score were explicitly set to 0, we weren't passing them
through to the request because they evaluated as "false-y" when
we checked to see if they had been provided.
  • Loading branch information
cprice404 authored Mar 8, 2023
1 parent a60b8a0 commit e280266
Show file tree
Hide file tree
Showing 3 changed files with 508 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/internal/cache-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
validateSortedSetOffset,
validateSortedSetCount,
validateSortedSetIndices,
validateSortedSetScores,
} from './utils/validators';
import {SimpleCacheClientProps} from '../simple-cache-client-props';
import {Middleware} from '../config/middleware/middleware';
Expand Down Expand Up @@ -1922,6 +1923,7 @@ export class CacheClient {
try {
validateCacheName(cacheName);
validateSortedSetName(sortedSetName);
validateSortedSetScores(minScore, maxScore);
if (offset !== undefined) {
validateSortedSetOffset(offset);
}
Expand Down Expand Up @@ -1968,7 +1970,7 @@ export class CacheClient {
count?: number
): Promise<CacheSortedSetFetch.Response> {
const by_score = new grpcCache._SortedSetFetchRequest._ByScore();
if (minScore) {
if (minScore !== undefined) {
by_score.min_score = new grpcCache._SortedSetFetchRequest._ByScore._Score(
{
score: minScore,
Expand All @@ -1978,7 +1980,7 @@ export class CacheClient {
} else {
by_score.unbounded_min = new grpcCache._Unbounded();
}
if (maxScore) {
if (maxScore !== undefined) {
by_score.max_score = new grpcCache._SortedSetFetchRequest._ByScore._Score(
{
score: maxScore,
Expand Down
10 changes: 10 additions & 0 deletions src/internal/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export function validateSortedSetIndices(
}
}

export function validateSortedSetScores(minScore?: number, maxScore?: number) {
if (minScore === undefined) return;
if (maxScore === undefined) return;
if (minScore > maxScore) {
throw new InvalidArgumentError(
'minScore must be less than or equal to maxScore'
);
}
}

export function validateSortedSetOffset(offset: number) {
if (offset < 0) {
throw new InvalidArgumentError('offset must be non-negative (>= 0)');
Expand Down
Loading

0 comments on commit e280266

Please sign in to comment.