Skip to content

Commit c997246

Browse files
committed
test: add test cases for boolean and datetime query methods
Add test cases to verify that findByIsActive and findByRegistrationDateBetween query methods work correctly with indexed Boolean and LocalDateTime fields. - Add Doc5 model with indexed Boolean and LocalDateTime fields - Add Doc5Repository with query methods - Add test methods to RepositoryIssuesTest for these query types
1 parent 33958ad commit c997246

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
lines changed

tests/src/test/java/com/redis/om/spring/annotations/document/RepositoryIssuesTest.java

+68-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.redis.om.spring.annotations.document;
22

33
import com.redis.om.spring.AbstractBaseDocumentTest;
4-
import com.redis.om.spring.fixtures.document.model.SKU;
5-
import com.redis.om.spring.fixtures.document.model.Student;
6-
import com.redis.om.spring.fixtures.document.model.User2;
4+
import com.redis.om.spring.fixtures.document.model.*;
5+
import com.redis.om.spring.fixtures.document.repository.Doc5Repository;
76
import com.redis.om.spring.fixtures.document.repository.SKUCacheRepository;
87
import com.redis.om.spring.fixtures.document.repository.StudentRepository;
98
import com.redis.om.spring.fixtures.document.repository.User2Repository;
9+
import com.redis.om.spring.search.stream.EntityStream;
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.Test;
1212
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Set;
2121
import java.util.function.Function;
22+
import java.util.stream.Collectors;
2223

2324
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.junit.jupiter.api.Assertions.assertAll;
@@ -34,6 +35,12 @@ class RepositoryIssuesTest extends AbstractBaseDocumentTest {
3435
@Autowired
3536
StudentRepository studentRepository;
3637

38+
@Autowired
39+
private Doc5Repository doc5Repository;
40+
41+
@Autowired
42+
private EntityStream entityStream;
43+
3744
@BeforeEach
3845
void cleanUp() {
3946
repository.deleteAll();
@@ -54,6 +61,21 @@ void cleanUp() {
5461
students.add(student);
5562
}
5663
studentRepository.saveAll(students);
64+
65+
doc5Repository.deleteAll();
66+
67+
// Create test data
68+
Doc5 model1 = new Doc5();
69+
model1.setAge(30);
70+
model1.setRegistrationDate(LocalDateTime.now().minusDays(5));
71+
model1.setIsActive(true);
72+
73+
Doc5 model2 = new Doc5();
74+
model2.setAge(40);
75+
model2.setRegistrationDate(LocalDateTime.now().minusDays(10));
76+
model2.setIsActive(false);
77+
78+
doc5Repository.saveAll(List.of(model1, model2));
5779
}
5880

5981
// RediSearchQuery wrong preparedQuery #187
@@ -130,4 +152,47 @@ void testQBEWithAliasWithHyphensAndOrderBy() {
130152
Student result = studentRepository.findFirstByPropertyOrderByEventTimestamp(student, matcher, sortFunction);
131153
assertThat(result.getUserName()).isEqualTo("Student2");
132154
}
155+
156+
@Test
157+
void testFindByAge() {
158+
// This works fine
159+
List<Doc5> resultsByAge = doc5Repository.findByAge(30);
160+
161+
assertThat(resultsByAge).isNotEmpty();
162+
assertThat(resultsByAge).hasSize(1);
163+
assertThat(resultsByAge.get(0).getAge()).isEqualTo(30);
164+
}
165+
166+
@Test
167+
void testFindByDateBetween() {
168+
// LocalDateTime query test
169+
LocalDateTime from = LocalDateTime.now().minusDays(20);
170+
LocalDateTime to = LocalDateTime.now();
171+
172+
// First debug using EntityStream
173+
List<Doc5> resultsWithEntityStream = entityStream.of(Doc5.class)
174+
.filter(Doc5$.REGISTRATION_DATE.between(from, to))
175+
.collect(Collectors.toList());
176+
177+
// Now try repository method
178+
List<Doc5> resultsByDate = doc5Repository.findByRegistrationDateBetween(from, to);
179+
180+
assertThat(resultsByDate).isNotEmpty();
181+
assertThat(resultsByDate).hasSize(2);
182+
}
183+
184+
@Test
185+
void testFindByIsActive() {
186+
// First debug using EntityStream
187+
List<Doc5> resultsWithEntityStream = entityStream.of(Doc5.class)
188+
.filter(Doc5$.IS_ACTIVE.eq(true))
189+
.collect(Collectors.toList());
190+
191+
// Now try repository method
192+
List<Doc5> resultsByActive = doc5Repository.findByIsActive(true);
193+
194+
assertThat(resultsByActive).isNotEmpty();
195+
assertThat(resultsByActive).hasSize(1);
196+
assertThat(resultsByActive.get(0).getIsActive()).isTrue();
197+
}
133198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.redis.om.spring.fixtures.document.model;
2+
3+
import com.redis.om.spring.annotations.Document;
4+
import com.redis.om.spring.annotations.Indexed;
5+
import org.springframework.data.annotation.Id;
6+
7+
import java.time.LocalDateTime;
8+
import java.util.UUID;
9+
10+
@Document
11+
public class Doc5 {
12+
@Id
13+
private String id;
14+
15+
@Indexed
16+
private int age;
17+
18+
@Indexed
19+
private LocalDateTime registrationDate;
20+
21+
@Indexed
22+
private Boolean isActive;
23+
24+
public Doc5() {
25+
this.id = UUID.randomUUID().toString();
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public int getAge() {
37+
return age;
38+
}
39+
40+
public void setAge(int age) {
41+
this.age = age;
42+
}
43+
44+
public LocalDateTime getRegistrationDate() {
45+
return registrationDate;
46+
}
47+
48+
public void setRegistrationDate(LocalDateTime registrationDate) {
49+
this.registrationDate = registrationDate;
50+
}
51+
52+
public Boolean getIsActive() {
53+
return isActive;
54+
}
55+
56+
public void setIsActive(Boolean isActive) {
57+
this.isActive = isActive;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.redis.om.spring.fixtures.document.repository;
2+
3+
import com.redis.om.spring.fixtures.document.model.Doc5;
4+
import com.redis.om.spring.repository.RedisDocumentRepository;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.List;
8+
9+
public interface Doc5Repository extends RedisDocumentRepository<Doc5, String> {
10+
List<Doc5> findByRegistrationDateBetween(LocalDateTime from, LocalDateTime to);
11+
List<Doc5> findByIsActive(boolean active);
12+
List<Doc5> findByAge(int age);
13+
}

0 commit comments

Comments
 (0)