From b1cd0151d06068b53e231bde9cc838b1b8152988 Mon Sep 17 00:00:00 2001 From: Egor Badmaev <60363270+htmlprogrammist@users.noreply.github.com> Date: Sat, 7 Jan 2023 03:14:39 +0300 Subject: [PATCH] feat: add unit-tests for this module --- Networking.podspec | 2 +- Networking/Sources/API/APIEndpoints.swift | 3 +- Networking/Sources/NetworkManagerError.swift | 14 ++++ .../Tests/BaseRecipesInteractorTests.swift | 27 ------- Networking/Tests/EndpointTests.swift | 71 +++++++++++++++++++ Networking/Tests/NetworkManagerTests.swift | 42 +++++++++++ Networking/Tests/NetworkRequestTests.swift | 46 ++++++++++++ Networking/Tests/URLEndpointTests.swift | 35 +++++++++ 8 files changed, 211 insertions(+), 29 deletions(-) delete mode 100644 Networking/Tests/BaseRecipesInteractorTests.swift create mode 100644 Networking/Tests/EndpointTests.swift create mode 100644 Networking/Tests/NetworkManagerTests.swift create mode 100644 Networking/Tests/NetworkRequestTests.swift create mode 100644 Networking/Tests/URLEndpointTests.swift diff --git a/Networking.podspec b/Networking.podspec index 57cfbb8..c61e310 100644 --- a/Networking.podspec +++ b/Networking.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Networking' - s.version = '2.3.0' + s.version = '2.4.0' s.summary = 'Layer responsible for all networking of the application.' s.homepage = 'https://github.com/iCookbook/Networking' s.author = { 'htmlprogrammist' => '60363270+htmlprogrammist@users.noreply.github.com' } diff --git a/Networking/Sources/API/APIEndpoints.swift b/Networking/Sources/API/APIEndpoints.swift index 9819014..1175e80 100644 --- a/Networking/Sources/API/APIEndpoints.swift +++ b/Networking/Sources/API/APIEndpoints.swift @@ -9,6 +9,7 @@ import Foundation /// Here you can see endpoints for current API - _"Edamam API"_. public extension Endpoint { + /// Endpoint for 20 random recipes. /// /// - Returns: ``Endpoint`` instance. @@ -57,7 +58,7 @@ public extension Endpoint { var parameters = [("type", "public"), ("app_id", Credentials.appId), ("app_key", Credentials.apiKey), - ("q", keywords.randomElement() ?? "chicken")] + ("q", keyword)] parameters.append(contentsOf: meals) parameters.append(contentsOf: diets) diff --git a/Networking/Sources/NetworkManagerError.swift b/Networking/Sources/NetworkManagerError.swift index 5ea65ae..459af9d 100644 --- a/Networking/Sources/NetworkManagerError.swift +++ b/Networking/Sources/NetworkManagerError.swift @@ -20,3 +20,17 @@ public enum NetworkManagerError: Error { /// Error on parsing JSON with decoder. case decodingError } + +extension NetworkManagerError: Equatable { + + public static func == (lhs: NetworkManagerError, rhs: NetworkManagerError) -> Bool { + switch (lhs, rhs) { + case (let .unsuccessfulStatusCode(lhsCode), let .unsuccessfulStatusCode(rhsCode)): + return lhsCode.rawValue == rhsCode.rawValue + case (let .networkError(lhsError), let .networkError(rhsError)): + return lhsError.localizedDescription == rhsError.localizedDescription + default: + return lhs.localizedDescription == rhs.localizedDescription + } + } +} diff --git a/Networking/Tests/BaseRecipesInteractorTests.swift b/Networking/Tests/BaseRecipesInteractorTests.swift deleted file mode 100644 index a7e606e..0000000 --- a/Networking/Tests/BaseRecipesInteractorTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// BaseRecipesInteractorTests.swift -// Common-Unit-Tests -// -// Created by Егор Бадмаев on 03.01.2023. -// - -import XCTest -@testable import Common - -class BaseRecipesInteractorTests: XCTestCase { - - /// SUT. - var interactor: BaseRecipesInteractor! - let mockNetworkManager = MockNetworkManager() - - override func setUpWithError() throws { - interactor = BaseRecipesInteractor(networkManager: mockNetworkManager) - } - - override func tearDownWithError() throws { - interactor = nil - } - - func testExample() throws { - } -} diff --git a/Networking/Tests/EndpointTests.swift b/Networking/Tests/EndpointTests.swift new file mode 100644 index 0000000..a465f52 --- /dev/null +++ b/Networking/Tests/EndpointTests.swift @@ -0,0 +1,71 @@ +// +// EndpointTests.swift +// Networking-Unit-Tests +// +// Created by Егор Бадмаев on 07.01.2023. +// + +import XCTest +@testable import Networking + +class EndpointTests: XCTestCase { + + var endpoint: Endpoint! + + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + endpoint = nil + } + + func testRandomDataEndpoint() throws { + XCTAssertNoThrow( + endpoint = Endpoint.random() + ) + } + + func testRandomDataByCuisineEndpoint() throws { + let cuisine = "italian" + + endpoint = Endpoint.random(by: cuisine) + + XCTAssertTrue(endpoint.url!.absoluteString.contains("cuisineType=\(cuisine)")) + } + + func test_dataByKeywordEndpoint_onlyMeals() throws { + let keyword = "Chicken" + let meals = [("mealType", "breakfast"), ("mealType", "lunch/dinner"), ("mealType", "teatime")] + + endpoint = Endpoint.create(by: keyword, meals: meals, diets: [(String, String)](), cuisines: [(String, String)](), dishes: [(String, String)]()) + + XCTAssertEqual(endpoint.url, URL(string: "https://api.edamam.com/api/recipes/v2?type=public&app_id=d6544fa1&app_key=46ffea991d22cd980b515e373b4b852a&q=Chicken&mealType=breakfast&mealType=lunch/dinner&mealType=teatime")) + } + + func test_dataByKeywordEndpoint_onlyDiets() throws { + let keyword = "Chicken" + let diets = [("diet", "Balanced"), ("diet", "High-Fiber"), ("diet", "High-Protein")] + + endpoint = Endpoint.create(by: keyword, meals: [(String, String)](), diets: diets, cuisines: [(String, String)](), dishes: [(String, String)]()) + + XCTAssertEqual(endpoint.url, URL(string: "https://api.edamam.com/api/recipes/v2?type=public&app_id=d6544fa1&app_key=46ffea991d22cd980b515e373b4b852a&q=Chicken&diet=Balanced&diet=High-Fiber&diet=High-Protein")) + } + + func test_dataByKeywordEndpoint_onlyCuisines() throws { + let keyword = "Chicken" + let cuisines = [("cuisineType", "british"), ("cuisineType", "italian"), ("cuisineType", "japanese")] + + endpoint = Endpoint.create(by: keyword, meals: [(String, String)](), diets: [(String, String)](), cuisines: cuisines, dishes: [(String, String)]()) + + XCTAssertEqual(endpoint.url, URL(string: "https://api.edamam.com/api/recipes/v2?type=public&app_id=d6544fa1&app_key=46ffea991d22cd980b515e373b4b852a&q=Chicken&cuisineType=british&cuisineType=italian&cuisineType=japanese")) + } + + func test_dataByKeywordEndpoint_onlyDishes() throws { + let keyword = "Chicken" + let dishes = [("dishType", "biscuits and cookies"), ("dishType", "bread"), ("dishType", "alcohol cocktail")] + + endpoint = Endpoint.create(by: keyword, meals: [(String, String)](), diets: [(String, String)](), cuisines: [(String, String)](), dishes: dishes) + + XCTAssertEqual(endpoint.url, URL(string: "https://api.edamam.com/api/recipes/v2?type=public&app_id=d6544fa1&app_key=46ffea991d22cd980b515e373b4b852a&q=Chicken&dishType=biscuits%20and%20cookies&dishType=bread&dishType=alcohol%20cocktail")) + } +} diff --git a/Networking/Tests/NetworkManagerTests.swift b/Networking/Tests/NetworkManagerTests.swift new file mode 100644 index 0000000..19314a3 --- /dev/null +++ b/Networking/Tests/NetworkManagerTests.swift @@ -0,0 +1,42 @@ +// +// NetworkManagerTests.swift +// Networking-Unit-Tests +// +// Created by Егор Бадмаев on 07.01.2023. +// + +import XCTest +@testable import Networking + +class NetworkManagerTests: XCTestCase { + + let defaultSession = URLSession(configuration: URLSessionConfiguration.default) + let defaultDecoder = JSONDecoder() + /// SUT. + var networkManager: NetworkManager! + + override func setUpWithError() throws { + networkManager = NetworkManager(session: defaultSession, decoder: defaultDecoder) + } + + override func tearDownWithError() throws { + networkManager = nil + } + + func testObtainingData() throws { + let expectation = expectation(description: "Test obtainData method (URLSession asynchronous by default)") + let requst = NetworkRequest(endpoint: Endpoint.random()) + + networkManager.obtainData(request: requst) { result in + switch result { + case .success(let data): + XCTAssertNotNil(data) + case .failure(let error): + XCTAssertNotNil(error) + } + expectation.fulfill() + } + + wait(for: [expectation], timeout: 5.0) + } +} diff --git a/Networking/Tests/NetworkRequestTests.swift b/Networking/Tests/NetworkRequestTests.swift new file mode 100644 index 0000000..4496bf2 --- /dev/null +++ b/Networking/Tests/NetworkRequestTests.swift @@ -0,0 +1,46 @@ +// +// NetworkRequestTests.swift +// Networking-Unit-Tests +// +// Created by Егор Бадмаев on 07.01.2023. +// + +import XCTest +@testable import Networking + +class NetworkRequestTests: XCTestCase { + + let mockEndpoint = Endpoint(path: "test") + /// SUT. + var networkRequest: NetworkRequest! + + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + networkRequest = nil + } + + func test_InitializingNetworkRequest_withoutAdditionalData() throws { + XCTAssertNoThrow( + networkRequest = NetworkRequest(endpoint: mockEndpoint) + ) + } + + func test_InitializingNetworkRequest_withAdditionalData() throws { + let method = HTTPMethod.get + let httpHeaderFields = [HTTPHeader.accept("value"), HTTPHeader.authorization("value")] + let timeoutInterval: TimeInterval = 0 + + networkRequest = NetworkRequest(endpoint: mockEndpoint, + method: method, + httpHeaderFields: httpHeaderFields, + timeoutInterval: timeoutInterval + ) + + XCTAssertEqual(networkRequest.endpoint.url, mockEndpoint.url) + XCTAssertEqual(networkRequest.method, method) + XCTAssertEqual(networkRequest.httpHeaderFields, httpHeaderFields) + XCTAssertEqual(networkRequest.timeoutInterval, timeoutInterval) + } +} diff --git a/Networking/Tests/URLEndpointTests.swift b/Networking/Tests/URLEndpointTests.swift new file mode 100644 index 0000000..f37d1d7 --- /dev/null +++ b/Networking/Tests/URLEndpointTests.swift @@ -0,0 +1,35 @@ +// +// URLEndpointTests.swift +// Networking-Unit-Tests +// +// Created by Егор Бадмаев on 07.01.2023. +// + +import XCTest +@testable import Networking + +class URLEndpointTests: XCTestCase { + + var endpoint: URLEndpoint! + + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + endpoint = nil + } + + func testEndpointWithEmptyString() throws { + endpoint = URLEndpoint(urlString: "") + + XCTAssertTrue(endpoint.urlString.isEmpty) + XCTAssertNil(endpoint.url) + } + + func testEndpointWithPath() throws { + endpoint = URLEndpoint(urlString: "path") + + XCTAssertFalse(endpoint.urlString.isEmpty) + XCTAssertNotNil(endpoint.url) + } +}