|
| 1 | +package com.redis.om.spring.annotations.hash; |
| 2 | + |
| 3 | +import com.redis.om.spring.AbstractBaseEnhancedRedisTest; |
| 4 | +import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics; |
| 5 | +import com.redis.om.spring.fixtures.hash.repository.HashWithAllTheNumericsRepository; |
| 6 | +import com.redis.om.spring.search.stream.EntityStream; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | + |
| 11 | +import java.math.BigDecimal; |
| 12 | +import java.math.BigInteger; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +@SuppressWarnings("SpellCheckingInspection") |
| 19 | +class HashWithAllTheNumericsMappingTest extends AbstractBaseEnhancedRedisTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + HashWithAllTheNumericsRepository repo; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + EntityStream es; |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void createTestDataIfNeeded() { |
| 29 | + HashWithAllTheNumerics hwatn1 = HashWithAllTheNumerics.of("hash1", 1.0f, 1.0, BigInteger.valueOf(1), |
| 30 | + BigDecimal.valueOf(1.0)); |
| 31 | + HashWithAllTheNumerics hwatn2 = HashWithAllTheNumerics.of("hash2", 2.0f, 2.0, BigInteger.valueOf(2), |
| 32 | + BigDecimal.valueOf(2.0)); |
| 33 | + HashWithAllTheNumerics hwatn3 = HashWithAllTheNumerics.of("hash3", 3.0f, 3.0, BigInteger.valueOf(3), |
| 34 | + BigDecimal.valueOf(3.0)); |
| 35 | + HashWithAllTheNumerics hwatn4 = HashWithAllTheNumerics.of("hash4", 4.0f, 4.0, BigInteger.valueOf(4), |
| 36 | + BigDecimal.valueOf(4.0)); |
| 37 | + HashWithAllTheNumerics hwatn5 = HashWithAllTheNumerics.of("hash5", 5.0f, 5.0, BigInteger.valueOf(5), |
| 38 | + BigDecimal.valueOf(5.0)); |
| 39 | + HashWithAllTheNumerics hwatn6 = HashWithAllTheNumerics.of("hash6", 6.0f, 6.0, BigInteger.valueOf(6), |
| 40 | + BigDecimal.valueOf(6.0)); |
| 41 | + repo.saveAll(Set.of(hwatn1, hwatn2, hwatn3, hwatn4, hwatn5, hwatn6)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testValuesAreStoredCorrectly() { |
| 46 | + for (int i = 1; i <= 6; i++) { |
| 47 | + String id = "hash" + i; |
| 48 | + Optional<HashWithAllTheNumerics> hwatn = repo.findById(id); |
| 49 | + |
| 50 | + assertThat(hwatn).isPresent(); |
| 51 | + |
| 52 | + HashWithAllTheNumerics entity = hwatn.get(); |
| 53 | + assertThat(entity.getId()).isEqualTo(id); |
| 54 | + assertThat(entity.getAfloat()).isEqualTo((float) i); |
| 55 | + assertThat(entity.getAdouble()).isEqualTo((double) i); |
| 56 | + assertThat(entity.getAbigInteger()).isEqualTo(BigInteger.valueOf(i)); |
| 57 | + assertThat(entity.getAbigDecimal()).isEqualTo(new BigDecimal(i + ".0")); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testFindByAFloatBetween() { |
| 63 | + Iterable<HashWithAllTheNumerics> result = repo.findByAfloatBetween(2.5f, 4.5f); |
| 64 | + assertThat(result).hasSize(2); |
| 65 | + assertThat(result).extracting(HashWithAllTheNumerics::getAfloat) |
| 66 | + .containsExactlyInAnyOrder(3.0f, 4.0f); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testFindByADoubleBetween() { |
| 71 | + Iterable<HashWithAllTheNumerics> result = repo.findByAdoubleBetween(3.5, 5.5); |
| 72 | + |
| 73 | + assertThat(result).hasSize(2); |
| 74 | + assertThat(result).extracting(HashWithAllTheNumerics::getAdouble) |
| 75 | + .containsExactlyInAnyOrder(4.0, 5.0); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testFindByABigDecimalBetween() { |
| 80 | + Iterable<HashWithAllTheNumerics> result = repo.findByAbigDecimalBetween( |
| 81 | + new BigDecimal("1.5"), new BigDecimal("3.5")); |
| 82 | + |
| 83 | + assertThat(result).hasSize(2); |
| 84 | + assertThat(result).extracting(HashWithAllTheNumerics::getAbigDecimal) |
| 85 | + .containsExactlyInAnyOrder(new BigDecimal("2.0"), new BigDecimal("3.0")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testFindByABigIntegerBetween() { |
| 90 | + Iterable<HashWithAllTheNumerics> result = repo.findByAbigIntegerBetween( |
| 91 | + BigInteger.valueOf(4), BigInteger.valueOf(6)); |
| 92 | + |
| 93 | + assertThat(result).hasSize(3); |
| 94 | + assertThat(result).extracting(HashWithAllTheNumerics::getAbigInteger) |
| 95 | + .containsExactlyInAnyOrder(BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6)); |
| 96 | + } |
| 97 | +} |
0 commit comments