Skip to content

Commit

Permalink
Merge pull request #44 from freshOS/tests
Browse files Browse the repository at this point in the history
Adds get, post, put patch & delete unit tests
  • Loading branch information
s4cha authored Apr 12, 2022
2 parents 3323942 + 398d6fb commit 0c7c6d2
Show file tree
Hide file tree
Showing 8 changed files with 1,151 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public extension NetworkingClient {
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

// Array version
func post<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) -> AnyPublisher<[T], Error> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return post(route, params: params)
.tryMap { json -> [T] in try NetworkingParser().toModels(json, keypath: keypath) }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

func put<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
Expand All @@ -53,6 +64,18 @@ public extension NetworkingClient {
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}


// Array version
func put<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) -> AnyPublisher<[T], Error> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return put(route, params: params)
.tryMap { json -> [T] in try NetworkingParser().toModels(json, keypath: keypath) }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

func patch<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
Expand All @@ -62,6 +85,17 @@ public extension NetworkingClient {
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

// Array version
func patch<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) -> AnyPublisher<[T], Error> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return patch(route, params: params)
.tryMap { json -> [T] in try NetworkingParser().toModels(json, keypath: keypath) }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

func delete<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
Expand All @@ -71,6 +105,17 @@ public extension NetworkingClient {
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

// Array version
func delete<T: NetworkingJSONDecodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) -> AnyPublisher<[T], Error> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return delete(route, params: params)
.tryMap { json -> [T] in try NetworkingParser().toModels(json, keypath: keypath) }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}

// Provide default implementation for Decodable models.
Expand All @@ -83,3 +128,4 @@ public extension NetworkingJSONDecodable where Self: Decodable {
return model
}
}

212 changes: 212 additions & 0 deletions Tests/NetworkingTests/DeleteRequestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
//
// DeleteRequestTests.swift
//
//
// Created by Sacha DSO on 12/04/2022.
//

import Foundation
import XCTest
import Combine

@testable
import Networking

class DeletehRequestTests: XCTestCase {

private let network = NetworkingClient(baseURL: "https://mocked.com")
private var cancellables = Set<AnyCancellable>()

override func setUpWithError() throws {
network.sessionConfiguration.protocolClasses = [MockingURLProtocol.self]
}

override func tearDownWithError() throws {
MockingURLProtocol.mockedResponse = ""
MockingURLProtocol.currentRequest = nil
}

func testPOSTVoidWorks() {
MockingURLProtocol.mockedResponse =
"""
{ "response": "OK" }
"""
let expectationWorks = expectation(description: "Call works")
let expectationFinished = expectation(description: "Finished")
network.delete("/users").sink { completion in
switch completion {
case .failure(_):
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users")
expectationFinished.fulfill()
}
} receiveValue: { () in
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

func testPOSTDataWorks() {
MockingURLProtocol.mockedResponse =
"""
{ "response": "OK" }
"""
let expectationWorks = expectation(description: "ReceiveValue called")
let expectationFinished = expectation(description: "Finished called")
network.delete("/users").sink { completion in
switch completion {
case .failure:
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users")
expectationFinished.fulfill()
}
} receiveValue: { (data: Data) in
XCTAssertEqual(data, MockingURLProtocol.mockedResponse.data(using: String.Encoding.utf8))
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

func testPOSTJSONWorks() {
MockingURLProtocol.mockedResponse =
"""
{"response":"OK"}
"""
let expectationWorks = expectation(description: "ReceiveValue called")
let expectationFinished = expectation(description: "Finished called")
network.delete("/users").sink { completion in
switch completion {
case .failure:
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users")
expectationFinished.fulfill()
}
} receiveValue: { (json: Any) in
let data = try? JSONSerialization.data(withJSONObject: json, options: [])
let expectedResponseData =
"""
{"response":"OK"}
""".data(using: String.Encoding.utf8)

XCTAssertEqual(data, expectedResponseData)
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

func testPOSTNetworkingJSONDecodableWorks() {
MockingURLProtocol.mockedResponse =
"""
{
"firstname":"John",
"lastname":"Doe",
}
"""
let expectationWorks = expectation(description: "ReceiveValue called")
let expectationFinished = expectation(description: "Finished called")
network.delete("/users/1")
.sink { completion in
switch completion {
case .failure:
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users/1")
expectationFinished.fulfill()
}
} receiveValue: { (userJSON: UserJSON) in
XCTAssertEqual(userJSON.firstname, "John")
XCTAssertEqual(userJSON.lastname, "Doe")
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

func testPOSTArrayOfNetworkingJSONDecodableWorks() {
MockingURLProtocol.mockedResponse =
"""
[
{
"firstname":"John",
"lastname":"Doe"
},
{
"firstname":"Jimmy",
"lastname":"Punchline"
}
]
"""
let expectationWorks = expectation(description: "ReceiveValue called")
let expectationFinished = expectation(description: "Finished called")
network.delete("/users")
.sink { completion in
switch completion {
case .failure:
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users")
expectationFinished.fulfill()
}
} receiveValue: { (userJSON: [UserJSON]) in
XCTAssertEqual(userJSON[0].firstname, "John")
XCTAssertEqual(userJSON[0].lastname, "Doe")
XCTAssertEqual(userJSON[1].firstname, "Jimmy")
XCTAssertEqual(userJSON[1].lastname, "Punchline")
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

func testPOSTArrayOfNetworkingJSONDecodableWithKeypathWorks() {
MockingURLProtocol.mockedResponse =
"""
{
"users" :
[
{
"firstname":"John",
"lastname":"Doe"
},
{
"firstname":"Jimmy",
"lastname":"Punchline"
}
]
}
"""
let expectationWorks = expectation(description: "ReceiveValue called")
let expectationFinished = expectation(description: "Finished called")
network.delete("/users", keypath: "users")
.sink { completion in
switch completion {
case .failure:
XCTFail()
case .finished:
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE")
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users")
expectationFinished.fulfill()
}
} receiveValue: { (userJSON: [UserJSON]) in
XCTAssertEqual(userJSON[0].firstname, "John")
XCTAssertEqual(userJSON[0].lastname, "Doe")
XCTAssertEqual(userJSON[1].firstname, "Jimmy")
XCTAssertEqual(userJSON[1].lastname, "Punchline")
expectationWorks.fulfill()
}
.store(in: &cancellables)
waitForExpectations(timeout: 0.1)
}

}
Loading

0 comments on commit 0c7c6d2

Please sign in to comment.