Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
malandis committed Sep 4, 2024
1 parent e7453a6 commit f3d0076
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ public Mono<String> ltrim(K k, long l, long l1) {
ValidatorUtils.ensureInIntegerRange(l1, "l1");

// When the range is empty, Redis dictates that the list should be deleted.
if (l >= 0 && l1 >= 0 && l > l1 || l < 0 && l1 < 0 && l < l1) {
if (((l >= 0 && l1 >= 0) || (l < 0 && l1 < 0)) && l > l1) {
return unlink(k).then(Mono.just(RedisResponse.OK));
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/momento/lettuce/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void testLTrimShouldDeleteListOnEmptyRange() {
var key = randomString();
var values = generateListOfRandomStrings(10);

// Positive offsets
var lPushResponse = client.lpush(key, values.toArray(new String[0])).block();
assertEquals(10, lPushResponse);

Expand All @@ -145,6 +146,18 @@ public void testLTrimShouldDeleteListOnEmptyRange() {
// Verify the list is empty
var lRangeResponse = client.lrange(key, 0, -1).collectList().block();
assertEquals(0, lRangeResponse.size());

// Negative offsets
lPushResponse = client.lpush(key, values.toArray(new String[0])).block();
assertEquals(10, lPushResponse);

// Trim on an empty range deletes the list
lTrimResponse = client.ltrim(key, -4, -5).block();
assertEquals("OK", lTrimResponse);

// Verify the list is empty
lRangeResponse = client.lrange(key, 0, -1).collectList().block();
assertEquals(0, lRangeResponse.size());
}

@Test
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/momento/lettuce/utils/RangeUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package momento.lettuce.utils;

import io.lettuce.core.ExpireArgs;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RangeUtilsTest {
@Test
public void testAdjustEndRangeFromInclusiveToExclusive() {
assertEquals(3, RangeUtils.adjustEndRangeFromInclusiveToExclusive(2));
assertEquals(null, RangeUtils.adjustEndRangeFromInclusiveToExclusive(-1));
assertEquals(-1, RangeUtils.adjustEndRangeFromInclusiveToExclusive(-2));
}
}

0 comments on commit f3d0076

Please sign in to comment.