Skip to content

Commit

Permalink
feat: remove incorrectly used dbQueryInner function
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 24, 2024
1 parent 2928733 commit d379354
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 74 deletions.
10 changes: 5 additions & 5 deletions app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.sakurasou.config

import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.DatabaseSingleton.dbQueryInner
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.album.AlbumDaoImpl
import io.sakurasou.model.dao.group.GroupDao
Expand Down Expand Up @@ -90,9 +89,10 @@ object InstanceCenter {
authService = AuthServiceImpl(userDao, relationDao)
groupService = GroupServiceImpl(groupDao, relationDao)

userService = UserServiceImpl(userDao, albumDao, settingService)
commonService = CommonServiceImpl(userDao, albumDao, settingService)

roleService = RoleServiceImpl(roleDao, permissionDao, relationDao)
userService = UserServiceImpl(userDao, albumService, settingService)
commonService = CommonServiceImpl(userDao, albumService, settingService)
}

fun initSystemStatus() {
Expand All @@ -104,9 +104,9 @@ object InstanceCenter {
fun initRolePermissions() {
rolePermissions = runBlocking {
dbQuery {
val roles = dbQueryInner { roleDao.listRoles() }
val roles = roleDao.listRoles()
roles.associate {
it.name to dbQueryInner { relationDao.listPermissionByRole(it.name).toSet() }
it.name to relationDao.listPermissionByRole(it.name).toSet()
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/kotlin/io/sakurasou/model/DatabaseSingleton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ object DatabaseSingleton {
}
}

suspend fun <T> dbQueryInner(block: suspend () -> T): T = newSuspendedTransaction { block() }

suspend fun <T> dbQuery(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import io.sakurasou.config.InstanceCenter
import io.sakurasou.controller.request.SiteInitRequest
import io.sakurasou.exception.SiteRepeatedInitializationException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.DatabaseSingleton.dbQueryInner
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.UserInsertDTO
import io.sakurasou.model.setting.SiteSetting
import io.sakurasou.model.setting.SystemStatus
import io.sakurasou.service.album.AlbumService
import io.sakurasou.service.setting.SettingService
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
Expand All @@ -22,7 +21,7 @@ import kotlinx.datetime.toLocalDateTime
*/
class CommonServiceImpl(
private val userDao: UserDao,
private val albumService: AlbumService,
private val albumDao: AlbumDao,
private val settingService: SettingService
) : CommonService {
override suspend fun initSite(siteInitRequest: SiteInitRequest) {
Expand Down Expand Up @@ -58,11 +57,11 @@ class CommonServiceImpl(
)

dbQuery {
val userId = dbQueryInner { userDao.saveUser(userInsertDTO) }
val defaultAlbumId = dbQueryInner { albumService.initAlbumForUser(userId) }
dbQueryInner { userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) }
dbQueryInner { settingService.updateSiteSetting(siteSettingConfig) }
dbQueryInner { settingService.updateSystemStatus(systemStatus) }
val userId = userDao.saveUser(userInsertDTO)
val defaultAlbumId = albumDao.initAlbumForUser(userId)
userDao.updateUserDefaultAlbumId(userId, defaultAlbumId)
settingService.updateSiteSetting(siteSettingConfig)
settingService.updateSystemStatus(systemStatus)
}
InstanceCenter.systemStatus = systemStatus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import io.sakurasou.controller.vo.GroupPageVO
import io.sakurasou.controller.vo.GroupVO
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.exception.GroupNotExistException
import io.sakurasou.exception.RoleNotExistException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.DatabaseSingleton.dbQueryInner
import io.sakurasou.model.dao.group.GroupDao
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dto.GroupInsertDTO
Expand All @@ -32,8 +32,12 @@ class GroupServiceImpl(
val groupRoles = insertRequest.roles

dbQuery {
val groupId = dbQueryInner { groupDao.saveGroup(groupInsertDTO) }
dbQueryInner { relationDao.batchInsertGroupToRoles(groupId, groupRoles) }
val groupId = groupDao.saveGroup(groupInsertDTO)
runCatching {
relationDao.batchInsertGroupToRoles(groupId, groupRoles)
}.onFailure {
throw RoleNotExistException()
}
}
}

Expand Down
13 changes: 6 additions & 7 deletions app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.sakurasou.controller.vo.RoleVO
import io.sakurasou.exception.PermissionNotExistException
import io.sakurasou.exception.RoleNotExistException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.DatabaseSingleton.dbQueryInner
import io.sakurasou.model.dao.permission.PermissionDao
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dao.role.RoleDao
Expand All @@ -21,11 +20,11 @@ class RoleServiceImpl(
) : RoleService {
override suspend fun listRolesWithPermissions(): Map<String, RoleVO> {
return dbQuery {
val roles = dbQueryInner { roleDao.listRoles() }
val roles = roleDao.listRoles()
roles.associate { role ->
val permissionNames = dbQueryInner { relationDao.listPermissionByRole(role.name) }
val permissionNames = relationDao.listPermissionByRole(role.name)
val permissionVOList = permissionNames.map { permissionName ->
val permission = dbQueryInner { permissionDao.findPermissionByName(permissionName) }
val permission = permissionDao.findPermissionByName(permissionName)
?: throw PermissionNotExistException()
PermissionVO(
permission.name,
Expand All @@ -44,10 +43,10 @@ class RoleServiceImpl(
override suspend fun listRolesWithPermissionsOfUser(roleNames: List<String>): Map<String, RoleVO> {
return dbQuery {
roleNames.associate { roleName ->
val role = dbQueryInner { roleDao.findRoleByName(roleName) } ?: throw RoleNotExistException()
val permissionNames = dbQueryInner { relationDao.listPermissionByRole(roleName) }
val role = roleDao.findRoleByName(roleName) ?: throw RoleNotExistException()
val permissionNames = relationDao.listPermissionByRole(roleName)
val permissionVOList = permissionNames.map { permissionName ->
val permission = dbQueryInner { permissionDao.findPermissionByName(permissionName) }
val permission = permissionDao.findPermissionByName(permissionName)
?: throw PermissionNotExistException()
PermissionVO(
permission.name,
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import at.favre.lib.crypto.bcrypt.BCrypt
import io.sakurasou.controller.request.UserInsertRequest
import io.sakurasou.exception.SignupNotAllowedException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.DatabaseSingleton.dbQueryInner
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.UserInsertDTO
import io.sakurasou.service.album.AlbumService
import io.sakurasou.service.setting.SettingService
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
Expand All @@ -19,7 +18,7 @@ import kotlinx.datetime.toLocalDateTime
*/
class UserServiceImpl(
private val userDao: UserDao,
private val albumService: AlbumService,
private val albumDao: AlbumDao,
private val settingService: SettingService
) : UserService {
override suspend fun saveUser(userInsertRequest: UserInsertRequest) {
Expand All @@ -42,9 +41,9 @@ class UserServiceImpl(
updateTime = now
)
dbQuery {
val userId = dbQueryInner { userDao.saveUser(userInsertDTO) }
val defaultAlbumId = dbQueryInner { albumService.initAlbumForUser(userId) }
dbQueryInner { userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) }
val userId = userDao.saveUser(userInsertDTO)
val defaultAlbumId = albumDao.initAlbumForUser(userId)
userDao.updateUserDefaultAlbumId(userId, defaultAlbumId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import io.mockk.*
import io.sakurasou.controller.request.SiteInitRequest
import io.sakurasou.exception.SiteRepeatedInitializationException
import io.sakurasou.model.DatabaseSingleton
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.UserInsertDTO
import io.sakurasou.model.setting.SiteSetting
import io.sakurasou.model.setting.SystemStatus
import io.sakurasou.service.album.AlbumService
import io.sakurasou.service.setting.SettingService
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
Expand All @@ -25,7 +25,7 @@ import kotlin.test.assertFailsWith
*/
class CommonServiceTest {
private lateinit var userDao: UserDao
private lateinit var albumService: AlbumService
private lateinit var albumDao: AlbumDao
private lateinit var settingService: SettingService
private lateinit var commonService: CommonServiceImpl

Expand All @@ -35,18 +35,12 @@ class CommonServiceTest {
mockkStatic(BCrypt::class)
mockkObject(Clock.System)
userDao = mockk()
albumService = mockk()
albumDao = mockk()
settingService = mockk()
commonService = CommonServiceImpl(userDao, albumService, settingService)
commonService = CommonServiceImpl(userDao, albumDao, settingService)
coEvery { DatabaseSingleton.dbQuery<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Long>(any()) } coAnswers {
this.arg<suspend () -> Long>(0).invoke()
}
}

@Test
Expand Down Expand Up @@ -110,7 +104,7 @@ class CommonServiceTest {
coEvery { settingService.getSiteSetting() } returns oldSiteSetting
every { BCrypt.withDefaults().hashToString(12, siteInitRequest.password.toCharArray()) } returns encodedPassword
coEvery { userDao.saveUser(userInsertDTO) } returns 1
coEvery { albumService.initAlbumForUser(1) } returns 1
coEvery { albumDao.initAlbumForUser(1) } returns 1
coEvery { userDao.updateUserDefaultAlbumId(1L, 1L) } just Runs
coEvery { settingService.updateSiteSetting(siteSettingConfig) } just Runs
coEvery { settingService.updateSystemStatus(systemStatus) } just Runs
Expand All @@ -119,10 +113,9 @@ class CommonServiceTest {

coVerify(exactly = 1) { DatabaseSingleton.dbQuery<Unit>(any()) }
coVerify(exactly = 1) { userDao.saveUser(userInsertDTO) }
coVerify(exactly = 1) { albumService.initAlbumForUser(1) }
coVerify(exactly = 1) { albumDao.initAlbumForUser(1) }
coVerify(exactly = 1) { userDao.updateUserDefaultAlbumId(1L, 1L) }
coVerify(exactly = 1) { settingService.updateSiteSetting(siteSettingConfig) }
coVerify(exactly = 1) { settingService.updateSystemStatus(systemStatus) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ class GroupServiceTest {
coEvery { DatabaseSingleton.dbQuery<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Long>(any()) } coAnswers {
this.arg<suspend () -> Long>(0).invoke()
}
every { groupDao.saveGroup(exceptedInsertDTO) } returns 1
every { relationDao.batchInsertGroupToRoles(1, exceptedRoles) } just Runs

Expand Down
12 changes: 0 additions & 12 deletions app/src/test/kotlin/io/sakurasou/service/role/RoleServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ class RoleServiceTest {
coEvery { DatabaseSingleton.dbQuery<Map<String, RoleVO>>(any()) } coAnswers {
this.arg<suspend () -> Map<String, RoleVO>>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<List<String>>(any()) } coAnswers {
this.arg<suspend () -> List<String>>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Permission?>(any()) } coAnswers {
this.arg<suspend () -> Permission?>(0).invoke()
}

val rolesWithPermissions = roleService.listRolesWithPermissions()

Expand All @@ -100,12 +94,6 @@ class RoleServiceTest {
coEvery { DatabaseSingleton.dbQuery<Map<String, RoleVO>>(any()) } coAnswers {
this.arg<suspend () -> Map<String, RoleVO>>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<List<String>>(any()) } coAnswers {
this.arg<suspend () -> List<String>>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Permission?>(any()) } coAnswers {
this.arg<suspend () -> Permission?>(0).invoke()
}

val rolesWithPermissions = roleService.listRolesWithPermissionsOfUser(listOf(role1.name))

Expand Down
18 changes: 6 additions & 12 deletions app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import io.mockk.*
import io.sakurasou.controller.request.UserInsertRequest
import io.sakurasou.exception.SignupNotAllowedException
import io.sakurasou.model.DatabaseSingleton
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.UserInsertDTO
import io.sakurasou.model.setting.SystemSetting
import io.sakurasou.service.album.AlbumService
import io.sakurasou.service.setting.SettingService
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
Expand All @@ -24,7 +24,7 @@ import kotlin.test.assertFailsWith
*/
class UserServiceTest {
private lateinit var userDao: UserDao
private lateinit var albumService: AlbumService
private lateinit var albumDao: AlbumDao
private lateinit var settingService: SettingService
private lateinit var userService: UserServiceImpl

Expand All @@ -34,18 +34,12 @@ class UserServiceTest {
mockkStatic(BCrypt::class)
mockkObject(Clock.System)
userDao = mockk()
albumService = mockk()
albumDao = mockk()
settingService = mockk()
userService = UserServiceImpl(userDao, albumService, settingService)
userService = UserServiceImpl(userDao, albumDao, settingService)
coEvery { DatabaseSingleton.dbQuery<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Unit>(any()) } coAnswers {
this.arg<suspend () -> Unit>(0).invoke()
}
coEvery { DatabaseSingleton.dbQueryInner<Long>(any()) } coAnswers {
this.arg<suspend () -> Long>(0).invoke()
}
}

@Test
Expand Down Expand Up @@ -99,14 +93,14 @@ class UserServiceTest {
BCrypt.withDefaults().hashToString(12, userInsertRequest.password.toCharArray())
} returns encodedPassword
coEvery { userDao.saveUser(userInsertDTO) } returns 1
coEvery { albumService.initAlbumForUser(1L) } returns 1
coEvery { albumDao.initAlbumForUser(1L) } returns 1
coEvery { userDao.updateUserDefaultAlbumId(1L, 1L) } just Runs

userService.saveUser(userInsertRequest)

coVerify(exactly = 1) { DatabaseSingleton.dbQuery<Unit>(any()) }
coVerify(exactly = 1) { userDao.saveUser(userInsertDTO) }
coVerify(exactly = 1) { albumService.initAlbumForUser(1L) }
coVerify(exactly = 1) { albumDao.initAlbumForUser(1L) }
coVerify(exactly = 1) { userDao.updateUserDefaultAlbumId(1L, 1L) }
}
}

0 comments on commit d379354

Please sign in to comment.