Skip to content

Commit

Permalink
refactor repository query tests, update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Oct 6, 2024
1 parent 92d0cdc commit 5750879
Show file tree
Hide file tree
Showing 29 changed files with 1,054 additions and 793 deletions.
2 changes: 2 additions & 0 deletions src/main/asciidoc/reference/query-methods-id.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[[aerospike.query_methods.id]]
= Id Repository Queries

*Id repository reading queries* (like `findById()`, `findByIds()`, `findByFirstNameAndId()`, `findAllById()`, `countById()`, `existsById()` etc.) utilize `get` operation of the underlying Java client (`client.get()`).

[width="100%",cols="<7%,<30%,<25%,<20%",options="header",]
|===
|Keyword |Repository query sample |Snippet |Notes
Expand Down
2 changes: 2 additions & 0 deletions src/main/asciidoc/reference/query-methods-preface.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Here are the references to the examples of repository queries:

Below is an example of an interface with several query methods:

NOTE: *Id repository read queries* (like `findById()`, `findByIds()`, `findByFirstNameAndId()`, `findAllById()`, `countById()`, `existsById()` etc.) utilize `get()` operation of the underlying Java client. *Repository read queries without id* (like `findByFirstName()`, `findByFirstNameAndLastName()`, `findAll()` etc.) utilize `query()` operation of the underlying Java client.

[source,java]
----
public interface PersonRepository extends AerospikeRepository<Person, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public abstract class BaseReactiveIntegrationTests extends BaseIntegrationTests
protected IndexesCache indexesCache;
@Autowired
protected MappingContext<BasicAerospikePersistentEntity<?>, AerospikePersistentProperty> mappingContext;
@Autowired
protected ReactiveBlockingAerospikeTestOperations reactiveBlockingAerospikeTestOperations;

protected <T> T findById(Serializable id, Class<T> type) {
return reactiveTemplate.findById(id, type).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void countBySimplePropertyEquals_String() {
}

@Test
void countPersonById_AND_simpleProperty() {
void countById_AND_simpleProperty() {
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
long persons = repository.countByIdAndFirstName(ids, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.springframework.data.aerospike.repository.query.blocking.noindex.deleteBy;

import org.junit.jupiter.api.Test;
import org.springframework.data.aerospike.query.QueryParam;
import org.springframework.data.aerospike.repository.query.blocking.noindex.PersonRepositoryQueryTests;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.aerospike.query.QueryParam.of;

/**
* Tests for the "Equals" repository query. Keywords: Is, Equals (or no keyword).
Expand All @@ -22,21 +24,76 @@ void deleteBySimplePropertyEquals_String() {
assertThat(repository.findByFirstNameIgnoreCase("lEroi")).isNotEmpty();
repository.deleteByFirstNameIgnoreCase("lEroi");
assertThat(repository.findByFirstNameIgnoreCase("lEroi")).isEmpty();
// cleanup
repository.save(leroi);
}

@Test
void deleteById() {
assertThat(repository.existsById(dave.getId())).isTrue();
repository.deleteById(dave.getId());
assertThat(repository.existsById(dave.getId())).isFalse();
// cleanup
repository.save(dave);
}

@Test
void deleteById_AND_simpleProperty() {
assertThat(repository.existsById(leroi.getId())).isTrue();
assertThat(repository.existsById(dave.getId())).isTrue();
assertThat(repository.existsById(carter.getId())).isTrue();

QueryParam id = of(leroi.getId());
QueryParam name = of(leroi.getFirstName());
assertThat(repository.existsByIdAndFirstName(id, name)).isTrue();
repository.deleteByIdAndFirstName(id, name);
assertThat(repository.existsByIdAndFirstName(id, name)).isFalse();

QueryParam id2 = of(dave.getId());
QueryParam name2 = of(carter.getFirstName());
// there is no record with Dave's id and Carter's first name
assertThat(repository.existsByIdAndFirstName(id2, name2)).isFalse();
// delete using id and first name of different records must not change anything
repository.deleteByIdAndFirstName(id2, name2);
assertThat(repository.existsById(dave.getId())).isTrue();
assertThat(repository.existsById(carter.getId())).isTrue();

repository.deleteAllById(List.of(dave.getId(), carter.getId()));
assertThat(repository.existsById(dave.getId())).isFalse();
assertThat(repository.existsById(carter.getId())).isFalse();

// cleanup
repository.save(leroi);
repository.save(dave);
repository.save(carter);
}

@Test
void deletePersonById_AND_simpleProperty() {
// QueryParam ids = of(dave.getId());
// QueryParam name = of(carter.getFirstName());
// repository.deleteByIdAndFirstName(ids, name);
// assertThat(repository.findByIdAndFirstName(ids, name)).isEmpty();
//
// ids = of(dave.getId());
// name = of(dave.getFirstName());
// assertThat(repository.findByIdAndFirstName(ids, name)).isNotEmpty();
// repository.deleteByIdAndFirstName(ids, name);
// assertThat(repository.findByIdAndFirstName(ids, name)).isEmpty();
void deleteAllByIds() {
assertThat(repository.existsById(dave.getId())).isTrue();
assertThat(repository.existsById(carter.getId())).isTrue();

repository.deleteAllById(List.of(dave.getId(), carter.getId()));

assertThat(repository.existsById(dave.getId())).isFalse();
assertThat(repository.existsById(carter.getId())).isFalse();

// cleanup
repository.save(dave);
repository.save(carter);
}

@Test
void deleteAll() {
if (serverVersionSupport.isBatchWriteSupported()) {
// batch delete requires server ver. >= 6.0.0
repository.deleteAll(List.of(dave, carter));
} else {
List.of(dave, carter).forEach(repository::delete);
}
assertThat(repository.findAllById(List.of(dave.getId(), carter.getId()))).isEmpty();
// cleanup
repository.save(dave);
repository.save(carter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ void existsBySimplePropertyEquals_String() {
}

@Test
void existsPersonById_AND_simpleProperty() {
void existsById() {
boolean result = repository.existsById(dave.getId());
assertThat(result).isTrue();

boolean result2 = repository.existsById(dave.getId() + "test1234__**");
assertThat(result2).isFalse();
}

@Test
void existsById_AND_simpleProperty() {
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
boolean result = repository.existsByIdAndFirstName(ids, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class CrudRepositoryQueryTests extends PersonRepositoryQueryTests {

@Test
void findPersonById() {
void findById() {
Optional<Person> person = repository.findById(dave.getId());

assertThat(person).hasValueSatisfying(actual -> {
Expand All @@ -28,6 +28,12 @@ void findPersonById() {
});
}

@Test
void findAllByIds() {
Iterable<Person> result = repository.findAllById(List.of(dave.getId(), carter.getId()));
assertThat(result).containsExactlyInAnyOrder(dave, carter);
}

@Test
void findAll() {
List<Person> result = (List<Person>) repository.findAll();
Expand Down Expand Up @@ -68,26 +74,4 @@ void findAll_doesNotFindPersonDeletedById() {
repository.save(dave);
}
}

@Test
void deletePersonById() {
repository.deleteById(dave.getId());

assertThat(repository.findById(dave.getId())).isEmpty();

repository.save(dave); // cleanup
}

@Test
void deleteAllPersonsFromList() {
if (serverVersionSupport.isBatchWriteSupported()) {
// batch delete requires server ver. >= 6.0.0
repository.deleteAll(List.of(dave, carter));
} else {
List.of(dave, carter).forEach(repository::delete);
}
assertThat(repository.findAllById(List.of(dave.getId(), carter.getId()))).isEmpty();
repository.save(dave); // cleanup
repository.save(carter); // cleanup
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -41,6 +42,12 @@ void findBySimplePropertyEquals_String() {
assertThat(result3).hasSize(0);
}

@Test
void findBySimpleProperty_String_projection() {
List<PersonSomeFields> result = repository.findPersonSomeFieldsByLastName("Beauford");
assertThat(result).containsOnly(carter.toPersonSomeFields());
}

@Test
void findBySimplePropertyEquals_String_NegativeTest() {
assertThatThrownBy(() -> negativeTestsRepository.findByFirstName(100))
Expand Down Expand Up @@ -124,7 +131,14 @@ void findBySimplePropertyEquals_Boolean_NegativeTest() {
}

@Test
void findPersonById_AND_simpleProperty() {
void findById() {
Optional<Person> result = repository.findById(dave.getId());
assertThat(result).isPresent();
assertThat(result.get()).isEqualTo(dave);
}

@Test
void findById_AND_simpleProperty() {
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
List<Person> persons = repository.findByIdAndFirstName(ids, name);
Expand Down Expand Up @@ -164,36 +178,11 @@ void findPersonById_AND_simpleProperty() {
}

@Test
void findAllByIds() {
Iterable<Person> result = repository.findAllById(List.of(dave.getId(), carter.getId()));
assertThat(result).containsExactlyInAnyOrder(dave, carter);
}

@Test
void findAllPersonByIds_AND_simpleProperty() {
QueryParam ids1 = of(List.of(dave.getId(), boyd.getId()));
QueryParam name1 = of(dave.getFirstName());
List<Person> persons1 = repository.findAllByIdAndFirstName(ids1, name1);
assertThat(persons1).contains(dave);

QueryParam ids2 = of(List.of(dave.getId(), boyd.getId()));
QueryParam name2 = of(carter.getFirstName());
List<Person> persons2 = repository.findAllByIdAndFirstName(ids2, name2);
assertThat(persons2).isEmpty();
}

@Test
void findByIds_dynamicProjection() {
void findById_dynamicProjection() {
List<PersonSomeFields> result = repository.findById(dave.getId(), PersonSomeFields.class);
assertThat(result).containsOnly(dave.toPersonSomeFields());
}

@Test
void findBySimpleProperty_String_projection() {
List<PersonSomeFields> result = repository.findPersonSomeFieldsByLastName("Beauford");
assertThat(result).containsOnly(carter.toPersonSomeFields());
}

@Test
void findById_AND_simpleProperty_dynamicProjection() {
QueryParam ids = of(List.of(boyd.getId(), dave.getId(), carter.getId()));
Expand Down Expand Up @@ -227,6 +216,25 @@ void findBySimpleProperty_AND_simpleProperty_DynamicProjection() {
assertThat(result).containsOnly(carter.toPersonSomeFields());
}

@Test
void findAllByIds() {
Iterable<Person> result = repository.findAllById(List.of(dave.getId(), carter.getId()));
assertThat(result).containsExactlyInAnyOrder(dave, carter);
}

@Test
void findAllByIds_AND_simpleProperty() {
QueryParam ids1 = of(List.of(dave.getId(), boyd.getId()));
QueryParam name1 = of(dave.getFirstName());
List<Person> persons1 = repository.findAllByIdAndFirstName(ids1, name1);
assertThat(persons1).contains(dave);

QueryParam ids2 = of(List.of(dave.getId(), boyd.getId()));
QueryParam name2 = of(carter.getFirstName());
List<Person> persons2 = repository.findAllByIdAndFirstName(ids2, name2);
assertThat(persons2).isEmpty();
}

@Test
void findByNestedSimplePropertyEquals() {
String zipCode = "C0124";
Expand Down
Loading

0 comments on commit 5750879

Please sign in to comment.