From f3d0076cce09541881bf9790eff6b489b7359c36 Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Wed, 4 Sep 2024 12:59:24 -0700 Subject: [PATCH] test: add more tests --- .../lettuce/MomentoRedisReactiveClient.java | 2 +- src/test/java/momento/lettuce/ListTest.java | 13 +++++++++++++ .../momento/lettuce/utils/RangeUtilsTest.java | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/test/java/momento/lettuce/utils/RangeUtilsTest.java diff --git a/src/main/java/momento/lettuce/MomentoRedisReactiveClient.java b/src/main/java/momento/lettuce/MomentoRedisReactiveClient.java index ed732ed..5a864e9 100644 --- a/src/main/java/momento/lettuce/MomentoRedisReactiveClient.java +++ b/src/main/java/momento/lettuce/MomentoRedisReactiveClient.java @@ -1582,7 +1582,7 @@ public Mono 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)); } diff --git a/src/test/java/momento/lettuce/ListTest.java b/src/test/java/momento/lettuce/ListTest.java index daa5089..120240b 100644 --- a/src/test/java/momento/lettuce/ListTest.java +++ b/src/test/java/momento/lettuce/ListTest.java @@ -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); @@ -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 diff --git a/src/test/java/momento/lettuce/utils/RangeUtilsTest.java b/src/test/java/momento/lettuce/utils/RangeUtilsTest.java new file mode 100644 index 0000000..10bf63c --- /dev/null +++ b/src/test/java/momento/lettuce/utils/RangeUtilsTest.java @@ -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)); + } +}