|
| 1 | +// |
| 2 | +// This source file is part of the Marvin open source project. |
| 3 | +// Copyright © 2018 Monk. All rights reserved. |
| 4 | +// |
| 5 | + |
| 6 | +import XCTest |
| 7 | +import Marvin |
| 8 | + |
| 9 | +func currentQueueName() -> String? { |
| 10 | + let name = __dispatch_queue_get_label(nil) |
| 11 | + return String(cString: name, encoding: .utf8) |
| 12 | +} |
| 13 | + |
| 14 | +class NetworkTests: XCTestCase { |
| 15 | + |
| 16 | + // _____________________________________________________________________________ |
| 17 | + // MARK: - Queues |
| 18 | + |
| 19 | + func test_sync_fromMain() { |
| 20 | + Network.execute(mode: .sync, request: Requests.testGet(id: 1)) { _ in |
| 21 | + XCTAssert(Thread.isMainThread) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + func test_Sync_NotFromMain() { |
| 26 | + let exp = expectation(description: "test_sync_fromMain") |
| 27 | + let queue = DispatchQueue(label: "test_sync_fromMain", qos: .default); |
| 28 | + |
| 29 | + queue.async { |
| 30 | + Network.execute(mode: .sync, |
| 31 | + request: Requests.testGet(id: 1)) |
| 32 | + { _ in |
| 33 | + XCTAssertEqual(currentQueueName(), "test_sync_fromMain") |
| 34 | + exp.fulfill() |
| 35 | + } |
| 36 | + } |
| 37 | + wait(for: [exp], timeout: 60); |
| 38 | + } |
| 39 | + |
| 40 | + func test_async() { |
| 41 | + let exp = expectation(description: "test_async") |
| 42 | + let queue = DispatchQueue(label: "test_async", qos: .default); |
| 43 | + |
| 44 | + Network.execute(mode: .async(responseQueue: queue), |
| 45 | + request: Requests.testGet(id: 1)) |
| 46 | + { _ in |
| 47 | + XCTAssertEqual(currentQueueName(), "test_async") |
| 48 | + exp.fulfill() |
| 49 | + } |
| 50 | + |
| 51 | + wait(for: [exp], timeout: 60); |
| 52 | + } |
| 53 | + |
| 54 | + // _____________________________________________________________________________ |
| 55 | + // MARK: - Methods |
| 56 | + |
| 57 | + func test_get_custom() { |
| 58 | + Network.execute(mode: .sync, |
| 59 | + request: Requests.testGetCustom(id: 1)) |
| 60 | + { result in |
| 61 | + switch result { |
| 62 | + case .ok(let response): XCTAssert(response.item.userId == 1) |
| 63 | + case .err(let error): XCTFail(error.localizedDescription) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + func test_get() { |
| 69 | + let req = Requests.testGet(id: 12) |
| 70 | + |
| 71 | + Network.execute(mode: .sync, |
| 72 | + request: req) |
| 73 | + { result in |
| 74 | + switch result { |
| 75 | + case .ok(let item): |
| 76 | + print(item) |
| 77 | + case .err(let e): |
| 78 | + switch e { |
| 79 | + case .networkError(_): break |
| 80 | + case .parseJSONError(_): break |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func test_delete() { |
| 87 | + let item = Item(userId: 1, title: "title", body: "body") |
| 88 | + let req = Requests.testDelete(item: item) |
| 89 | + |
| 90 | + Network.execute(mode: .sync, |
| 91 | + request: req) |
| 92 | + { result in |
| 93 | + switch result { |
| 94 | + case .ok(let item): |
| 95 | + print(item) |
| 96 | + case .err(let e): |
| 97 | + switch e { |
| 98 | + case .networkError(_): break |
| 99 | + case .parseJSONError(_): break |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + func test_operation() { |
| 106 | + let exp = expectation(description: "test_operation") |
| 107 | + |
| 108 | + let item = Item(userId: 1, title: "title", body: "body") |
| 109 | + let operation = DeleteOperation(item: item) |
| 110 | + operation.completionBlock = { |
| 111 | + exp.fulfill() |
| 112 | + } |
| 113 | + OperationQueue().addOperation(operation) |
| 114 | + |
| 115 | + waitForExpectations(timeout: 10) { error in |
| 116 | + if error != nil { |
| 117 | + XCTFail() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +class DeleteOperation: Operation, Codable { |
| 126 | + |
| 127 | + let item: Item |
| 128 | + |
| 129 | + init(item: Item) { |
| 130 | + self.item = item |
| 131 | + } |
| 132 | + |
| 133 | + override func main() { |
| 134 | + let req = Requests.testDelete(item: item) |
| 135 | + |
| 136 | + Network.execute(mode: .sync, |
| 137 | + request: req) |
| 138 | + { result in |
| 139 | + switch result { |
| 140 | + case .ok(_): |
| 141 | + break |
| 142 | + case .err(let error): |
| 143 | + XCTFail(error.localizedDescription) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
0 commit comments