diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/HashWithAllTheNumericsMappingTest.java b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/HashWithAllTheNumericsMappingTest.java new file mode 100644 index 00000000..71ed6ca4 --- /dev/null +++ b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/HashWithAllTheNumericsMappingTest.java @@ -0,0 +1,97 @@ +package com.redis.om.spring.annotations.hash; + +import com.redis.om.spring.AbstractBaseEnhancedRedisTest; +import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics; +import com.redis.om.spring.fixtures.hash.repository.HashWithAllTheNumericsRepository; +import com.redis.om.spring.search.stream.EntityStream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Optional; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +@SuppressWarnings("SpellCheckingInspection") +class HashWithAllTheNumericsMappingTest extends AbstractBaseEnhancedRedisTest { + + @Autowired + HashWithAllTheNumericsRepository repo; + + @Autowired + EntityStream es; + + @BeforeEach + void createTestDataIfNeeded() { + HashWithAllTheNumerics hwatn1 = HashWithAllTheNumerics.of("hash1", 1.0f, 1.0, BigInteger.valueOf(1), + BigDecimal.valueOf(1.0)); + HashWithAllTheNumerics hwatn2 = HashWithAllTheNumerics.of("hash2", 2.0f, 2.0, BigInteger.valueOf(2), + BigDecimal.valueOf(2.0)); + HashWithAllTheNumerics hwatn3 = HashWithAllTheNumerics.of("hash3", 3.0f, 3.0, BigInteger.valueOf(3), + BigDecimal.valueOf(3.0)); + HashWithAllTheNumerics hwatn4 = HashWithAllTheNumerics.of("hash4", 4.0f, 4.0, BigInteger.valueOf(4), + BigDecimal.valueOf(4.0)); + HashWithAllTheNumerics hwatn5 = HashWithAllTheNumerics.of("hash5", 5.0f, 5.0, BigInteger.valueOf(5), + BigDecimal.valueOf(5.0)); + HashWithAllTheNumerics hwatn6 = HashWithAllTheNumerics.of("hash6", 6.0f, 6.0, BigInteger.valueOf(6), + BigDecimal.valueOf(6.0)); + repo.saveAll(Set.of(hwatn1, hwatn2, hwatn3, hwatn4, hwatn5, hwatn6)); + } + + @Test + void testValuesAreStoredCorrectly() { + for (int i = 1; i <= 6; i++) { + String id = "hash" + i; + Optional hwatn = repo.findById(id); + + assertThat(hwatn).isPresent(); + + HashWithAllTheNumerics entity = hwatn.get(); + assertThat(entity.getId()).isEqualTo(id); + assertThat(entity.getAfloat()).isEqualTo((float) i); + assertThat(entity.getAdouble()).isEqualTo((double) i); + assertThat(entity.getAbigInteger()).isEqualTo(BigInteger.valueOf(i)); + assertThat(entity.getAbigDecimal()).isEqualTo(new BigDecimal(i + ".0")); + } + } + + @Test + void testFindByAFloatBetween() { + Iterable result = repo.findByAfloatBetween(2.5f, 4.5f); + assertThat(result).hasSize(2); + assertThat(result).extracting(HashWithAllTheNumerics::getAfloat) + .containsExactlyInAnyOrder(3.0f, 4.0f); + } + + @Test + void testFindByADoubleBetween() { + Iterable result = repo.findByAdoubleBetween(3.5, 5.5); + + assertThat(result).hasSize(2); + assertThat(result).extracting(HashWithAllTheNumerics::getAdouble) + .containsExactlyInAnyOrder(4.0, 5.0); + } + + @Test + void testFindByABigDecimalBetween() { + Iterable result = repo.findByAbigDecimalBetween( + new BigDecimal("1.5"), new BigDecimal("3.5")); + + assertThat(result).hasSize(2); + assertThat(result).extracting(HashWithAllTheNumerics::getAbigDecimal) + .containsExactlyInAnyOrder(new BigDecimal("2.0"), new BigDecimal("3.0")); + } + + @Test + void testFindByABigIntegerBetween() { + Iterable result = repo.findByAbigIntegerBetween( + BigInteger.valueOf(4), BigInteger.valueOf(6)); + + assertThat(result).hasSize(3); + assertThat(result).extracting(HashWithAllTheNumerics::getAbigInteger) + .containsExactlyInAnyOrder(BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6)); + } +} diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/HashWithAllTheNumerics.java b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/HashWithAllTheNumerics.java new file mode 100644 index 00000000..529f9d06 --- /dev/null +++ b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/HashWithAllTheNumerics.java @@ -0,0 +1,35 @@ +package com.redis.om.spring.fixtures.hash.model; + +import com.redis.om.spring.annotations.Indexed; +import lombok.*; +import org.springframework.data.annotation.Id; +import org.springframework.data.redis.core.RedisHash; + +import java.math.BigDecimal; +import java.math.BigInteger; + +@Data +@NoArgsConstructor(force = true) +@RequiredArgsConstructor(staticName = "of") +@RedisHash +public class HashWithAllTheNumerics { + @Id + @NonNull + private String id; + + @NonNull + @Indexed + private Float afloat; + + @NonNull + @Indexed + private Double adouble; + + @NonNull + @Indexed + private BigInteger abigInteger; + + @NonNull + @Indexed + private BigDecimal abigDecimal; +} diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/repository/HashWithAllTheNumericsRepository.java b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/repository/HashWithAllTheNumericsRepository.java new file mode 100644 index 00000000..9145ec96 --- /dev/null +++ b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/repository/HashWithAllTheNumericsRepository.java @@ -0,0 +1,17 @@ +package com.redis.om.spring.fixtures.hash.repository; + +import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics; +import com.redis.om.spring.repository.RedisEnhancedRepository; + +import java.math.BigDecimal; +import java.math.BigInteger; + +public interface HashWithAllTheNumericsRepository extends RedisEnhancedRepository { + Iterable findByAfloatBetween(Float low, Float high); + Iterable findByAdoubleBetween(Double low, Double high); + Iterable findByAbigDecimalBetween(BigDecimal low, BigDecimal high); + Iterable findByAbigIntegerBetween(BigInteger low, BigInteger high); +} + + +