Skip to content

test: add test cases for boolean and datetime query methods #565

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.redis.om.spring.annotations.document;

import com.redis.om.spring.AbstractBaseDocumentTest;
import com.redis.om.spring.fixtures.document.model.SKU;
import com.redis.om.spring.fixtures.document.model.Student;
import com.redis.om.spring.fixtures.document.model.User2;
import com.redis.om.spring.fixtures.document.model.*;
import com.redis.om.spring.fixtures.document.repository.Doc5Repository;
import com.redis.om.spring.fixtures.document.repository.SKUCacheRepository;
import com.redis.om.spring.fixtures.document.repository.StudentRepository;
import com.redis.om.spring.fixtures.document.repository.User2Repository;
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;
Expand All @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

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

@Autowired
private Doc5Repository doc5Repository;

@Autowired
private EntityStream entityStream;

@BeforeEach
void cleanUp() {
repository.deleteAll();
Expand All @@ -54,6 +61,21 @@ void cleanUp() {
students.add(student);
}
studentRepository.saveAll(students);

doc5Repository.deleteAll();

// Create test data
Doc5 model1 = new Doc5();
model1.setAge(30);
model1.setRegistrationDate(LocalDateTime.now().minusDays(5));
model1.setIsActive(true);

Doc5 model2 = new Doc5();
model2.setAge(40);
model2.setRegistrationDate(LocalDateTime.now().minusDays(10));
model2.setIsActive(false);

doc5Repository.saveAll(List.of(model1, model2));
}

// RediSearchQuery wrong preparedQuery #187
Expand Down Expand Up @@ -130,4 +152,47 @@ void testQBEWithAliasWithHyphensAndOrderBy() {
Student result = studentRepository.findFirstByPropertyOrderByEventTimestamp(student, matcher, sortFunction);
assertThat(result.getUserName()).isEqualTo("Student2");
}

@Test
void testFindByAge() {
// This works fine
List<Doc5> resultsByAge = doc5Repository.findByAge(30);

assertThat(resultsByAge).isNotEmpty();
assertThat(resultsByAge).hasSize(1);
assertThat(resultsByAge.get(0).getAge()).isEqualTo(30);
}

@Test
void testFindByDateBetween() {
// LocalDateTime query test
LocalDateTime from = LocalDateTime.now().minusDays(20);
LocalDateTime to = LocalDateTime.now();

// First debug using EntityStream
List<Doc5> resultsWithEntityStream = entityStream.of(Doc5.class)
.filter(Doc5$.REGISTRATION_DATE.between(from, to))
.collect(Collectors.toList());

// Now try repository method
List<Doc5> resultsByDate = doc5Repository.findByRegistrationDateBetween(from, to);

assertThat(resultsByDate).isNotEmpty();
assertThat(resultsByDate).hasSize(2);
}

@Test
void testFindByIsActive() {
// First debug using EntityStream
List<Doc5> resultsWithEntityStream = entityStream.of(Doc5.class)
.filter(Doc5$.IS_ACTIVE.eq(true))
.collect(Collectors.toList());

// Now try repository method
List<Doc5> resultsByActive = doc5Repository.findByIsActive(true);

assertThat(resultsByActive).isNotEmpty();
assertThat(resultsByActive).hasSize(1);
assertThat(resultsByActive.get(0).getIsActive()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.redis.om.spring.fixtures.document.model;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import org.springframework.data.annotation.Id;

import java.time.LocalDateTime;
import java.util.UUID;

@Document
public class Doc5 {
@Id
private String id;

@Indexed
private int age;

@Indexed
private LocalDateTime registrationDate;

@Indexed
private Boolean isActive;

public Doc5() {
this.id = UUID.randomUUID().toString();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public LocalDateTime getRegistrationDate() {
return registrationDate;
}

public void setRegistrationDate(LocalDateTime registrationDate) {
this.registrationDate = registrationDate;
}

public Boolean getIsActive() {
return isActive;
}

public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.redis.om.spring.fixtures.document.repository;

import com.redis.om.spring.fixtures.document.model.Doc5;
import com.redis.om.spring.repository.RedisDocumentRepository;

import java.time.LocalDateTime;
import java.util.List;

public interface Doc5Repository extends RedisDocumentRepository<Doc5, String> {
List<Doc5> findByRegistrationDateBetween(LocalDateTime from, LocalDateTime to);
List<Doc5> findByIsActive(boolean active);
List<Doc5> findByAge(int age);
}
Loading