Skip to content

Commit d03b52b

Browse files
committed
test: add tests for numeric data types in repository queries
1 parent 524b8d1 commit d03b52b

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.redis.om.spring.fixtures.hash.model;
2+
3+
import com.redis.om.spring.annotations.Indexed;
4+
import lombok.*;
5+
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.redis.core.RedisHash;
7+
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
10+
11+
@Data
12+
@NoArgsConstructor(force = true)
13+
@RequiredArgsConstructor(staticName = "of")
14+
@RedisHash
15+
public class HashWithAllTheNumerics {
16+
@Id
17+
@NonNull
18+
private String id;
19+
20+
@NonNull
21+
@Indexed
22+
private Float afloat;
23+
24+
@NonNull
25+
@Indexed
26+
private Double adouble;
27+
28+
@NonNull
29+
@Indexed
30+
private BigInteger abigInteger;
31+
32+
@NonNull
33+
@Indexed
34+
private BigDecimal abigDecimal;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.redis.om.spring.fixtures.hash.repository;
2+
3+
import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics;
4+
import com.redis.om.spring.repository.RedisEnhancedRepository;
5+
6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
9+
public interface HashWithAllTheNumericsRepository extends RedisEnhancedRepository<HashWithAllTheNumerics, String> {
10+
Iterable<HashWithAllTheNumerics> findByAfloatBetween(Float low, Float high);
11+
Iterable<HashWithAllTheNumerics> findByAdoubleBetween(Double low, Double high);
12+
Iterable<HashWithAllTheNumerics> findByAbigDecimalBetween(BigDecimal low, BigDecimal high);
13+
Iterable<HashWithAllTheNumerics> findByAbigIntegerBetween(BigInteger low, BigInteger high);
14+
}
15+
16+
17+

0 commit comments

Comments
 (0)