Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mapping from model mock inputs to JSON input, error JSON structure, and backwards Xcode compatibility #430

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Source/SwiftyDropbox/Shared/Handwritten/MockApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ extension MockApiRequest {
case .none:
mappedInput = .none
case .success(let model):
mappedInput = .success(json: try MockingUtilities.jsonObject(from: model))
mappedInput = .success(json: try MockingUtilities.jsonObject(from: model, isError: false))
case .downloadSuccess(let model, let downloadLocation):
mappedInput = .downloadSuccess(json: try MockingUtilities.jsonObject(from: model), downloadLocation: downloadLocation)
mappedInput = .downloadSuccess(json: try MockingUtilities.jsonObject(from: model, isError: false), downloadLocation: downloadLocation)
case .requestError(let model, let code):
mappedInput = .requestError(json: try MockingUtilities.jsonObject(from: model), code: code)
mappedInput = .requestError(json: try MockingUtilities.jsonObject(from: model, isError: true), code: code)
case .routeError(let model):
mappedInput = .success(json: try MockingUtilities.jsonObject(from: model))
mappedInput = .routeError(json: try MockingUtilities.jsonObject(from: model, isError: true))
}

try _handleMockInput(mappedInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ enum MockingUtilities {
return (namespaceObject, mockTransportClient)
}

static func jsonObject<T: JSONRepresentable>(from result: T) throws -> [String: Any] {
static func jsonObject<T: JSONRepresentable>(from result: T, isError: Bool) throws -> [String: Any] {
let json = try result.json()
let jsonObject = try (SerializeUtil.prepareJSONForSerialization(json) as? [String: Any]).orThrow()
var jsonObject = try (SerializeUtil.prepareJSONForSerialization(json) as? [String: Any]).orThrow()
if isError {
jsonObject = [
"error": jsonObject,
"error_summary": "error_summary",
]
}
return jsonObject
}
}
Expand Down
42 changes: 39 additions & 3 deletions Source/SwiftyDropboxUnitTests/TestMockingUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,51 @@ final class TestMockingUtilities: XCTestCase {

let (filesRoutes, mockTransportClient) = MockingUtilities.makeMock(forType: FilesRoutes.self)
let webService = ExampleWebService(routes: filesRoutes)
let expectedError = Files.GetMetadataError.path(.notFound)

webService.getMetadata { _, error in
XCTAssertNotNil(error)
let error = try! XCTUnwrap(error)
if case .routeError = error {
// successfully produced a Files.GetMetadataError
} else {
XCTFail()
}
e.fulfill()
}

let error = Files.GetMetadataError.path(.notFound)
try mockTransportClient.getLastRequest()?.handleMockInput(
.routeError(model: error)
.routeError(model: expectedError)
)

wait(for: [e], timeout: 1)
}

func testExampleErrorJSON() throws {
let e = expectation(description: "webservice closure called")

let (filesRoutes, mockTransportClient) = MockingUtilities.makeMock(forType: FilesRoutes.self)
let webService = ExampleWebService(routes: filesRoutes)

let expectedErrorJSON: [String: Any] = [
"path": [".tag": "not_found"],
".tag":"path"
]

webService.getMetadata { _, error in
let error = try! XCTUnwrap(error)
if case .routeError = error {
// successfully produced a Files.GetMetadataError
} else {
XCTFail()
}
e.fulfill()
}

try mockTransportClient.getLastRequest()?.handleMockInput(
.routeError(json: [
"error": expectedErrorJSON,
"error_summary": "error_summary",
])
)

wait(for: [e], timeout: 1)
Expand Down
27 changes: 27 additions & 0 deletions Source/SwiftyDropboxUnitTests/TestRequestWithTokenRefresh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ extension MockFileManager: FileManagerProtocol {
}
}

#if compiler(>=6)
extension ClientError: @retroactive RawRepresentable, @retroactive Equatable {
public typealias RawValue = String

Expand All @@ -622,3 +623,29 @@ extension ClientError: @retroactive RawRepresentable, @retroactive Equatable {
}
}
}
#else
extension ClientError: RawRepresentable, Equatable {
public typealias RawValue = String

public init?(rawValue: String) {
fatalError("unimplemented")
}

public var rawValue: String {
switch self {
case .oauthError:
return "oauthError"
case .urlSessionError:
return "urlSessionError"
case .fileAccessError:
return "fileAccessError"
case .requestObjectDeallocated:
return "requestObjectDeallocated"
case .unexpectedState:
return "unexpectedState"
case .other:
return "unknown"
}
}
}
#endif
Loading