Skip to content

Commit

Permalink
Add tests for default sorting with MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
christophstrobl committed Jul 17, 2024
1 parent 4d85e92 commit 0cc5d78
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ void findByLastName(AssertableOutput output) {
});
}

@Test
void findWithDefaultSort(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("annotated-query-default-sort(): [last-3, last-2, last-1]");
assertThat(output).hasSingleLineContaining("derived-query-default-sort(): [last-3, last-2, last-1]");
});
}

}
50 changes: 39 additions & 11 deletions data/data-mongodb/src/main/java/com/example/data/mongodb/CLR.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -61,18 +62,12 @@ public void run(String... args) {
runQueryByExample(product1);
runInTransaction(product1);

personRepository.save(new Person("first-1", "last-1"));
personRepository.save(new Person("first-2", "last-2"));
personRepository.save(new Person("first-3", "last-3"));

for (Person person : this.personRepository.findAll()) {
System.out.printf("findAll(): %s%n", person);
}

for (Person person : this.personRepository.findByLastname("last-3")) {
System.out.printf("findByLastname(): %s%n", person);
}
Person person1 = new Person("first-1", "last-1");
Person person2 = new Person("first-2", "last-2");
Person person3 = new Person("first-3", "last-3");

runDerivedFinder(person1, person2, person3);
runQueriesWithDefaultSort(person1, person2, person3);
}

// Prepare Collections to avoid timeouts on slow ci/docker/...
Expand Down Expand Up @@ -324,6 +319,39 @@ private void runInTransaction(LineItem product1) {
log("-----------------\n\n\n");
}

private void runDerivedFinder(Person person1, Person person2, Person person3) {

log("---- DERIVED FINDER ----");
personRepository.deleteAll();
personRepository.saveAll(List.of(person1, person2, person3));

for (Person person : this.personRepository.findAll()) {
System.out.printf("findAll(): %s%n", person);
}

for (Person person : this.personRepository.findByLastname("last-3")) {
System.out.printf("findByLastname(): %s%n", person);
}
log("-----------------\n\n\n");
}

private void runQueriesWithDefaultSort(Person person1, Person person2, Person person3) {

log("---- DEFAULT SORT ----");
personRepository.deleteAll();
personRepository.saveAll(List.of(person1, person2, person3));

List<Person> annotatedQueryResult = this.personRepository.findAndSortPersonsDescByLastnameViaAnnotation("last");
System.out.printf("annotated-query-default-sort(): %s",
annotatedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));

List<Person> derivedQueryResult = this.personRepository.findWithDefaultSortByLastnameStartingWith("last");
System.out.printf("derived-query-default-sort(): %s",
derivedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));

log("-----------------\n\n\n");
}

private Order newOrder(String customerId, LineItem... items) {
return newOrder(customerId, new Date(), items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@

import java.util.List;

import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.ListCrudRepository;

public interface PersonRepository extends ListCrudRepository<Person, String> {

List<Person> findByLastname(String lastname);

@Query(value = "{ 'lastname' : { '$regex' : '?0.*'} }", sort = "{ 'lastname' : -1 }")
List<Person> findAndSortPersonsDescByLastnameViaAnnotation(String lastname);

@Query(sort = "{ 'lastname' : -1 }")
List<Person> findWithDefaultSortByLastnameStartingWith(String lastname);

}

0 comments on commit 0cc5d78

Please sign in to comment.