From 675cb12ed1fe862b45a37424b69b7bc370899038 Mon Sep 17 00:00:00 2001 From: kihwankim Date: Sat, 18 Nov 2023 00:24:05 +0900 Subject: [PATCH] test: add findOne integration and unit test --- .../jpa/javax/jpql/select/SelectExample.kt | 35 ++++++++++++++++++ .../data/jpa/jpql/select/SelectExample.kt | 35 ++++++++++++++++++ .../KotlinJdslJpqlExecutorImplTest.kt | 37 +++++++++++++++++++ .../KotlinJdslJpqlExecutorImplTest.kt | 37 +++++++++++++++++++ 4 files changed, 144 insertions(+) diff --git a/example/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/javax/jpql/select/SelectExample.kt b/example/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/javax/jpql/select/SelectExample.kt index 5f30e16c0..3d7ec51ae 100644 --- a/example/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/javax/jpql/select/SelectExample.kt +++ b/example/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/javax/jpql/select/SelectExample.kt @@ -12,8 +12,10 @@ import com.linecorp.kotlinjdsl.example.spring.data.jpa.javax.jpql.repository.boo import com.linecorp.kotlinjdsl.example.spring.data.jpa.javax.jpql.repository.employee.EmployeeRepository import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Sort import org.springframework.transaction.annotation.Transactional @@ -31,6 +33,39 @@ class SelectExample : WithAssertions { @Autowired private lateinit var employeeRepository: EmployeeRepository + @Test + fun `only one author by id`() { + // when + val actual = authorRepository.findOne { + select( + path(Author::authorId), + ).from( + entity(Author::class), + ).where( + path(Author::authorId).equal(1L), + ) + } + + // then + assertThat(actual).isEqualTo(1L) + } + + @Test + fun `throw if result has more than 2 rows`() { + // when, then + assertThrows { + authorRepository.findOne { + select( + path(BookAuthor::authorId), + ).from( + entity(BookAuthor::class), + ).where( + path(BookAuthor::authorId).equal(1L), + ) + } + } + } + @Test fun `the most prolific author`() { // when diff --git a/example/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/SelectExample.kt b/example/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/SelectExample.kt index eb731f5f8..18f9a2481 100644 --- a/example/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/SelectExample.kt +++ b/example/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/SelectExample.kt @@ -12,8 +12,10 @@ import com.linecorp.kotlinjdsl.example.spring.data.jpa.jpql.repository.book.Book import com.linecorp.kotlinjdsl.example.spring.data.jpa.jpql.repository.employee.EmployeeRepository import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Sort import org.springframework.transaction.annotation.Transactional @@ -31,6 +33,39 @@ class SelectExample : WithAssertions { @Autowired private lateinit var employeeRepository: EmployeeRepository + @Test + fun `only one author by id`() { + // when + val actual = authorRepository.findOne { + select( + path(Author::authorId), + ).from( + entity(Author::class), + ).where( + path(Author::authorId).equal(1L), + ) + } + + // then + assertThat(actual).isEqualTo(1L) + } + + @Test + fun `throw if result has more than 2 rows`() { + // when, then + assertThrows { + authorRepository.findOne { + select( + path(BookAuthor::authorId), + ).from( + entity(BookAuthor::class), + ).where( + path(BookAuthor::authorId).equal(1L), + ) + } + } + } + @Test fun `the most prolific author`() { // when diff --git a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt index 38ad6ed93..afd677bf1 100644 --- a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt +++ b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt @@ -19,7 +19,9 @@ import io.mockk.verifySequence import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Slice @@ -129,6 +131,41 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { excludeRecords { deleteQuery2.toQuery() } } + @Test + fun findOne() { + // given + val result = listOf("1") + every { JpqlEntityManagerUtils.createQuery(any(), any>(), any()) } returns typedQuery1 + every { typedQuery1.resultList } returns result + + // when + val actual = sut.findOne(createSelectQuery1) + + // then + assertThat(actual).isEqualTo("1") + verifySequence { + JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext) + typedQuery1.resultList + } + } + + @Test + fun `findOne() throw if result has more than 2 rows`() { + // given + val result = listOf("1", "2") + every { JpqlEntityManagerUtils.createQuery(any(), any>(), any()) } returns typedQuery1 + every { typedQuery1.resultList } returns result + + // when, then + assertThrows { + sut.findOne(createSelectQuery1) + } + verifySequence { + JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext) + typedQuery1.resultList + } + } + @Test fun findAll() { // given diff --git a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt index a71e8b4fd..69785a79b 100644 --- a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt +++ b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt @@ -22,7 +22,9 @@ import jakarta.persistence.TypedQuery import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Slice @@ -129,6 +131,41 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { excludeRecords { deleteQuery2.toQuery() } } + @Test + fun findOne() { + // given + val result = listOf("1") + every { JpqlEntityManagerUtils.createQuery(any(), any>(), any()) } returns typedQuery1 + every { typedQuery1.resultList } returns result + + // when + val actual = sut.findOne(createSelectQuery1) + + // then + assertThat(actual).isEqualTo("1") + verifySequence { + JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext) + typedQuery1.resultList + } + } + + @Test + fun `findOne() throw if result has more than 2 rows`() { + // given + val result = listOf("1", "2") + every { JpqlEntityManagerUtils.createQuery(any(), any>(), any()) } returns typedQuery1 + every { typedQuery1.resultList } returns result + + // when, then + assertThrows { + sut.findOne(createSelectQuery1) + } + verifySequence { + JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext) + typedQuery1.resultList + } + } + @Test fun findAll() { // given