Skip to content

Commit

Permalink
feat: add unit-tests for this module
Browse files Browse the repository at this point in the history
  • Loading branch information
htmlprogrammist committed Jan 7, 2023
1 parent 8fb72c5 commit b1cd015
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Networking.podspec
Original file line number Diff line number Diff line change
@@ -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' }
Expand Down
3 changes: 2 additions & 1 deletion Networking/Sources/API/APIEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions Networking/Sources/NetworkManagerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
27 changes: 0 additions & 27 deletions Networking/Tests/BaseRecipesInteractorTests.swift

This file was deleted.

71 changes: 71 additions & 0 deletions Networking/Tests/EndpointTests.swift
Original file line number Diff line number Diff line change
@@ -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"))
}
}
42 changes: 42 additions & 0 deletions Networking/Tests/NetworkManagerTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
46 changes: 46 additions & 0 deletions Networking/Tests/NetworkRequestTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
35 changes: 35 additions & 0 deletions Networking/Tests/URLEndpointTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit b1cd015

Please sign in to comment.