Skip to content

Commit

Permalink
test: add findOne integration and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kihwankim committed Nov 17, 2023
1 parent 4bd39f4 commit 675cb12
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<IncorrectResultSizeDataAccessException> {
authorRepository.findOne {
select(
path(BookAuthor::authorId),
).from(
entity(BookAuthor::class),
).where(
path(BookAuthor::authorId).equal(1L),
)
}
}
}

@Test
fun `the most prolific author`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<IncorrectResultSizeDataAccessException> {
authorRepository.findOne {
select(
path(BookAuthor::authorId),
).from(
entity(BookAuthor::class),
).where(
path(BookAuthor::authorId).equal(1L),
)
}
}
}

@Test
fun `the most prolific author`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,6 +131,41 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions {
excludeRecords { deleteQuery2.toQuery() }
}

@Test
fun findOne() {
// given
val result = listOf("1")
every { JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), 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<SelectQuery<String>>(), any()) } returns typedQuery1
every { typedQuery1.resultList } returns result

// when, then
assertThrows<IncorrectResultSizeDataAccessException> {
sut.findOne(createSelectQuery1)
}
verifySequence {
JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext)
typedQuery1.resultList
}
}

@Test
fun findAll() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,6 +131,41 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions {
excludeRecords { deleteQuery2.toQuery() }
}

@Test
fun findOne() {
// given
val result = listOf("1")
every { JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), 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<SelectQuery<String>>(), any()) } returns typedQuery1
every { typedQuery1.resultList } returns result

// when, then
assertThrows<IncorrectResultSizeDataAccessException> {
sut.findOne(createSelectQuery1)
}
verifySequence {
JpqlEntityManagerUtils.createQuery(entityManager, selectQuery1, renderContext)
typedQuery1.resultList
}
}

@Test
fun findAll() {
// given
Expand Down

0 comments on commit 675cb12

Please sign in to comment.