Skip to content

Commit

Permalink
test: 공모글 작성 이미지 테스트 코드 작성 (#260)
Browse files Browse the repository at this point in the history
* refactor: 상수 가시성 변경

* feat: test fixture 구현

* feat: fake repository 이미지 업로드 기능 추가

* test: OfferingWriteViewModelTest 이미지 업로드 test 코드 작성
  • Loading branch information
chaehyuns authored and ChooSeoyeon committed Oct 11, 2024
1 parent c93ce60 commit 7e4fbd2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class OfferingWriteViewModel(
private const val MAXIMUM_TOTAL_COUNT = 10_000
private const val INPUT_DATE_TIME_FORMAT = "yyyy년 M월 d일 a h시 m분"
private const val OUTPUT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
private const val HTTPS = "https:"
const val HTTPS = "https:"

@Suppress("UNCHECKED_CAST")
fun getFactory(offeringRepository: OfferingRepository) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.zzang.chongdae.presentation.view.write

import com.zzang.chongdae.domain.repository.OfferingRepository
import com.zzang.chongdae.presentation.view.write.OfferingWriteViewModel.Companion.HTTPS
import com.zzang.chongdae.repository.FakeOfferingRepository
import com.zzang.chongdae.util.CoroutinesTestExtension
import com.zzang.chongdae.util.InstantTaskExecutorExtension
import com.zzang.chongdae.util.TestFixture.martiPartBody
import com.zzang.chongdae.util.TestFixture.productUrl
import com.zzang.chongdae.util.getOrAwaitValue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExperimentalCoroutinesApi
@ExtendWith(CoroutinesTestExtension::class)
@ExtendWith(InstantTaskExecutorExtension::class)
class OfferingWriteViewModelTest {
private lateinit var viewModel: OfferingWriteViewModel
private lateinit var offeringRepository: OfferingRepository

@BeforeEach
fun setUp() {
offeringRepository = FakeOfferingRepository()
viewModel = OfferingWriteViewModel(offeringRepository)
}

@DisplayName("상품 url을 통해 og 이미지 정보를 가져온다")
@Test
fun saveProductImageOg() {
// given
// when
viewModel.postProductImageOg()

// then
val result = viewModel.thumbnailUrl.getOrAwaitValue()
assertThat(result).isEqualTo(HTTPS + productUrl.imageUrl)
}

@DisplayName("상품 이미지를 S3에 저장한다")
@Test
fun saveProductImageS3() {
// given
// when
viewModel.uploadImageFile(martiPartBody)

// then
val result = viewModel.thumbnailUrl.getOrAwaitValue()
assertThat(result).isEqualTo(productUrl.imageUrl)
}

@DisplayName("업로드한 상품 이미지를 지울 수 있다")
@Test
fun deleteProductImage() {
// given
viewModel.uploadImageFile(martiPartBody)

// when
viewModel.clearProductImage()

// then
val result = viewModel.thumbnailUrl.getOrAwaitValue()
assertThat(result).isEqualTo(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class FakeOfferingRepository : OfferingRepository {
}

override suspend fun saveProductImageOg(productUrl: String): Result<ProductUrl> {
TODO("Not yet implemented")
return Result.success(TestFixture.productUrl)
}

override suspend fun saveProductImageS3(image: MultipartBody.Part): Result<ProductUrl> {
TODO("Not yet implemented")
return Result.success(TestFixture.productUrl)
}

override suspend fun fetchFilters(): Result<List<Filter>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.zzang.chongdae.domain.model.Meetings
import com.zzang.chongdae.domain.model.Offering
import com.zzang.chongdae.domain.model.OfferingCondition
import com.zzang.chongdae.domain.model.OfferingStatus
import com.zzang.chongdae.domain.model.ProductUrl
import okhttp3.MultipartBody

object TestFixture {
val meetings: Meetings =
Expand Down Expand Up @@ -76,4 +78,8 @@ object TestFixture {
type = FilterType.VISIBLE,
)
}

val productUrl: ProductUrl = ProductUrl("url")

val martiPartBody = MultipartBody.Part.createFormData("image", "image")
}

0 comments on commit 7e4fbd2

Please sign in to comment.