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

SPT-1998 Модульные тесты часть 3 #127

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions NodeKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

207 changes: 207 additions & 0 deletions NodeKitTests/UnitTests/ArrayDTODecodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//
// ArrayDTODecodableTests.swift
// NodeKitTests
//
// Created by Andrei Frolov on 09.04.24.
// Copyright © 2024 Surf. All rights reserved.
//

@testable import NodeKit
import XCTest

final class ArrayDTODecodableTests: XCTestCase {

// MARK: - Lifecycle

override func tearDown() {
super.tearDown()
DTODecodableMock.flush()
}

// MARK: - Tests

func testFromDTO_thenFromDTOCalledInEachElement() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock(),
RawDecodableMock(),
RawDecodableMock()
]

DTODecodableMock.stubbedFromResult = .success(.init())

// when

_ = try? Array<DTODecodableMock>.from(dto: array)

// then

XCTAssertEqual(DTODecodableMock.invokedFromCount, array.count)

DTODecodableMock.invokedFromParameterList.enumerated().forEach {
XCTAssertTrue($0.element === array[$0.offset])
}
}

func testFromDTO_whenDecodingFailure_thenFailureReceived() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock()
]

var result: [DTODecodableMock]?
var receivedError: Error?

DTODecodableMock.stubbedFromResult = .failure(MockError.firstError)

// when

do {
result = try [DTODecodableMock].from(dto: array)
} catch {
receivedError = error
}

// then

let error = try XCTUnwrap(receivedError as? MockError)

XCTAssertNil(result)
XCTAssertEqual(error, .firstError)
}

func testFromDTO_whenDecodingSuccess_thenSuccessResultReceived() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock(),
RawDecodableMock(),
RawDecodableMock()
]
let dtoDecodableMock = DTODecodableMock()

var result: [DTODecodableMock]?
var receivedError: Error?

DTODecodableMock.stubbedFromResult = .success(dtoDecodableMock)

// when

do {
result = try Array<DTODecodableMock>.from(dto: array)
} catch {
receivedError = error
}

// then

let value = try XCTUnwrap(result)

XCTAssertNil(receivedError)
XCTAssertEqual(value.count, array.count)

value.forEach {
XCTAssertTrue($0 === dtoDecodableMock)
}
}

func testToDTO_thenToDTOCalledInEachElement() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(.init())
array[1].stubbedToDTOResult = .success(.init())
array[2].stubbedToDTOResult = .success(.init())

// when

_ = try? array.toDTO()

// then

array.forEach {
XCTAssertEqual($0.invokedToDTOCount, 1)
}
}

func testToDTO_whenEncodingFailure_thenFailureReceived() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(.init())
array[1].stubbedToDTOResult = .failure(MockError.secondError)
array[2].stubbedToDTOResult = .success(.init())

var result: [RawEncodableMock<Json>]?
var receivedError: Error?

// when

do {
result = try array.toDTO()
} catch {
receivedError = error
}

// then

let error = try XCTUnwrap(receivedError as? MockError)

XCTAssertNil(result)
XCTAssertEqual(error, .secondError)
}

func testToDTO_whenEncodingSuccess_thenSuccessResultReceived() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

let expectedResult: [RawEncodableMock<Json>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(expectedResult[0])
array[1].stubbedToDTOResult = .success(expectedResult[1])
array[2].stubbedToDTOResult = .success(expectedResult[2])

var result: [RawEncodableMock<Json>]?
var receivedError: Error?

// when

do {
result = try array.toDTO()
} catch {
receivedError = error
}

// then

let value = try XCTUnwrap(result)

XCTAssertNil(receivedError)
XCTAssertEqual(value.count, array.count)

value.enumerated().forEach {
XCTAssertTrue($0.element === expectedResult[$0.offset])
}
}
}
Loading
Loading