Skip to content

Commit

Permalink
Merge pull request #216 from swm-standard/seyeon/swm-177
Browse files Browse the repository at this point in the history
Fix: ํŠธ๋žœ์žญ์…˜ ์„ค์ • ๋ฐ fixtureMonkey ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ „์ฒด ์ ์šฉ
  • Loading branch information
adorableco authored Sep 12, 2024
2 parents 89cbfb8 + 816b162 commit 47263f9
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 276 deletions.
5 changes: 2 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id("org.springframework.boot") version "3.3.1"
id("io.spring.dependency-management") version "1.1.5"
id("org.asciidoctor.jvm.convert") version "3.3.2"
id("io.sentry.jvm.gradle") version "4.8.0"
id("org.jlleitschuh.gradle.ktlint") version "11.1.0"
kotlin("plugin.jpa") version "1.9.24"
kotlin("jvm") version "1.9.24"
Expand Down Expand Up @@ -65,7 +64,7 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.9.0-RC")
implementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-kotlin:1.0.23")
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-kotlin:1.0.25")

// querydsl
implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta")
Expand Down Expand Up @@ -186,7 +185,7 @@ tasks.jacocoTestCoverageVerification {
limit {
counter = "METHOD"
value = "COVEREDRATIO"
minimum = 0.40.toBigDecimal()
minimum = 0.30.toBigDecimal()
}

val qDomains = emptyList<String>().toMutableList()
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/swm_standard/phote/entity/Workbook.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.swm_standard.phote.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.swm_standard.phote.common.exception.BadRequestException
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
Expand Down Expand Up @@ -75,6 +76,10 @@ data class Workbook(
}

fun decreaseQuantity() {
if (this.quantity - 1 < 0) {
throw BadRequestException(fieldName = "quantity", message = "์‚ญ์ œ ์‹œ quantity๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.")
}

this.quantity -= 1
modifiedAt = LocalDateTime.now()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.swm_standard.phote.service

import com.swm_standard.phote.repository.MemberRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.UUID

@Service
class MemberService(
private val memberRepository: MemberRepository,
) {
@Transactional
fun deleteMember(memberId: UUID): UUID {
memberRepository.deleteById(memberId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.swm_standard.phote.common.exception.ExpiredTokenException
import com.swm_standard.phote.entity.RefreshToken
import com.swm_standard.phote.repository.RefreshTokenRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.UUID
import kotlin.jvm.optionals.getOrElse

@Service
@Transactional(readOnly = true)
class TokenService(
private val refreshTokenRepository: RefreshTokenRepository,
private val jwtTokenProvider: JwtTokenProvider,
Expand All @@ -22,6 +24,7 @@ class TokenService(
return jwtTokenProvider.createToken(refresh.memberId)
}

@Transactional
fun generateRefreshToken(memberId: UUID): UUID {
val refreshToken =
refreshTokenRepository.findByMemberId(memberId) ?: RefreshToken(UUID.randomUUID(), memberId)
Expand Down
124 changes: 38 additions & 86 deletions src/test/kotlin/com/swm_standard/phote/entity/AnswerTest.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
package com.swm_standard.phote.entity

import org.assertj.core.api.Assertions
import com.navercorp.fixturemonkey.FixtureMonkey
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector
import com.navercorp.fixturemonkey.kotlin.KotlinPlugin
import com.navercorp.fixturemonkey.kotlin.giveMeBuilder
import com.navercorp.fixturemonkey.kotlin.giveMeOne
import com.navercorp.fixturemonkey.kotlin.setExp
import net.jqwik.api.Arbitraries
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.UUID

class AnswerTest {
private val fixtureMonkey =
FixtureMonkey
.builder()
.plugin(KotlinPlugin())
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
.build()

@Test
fun `๋ฌธ์ œ๊ฐ€ ๊ฐ๊ด€์‹์ด๋ฉด ์ •์˜ค๋‹ต ์ฒดํฌํ•œ๋‹ค`() {
val submittedAnswer = Arbitraries.strings().numeric().sample()
val category = Category.MULTIPLE
val submittedAnswer = "1"
val correctAnswer = "5"
val answer = createAnswer(category, submittedAnswer, correctAnswer)
val correctAnswer = Arbitraries.strings().numeric().sample()
val answer =
fixtureMonkey
.giveMeBuilder<Answer>()
.setExp(Answer::submittedAnswer, submittedAnswer)
.setExp(
Answer::question,
fixtureMonkey.giveMeBuilder<Question>().setExp(Question::answer, correctAnswer).sample(),
).sample()

answer.checkMultipleAnswer()

Assertions.assertThat(submittedAnswer == correctAnswer).isEqualTo(answer.isCorrect)
Assertions.assertThat(answer.isCorrect).isFalse()
assertThat(submittedAnswer == correctAnswer).isEqualTo(answer.isCorrect)
assertThat(answer.isCorrect).isFalse()
}

@Test
fun `์ œ์ถœํ•œ ๋‹ต์•ˆ์„ ์ƒ์„ฑํ•œ๋‹ค`() {
val exam = createExam()
val submittedAnswer = "1"
val question = createQuestion(answer = submittedAnswer)
val sequence = 2
val exam: Exam = fixtureMonkey.giveMeOne()
val submittedAnswer = Arbitraries.strings().numeric().sample()
val question =
fixtureMonkey
.giveMeBuilder<Question>()
.setExp(Question::answer, submittedAnswer)
.sample()
val sequence = Arbitraries.integers().sample()

val createAnswer =
Answer.createAnswer(
Expand All @@ -33,80 +57,8 @@ class AnswerTest {
sequence = sequence,
)

Assertions.assertThat(createAnswer.submittedAnswer).isEqualTo(submittedAnswer)
Assertions.assertThat(createAnswer.exam).isEqualTo(exam)
Assertions.assertThat(createAnswer.sequence).isEqualTo(sequence)
assertThat(createAnswer.submittedAnswer).isEqualTo(submittedAnswer)
assertThat(createAnswer.exam).isEqualTo(exam)
assertThat(createAnswer.sequence).isEqualTo(sequence)
}

fun createWorkbook(): Workbook =
Workbook(
title = "hinc",
description = null,
member = createMember(),
emoji = "๐Ÿ“š",
)

fun createExam() =
Exam(
member = createMember(),
workbook = createWorkbook(),
sequence = 4282,
time = 40,
)

fun createAnswer(
category: Category,
submittedAnswer: String?,
correctAnswer: String,
) = Answer(
question =
Question(
id = UUID.randomUUID(),
member =
Member(
name = "Erik Ashley",
email = "[email protected]",
image = "per",
provider = Provider.APPLE,
),
statement = "Kentucky",
options = null,
image = null,
answer = correctAnswer,
category = category,
questionSet = listOf(),
memo = null,
),
exam = createExam(),
submittedAnswer = submittedAnswer,
sequence = 2017,
)

private fun createQuestion(answer: String) =
Question(
id = UUID.randomUUID(),
member =
Member(
name = "Charmaine Joseph",
email = "[email protected]",
image = "maiorum",
provider = Provider.APPLE,
),
statement = "Missouri",
options = null,
image = null,
answer = answer,
category = Category.MULTIPLE,
questionSet = listOf(),
tags = mutableListOf(),
memo = null,
)

private fun createMember() =
Member(
name = "Wilbur Noel",
email = "[email protected]",
image = "ocurreret",
provider = Provider.APPLE,
)
}
92 changes: 31 additions & 61 deletions src/test/kotlin/com/swm_standard/phote/entity/ExamTest.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
package com.swm_standard.phote.entity

import org.assertj.core.api.Assertions
import com.navercorp.fixturemonkey.FixtureMonkey
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector
import com.navercorp.fixturemonkey.kotlin.KotlinPlugin
import com.navercorp.fixturemonkey.kotlin.giveMeBuilder
import com.navercorp.fixturemonkey.kotlin.giveMeOne
import com.navercorp.fixturemonkey.kotlin.setExp
import com.navercorp.fixturemonkey.kotlin.sizeExp
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class ExamTest {
private fun createExam(): Exam {
val member = Member("phote", "[email protected]", "imageUrl", Provider.KAKAO)
val workbook = Workbook.createWorkbook(title = "testTitle", description = "", member = member)

val exam =
Exam(
member = member,
workbook = workbook,
sequence = 1,
time = 30,
)

val answer =
Answer(
question =
Question(
member = member,
statement = "๋ชจ๋“  ๊ฐ์ด ๋™์ผํ•œ ์‚ผ๊ฐํ˜•์€?",
image = "http://example.com/image.jpg",
answer = "์ •์‚ผ๊ฐํ˜•",
category = Category.ESSAY,
memo = "์‚ผ๊ฐํ˜• ๋‚ด๊ฐ์˜ ํ•ฉ์€ 180๋„์ด๋‹ค.",
),
exam = exam,
submittedAnswer = "์ •์‚ผ๊ฐํ˜•",
sequence = 1,
)

answer.isCorrect = true

exam.answers.add(answer)
exam.answers.add(answer)

return exam
}
private val fixtureMonkey =
FixtureMonkey
.builder()
.plugin(KotlinPlugin())
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
.build()

@Test
fun `๋ฌธ์ œ ํ’€์ดํ•œ ์ด ๋ฌธ์ œ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค`() {
// given
val exam = createExam()
val exam: Exam =
fixtureMonkey
.giveMeBuilder<Exam>()
.sizeExp(Exam::answers, 2)
.sample()

// when
val totalQuantity = exam.calculateTotalQuantity()
Expand All @@ -55,42 +37,30 @@ class ExamTest {

@Test
fun `์‹œํ—˜ ์ƒ์„ฑ์— ์„ฑ๊ณตํ•œ๋‹ค`() {
val workbook = createWorkbook()
val member = createMember()
val workbook: Workbook = fixtureMonkey.giveMeOne()
val member: Member = fixtureMonkey.giveMeOne()
val sequence: Int = 2
val time = 20

val exam = Exam.createExam(member, workbook, sequence, time)

Assertions.assertThat(exam.workbook).isEqualTo(workbook)
Assertions.assertThat(exam.member.name).isEqualTo(member.name)
Assertions.assertThat(exam.sequence).isEqualTo(sequence)
assertThat(exam.workbook).isEqualTo(workbook)
assertThat(exam.member.name).isEqualTo(member.name)
assertThat(exam.sequence).isEqualTo(sequence)
}

@Test
fun `totalCorrect๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค`() {
val exam = createExam()
val count = 3
val totalCorrect = exam.totalCorrect
val totalCorrect = 2
val exam =
fixtureMonkey
.giveMeBuilder<Exam>()
.setExp(Exam::totalCorrect, totalCorrect)
.sample()

exam.increaseTotalCorrect(count)

Assertions.assertThat(exam.totalCorrect).isEqualTo(totalCorrect + count)
assertThat(exam.totalCorrect).isEqualTo(totalCorrect + count)
}

fun createWorkbook(): Workbook =
Workbook(
title = "hinc",
description = null,
member = createMember(),
emoji = "๐Ÿ“š",
)

fun createMember(): Member =
Member(
name = "Mayra Payne",
email = "[email protected]",
image = "dicant",
provider = Provider.APPLE,
)
}
Loading

0 comments on commit 47263f9

Please sign in to comment.