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 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
// 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() |
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
// Use of the dataTaskPublisher API to implement Publisher with the decoded data | |
struct Token: Codable { | |
let string: String | |
} | |
// We'll use the following function to validate our dataTaskPublisher output in the pipeline | |
func validate(_ data: Data, _ response: URLResponse) throws -> Data { | |
guard let httpResponse = response as? HTTPURLResponse else { | |
throw APIError.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
//Let's use our brand new Publishers | |
let todoList = [Todo(id: nil, title: "Learn Composite"), | |
Todo(id: nil, title: "Learn SwiftUI")] | |
// use login to get the Bearer Token | |
let cancellableLogin = login(email: "[email protected]", password: "password2") | |
.sink(receiveCompletion: { (completion) in | |
switch completion { |
NewerOlder