-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat/#101] ChallengeRepository 테스트코드 작성
- Loading branch information
Showing
27 changed files
with
807 additions
and
35 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
HMH_Tuist_iOS/Projects/Data/Sources/Mapper/Challenge/ChallengeSuccessInfoMapper.swift
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,18 @@ | ||
// | ||
// DailyChallengeTransform.swift | ||
// Data | ||
// | ||
// Created by 류희재 on 10/29/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import Domain | ||
import Networks | ||
|
||
extension FinishedDailyChallenge { | ||
func toEntity() -> ChallengeSuccessInfo { | ||
return .init(challengeDate: challengeDate, isSuccess: isSuccess) | ||
} | ||
} |
16 changes: 9 additions & 7 deletions
16
HMH_Tuist_iOS/Projects/Data/Sources/Mapper/Challenge/DailyChallengeInfoMapper.swift
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,20 @@ | ||
// | ||
// DailyChallengeTransform.swift | ||
// HomeChallengeDetailMapper.swift | ||
// Data | ||
// | ||
// Created by 류희재 on 10/29/24. | ||
// Created by 류희재 on 11/5/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import Domain | ||
import Networks | ||
|
||
extension FinishedDailyChallenge { | ||
func toEntity() -> DailyChallengeInfo { | ||
return .init(challengeDate: challengeDate, isSuccess: isSuccess) | ||
extension DailyChallengeResult { | ||
public func toEntity() -> DailyChallengeInfo { | ||
.init( | ||
status: status, | ||
goalTime: goalTime, | ||
apps: apps.map { $0.toEntity() } | ||
) | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
...uist_iOS/Projects/Data/Tests/Sources/ChallengeRepositoryTests/AddAppTest/AddAppTest.swift
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,58 @@ | ||
// | ||
// AddAppTest.swift | ||
// DataTests | ||
// | ||
// Created by 류희재 on 11/5/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
|
||
import Networks | ||
import Core | ||
import Domain | ||
|
||
/// 스크린타임 설정할 앱 추가 API 데이터 변환 테스트 | ||
extension ChallegeRepositoryTests { | ||
func test_스크린타임설정할앱추가_정상적인변환() { | ||
|
||
let expectation = XCTestExpectation(description: "스크린타임 설정할 앱 추가 API 관련 레포지토리 변환이 정상적으로 성공했습니다!") | ||
|
||
sut.addApp(apps: [.stub]) | ||
.sink(receiveCompletion: { completion in | ||
if case .failure(let error) = completion { | ||
XCTFail("스크린타임 설정할 앱 추가 API 변환 중 실패했습니다: 에러 \(error)") | ||
} | ||
expectation.fulfill() | ||
}, receiveValue: { _ in | ||
expectation.fulfill() | ||
}) | ||
.store(in: cancelBag) | ||
|
||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
|
||
func test_스크린타임설정할앱추가_네트워크에러발생시_에러반환() { | ||
|
||
let testCases = HMHNetworkError.mockNetworkError | ||
let expectation = XCTestExpectation(description: "에러 발생 시 네트워크 에러 반환") | ||
|
||
for expected in testCases { | ||
mockService.addAppResult = Fail(error: expected).eraseToAnyPublisher() | ||
|
||
sut.addApp(apps: [.stub]) | ||
.sink( | ||
receiveCompletion: handleCompletion( | ||
expectedError: .networkError, | ||
expectation: expectation | ||
), | ||
receiveValue: failureExpectedValueHandler() | ||
) | ||
.store(in: cancelBag) | ||
|
||
} | ||
|
||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...S/Projects/Data/Tests/Sources/ChallengeRepositoryTests/Base/ChallegeRepositoryTests.swift
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,59 @@ | ||
// | ||
// ChallegeRepositoryTests.swift | ||
// DataTests | ||
// | ||
// Created by 류희재 on 11/5/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
|
||
import Networks | ||
import Core | ||
import Domain | ||
import Data | ||
|
||
final class ChallegeRepositoryTests: XCTestCase { | ||
|
||
var sut: ChallengeRepositoryType! | ||
var mockService: MockChallengeService! | ||
var cancelBag: CancelBag! | ||
|
||
override func setUpWithError() throws { | ||
cancelBag = CancelBag() | ||
mockService = MockChallengeService() | ||
sut = ChallengeRepository(service: mockService) | ||
} | ||
|
||
override func tearDown() { | ||
cancelBag = nil | ||
mockService = nil | ||
sut = nil | ||
} | ||
} | ||
|
||
extension ChallegeRepositoryTests { | ||
public func handleCompletion<T: Error & Equatable>(expectedError: T? = nil, expectation: XCTestExpectation) -> (Subscribers.Completion<T>) -> Void { | ||
return { completion in | ||
if case .failure(let error) = completion { | ||
XCTAssertEqual(error, expectedError, "Expected error \(String(describing: expectedError)), but got \(error)") | ||
expectation.fulfill() | ||
} else { | ||
XCTFail("Expected failure with error \(String(describing: expectedError)), but received success") | ||
} | ||
} | ||
} | ||
|
||
public func valueHandler<T: Equatable>(expectation: XCTestExpectation, expectedValue: T) -> (T) -> Void { | ||
return { receivedValue in | ||
XCTAssertEqual(receivedValue, expectedValue, "Received value \(receivedValue) does not match expected data \(expectedValue)") | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
public func failureExpectedValueHandler<T>() -> (T) -> Void { | ||
return { _ in XCTFail("Expected failure, but got success") } | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
..._iOS/Projects/Data/Tests/Sources/ChallengeRepositoryTests/Base/MockChallengeService.swift
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,61 @@ | ||
// | ||
// MockChallengeService.swift | ||
// Data | ||
// | ||
// Created by 류희재 on 11/5/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
import Domain | ||
import Networks | ||
|
||
final public class MockChallengeService: ChallengeServiceType { | ||
|
||
public init() {} | ||
|
||
public var getDailyChallengeResult:AnyPublisher<DailyChallengeResult, HMHNetworkError>! | ||
public var getSuccesChallengeResult:AnyPublisher<ChallengeSuccessResult, HMHNetworkError>! | ||
public var createChallengeResult:AnyPublisher<Void, HMHNetworkError>! | ||
public var getLockChallengeResult:AnyPublisher<GetLockResult, HMHNetworkError>! | ||
public var postLockChallengeResult:AnyPublisher<Void, HMHNetworkError>! | ||
public var deleteAppResult:AnyPublisher<Void, HMHNetworkError>! | ||
public var addAppResult:AnyPublisher<Void, HMHNetworkError>! | ||
public var getChallengeResult:AnyPublisher<GetChallengeResult, HMHNetworkError>! | ||
|
||
|
||
public func getDailyChallenge() -> AnyPublisher<DailyChallengeResult, HMHNetworkError> { | ||
return getDailyChallengeResult | ||
} | ||
|
||
public func getSuccesChallenge() -> AnyPublisher<ChallengeSuccessResult, HMHNetworkError> { | ||
return getSuccesChallengeResult | ||
} | ||
|
||
public func createChallenge(request: CreateChallengeRequest) -> AnyPublisher<Void, HMHNetworkError> { | ||
return createChallengeResult | ||
} | ||
|
||
public func getLockChallenge() -> AnyPublisher<GetLockResult, HMHNetworkError> { | ||
return getLockChallengeResult | ||
} | ||
|
||
public func postLockChallenge() -> AnyPublisher<Void, HMHNetworkError> { | ||
return postLockChallengeResult | ||
} | ||
|
||
public func deleteApp(request: DeleteAppRequest) -> AnyPublisher<Void, HMHNetworkError> { | ||
return deleteAppResult | ||
} | ||
|
||
public func addApp(request: AddAppRequest) -> AnyPublisher<Void, HMHNetworkError> { | ||
return addAppResult | ||
} | ||
|
||
public func getChallenge() -> AnyPublisher<GetChallengeResult, HMHNetworkError> { | ||
return getChallengeResult | ||
} | ||
} | ||
|
57 changes: 57 additions & 0 deletions
57
...Data/Tests/Sources/ChallengeRepositoryTests/CreateChallengeTest/CreateChallengeTest.swift
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 @@ | ||
// | ||
// CreateChallengeTest.swift | ||
// DataTests | ||
// | ||
// Created by 류희재 on 11/5/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
|
||
import Networks | ||
import Core | ||
import Domain | ||
|
||
/// 챌린지 생성 API 데이터 변환 테스트 | ||
extension ChallegeRepositoryTests { | ||
func test_챌린지생성_정상적인변환() { | ||
|
||
let expectation = XCTestExpectation(description: "챌린지 생성 API 관련 레포지토리 변환이 정상적으로 성공했습니다!") | ||
|
||
sut.createChallenge(period: 7, goalTime: 200000) | ||
.sink(receiveCompletion: { completion in | ||
if case .failure(let error) = completion { | ||
XCTFail("챌린지 생성 API 변환 중 실패했습니다: 에러 \(error)") | ||
} | ||
expectation.fulfill() | ||
}, receiveValue: { _ in | ||
expectation.fulfill() | ||
}) | ||
.store(in: cancelBag) | ||
|
||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
|
||
func test_챌린지생성_네트워크에러발생시_에러반환() { | ||
|
||
let testCases = HMHNetworkError.mockNetworkError | ||
let expectation = XCTestExpectation(description: "에러 발생 시 네트워크 에러 반환") | ||
|
||
for expected in testCases { | ||
mockService.createChallengeResult = Fail(error: expected).eraseToAnyPublisher() | ||
|
||
sut.createChallenge(period: 7, goalTime: 200000) | ||
.sink( | ||
receiveCompletion: handleCompletion( | ||
expectedError: .networkError, | ||
expectation: expectation | ||
), | ||
receiveValue: failureExpectedValueHandler() | ||
) | ||
.store(in: cancelBag) | ||
|
||
} | ||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
} |
Oops, something went wrong.