1+ package com .redis .om .spring .repository .query ;
2+
3+ import com .redis .om .spring .AbstractBaseDocumentTest ;
4+ import com .redis .om .spring .fixtures .document .model .Company ;
5+ import com .redis .om .spring .fixtures .document .repository .CompanyRepositoryWithLimiting ;
6+ import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .data .geo .Point ;
10+
11+ import java .time .LocalDate ;
12+ import java .util .List ;
13+ import java .util .Optional ;
14+ import java .util .Set ;
15+
16+ import static org .assertj .core .api .Assertions .assertThat ;
17+
18+ /**
19+ * Tests to verify that findTop/findFirst repository methods properly limit results.
20+ * This tests the issue where findTopByTeamOrderByDueDateAsc returns LIMIT 0 10000 instead of LIMIT 0 1.
21+ */
22+ class FindTopLimitingTest extends AbstractBaseDocumentTest {
23+
24+ @ Autowired
25+ CompanyRepositoryWithLimiting repository ;
26+
27+ @ BeforeEach
28+ void setup () {
29+ repository .deleteAll ();
30+
31+ // Create test data
32+ Company company1 = Company .of ("RedisInc" , 2011 , LocalDate .of (2011 , 5 , 1 ),
33+ new Point (-
122.066540 ,
37.377690 ),
"[email protected] " );
34+ company1 .setTags (Set .of ("fast" , "scalable" , "reliable" ));
35+
36+ Company company2 = Company .of ("Microsoft" , 1975 , LocalDate .of (1975 , 4 , 4 ),
37+ new Point (-
122.124500 ,
47.640160 ),
"[email protected] " );
38+ company2 .setTags (Set .of ("software" , "cloud" , "enterprise" ));
39+
40+ Company company3 = Company .of ("Tesla" , 2003 , LocalDate .of (2003 , 7 , 1 ),
41+ new Point (-
122.145800 ,
37.396400 ),
"[email protected] " );
42+ company3 .setTags (Set .of ("electric" , "automotive" , "innovative" ));
43+
44+ repository .saveAll (List .of (company1 , company2 , company3 ));
45+ }
46+
47+ @ Test
48+ void testFindFirstByTags () {
49+ // Test findFirst with a tag filter
50+ Optional <Company > result = repository .findFirstByTags ("software" );
51+
52+ assertThat (result ).isPresent ();
53+ assertThat (result .get ().getName ()).isEqualTo ("Microsoft" );
54+ }
55+
56+ @ Test
57+ void testFindFirstByOrderBy () {
58+ // Test findFirst with ordering
59+ Optional <Company > result = repository .findFirstByOrderByYearFoundedAsc ();
60+
61+ assertThat (result ).isPresent ();
62+ assertThat (result .get ().getName ()).isEqualTo ("Microsoft" );
63+ assertThat (result .get ().getYearFounded ()).isEqualTo (1975 );
64+ }
65+
66+ @ Test
67+ void testFindTopByOrderBy () {
68+ // Test findTop with ordering
69+ Optional <Company > result = repository .findTopByOrderByYearFoundedDesc ();
70+
71+ assertThat (result ).isPresent ();
72+ assertThat (result .get ().getName ()).isEqualTo ("RedisInc" );
73+ assertThat (result .get ().getYearFounded ()).isEqualTo (2011 );
74+ }
75+
76+ @ Test
77+ void testFindTop2ByOrderBy () {
78+ // Test findTop with explicit number
79+ List <Company > results = repository .findTop2ByOrderByYearFoundedAsc ();
80+
81+ // This test will likely fail because Redis OM Spring doesn't properly parse the limit from method names
82+ assertThat (results ).hasSize (2 );
83+ assertThat (results .get (0 ).getName ()).isEqualTo ("Microsoft" );
84+ assertThat (results .get (1 ).getName ()).isEqualTo ("Tesla" );
85+ }
86+
87+ @ Test
88+ void testFindFirstByTagsOrderBy () {
89+ // Test the reported issue pattern: findTopByTeamOrderByDueDateAsc
90+ Optional <Company > result = repository .findFirstByTagsOrderByYearFoundedAsc ("automotive" );
91+
92+ assertThat (result ).isPresent ();
93+ assertThat (result .get ().getName ()).isEqualTo ("Tesla" );
94+ }
95+
96+ @ Test
97+ void testFindTop5ByOrderBy () {
98+ // Add more test data to verify limit 5
99+ Company company4 = Company .of ("Apple" , 1976 , LocalDate .of (1976 , 4 , 1 ),
100+ new Point (-
122.030000 ,
37.330000 ),
"[email protected] " );
101+ Company company5 = Company .of ("Amazon" , 1994 , LocalDate .of (1994 , 7 , 5 ),
102+ new Point (-
122.329200 ,
47.614900 ),
"[email protected] " );
103+ Company company6 = Company .of ("Google" , 1998 , LocalDate .of (1998 , 9 , 4 ),
104+ new Point (-
122.084000 ,
37.422000 ),
"[email protected] " );
105+ repository .saveAll (List .of (company4 , company5 , company6 ));
106+
107+ List <Company > results = repository .findTop5ByOrderByNameAsc ();
108+
109+ assertThat (results ).hasSize (5 );
110+ assertThat (results .get (0 ).getName ()).isEqualTo ("Amazon" );
111+ assertThat (results .get (1 ).getName ()).isEqualTo ("Apple" );
112+ assertThat (results .get (2 ).getName ()).isEqualTo ("Google" );
113+ assertThat (results .get (3 ).getName ()).isEqualTo ("Microsoft" );
114+ assertThat (results .get (4 ).getName ()).isEqualTo ("RedisInc" );
115+ }
116+
117+ @ Test
118+ void testFindFirst3ByOrderBy () {
119+ List <Company > results = repository .findFirst3ByOrderByEmailDesc ();
120+
121+ assertThat (results ).hasSize (3 );
122+ assertThat (
results .
get (
0 ).
getEmail ()).
isEqualTo (
"[email protected] " );
123+ assertThat (
results .
get (
1 ).
getEmail ()).
isEqualTo (
"[email protected] " );
124+ assertThat (
results .
get (
2 ).
getEmail ()).
isEqualTo (
"[email protected] " );
125+ }
126+
127+ @ Test
128+ void testFindTopByTagsOrderBy () {
129+ Optional <Company > result = repository .findTopByTagsOrderByNameAsc ("software" );
130+
131+ assertThat (result ).isPresent ();
132+ assertThat (result .get ().getName ()).isEqualTo ("Microsoft" );
133+ }
134+
135+ @ Test
136+ void testFindTop10WithCriteria () {
137+ // This should return only the companies founded after 2000, limited to 10
138+ List <Company > results = repository .findTop10ByYearFoundedGreaterThanOrderByNameAsc (2000 );
139+
140+ assertThat (results ).hasSize (2 ); // Only RedisInc (2011) and Tesla (2003) match
141+ assertThat (results .get (0 ).getName ()).isEqualTo ("RedisInc" );
142+ assertThat (results .get (1 ).getName ()).isEqualTo ("Tesla" );
143+ }
144+
145+ }
0 commit comments