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
import Combine | |
import Foundation | |
enum APIError: Error { | |
case invalidBody | |
case invalidEndpoint | |
case invalidURL | |
case emptyData | |
case invalidJSON | |
case invalidResponse |
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
MOUNT_ROOT=$(shell pwd) | |
DOCKER_TAG=emerge:1.0.0 | |
docker_build: | |
docker build --tag $(DOCKER_TAG) . | |
docker_bash: | |
docker run \ | |
-it \ | |
--rm \ |
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
FROM python:3.9.9-slim-buster | |
RUN apt-get -qq update | |
RUN apt-get install -qqy graphviz graphviz-dev gcc | |
RUN rm -rf /var/lib/apt/lists/* | |
RUN pip install --upgrade pip | |
RUN pip install emerge-viz |
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
import XCTest | |
import Foundation | |
import Combine | |
@testable import CombineAPIDemo | |
class Mocks { | |
let user = User(name: "name", | |
email: "email", | |
password: "password", |
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
func testCreate() { | |
//Setup fixture | |
let usersURL = URL(string: APIDemo.baseURL + "/users") | |
URLProtocolMock.testURLs = [usersURL: Data(Fixtures.createUserResponse.utf8)] | |
//1) When is valid | |
APIDemo.publisher = customPublisher | |
URLProtocolMock.response = mocks.validResponse | |
let publisher = APIDemo.create(user: self.mocks.user) |
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
func evalValidResponseTest<T:Publisher>(publisher: T?) -> (expectations:[XCTestExpectation], cancellable: AnyCancellable?) { | |
XCTAssertNotNil(publisher) | |
let expectationFinished = expectation(description: "finished") | |
let expectationReceive = expectation(description: "receiveValue") | |
let expectationFailure = expectation(description: "failure") | |
expectationFailure.isInverted = true | |
let cancellable = publisher?.sink (receiveCompletion: { (completion) in | |
switch completion { |
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
import Foundation | |
//References: | |
// --: https://www.hackingwithswift.com/articles/153/how-to-test-ios-networking-code-the-easy-way | |
// --: https://nshipster.com/nsurlprotocol/ | |
@objc class URLProtocolMock: URLProtocol { | |
// this dictionary maps URLs to test data | |
static var testURLs = [URL?: Data]() | |
static var response: URLResponse? |
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
import Cocoa | |
import Foundation | |
let baseURL = "http://localhost:8080" | |
enum APIError: Error { | |
case invalidBody | |
case invalidEndpoint | |
case invalidURL | |
case emptyData |
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
// Refactoring of the URLRequest with dataTask avoiding Throws using fatalError | |
func buildCreateUserURLRequest(user: User) -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
] | |
let encoder = JSONEncoder() | |
guard let postData = try? encoder.encode(user) else { | |
fatalError("APIError.invalidBody") | |
} |
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
// Combining login and postTodo in a single call | |
func post(email: String, password: String, todo: Todo) -> AnyPublisher<Todo, Error>? { | |
return login(email: email, password: password) | |
.map { token -> String in | |
return token.string | |
} | |
.flatMap { (token) -> AnyPublisher<Todo, Error> in | |
return postTodo(authToken: token, todo: todoList[1]) | |
} | |
.eraseToAnyPublisher() |
NewerOlder