Skip to content

Commit

Permalink
add tests for keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Sep 8, 2024
1 parent c48a021 commit 9d9d36d
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.springframework.data.aerospike.repository.query.blocking.noindex.countBy;

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).
*/
public class EqualsTests extends PersonRepositoryQueryTests {

@Test
void countBySimplePropertyEquals_String() {
long result = repository.countByFirstName("Leroi");
assertThat(result).isEqualTo(2);

long result1 = repository.countByFirstNameIgnoreCase("lEroi");
assertThat(result1).isEqualTo(2);

long result2 = repository.countByFirstNameIs("lEroi"); // another way to call the query method
assertThat(result2).isZero();
}

@Test
void countPersonById_AND_simpleProperty() {
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
long persons = repository.countByIdAndFirstName(ids, name);
assertThat(persons).isZero();

ids = of(dave.getId());
name = of(dave.getFirstName());
persons = repository.countByIdAndFirstName(ids, name);
assertThat(persons).isEqualTo(1);

ids = of(List.of(leroi.getId(), leroi2.getId(), carter.getId()));
QueryParam firstName = of(leroi.getFirstName());
QueryParam age = of(stefan.getAge());
long persons4 = repository.countByIdAndFirstNameOrAge(ids, firstName, age);
assertThat(persons4).isEqualTo(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 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).
*/
public class EqualsTests extends PersonRepositoryQueryTests {

@Test
void deleteBySimplePropertyEquals_String() {
assertThat(repository.findByFirstName("Leroi")).isNotEmpty();
repository.deleteByFirstName("Leroi");
assertThat(repository.findByFirstName("Leroi")).isEmpty();

repository.save(leroi);
assertThat(repository.findByFirstNameIgnoreCase("lEroi")).isNotEmpty();
repository.deleteByFirstNameIgnoreCase("lEroi");
assertThat(repository.findByFirstNameIgnoreCase("lEroi")).isEmpty();
}

@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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.springframework.data.aerospike.repository.query.blocking.noindex.existsBy;

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).
*/
public class EqualsTests extends PersonRepositoryQueryTests {

@Test
void existsBySimplePropertyEquals_String() {
boolean result = repository.existsByFirstName("Leroi");
assertThat(result).isTrue();

boolean result1 = repository.existsByFirstNameIgnoreCase("lEroi");
assertThat(result1).isTrue();

boolean result2 = repository.existsByFirstNameIs("lEroi"); // another way to call the query method
assertThat(result2).isFalse();
}

@Test
void existsPersonById_AND_simpleProperty() {
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
boolean result = repository.existsByIdAndFirstName(ids, name);
assertThat(result).isFalse();

ids = of(dave.getId());
name = of(dave.getFirstName());
result = repository.existsByIdAndFirstName(ids, name);
assertThat(result).isTrue();

ids = of(List.of(leroi.getId(), leroi2.getId(), carter.getId()));
QueryParam firstName = of(leroi.getFirstName());
QueryParam age = of(stefan.getAge());
boolean result2 = repository.existsByIdAndFirstNameOrAge(ids, firstName, age);
assertThat(result2).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,42 +125,65 @@ void findBySimplePropertyEquals_Boolean_NegativeTest() {

@Test
void findPersonById_AND_simpleProperty() {
QueryParam ids = of(List.of(dave.getId(), boyd.getId()));
QueryParam names = of(carter.getFirstName());
List<Person> persons = repository.findByIdAndFirstName(ids, names);
QueryParam ids = of(dave.getId());
QueryParam name = of(carter.getFirstName());
List<Person> persons = repository.findByIdAndFirstName(ids, name);
assertThat(persons).isEmpty();

ids = of(dave.getId());
name = of(dave.getFirstName());
persons = repository.findByIdAndFirstName(ids, name);
assertThat(persons).containsOnly(dave);

ids = of(List.of(boyd.getId(), dave.getId(), carter.getId()));
names = of(dave.getFirstName());
persons = repository.findByIdAndFirstName(ids, names);
name = of(dave.getFirstName());
// when in a combined query, "findById" part can receive multiple ids wrapped in QueryParam
persons = repository.findByIdAndFirstName(ids, name);
assertThat(persons).containsOnly(dave);

ids = of(List.of(leroi.getId(), leroi2.getId(), carter.getId()));
QueryParam firstNames = of(leroi.getFirstName());
QueryParam ages = of(leroi2.getAge());
List<Person> persons2 = repository.findByIdAndFirstNameAndAge(ids, firstNames, ages);
QueryParam firstName = of(leroi.getFirstName());
QueryParam age = of(leroi2.getAge());
List<Person> persons2 = repository.findByIdAndFirstNameAndAge(ids, firstName, age);
assertThat(persons2).containsOnly(leroi2);

// "findByIdOr..." will return empty list because primary keys are used to firstly narrow down the results.parts
// In a combined id query "OR" can be put only between the non-id fields like shown below,
// and the results are limited by the given ids
ids = of(List.of(leroi.getId(), leroi2.getId(),
carter.getId()));
firstNames = of(leroi.getFirstName());
ages = of(leroi2.getAge());
List<Person> persons3 = repository.findByIdAndFirstNameOrAge(ids, firstNames, ages);
ids = of(List.of(leroi.getId(), leroi2.getId(), carter.getId()));
firstName = of(leroi.getFirstName());
age = of(leroi2.getAge());
List<Person> persons3 = repository.findByIdAndFirstNameOrAge(ids, firstName, age);
assertThat(persons3).containsOnly(leroi, leroi2);

ids = of(List.of(leroi.getId(), leroi2.getId(),
carter.getId()));
firstNames = of(leroi.getFirstName());
ages = of(stefan.getAge());
List<Person> persons4 = repository.findByIdAndFirstNameOrAge(ids, firstNames, ages);
ids = of(List.of(leroi.getId(), leroi2.getId(), carter.getId()));
firstName = of(leroi.getFirstName());
age = of(stefan.getAge());
List<Person> persons4 = repository.findByIdAndFirstNameOrAge(ids, firstName, age);
assertThat(persons4).containsOnly(leroi, leroi2);
}

@Test
void findById_dynamicProjection() {
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() {
List<PersonSomeFields> result = repository.findById(dave.getId(), PersonSomeFields.class);
assertThat(result).containsOnly(dave.toPersonSomeFields());
}
Expand All @@ -174,32 +197,32 @@ void findBySimpleProperty_String_projection() {
@Test
void findById_AND_simpleProperty_dynamicProjection() {
QueryParam ids = of(List.of(boyd.getId(), dave.getId(), carter.getId()));
QueryParam lastNames = of(carter.getLastName());
List<PersonSomeFields> result = repository.findByIdAndLastName(ids, lastNames, PersonSomeFields.class);
QueryParam lastName = of(carter.getLastName());
List<PersonSomeFields> result = repository.findByIdAndLastName(ids, lastName, PersonSomeFields.class);
assertThat(result).containsOnly(carter.toPersonSomeFields());
}

@Test
void findById_AND_simpleProperty_DynamicProjection_EmptyResult() {
QueryParam ids = of(List.of(carter.getId(), boyd.getId()));
QueryParam lastNames = of(dave.getLastName());
List<PersonSomeFields> result = repository.findByIdAndLastName(ids, lastNames, PersonSomeFields.class);
QueryParam lastName = of(dave.getLastName());
List<PersonSomeFields> result = repository.findByIdAndLastName(ids, lastName, PersonSomeFields.class);
assertThat(result).isEmpty();
}

@Test
void findBySimpleProperty_AND_id_dynamicProjection() {
QueryParam ids = of(dave.getId());
QueryParam lastNames = of(dave.getLastName());
List<PersonSomeFields> result = repository.findByLastNameAndId(lastNames, ids, PersonSomeFields.class);
QueryParam id = of(dave.getId());
QueryParam lastName = of(dave.getLastName());
List<PersonSomeFields> result = repository.findByLastNameAndId(lastName, id, PersonSomeFields.class);
assertThat(result).containsOnly(dave.toPersonSomeFields());
}

@Test
void findBySimpleProperty_AND_simpleProperty_DynamicProjection() {
QueryParam firstNames = of(carter.getFirstName());
QueryParam lastNames = of(carter.getLastName());
List<PersonSomeFields> result = repository.findByFirstNameAndLastName(firstNames, lastNames,
QueryParam firstName = of(carter.getFirstName());
QueryParam lastName = of(carter.getLastName());
List<PersonSomeFields> result = repository.findByFirstNameAndLastName(firstName, lastName,
PersonSomeFields.class);
assertThat(result).containsOnly(carter.toPersonSomeFields());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.springframework.data.aerospike.repository.reactive;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseReactiveIntegrationTests;
import org.springframework.data.aerospike.query.QueryParam;
import org.springframework.data.aerospike.sample.Customer;
import org.springframework.data.aerospike.sample.ReactiveCustomerRepository;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import java.util.List;

public class ReactiveAerospikeRepositoryCountRelatedTests extends BaseReactiveIntegrationTests {

@Autowired
ReactiveCustomerRepository customerRepo;

private Customer customer1, customer2;

@BeforeEach
public void setUp() {
customer1 = Customer.builder().id(nextId()).firstName("Philip J.").lastName("Fry").age(1029).build();
customer2 = Customer.builder().id(nextId()).firstName("Leela").lastName("Turanga").age(29).build();
StepVerifier.create(customerRepo.saveAll(Flux.just(customer1, customer2))).expectNextCount(2).verifyComplete();
}

@Test
public void countByAgeBetween() {
StepVerifier.create(customerRepo.countByAgeBetween(20, 1100))
.expectNext(2L)
.verifyComplete();
}

@Test
public void countByIdAndFirstNameIn() {
QueryParam ids = QueryParam.of(List.of(customer1.getId(), customer2.getId()));
QueryParam firstNames = QueryParam.of(List.of(customer1.getFirstName(), customer2.getFirstName(), "FirstName"));
StepVerifier.create(customerRepo.countByIdAndFirstNameIn(ids, firstNames))
.expectNext(2L)
.verifyComplete();

firstNames = QueryParam.of(List.of("FirstName"));
StepVerifier.create(customerRepo.countByIdAndFirstNameIn(ids, firstNames))
.expectNext(0L)
.verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseReactiveIntegrationTests;
import org.springframework.data.aerospike.query.QueryParam;
import org.springframework.data.aerospike.sample.Customer;
import org.springframework.data.aerospike.sample.ReactiveCustomerRepository;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -170,4 +171,38 @@ public void deleteAll_ShouldDelete() {
StepVerifier.create(customerRepo.findById(customer1.getId())).expectNextCount(0).verifyComplete();
StepVerifier.create(customerRepo.findById(customer2.getId())).expectNextCount(0).verifyComplete();
}

@Test
public void deleteByIdAndFirstNameIn() {
StepVerifier.create(customerRepo.saveAll(Flux.just(customer1, customer2)))
.expectNextCount(2)
.verifyComplete();

StepVerifier.create(customerRepo.findAllById(List.of(customer1.getId(), customer2.getId())).collectList())
.expectNextMatches(list -> list.size() == 2 && list.contains(customer1) && list.contains(customer2))
.verifyComplete();

QueryParam ids = QueryParam.of(List.of(customer1.getId(), customer2.getId()));
QueryParam firstNames = QueryParam.of(List.of("FirstName"));
// no records satisfying the condition
StepVerifier.create(customerRepo.deleteByIdAndFirstNameIn(ids, firstNames))
.expectComplete()
.verify();

// no records get deleted
StepVerifier.create(customerRepo.findAllById(List.of(customer1.getId(), customer2.getId())).collectList())
.expectNextMatches(list -> list.size() == 2 && list.contains(customer1) && list.contains(customer2))
.verifyComplete();

// 2 records satisfying the condition
firstNames = QueryParam.of(List.of(customer1.getFirstName(), customer2.getFirstName(), "FirstName"));
StepVerifier.create(customerRepo.deleteByIdAndFirstNameIn(ids, firstNames))
.expectComplete()
.verify();

// 2 records get deleted
StepVerifier.create(customerRepo.findAllById(List.of(customer1.getId(), customer2.getId())).collectList())
.expectNextMatches(List::isEmpty)
.verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseReactiveIntegrationTests;
import org.springframework.data.aerospike.query.QueryParam;
import org.springframework.data.aerospike.sample.Customer;
import org.springframework.data.aerospike.sample.ReactiveCustomerRepository;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.util.List;

/**
* @author Igor Ermolenko
*/
Expand Down Expand Up @@ -57,4 +60,18 @@ public void existsByIdPublisher_ShouldCheckOnlyFirstElement() {
.subscribeOn(Schedulers.parallel()))
.expectNext(true).verifyComplete();
}

@Test
public void existsByIdAndFirstNameIn() {
QueryParam ids = QueryParam.of(List.of(customer1.getId(), customer2.getId()));
QueryParam firstNames = QueryParam.of(List.of(customer1.getFirstName(), customer2.getFirstName(), "FirstName"));
StepVerifier.create(customerRepo.existsByIdAndFirstNameIn(ids, firstNames))
.expectNext(true)
.verifyComplete();

firstNames = QueryParam.of(List.of("FirstName"));
StepVerifier.create(customerRepo.existsByIdAndFirstNameIn(ids, firstNames))
.expectNext(false)
.verifyComplete();
}
}
Loading

0 comments on commit 9d9d36d

Please sign in to comment.