From 7c68f1f971cc8e3bde21eba2d48bc3a15b62e2c4 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 30 Mar 2024 18:35:11 +0000 Subject: [PATCH] test: create database manager scenario Signed-off-by: Otavio Santana --- .../semistructured/DatabaseManagerTest.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java b/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java index cb440ad80..34875dd27 100644 --- a/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java +++ b/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java @@ -11,6 +11,7 @@ package org.eclipse.jnosql.communication.semistructured; +import jakarta.data.exceptions.NonUniqueResultException; import jakarta.data.page.CursoredPage; import jakarta.data.page.PageRequest; import org.assertj.core.api.Assertions; @@ -451,7 +452,7 @@ void shouldReturnZeroWhenCountIsEmpty(){ @Test void shouldExists(){ SelectQuery query = SelectQuery.select().from("person").build(); - Mockito.when(databaseManager.select(query)).thenReturn(stream()); + Mockito.when(databaseManager.select(Mockito.any())).thenReturn(stream()); boolean exists = databaseManager.exists(query); Assertions.assertThat(exists).isTrue(); @@ -459,8 +460,8 @@ void shouldExists(){ @Test void shouldNotExists(){ - SelectQuery query = SelectQuery.select().from("person").build(); - Mockito.when(databaseManager.select(query)).thenReturn(Stream.empty()); + var query = SelectQuery.select().from("person").build(); + Mockito.when(databaseManager.select(Mockito.any())).thenReturn(Stream.empty()); boolean exists = databaseManager.exists(query); Assertions.assertThat(exists).isFalse(); @@ -481,6 +482,34 @@ void shouldPrepare(){ Assertions.assertThat(prepare).isNotNull(); } + @Test + void shouldReturnErrorSingleResult(){ + SelectQuery query = SelectQuery.select().from("person").build(); + Mockito.when(databaseManager.select(query)).thenReturn(stream()); + + Assertions.assertThatThrownBy(() -> databaseManager.singleResult(query)) + .isInstanceOf(NonUniqueResultException.class); + + } + + @Test + void shouldSingleResult(){ + SelectQuery query = SelectQuery.select().from("person").build(); + Mockito.when(databaseManager.select(query)).thenReturn(Stream.of(CommunicationEntity.of("name"))); + + var entity = databaseManager.singleResult(query); + Assertions.assertThat(entity).isPresent(); + } + + @Test + void shouldReturnEmptyAtSingleResult(){ + SelectQuery query = SelectQuery.select().from("person").build(); + Mockito.when(databaseManager.select(query)).thenReturn(Stream.empty()); + + var entity = databaseManager.singleResult(query); + Assertions.assertThat(entity).isEmpty(); + } + private Stream stream() { var entity = CommunicationEntity.of("name");