Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Step3] 메모 (Update, Delete) #12

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.*
class EditMemoViewModel(
private val memosRepository: MemosSource,
) : ViewModel() {
private val _memoSaved = SingleLiveEvent<Unit>()
private val _memoSaved = MutableLiveData<Unit>()
val memoSaved: LiveData<Unit> = _memoSaved

private val _toastMessage = SingleLiveEvent<Int>()
Expand All @@ -40,7 +40,7 @@ class EditMemoViewModel(
id = memoId ?: UUID.randomUUID().toString()
)
memosRepository.save(memo)
_memoSaved.call()
_memoSaved.value = Unit
}

fun loadMemo(memoId: String? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class EditMemoViewModelTest {
{ verify(exactly = 1) { memosRepository.save(any()) } },
{ assertThat(actualMemo.title).isEqualTo("title") },
{ assertThat(actualMemo.content).isEqualTo("content") },
{ assertThat(editMemoViewModel.memoSaved.takeValue()).isNull() },
{ assertThat(editMemoViewModel.memoSaved.takeValue()).isEqualTo(Unit) },
)
}

Expand Down
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ dependencies {
implementation(project(":domain"))
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("io.mockk:mockk:1.12.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import camp.nextstep.edu.data.local.MemosLocalSource
import camp.nextstep.edu.domain.Memo
import camp.nextstep.edu.domain.MemosSource
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll

Expand All @@ -13,6 +17,14 @@ import org.junit.jupiter.api.assertAll
*/

class MemosRepositoryTest {
lateinit var memosRepository: MemosRepository
lateinit var memosLocalSource: MemosSource

@BeforeEach
fun setUp() {
memosLocalSource = mockk(relaxed = true)
memosRepository = MemosRepository(memosLocalSource)
Comment on lines +25 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MockK를 활용하여 테스트 코드 간결하게 유지 👍

}

@Test
fun `메모를 저장하고 꺼내올 수 있다`() {
Expand Down Expand Up @@ -48,97 +60,37 @@ class MemosRepositoryTest {
@Test
fun `모든 메모를 두 번 불러오면 두 번째는 캐시된 메모를 불러온다`() {
// given
var getAllMemoCallCount = 0
val localMemos = listOf(Memo("title", "content", "1"))
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
getAllMemoCallCount++
return localMemos
}

override fun getMemo(id: String): Memo? {
error("")
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})
every { memosLocalSource.getAllMemos() } returns localMemos

// when
memoRepository.getAllMemos()
val twiceRetrievedMemos = memoRepository.getAllMemos()
memosRepository.getAllMemos()
val twiceRetrievedMemos = memosRepository.getAllMemos()

// then
assertAll(
{ verify(exactly = 1) { memosLocalSource.getAllMemos() } },
{ assertThat(twiceRetrievedMemos).containsExactlyElementsIn(localMemos) },
{ assertThat(getAllMemoCallCount).isEqualTo(1) },
)
}

@Test
fun `처음 불러오는 모든 메모는 반드시 캐시가 아닌 Source 객체에서 가져온다`() {
// given
var getAllMemoCallCount = 0
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
getAllMemoCallCount++
return emptyList()
}

override fun getMemo(id: String): Memo? {
error("")
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})

// when
memoRepository.getAllMemos()
memosRepository.getAllMemos()

// then
assertThat(getAllMemoCallCount).isEqualTo(1)
verify(exactly = 1) { memosLocalSource.getAllMemos() }
}

@Test
fun `초기 상태일 때, 메모를 저장하고 모든 메모를 불러오면 캐시된 메모가 아닌 source 객체에서 불러온다`() {
// given
var getAllMemoCallCount = 0
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
getAllMemoCallCount++
return emptyList()
}

override fun getMemo(id: String): Memo? {
error("")
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})

// when
memoRepository.save(Memo("title", "content", "1"))
memoRepository.getAllMemos()
memosRepository.save(Memo("title", "content"))
memosRepository.getAllMemos()

// then
assertThat(getAllMemoCallCount).isEqualTo(1)
verify(exactly = 1) { memosLocalSource.getAllMemos() }
}

@Test
Expand Down Expand Up @@ -175,105 +127,31 @@ class MemosRepositoryTest {
fun `메모를 저장하고 해당 메모를 가져오면, 캐시로부터 불러온다`() {
// given
val memo = Memo("title", "content", "1")
var getMemoCallCount = 0
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
return emptyList()
}

override fun getMemo(id: String): Memo? {
getMemoCallCount++
return memo
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})

// when
memoRepository.save(memo)
val retrieveMemo = memoRepository.getMemo("1")

memosRepository.save(memo)
val retrieveMemo = memosRepository.getMemo("1")

// then
assertAll(
{ assertThat(memo).isEqualTo(retrieveMemo) },
{ assertThat(getMemoCallCount).isEqualTo(0) },
{ verify(exactly = 0) { memosLocalSource.getMemo(any()) } },
)
}

@Test
fun `특정 메모를 가져오지 못하면, 특정 메모를 찾아 반환한다`() {
// given
val memo = Memo("title", "content", "1")
var getMemoCallCount = 0
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
return emptyList()
}

override fun getMemo(id: String): Memo? {
getMemoCallCount++
return memo
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})

// when
val retrieveMemo = memoRepository.getMemo("1")

// then
assertThat(memo).isEqualTo(retrieveMemo)
}

@Test
fun `특정 메모를 캐시에서 가져오지 못했다면, 다음 모든 메모는 source에서 가져온다`() {
// given
var getMemoCallCount = 0
var getAllMemoCallCount = 0
val memoRepository = MemosRepository(object : MemosSource {
override fun save(memo: Memo) {
/*Nothing*/
}

override fun getAllMemos(): List<Memo> {
getAllMemoCallCount++
return emptyList()
}

override fun getMemo(id: String): Memo? {
getMemoCallCount++
return Memo("title", "content", "1")
}

override fun deleteMemo(id: String) {
/*Nothing*/
}
})

// when
memoRepository.getMemo("1")
memosRepository.getMemo("1")

// then
assertThat(getMemoCallCount).isEqualTo(1)
verify(exactly = 1) { memosLocalSource.getMemo(any()) }

// when
memoRepository.getAllMemos()
memosRepository.getAllMemos()

// then
assertThat(getAllMemoCallCount).isEqualTo(1)
verify(exactly = 1) { memosLocalSource.getAllMemos() }
}

@Test
Expand Down
6 changes: 1 addition & 5 deletions domain/src/main/java/camp/nextstep/edu/domain/Memo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ data class Memo(
val title: String,
val content: String,
val id: String = UUID.randomUUID().toString(),
) {
override fun toString(): String {
return "Memo(title=$title, content=$content)"
}
}
)