Skip to content

Commit

Permalink
[Feat/#101] challengeRepository 추가적인 도메인 에러 상황 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Nov 5, 2024
1 parent 05e5d8d commit 60d1380
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extension ChallegeRepositoryTests {

let expectation = XCTestExpectation(description: "스크린타임 설정할 앱 추가 API 관련 레포지토리 변환이 정상적으로 성공했습니다!")

mockService.addAppResult = Just(())
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()

sut.addApp(apps: [.stub])
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
Expand Down Expand Up @@ -53,6 +57,6 @@ extension ChallegeRepositoryTests {

}

wait(for: [expectation], timeout: 1.0)
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extension ChallegeRepositoryTests {

let expectation = XCTestExpectation(description: "챌린지 생성 API 관련 레포지토리 변환이 정상적으로 성공했습니다!")

mockService.createChallengeResult = Just(())
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()

sut.createChallenge(period: 7, goalTime: 200000)
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
Expand Down Expand Up @@ -52,6 +56,102 @@ extension ChallegeRepositoryTests {
.store(in: cancelBag)

}
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}

func test_챌린지생성_챌린지기간이nil일때_에러반환() {
// Given
let message = "챌린지 기간은 null일 수 없습니다."
let networkError = HMHNetworkError.invalidResponse(.invalidStatusCode(code: 400, message: message))

mockService.createChallengeResult = Fail(error: networkError)
.eraseToAnyPublisher()

// When
let expectedError = ChallengeError.challengePeriodIsNil
let expectation = XCTestExpectation(description: message)

sut.createChallenge(period: 7, goalTime: 200000)
.sink(
receiveCompletion: handleCompletion(
expectedError: expectedError,
expectation: expectation
),receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

wait(for: [expectation], timeout: 1.0)
}

func test_챌린지생성_챌린지기간이유효하지않을때_에러반환() {
// Given
let message = "유효한 숫자의 챌린지 기간을 입력해주세요."
let networkError = HMHNetworkError.invalidResponse(.invalidStatusCode(code: 400, message: message))

mockService.createChallengeResult = Fail(error: networkError)
.eraseToAnyPublisher()

// When
let expectedError = ChallengeError.invalidChallengePeriod
let expectation = XCTestExpectation(description: message)

sut.createChallenge(period: 7, goalTime: 200000)
.sink(
receiveCompletion: handleCompletion(
expectedError: expectedError,
expectation: expectation
),receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

wait(for: [expectation], timeout: 1.0)
}

func test_챌린지생성_목표시간이nil일경우_에러반환() {
// Given
let message = "목표시간은 null일 수 없습니다."
let networkError = HMHNetworkError.invalidResponse(.invalidStatusCode(code: 400, message: message))

mockService.createChallengeResult = Fail(error: networkError)
.eraseToAnyPublisher()

// When
let expectedError = ChallengeError.goalTimeIsNil
let expectation = XCTestExpectation(description: message)

sut.createChallenge(period: 7, goalTime: 200000)
.sink(
receiveCompletion: handleCompletion(
expectedError: expectedError,
expectation: expectation
),receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

wait(for: [expectation], timeout: 1.0)
}

func test_챌린지생성_목표시간이이유효하지않을때_에러반환() {
// Given
let message = "유효한 숫자의 목표 시간을 입력해주세요."
let networkError = HMHNetworkError.invalidResponse(.invalidStatusCode(code: 400, message: message))

mockService.createChallengeResult = Fail(error: networkError)
.eraseToAnyPublisher()

// When
let expectedError = ChallengeError.invalidGoalTime
let expectation = XCTestExpectation(description: message)

sut.createChallenge(period: 7, goalTime: 200000)
.sink(
receiveCompletion: handleCompletion(
expectedError: expectedError,
expectation: expectation
),receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

wait(for: [expectation], timeout: 1.0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extension ChallegeRepositoryTests {

let expectation = XCTestExpectation(description: "스크린타임 설정한 앱 삭제 API 관련 레포지토리 변환이 정상적으로 성공했습니다!")

mockService.deleteAppResult = Just(())
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()

sut.deleteApp(appCode: "10000")
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
Expand Down Expand Up @@ -52,6 +56,6 @@ extension ChallegeRepositoryTests {
.store(in: cancelBag)

}
wait(for: [expectation], timeout: 1.0)
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,3 @@ extension GetChallengeResult {
)
}
}















Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ extension ChallegeRepositoryTests {

}

wait(for: [expectation], timeout: 1.0)
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ extension ChallegeRepositoryTests {

}

wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}

func test_홈이용시간통계불러오기_챌린지를찾을수없는경우_에러반환() {
// Given
let message = "챌린지를 찾을 수 없습니다."
let networkError = HMHNetworkError.invalidResponse(.invalidStatusCode(code: 404, message: message))

mockService.getDailyChallengeResult = Fail(error: networkError)
.eraseToAnyPublisher()

// When
let expectedError = ChallengeError.challengeNotFound
let expectation = XCTestExpectation(description: "챌린지를 찾을 수 없습니다.")

sut.getdailyChallenge()
.sink(
receiveCompletion: handleCompletion(
expectedError: expectedError,
expectation: expectation
),receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

wait(for: [expectation], timeout: 1.0)
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// GetLockChallengeMockData.swift
// Data
//
// Created by 류희재 on 11/6/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation

import Domain
import Networks

extension GetLockResult {
static public var expectedData: [Bool] {
return [true, false]
}
}

extension GetLockResult {
static public var resultData: [GetLockResult] {
return [
.init(isLockToday: true),
.init(isLockToday: false)
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// GetLockChallengeTest.swift
// DataTests
//
// Created by 류희재 on 11/6/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import XCTest
import Combine

import Networks
import Core
import Domain

/// 당일 잠금 여부 확인 API 데이터 변환 테스트
extension ChallegeRepositoryTests {
func test_당일잠금여부확인_정상적인변환() {
let testCases = Array(zip(GetLockResult.expectedData, GetLockResult.resultData))

let expectation = XCTestExpectation(description: "당일 잠금 여부 확인 API 관련 레포지토리 변환이 정상적으로 성공했습니다!")

expectation.expectedFulfillmentCount = testCases.count
for (expectedData, resultData) in testCases {
mockService.getLockChallengeResult = Just(resultData)
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()

sut.getLockChallenge()
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
XCTFail("챌린지 성공 여부 리스트 전송 API 변환 중 실패했습니다: 에러 \(error)")
}
}, receiveValue: valueHandler(expectation: expectation, expectedValue: expectedData))
.store(in: cancelBag)
}

wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}

func test_당일잠금여부확인_네트워크에러발생시_에러반환() {

let testCases = HMHNetworkError.mockNetworkError
let expectation = XCTestExpectation(description: "에러 발생 시 네트워크 에러 반환")

for expected in testCases {
mockService.postLockChallengeResult = Fail(error: expected).eraseToAnyPublisher()

sut.postLockChallenge()
.sink(
receiveCompletion: handleCompletion(
expectedError: .networkError,
expectation: expectation
),
receiveValue: failureExpectedValueHandler()
)
.store(in: cancelBag)

}
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extension ChallengeSuccessResult {
["","","","",""],
["asdfasdfasdfasdf","asdfasdfasdfasdf","asdfasdfasdfasdf"]
]

}
}

Expand All @@ -29,9 +28,6 @@ extension ChallengeSuccessResult {
.init(statuses: ["","","","",""]),
.init(statuses: ["asdfasdfasdfasdf","asdfasdfasdfasdf","asdfasdfasdfasdf"])
]



}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ extension ChallegeRepositoryTests {

}

wait(for: [expectation], timeout: 1.0)
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extension ChallegeRepositoryTests {

let expectation = XCTestExpectation(description: "당일 잠금 여부 전송 API 관련 레포지토리 변환이 정상적으로 성공했습니다!")

mockService.postLockChallengeResult = Just(())
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()

sut.postLockChallenge()
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
Expand Down Expand Up @@ -52,6 +56,6 @@ extension ChallegeRepositoryTests {
.store(in: cancelBag)

}
wait(for: [expectation], timeout: 1.0)
wait(for: [expectation], timeout: 1.0 * Double(testCases.count))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public enum ChallengeError: DomainError {
switch message {
case "챌린지를 찾을 수 없습니다.":
return .challengeNotFound
case "목표시간은 null일 수 없습니다.":
case "챌린지 기간은 null일 수 없습니다.":
return .challengePeriodIsNil
case "유효한 숫자의 챌린지 기간을 입력해주세요.":
return .invalidChallengePeriod
case "챌린지 기간은 null일 수 없습니다.":
case "목표시간은 null일 수 없습니다.":
return .goalTimeIsNil
case "유효한 숫자의 목표 시간을 입력해주세요.":
return .invalidGoalTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Foundation

public struct GetLockResult: Decodable {
public let isLockToday: Bool

public init(isLockToday: Bool) {
self.isLockToday = isLockToday
}
}

public extension GetLockResult {
Expand Down

0 comments on commit 60d1380

Please sign in to comment.