Skip to content

Commit

Permalink
fix: unit test of albumService
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Nov 9, 2024
1 parent 056a9b9 commit 70d2d5c
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import io.sakurasou.exception.service.album.AlbumUpdateFailedException
import io.sakurasou.model.DatabaseSingleton
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.image.ImageDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.AlbumInsertDTO
import io.sakurasou.model.dto.AlbumUpdateDTO
import io.sakurasou.model.entity.Album
import io.sakurasou.model.entity.User
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.*
import kotlin.test.BeforeTest
Expand All @@ -26,6 +28,7 @@ import kotlin.test.assertFailsWith
* 2024/9/13 21:47
*/
class AlbumServiceTest {
private lateinit var userDao: UserDao
private lateinit var albumDao: AlbumDao
private lateinit var imageDao: ImageDao
private lateinit var albumService: AlbumServiceImpl
Expand All @@ -36,9 +39,10 @@ class AlbumServiceTest {
fun setUp() {
mockkObject(DatabaseSingleton)
mockkObject(Clock.System)
userDao = mockk()
albumDao = mockk()
imageDao = mockk()
albumService = AlbumServiceImpl(albumDao, imageDao)
albumService = AlbumServiceImpl(userDao, albumDao, imageDao)
instant = Clock.System.now()
every { Clock.System.now() } returns instant
now = instant.toLocalDateTime(TimeZone.currentSystemDefault())
Expand Down Expand Up @@ -205,19 +209,33 @@ class AlbumServiceTest {
isUncategorized = false,
createTime = now
)
val user = User(
id = userId,
name = "testUser",
password = "testPassword",
email = "[email protected]",
groupId = 1,
isDefaultImagePrivate = true,
defaultAlbumId = albumId,
isBanned = false,
createTime = now,
updateTime = now
)
val expected = AlbumVO(
id = albumId,
name = "My Album",
description = "Test Album",
imageCount = 0,
isUncategorized = false,
isDefault = true,
createTime = album.createTime
)

coEvery { DatabaseSingleton.dbQuery<AlbumVO>(any()) } coAnswers {
this.arg<suspend () -> AlbumVO>(0).invoke()
}
every { albumDao.findAlbumById(albumId) } returns album
every { userDao.findUserById(userId) } returns user
every { imageDao.countImageByAlbumId(albumId) } returns 0

val result = albumService.fetchSelf(userId, albumId)
Expand All @@ -238,11 +256,24 @@ class AlbumServiceTest {
isUncategorized = false,
createTime = now
)
val user = User(
id = userId,
name = "testUser",
password = "testPassword",
email = "[email protected]",
groupId = 1,
isDefaultImagePrivate = true,
defaultAlbumId = albumId,
isBanned = false,
createTime = now,
updateTime = now
)

coEvery { DatabaseSingleton.dbQuery<Album>(any()) } coAnswers {
this.arg<suspend () -> Album>(0).invoke()
}
every { albumDao.findAlbumById(albumId) } returns album
every { userDao.findUserById(userId) } returns user

assertFailsWith<AlbumAccessDeniedException> {
albumService.fetchSelf(userId, albumId)
Expand Down

0 comments on commit 70d2d5c

Please sign in to comment.