Skip to content

Instantly share code, notes, and snippets.

View Andrea-Scuderi's full-sized avatar

Andrea Scuderi Andrea-Scuderi

View GitHub Profile
@Andrea-Scuderi
Andrea-Scuderi / Makefile.emerge
Last active January 15, 2022 13:34
Makefile.emerge
MOUNT_ROOT=$(shell pwd)
DOCKER_TAG=emerge:1.0.0
docker_build:
docker build --tag $(DOCKER_TAG) .
docker_bash:
docker run \
-it \
--rm \
@Andrea-Scuderi
Andrea-Scuderi / Dockerfile.emerge
Last active January 15, 2022 13:31
Dockerfile-emerge
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
@Andrea-Scuderi
Andrea-Scuderi / .swift
Created September 3, 2019 12:49
CombineAPIDemoTests
import XCTest
import Foundation
import Combine
@testable import CombineAPIDemo
class Mocks {
let user = User(name: "name",
email: "email",
password: "password",
@Andrea-Scuderi
Andrea-Scuderi / .swift
Created September 3, 2019 12:46
Create XCTest
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)
@Andrea-Scuderi
Andrea-Scuderi / .swift
Created September 3, 2019 12:37
XCTTest Publisher utils
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 {
@Andrea-Scuderi
Andrea-Scuderi / .swift
Last active September 3, 2019 11:24
URLProtocolMock
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?
@Andrea-Scuderi
Andrea-Scuderi / .swift
Created September 3, 2019 10:23
CombineAPIDemo
import Combine
import Foundation
enum APIError: Error {
case invalidBody
case invalidEndpoint
case invalidURL
case emptyData
case invalidJSON
case invalidResponse
@Andrea-Scuderi
Andrea-Scuderi / .swift
Last active August 1, 2019 10:22
Part 8
// 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()
// 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
}
@Andrea-Scuderi
Andrea-Scuderi / .swift
Last active August 1, 2019 10:18
Combine API - Part 7
//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: "user2@example.com", password: "password2")
.sink(receiveCompletion: { (completion) in
switch completion {