Skip to content

Commit

Permalink
setup for json mocks + versions endpoint mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanomondino committed Jan 28, 2022
1 parent 2ee0fde commit 59afce6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "swift-algorand-sdk_swift-algorand-sdkTests"
BuildableName = "swift-algorand-sdk_swift-algorand-sdkTests"
BlueprintName = "swift-algorand-sdk_swift-algorand-sdkTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ let package = Package(

.testTarget(
name: "swift-algorand-sdkTests",
dependencies: ["swift-algorand-sdk"]
dependencies: ["swift-algorand-sdk"],
resources: [.process("mocks")]
),

]
Expand Down
12 changes: 10 additions & 2 deletions Tests/swift-algorand-sdkTests/AlgodClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,17 @@ public class AlgodClientTests: XCTestCase {
}

func testGetVersion() {
let object = Version(build: .init(build_number: 1234))
let object = Version(build: .init(branch: "theBranch",
build_number: 123,
channel: "theChannel",
commit_hash: "theCommitHash",
major: 123,
minor: 123),
genesis_hash_string: "123",
genesis_id: "1234",
versions: ["1.0", "2.0"])
let request = GetVersion(client: client)
assertSuccessfulResponse(for: request, with: object)
assertSuccessfulResponse(for: request, json: "versions", with: object)
assertErrorResponse(for: request)
}

Expand Down
21 changes: 19 additions & 2 deletions Tests/swift-algorand-sdkTests/HTTPMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import XCTest

extension XCTestCase {
func assertSuccessfulResponse<CustomRequest>(for request: CustomRequest,
json: String? = nil,
with mock: CustomRequest.ResponseType,
file: StaticString = #filePath,
line: UInt = #line,
comparing compare: @escaping(CustomRequest.ResponseType?, CustomRequest.ResponseType?) -> Void)
where CustomRequest: swift_algorand_sdk.Request {
MockURLProtocol.response(for: request, with: mock)
if let json = json,
let url = Bundle.module.url(forResource: json, withExtension: "json"),
let data = try? Data(contentsOf: url) {
MockURLProtocol.response(for: request, with: data)
} else {
MockURLProtocol.response(for: request, with: mock)
}
let expectation = XCTestExpectation(description: "Performing request for \(request.self)")
request.execute { response in
compare(response.data, mock)
Expand All @@ -26,11 +33,13 @@ extension XCTestCase {
wait(for: [expectation], timeout: 1)
}
func assertSuccessfulResponse<CustomRequest>(for request: CustomRequest,
json: String? = nil,
with mock: CustomRequest.ResponseType,
file: StaticString = #filePath,
line: UInt = #line)
where CustomRequest: swift_algorand_sdk.Request, CustomRequest.ResponseType: Equatable {
assertSuccessfulResponse(for: request,
json: json,
with: mock,
file: file,
line: line,
Expand Down Expand Up @@ -116,11 +125,19 @@ final class MockURLProtocol: URLProtocol {
with mock: CustomRequest.ResponseType)
where CustomRequest: swift_algorand_sdk.Request {
let encoder = JSONEncoder()
let data = try? encoder.encode(mock)
self.response(for: request, with: data)
}

static func response<CustomRequest>(for request: CustomRequest,
with data: Data?)
where CustomRequest: swift_algorand_sdk.Request {

MockURLProtocol.responseType = MockURLProtocol.ResponseType
.success(HTTPURLResponse(url: URL(string: "https://something.useless")!,
statusCode: 200,
httpVersion: nil, headerFields: nil)!,
try? encoder.encode(mock))
data)
}
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/swift-algorand-sdkTests/mocks/algod/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"genesis_hash_b64": "123",
"genesis_id": "1234",
"versions": [
"1.0",
"2.0"
],
"build": {
"branch": "theBranch",
"build_number": 123,
"channel": "theChannel",
"commit_hash": "theCommitHash",
"major": 123,
"minor": 123
}
}

0 comments on commit 59afce6

Please sign in to comment.