Add tests to verify Limit is supported.
Closes #4397 Original pull request: #4398
This commit is contained in:
committed by
Mark Paluch
parent
8cd956e90a
commit
d78f47f035
@@ -21,6 +21,7 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Range;
|
||||
import org.springframework.data.domain.ScrollPosition;
|
||||
@@ -117,6 +118,11 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
|
||||
return delegate.getUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Limit getLimit() {
|
||||
return delegate.getLimit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given value with the underlying {@link MongoWriter}.
|
||||
*
|
||||
|
||||
@@ -213,6 +213,17 @@ public abstract class AbstractPersonRepositoryIntegrationTests implements Dirtie
|
||||
assertThat(page).contains(carter);
|
||||
}
|
||||
|
||||
@Test // GH-4397
|
||||
void appliesLimitToScrollingCorrectly() {
|
||||
|
||||
Window<Person> page = repository.findByLastnameLikeOrderByLastnameAscFirstnameAsc("*a*",
|
||||
ScrollPosition.keyset(), Limit.of(2));
|
||||
|
||||
assertThat(page.isLast()).isFalse();
|
||||
assertThat(page.size()).isEqualTo(2);
|
||||
assertThat(page).contains(carter);
|
||||
}
|
||||
|
||||
@Test // GH-4308
|
||||
void appliesScrollPositionWithProjectionCorrectly() {
|
||||
|
||||
@@ -236,6 +247,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests implements Dirtie
|
||||
assertThat(page).contains(carter, stefan);
|
||||
}
|
||||
|
||||
@Test // GH-4397
|
||||
void executesFinderCorrectlyWithSortAndLimit() {
|
||||
|
||||
List<Person> page = repository.findByLastnameLike("*a*", Sort.by(Direction.ASC, "lastname", "firstname"), Limit.of(2));
|
||||
|
||||
assertThat(page).containsExactly(carter, stefan);
|
||||
}
|
||||
|
||||
@Test
|
||||
void executesPagedFinderWithAnnotatedQueryCorrectly() {
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Range;
|
||||
@@ -126,6 +127,9 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
Window<Person> findTop2ByLastnameLikeOrderByLastnameAscFirstnameAsc(String lastname,
|
||||
ScrollPosition scrollPosition);
|
||||
|
||||
Window<Person> findByLastnameLikeOrderByLastnameAscFirstnameAsc(String lastname,
|
||||
ScrollPosition scrollPosition, Limit limit);
|
||||
|
||||
/**
|
||||
* Returns a scroll of {@link Person}s applying projections with a lastname matching the given one (*-wildcards
|
||||
* supported).
|
||||
@@ -145,6 +149,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
*/
|
||||
Page<Person> findByLastnameLike(String lastname, Pageable pageable);
|
||||
|
||||
List<Person> findByLastnameLike(String lastname, Sort sort, Limit limit);
|
||||
|
||||
@Query("{ 'lastname' : { '$regex' : '?0', '$options' : 'i'}}")
|
||||
Page<Person> findByLastnameLikeWithPageable(String lastname, Pageable pageable);
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -493,6 +494,30 @@ class AbstractMongoQueryUnitTests {
|
||||
assertThat(captor.getValue().getHint()).isEqualTo("idx-ln");
|
||||
}
|
||||
|
||||
@Test // GH-4397
|
||||
void limitShouldBeAppliedToQuery() {
|
||||
|
||||
createQueryForMethod("findWithLimit", String.class, Limit.class).execute(new Object[] { "dalinar", Limit.of(42) });
|
||||
|
||||
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
|
||||
verify(withQueryMock).matching(captor.capture());
|
||||
|
||||
assertThat(captor.getValue().getLimit()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test // GH-4397
|
||||
void sortAndLimitShouldBeAppliedToQuery() {
|
||||
|
||||
createQueryForMethod("findWithSortAndLimit", String.class, Sort.class, Limit.class)
|
||||
.execute(new Object[] { "dalinar", Sort.by("fn"), Limit.of(42) });
|
||||
|
||||
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
|
||||
verify(withQueryMock).matching(captor.capture());
|
||||
|
||||
assertThat(captor.getValue().getLimit()).isEqualTo(42);
|
||||
assertThat(captor.getValue().getSortObject()).isEqualTo(new Document("fn", 1));
|
||||
}
|
||||
|
||||
private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) {
|
||||
return createQueryForMethod(Repo.class, methodName, paramTypes);
|
||||
}
|
||||
@@ -614,6 +639,10 @@ class AbstractMongoQueryUnitTests {
|
||||
|
||||
@Hint("idx-fn")
|
||||
void findWithHintByFirstname(String firstname);
|
||||
|
||||
List<Person> findWithLimit(String firstname, Limit limit);
|
||||
|
||||
List<Person> findWithSortAndLimit(String firstname, Sort sort, Limit limit);
|
||||
}
|
||||
|
||||
// DATAMONGO-1872
|
||||
|
||||
Reference in New Issue
Block a user