-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
test: 퀴즈 게임 도메인 단위테스트추가
- Loading branch information
Showing
10 changed files
with
275 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package yjh.cstar.engine.domain.player | ||
|
||
import yjh.cstar.common.BaseErrorCode | ||
import yjh.cstar.common.BaseException | ||
|
||
class Players(val players: Map<Long, String>) { | ||
|
||
init { | ||
require(players.isNotEmpty()) { | ||
throw BaseException(BaseErrorCode.INTERNAL_SERVER_ERROR) | ||
} | ||
} | ||
|
||
companion object { | ||
fun of(players: Map<Long, String>) = Players(players) | ||
} | ||
|
||
fun getNickname(winnerId: Long) = players[winnerId] ?: "None" | ||
fun getNickname(winnerId: Long) = players[winnerId] | ||
?: throw BaseException(BaseErrorCode.INTERNAL_SERVER_ERROR) | ||
|
||
fun getPlayerIds(): List<Long> = players.keys.toList() | ||
} |
13 changes: 7 additions & 6 deletions
13
src/main/kotlin/yjh/cstar/engine/domain/quiz/PlayerAnswer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package yjh.cstar.engine.domain.quiz | ||
|
||
class PlayerAnswer(val roomId: Long, val playerId: Long, val quizId: Long, val playerAnswer: String) { | ||
class PlayerAnswer( | ||
val roomId: Long, | ||
val playerId: Long, | ||
val quizId: Long, | ||
val playerAnswer: String, | ||
) { | ||
|
||
fun isMatch(quizAnswer: String) = playerAnswer.equals(quizAnswer) | ||
|
||
fun isCorrect(quiz: Quiz): Boolean { | ||
return quiz.isSameAnswer(playerAnswer) | ||
} | ||
fun isCorrect(quiz: Quiz) = quiz.isSameAnswer(playerAnswer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
package yjh.cstar.engine.domain.quiz | ||
|
||
class Quizzes(private val quizzes: List<Quiz>) { | ||
|
||
companion object { | ||
fun of(quizzes: List<QuizDto>): Quizzes { | ||
return Quizzes(quizzes.map { quizDto -> quizDto.toModel() }) | ||
val quizList = quizzes.map { quizDto -> quizDto.toModel() } | ||
return Quizzes(quizList) | ||
} | ||
} | ||
|
||
fun getQuizList(): List<Quiz> { | ||
return quizzes | ||
} | ||
fun getQuizList() = quizzes | ||
|
||
fun getSize(): Int { | ||
return quizzes.size | ||
} | ||
fun getSize() = quizzes.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package yjh.cstar.engine.domain.ranking | ||
|
||
// <String, Int> "player:1", 10 | ||
class Ranking(private val ranking: LinkedHashMap<String, Int>) { | ||
class Ranking( | ||
private val ranking: LinkedHashMap<String, Int>, | ||
) { | ||
|
||
companion object { | ||
fun of(ranking: List<Pair<String?, Double?>>): Ranking { | ||
val mapOfRanking = ranking.filter { it.first != null && it.second != null } | ||
val mapOfRanking = ranking | ||
.filter { it.first != null && it.second != null } | ||
.associate { it.first!! to it.second!!.toInt() } | ||
.toMap(LinkedHashMap()) | ||
return Ranking(mapOfRanking) | ||
} | ||
} | ||
|
||
fun getRanking() = ranking.toList() | ||
fun getRanking(): LinkedHashMap<String, Int> = ranking.toList() | ||
.sortedByDescending { it.second } | ||
.toMap(LinkedHashMap()) | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/yjh/cstar/engine/domain/player/PlayersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package yjh.cstar.engine.domain.player | ||
|
||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.assertThrows | ||
import yjh.cstar.common.BaseException | ||
import kotlin.test.assertEquals | ||
|
||
@DisplayName("[Domain 테스트] Players") | ||
class PlayersTest { | ||
|
||
@Test | ||
fun `플레이어 목록 생성 테스트`() { | ||
// given | ||
val players: Map<Long, String> = mapOf(1L to "player1", 2L to "player2") | ||
|
||
// when & then | ||
assertDoesNotThrow { Players(players) } | ||
} | ||
|
||
@Test | ||
fun `플레이어 목록 생성시 비어있는 파라미터가 들어오면 예외발생`() { | ||
// given | ||
val players: Map<Long, String> = mapOf() | ||
|
||
// when & then | ||
assertThrows<BaseException> { Players(players) } | ||
} | ||
|
||
@Test | ||
fun `특정 id인 회원의 닉네임 조회 테스트`() { | ||
// given | ||
val players = Players(mapOf(1L to "player1", 2L to "player2")) | ||
|
||
// when | ||
val nickname = players.getNickname(1L) | ||
|
||
// then | ||
assertEquals("player1", nickname) | ||
} | ||
|
||
@Test | ||
fun `모든 플레이어의 id 조회 테스트`() { | ||
// given | ||
val players = Players(mapOf(1L to "player1", 2L to "player2")) | ||
|
||
// when | ||
val playerIds = players.getPlayerIds() | ||
|
||
// then | ||
assertEquals(2, playerIds.size) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/kotlin/yjh/cstar/engine/domain/quiz/PlayerAnswerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package yjh.cstar.engine.domain.quiz | ||
|
||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
@DisplayName("[Domain 테스트] PlayerAnswer") | ||
class PlayerAnswerTest { | ||
|
||
@Test | ||
fun `같은 정답인지 비교 테스트`() { | ||
// given | ||
val playerAnswer = PlayerAnswer(1L, 1L, 1L, "정답") | ||
val quiz = Quiz(1L, "질문", "정답") | ||
|
||
// when | ||
val isCorrect = playerAnswer.isCorrect(quiz) | ||
|
||
// then | ||
assertTrue(isCorrect) | ||
} | ||
|
||
@Test | ||
fun `같은 정답이랑 다른지 비교 테스트`() { | ||
// given | ||
val playerAnswer = PlayerAnswer(1L, 1L, 1L, "정답") | ||
val quiz = Quiz(1L, "질문", "잘못된 정답") | ||
|
||
// when | ||
val isCorrect = playerAnswer.isCorrect(quiz) | ||
|
||
// then | ||
assertFalse(isCorrect) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package yjh.cstar.engine.domain.quiz | ||
|
||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
@DisplayName("[Domain 테스트] Quiz") | ||
class QuizTest { | ||
|
||
@Test | ||
fun `퀴즈 생성 테스트`() { | ||
// given | ||
val quizId = 1L | ||
val question = "질문" | ||
val answer = "정답" | ||
|
||
// when & then | ||
assertDoesNotThrow { Quiz.of(quizId, question, answer) } | ||
} | ||
|
||
@Test | ||
fun `같은 정답을 가지고 있는지 테스트`() { | ||
// given | ||
val quizId = 1L | ||
val question = "질문" | ||
val answer = "정답" | ||
val quiz = Quiz(quizId, question, answer) | ||
|
||
// when | ||
val isSameAnswer = quiz.isSameAnswer("정답") | ||
|
||
// then | ||
assertTrue(isSameAnswer) | ||
} | ||
|
||
@Test | ||
fun `다른 정답을 가지고 있는지 테스트`() { | ||
// given | ||
val quizId = 1L | ||
val question = "질문" | ||
val answer = "정답" | ||
val quiz = Quiz(quizId, question, answer) | ||
|
||
// when | ||
val isSameAnswer = quiz.isSameAnswer("잘못된 정답") | ||
|
||
// then | ||
assertFalse(isSameAnswer) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/kotlin/yjh/cstar/engine/domain/quiz/QuizzesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package yjh.cstar.engine.domain.quiz | ||
|
||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import kotlin.test.assertEquals | ||
|
||
@DisplayName("[Domain 테스트] Quizzes") | ||
class QuizzesTest { | ||
|
||
@Test | ||
fun `퀴즈목록 생성 테스트`() { | ||
// given | ||
val quizDtos = listOf( | ||
QuizDto(1L, "question1", "answer1"), | ||
QuizDto(2L, "question2", "answer2"), | ||
QuizDto(3L, "question3", "answer3") | ||
) | ||
|
||
// when & then | ||
assertDoesNotThrow { Quizzes.of(quizDtos) } | ||
} | ||
|
||
@Test | ||
fun `퀴즈목록 조회 테스트`() { | ||
// given | ||
val quizDtos = listOf( | ||
QuizDto(1L, "question1", "answer1"), | ||
QuizDto(2L, "question2", "answer2"), | ||
QuizDto(3L, "question3", "answer3") | ||
) | ||
val quizzes = Quizzes.of(quizDtos) | ||
|
||
// when | ||
val quizList = quizzes.getQuizList() | ||
|
||
// then | ||
assertEquals(3, quizList.size) | ||
} | ||
|
||
@Test | ||
fun `퀴즈목록 개수 조회 테스트`() { | ||
// given | ||
val quizDtos = listOf( | ||
QuizDto(1L, "question1", "answer1"), | ||
QuizDto(2L, "question2", "answer2"), | ||
QuizDto(3L, "question3", "answer3") | ||
) | ||
val quizzes = Quizzes.of(quizDtos) | ||
|
||
// when | ||
val size = quizzes.getSize() | ||
|
||
// then | ||
assertEquals(3, size) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/yjh/cstar/engine/domain/ranking/RankingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package yjh.cstar.engine.domain.ranking | ||
|
||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import kotlin.test.assertEquals | ||
|
||
@DisplayName("[Domain 테스트] Ranking") | ||
class RankingTest { | ||
|
||
@Test | ||
fun `랭킹 생성 테스트`() { | ||
// given | ||
val rankingRawData = listOf( | ||
Pair("player1", 5.0), | ||
Pair("player2", 10.0), | ||
Pair("player3", 2.0) | ||
) | ||
|
||
// when & then | ||
assertDoesNotThrow { Ranking.of(rankingRawData) } | ||
} | ||
|
||
@Test | ||
fun `정렬된 랭킹 조회 테스트`() { | ||
// given | ||
val rankingRawData = listOf( | ||
Pair("player1", 5.0), | ||
Pair("player2", 10.0), | ||
Pair("player3", 2.0) | ||
) | ||
val ranking = Ranking.of(rankingRawData) | ||
|
||
// when | ||
val sortedRanking = ranking.getRanking() | ||
val (key, score) = sortedRanking.entries.first() | ||
|
||
// then | ||
assertEquals(10, score) | ||
assertEquals("player2", key) | ||
} | ||
} |