-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit-tests for this module
- Loading branch information
1 parent
8fb72c5
commit b1cd015
Showing
8 changed files
with
211 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
This file was deleted.
Oops, something went wrong.
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,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")) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |