Skip to content

Commit 62bc0d3

Browse files
committed
Refactor InMemoryDatabase and repository usage.
1 parent 22657e0 commit 62bc0d3

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

core/src/test/kotlin/io/github/gunkim/realworld/share/InMemoryDatabase.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.github.gunkim.realworld.share
22

3+
import io.github.gunkim.realworld.domain.article.model.Article
34
import io.github.gunkim.realworld.domain.article.model.ArticleId
5+
import io.github.gunkim.realworld.domain.user.model.User
46
import io.github.gunkim.realworld.domain.user.model.UserId
57

68
object InMemoryDatabase {
7-
val users: MutableMap<UserId, Any> = mutableMapOf() // User 저장소
8-
val articles: MutableMap<ArticleId, Any> = mutableMapOf() // Article 저장소
9+
val users: MutableMap<UserId, User> = mutableMapOf() // User 저장소
10+
val articles: MutableMap<ArticleId, Article> = mutableMapOf() // Article 저장소
911

1012
val followings: MutableMap<UserId, MutableSet<UserId>> = mutableMapOf()
1113
val favorites: MutableMap<ArticleId, MutableSet<UserId>> = mutableMapOf()

core/src/test/kotlin/io/github/gunkim/realworld/share/MockArticleRepository.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class ArticleCountProjectionImpl(
1515

1616
class MockArticleRepository : ArticleRepository {
1717
override fun save(article: Article): Article {
18-
val updatedAuthor: User = InMemoryDatabase.users[article.author.id] as? User ?: article.author
18+
val updatedAuthor: User = InMemoryDatabase.users[article.author.id] ?: article.author
1919
val updatedArticle = if (article is Article.Companion.Model) {
2020
article.copy(author = updatedAuthor)
2121
} else {
@@ -46,7 +46,7 @@ class MockArticleRepository : ArticleRepository {
4646
limit: Int,
4747
offset: Int,
4848
): List<Article> {
49-
var filtered = InMemoryDatabase.articles.values.toList().mapNotNull { it as? Article }
49+
var filtered = InMemoryDatabase.articles.values.toList().map { it }
5050

5151
if (tag != null) {
5252
filtered = filtered.filter { article ->
@@ -62,7 +62,7 @@ class MockArticleRepository : ArticleRepository {
6262

6363
if (favoritedUsername != null) {
6464
val favoriter = InMemoryDatabase.users.values
65-
.mapNotNull { it as? User }
65+
.map { it }
6666
.find { it.name == favoritedUsername }
6767
filtered = if (favoriter != null) {
6868
filtered.filter { article ->
@@ -78,7 +78,7 @@ class MockArticleRepository : ArticleRepository {
7878

7979
override fun findFeedArticles(userId: UserId, limit: Int, offset: Int): List<Article> {
8080
val followedUserIds = InMemoryDatabase.followings[userId] ?: emptySet()
81-
val feedArticles = InMemoryDatabase.articles.values.toList().mapNotNull { it as? Article }
81+
val feedArticles = InMemoryDatabase.articles.values.toList().map { it }
8282
.filter { article ->
8383
followedUserIds.contains(article.author.id)
8484
}
@@ -100,7 +100,7 @@ class MockArticleRepository : ArticleRepository {
100100

101101
override fun findBySlug(slug: Slug): Article? {
102102
return InMemoryDatabase.articles.values
103-
.mapNotNull { it as? Article }
103+
.map { it }
104104
.find { it.slug == slug }
105105
}
106106
}

core/src/test/kotlin/io/github/gunkim/realworld/share/MockUserRepository.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MockUserRepository : UserRepository {
1111
InMemoryDatabase.users[user.id] = user
1212

1313
if (existingUser != null) {
14-
InMemoryDatabase.articles.replaceAll { articleId, article ->
14+
InMemoryDatabase.articles.replaceAll { _, article ->
1515
if (article.author.id == user.id) {
1616
if (article is Article.Companion.Model) {
1717
article.copy(author = user)
@@ -27,7 +27,6 @@ class MockUserRepository : UserRepository {
2727
}
2828

2929
override fun follow(followerId: UserId, followeeId: UserId) {
30-
// followerId가 followeeId를 팔로우하도록 followings 맵을 업데이트합니다.
3130
val followingSet = InMemoryDatabase.followings.getOrPut(followerId) { mutableSetOf() }
3231
followingSet.add(followeeId)
3332
}

0 commit comments

Comments
 (0)