Skip to content

Commit

Permalink
Fix to edit UserEntity & Add User, School entities test code
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 10, 2024
1 parent 5e3b74d commit 98bfce8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class UserEntity(
fun active(nickname: String, graduatingYear: Int, school: SchoolEntity) {
this.state = UserState.NONE
this.nickname = nickname
this.graduatingYear = graduatingYear
this.school = school
updateGraduatingYear(graduatingYear)
}

fun update(
Expand All @@ -72,14 +72,18 @@ class UserEntity(
this.nickname = nickname
}
if (graduatingYear != null) {
val school = school ?: this.school
if (school?.type != null) {
val currentYear = LocalDateTime.now().year
this.graduatingYear = graduatingYear.coerceIn(currentYear + 1, currentYear + school.type.limit)
}
updateGraduatingYear(graduatingYear)
}
if (school != null) {
this.school = school
this.graduatingYear?.let(this::updateGraduatingYear)
}
}

private fun updateGraduatingYear(graduatingYear: Int) {
school?.type?.let { type ->
val currentYear = LocalDateTime.now().year
this.graduatingYear = graduatingYear.coerceIn(currentYear + 1, currentYear + type.limit)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class UserControllerTest {
.contentType(MediaType.APPLICATION_JSON)
.content(
EditUserReq(
nickname = editNickname
nickname = editNickname,
graduatingYear = null,
schoolId = null
).toJson()
)
.header("Authorization", "Bearer ${token.accessToken}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.bestswlkh0310.graduating.graduatingserver.entity

import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolEntity
import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolType
import java.time.LocalDate

class SchoolEntityTest {
companion object {
val name = "schoolName"
val cityName = "cityName"
val postalCode = "postalCode"
val address = "address"
val addressDetail = "addressDetail"
val phone = "010-0000-0000"
val website = "about:blank"
val createdAt = LocalDate.now()
val anniversary = LocalDate.now()
val code = "code"
val officeCode = "officeCode"

val highSchool = SchoolEntity(
name = name,
type = SchoolType.HIGH,
cityName = cityName,
postalCode = postalCode,
address = address,
addressDetail = addressDetail,
phone = phone,
website = website,
createdAt = createdAt,
anniversary = anniversary,
code = code,
officeCode = officeCode,
)

val middleSchool = SchoolEntity(
name = name,
type = SchoolType.MIDDLE,
cityName = cityName,
postalCode = postalCode,
address = address,
addressDetail = addressDetail,
phone = phone,
website = website,
createdAt = createdAt,
anniversary = anniversary,
code = code,
officeCode = officeCode,
)

val elementarySchool = SchoolEntity(
name = name,
type = SchoolType.ELEMENTARY,
cityName = cityName,
postalCode = postalCode,
address = address,
addressDetail = addressDetail,
phone = phone,
website = website,
createdAt = createdAt,
anniversary = anniversary,
code = code,
officeCode = officeCode,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bestswlkh0310.graduating.graduatingserver.entity

import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserEntity
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRole
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserState
import org.junit.jupiter.api.Test
import java.time.LocalDate
import kotlin.test.assertEquals
import kotlin.test.assertNull

class UserEntityTest {
companion object {
val email = "[email protected]"
val platformType = PlatformType.GOOGLE
val user = UserEntity(
email = email,
platformType = platformType,
)
val nickname = "testNickname"
val veryDistantFuture = 1234567891
val veryDistantPast = 1
}

@Test
fun `create user entity`() {
assertEquals(email, user.email)
assertEquals(UserRole.USER, user.role)
assertEquals(platformType, user.platformType)
assertEquals(UserState.PENDING, user.state)
assertNull(user.nickname)
assertNull(user.graduatingYear)
assertNull(user.school)
}

@Test
fun `active user entity`() {
user.active(
nickname = nickname,
graduatingYear = veryDistantFuture,
school = SchoolEntityTest.highSchool
)
assertEquals(email, user.email)
assertEquals(nickname, user.nickname)
// 년도에 따라 테스크 코드가 달리지는 경우 어떻게 해야 하지
assertEquals(2027, user.graduatingYear)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.bestswlkh0310.graduating.graduatingserver.util

import com.bestswlkh0310.graduating.graduatingserver.api.auth.res.TokenRes
import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserEntity
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRepository
import com.bestswlkh0310.graduating.graduatingserver.infra.token.JwtClient
import com.bestswlkh0310.graduating.graduatingserver.infra.token.Token

object TestUtil {

var token: TokenRes? = null
var token: Token? = null
var user: UserEntity? = null

fun initializeToken(
Expand Down

0 comments on commit 98bfce8

Please sign in to comment.