Skip to content

Commit

Permalink
test: create find all scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Nov 29, 2023
1 parent b118143 commit 84b8a24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/src/main/java/expert/os/harperdb/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public <K, T> Optional<T> findById(K id, Class<T> type) {
public <K, T> List<T> findAllById(Iterable<K> ids, Class<T> type) {
Objects.requireNonNull(ids, "ids is required");
Objects.requireNonNull(type, "type is required");
if (ids.spliterator().getExactSizeIfKnown() == 0) {
return Collections.emptyList();
}
var keys = StreamSupport.stream(ids.spliterator(), false)
.collect(Collectors.toSet());
var search = new SearchById<>(database, table(type), keys, ALL_ATTRIBUTES);
Expand Down
27 changes: 26 additions & 1 deletion core/src/test/java/expert/os/harperdb/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.testcontainers.shaded.org.checkerframework.checker.units.qual.A;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -80,7 +82,30 @@ void shouldFindAllById(List<Animal> animals){
template.insert(animals);
var ids = animals.stream().map(Animal::id).toList();
List<Animal> entities = template.findAllById(ids, Animal.class);
Assertions.assertThat(entities).hasSize(animals.size());
Assertions.assertThat(entities).hasSize(animals.size()).map(Animal::id).containsAll(ids);
}

@ParameterizedTest
@MethodSource("animals")
void shouldReturnEmptyWhenThereAreNoIds(List<Animal> animals){
var ids = animals.stream().map(Animal::id).toList();
var entities = template.findAllById(ids, Animal.class);
Assertions.assertThat(entities).isEmpty();
Assertions.assertThat(template.findAllById(Collections.emptyList(), Animal.class)).isEmpty();
}

@ParameterizedTest
@MethodSource("animal")
void shouldFindWhatThereAtTheDatabase(Animal animal){
template.insert(animal);
var ids = List.of(FAKER.idNumber().valid(), FAKER.idNumber().valid(), animal.id());
var entities = template.findAllById(ids, Animal.class);
Assertions.assertThat(entities).hasSize(1);
Animal entity = entities.get(0);
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(entity.id()).isEqualTo(animal.id());
softly.assertThat(entity.name()).isEqualTo(animal.name());
});
}

static Stream<Arguments> animal(){
Expand Down

0 comments on commit 84b8a24

Please sign in to comment.