-
Notifications
You must be signed in to change notification settings - Fork 101
fix: add limit support for findTop/findFirst repository methods #631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
.../java/com/redis/om/spring/fixtures/document/repository/CompanyRepositoryWithLimiting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.redis.om.spring.fixtures.document.repository; | ||
|
|
||
| import com.redis.om.spring.fixtures.document.model.Company; | ||
| import com.redis.om.spring.repository.RedisDocumentRepository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Repository interface to test findTop/findFirst limiting functionality. | ||
| */ | ||
| public interface CompanyRepositoryWithLimiting extends RedisDocumentRepository<Company, String> { | ||
| Optional<Company> findFirstByTags(String tag); | ||
| Optional<Company> findFirstByOrderByYearFoundedAsc(); | ||
| Optional<Company> findTopByOrderByYearFoundedDesc(); | ||
| List<Company> findTop2ByOrderByYearFoundedAsc(); | ||
| Optional<Company> findFirstByTagsOrderByYearFoundedAsc(String tag); | ||
|
|
||
| // Additional test cases for various limiting scenarios | ||
| List<Company> findTop5ByOrderByNameAsc(); | ||
| List<Company> findFirst3ByOrderByEmailDesc(); | ||
| Optional<Company> findTopByTagsOrderByNameAsc(String tag); | ||
| List<Company> findTop10ByYearFoundedGreaterThanOrderByNameAsc(int year); | ||
| } |
145 changes: 145 additions & 0 deletions
145
tests/src/test/java/com/redis/om/spring/repository/query/FindTopLimitingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package com.redis.om.spring.repository.query; | ||
|
|
||
| import com.redis.om.spring.AbstractBaseDocumentTest; | ||
| import com.redis.om.spring.fixtures.document.model.Company; | ||
| import com.redis.om.spring.fixtures.document.repository.CompanyRepositoryWithLimiting; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.geo.Point; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Tests to verify that findTop/findFirst repository methods properly limit results. | ||
| * This tests the issue where findTopByTeamOrderByDueDateAsc returns LIMIT 0 10000 instead of LIMIT 0 1. | ||
| */ | ||
| class FindTopLimitingTest extends AbstractBaseDocumentTest { | ||
|
|
||
| @Autowired | ||
| CompanyRepositoryWithLimiting repository; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| repository.deleteAll(); | ||
|
|
||
| // Create test data | ||
| Company company1 = Company.of("RedisInc", 2011, LocalDate.of(2011, 5, 1), | ||
| new Point(-122.066540, 37.377690), "[email protected]"); | ||
| company1.setTags(Set.of("fast", "scalable", "reliable")); | ||
|
|
||
| Company company2 = Company.of("Microsoft", 1975, LocalDate.of(1975, 4, 4), | ||
| new Point(-122.124500, 47.640160), "[email protected]"); | ||
| company2.setTags(Set.of("software", "cloud", "enterprise")); | ||
|
|
||
| Company company3 = Company.of("Tesla", 2003, LocalDate.of(2003, 7, 1), | ||
| new Point(-122.145800, 37.396400), "[email protected]"); | ||
| company3.setTags(Set.of("electric", "automotive", "innovative")); | ||
|
|
||
| repository.saveAll(List.of(company1, company2, company3)); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindFirstByTags() { | ||
| // Test findFirst with a tag filter | ||
| Optional<Company> result = repository.findFirstByTags("software"); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get().getName()).isEqualTo("Microsoft"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindFirstByOrderBy() { | ||
| // Test findFirst with ordering | ||
| Optional<Company> result = repository.findFirstByOrderByYearFoundedAsc(); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get().getName()).isEqualTo("Microsoft"); | ||
| assertThat(result.get().getYearFounded()).isEqualTo(1975); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindTopByOrderBy() { | ||
| // Test findTop with ordering | ||
| Optional<Company> result = repository.findTopByOrderByYearFoundedDesc(); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get().getName()).isEqualTo("RedisInc"); | ||
| assertThat(result.get().getYearFounded()).isEqualTo(2011); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindTop2ByOrderBy() { | ||
| // Test findTop with explicit number | ||
| List<Company> results = repository.findTop2ByOrderByYearFoundedAsc(); | ||
|
|
||
| // This test will likely fail because Redis OM Spring doesn't properly parse the limit from method names | ||
| assertThat(results).hasSize(2); | ||
| assertThat(results.get(0).getName()).isEqualTo("Microsoft"); | ||
| assertThat(results.get(1).getName()).isEqualTo("Tesla"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindFirstByTagsOrderBy() { | ||
| // Test the reported issue pattern: findTopByTeamOrderByDueDateAsc | ||
| Optional<Company> result = repository.findFirstByTagsOrderByYearFoundedAsc("automotive"); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get().getName()).isEqualTo("Tesla"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindTop5ByOrderBy() { | ||
| // Add more test data to verify limit 5 | ||
| Company company4 = Company.of("Apple", 1976, LocalDate.of(1976, 4, 1), | ||
| new Point(-122.030000, 37.330000), "[email protected]"); | ||
| Company company5 = Company.of("Amazon", 1994, LocalDate.of(1994, 7, 5), | ||
| new Point(-122.329200, 47.614900), "[email protected]"); | ||
| Company company6 = Company.of("Google", 1998, LocalDate.of(1998, 9, 4), | ||
| new Point(-122.084000, 37.422000), "[email protected]"); | ||
| repository.saveAll(List.of(company4, company5, company6)); | ||
|
|
||
| List<Company> results = repository.findTop5ByOrderByNameAsc(); | ||
|
|
||
| assertThat(results).hasSize(5); | ||
| assertThat(results.get(0).getName()).isEqualTo("Amazon"); | ||
| assertThat(results.get(1).getName()).isEqualTo("Apple"); | ||
| assertThat(results.get(2).getName()).isEqualTo("Google"); | ||
| assertThat(results.get(3).getName()).isEqualTo("Microsoft"); | ||
| assertThat(results.get(4).getName()).isEqualTo("RedisInc"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindFirst3ByOrderBy() { | ||
| List<Company> results = repository.findFirst3ByOrderByEmailDesc(); | ||
|
|
||
| assertThat(results).hasSize(3); | ||
| assertThat(results.get(0).getEmail()).isEqualTo("[email protected]"); | ||
| assertThat(results.get(1).getEmail()).isEqualTo("[email protected]"); | ||
| assertThat(results.get(2).getEmail()).isEqualTo("[email protected]"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindTopByTagsOrderBy() { | ||
| Optional<Company> result = repository.findTopByTagsOrderByNameAsc("software"); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get().getName()).isEqualTo("Microsoft"); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindTop10WithCriteria() { | ||
| // This should return only the companies founded after 2000, limited to 10 | ||
| List<Company> results = repository.findTop10ByYearFoundedGreaterThanOrderByNameAsc(2000); | ||
|
|
||
| assertThat(results).hasSize(2); // Only RedisInc (2011) and Tesla (2003) match | ||
| assertThat(results.get(0).getName()).isEqualTo("RedisInc"); | ||
| assertThat(results.get(1).getName()).isEqualTo("Tesla"); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.