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

Add assertions in unary RPC error tests #44

Merged
merged 1 commit into from
Dec 13, 2023
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 Sources/GoogleAI/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import Foundation

struct RPCError: Error {
let httpResponseCode: Int32
let httpResponseCode: Int
let message: String
let status: RPCStatus

init(httpResponseCode: Int32, message: String, status: RPCStatus) {
init(httpResponseCode: Int, message: String, status: RPCStatus) {
self.httpResponseCode = httpResponseCode
self.message = message
self.status = status
Expand Down Expand Up @@ -56,7 +56,7 @@ extension RPCError: Decodable {
}

struct ErrorStatus {
let code: Int32?
let code: Int?
let message: String?
let status: RPCStatus?
}
Expand All @@ -70,7 +70,7 @@ extension ErrorStatus: Decodable {

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
code = try container.decodeIfPresent(Int32.self, forKey: .code)
code = try container.decodeIfPresent(Int.self, forKey: .code)
message = try container.decodeIfPresent(String.self, forKey: .message)
do {
status = try container.decodeIfPresent(RPCStatus.self, forKey: .status)
Expand Down
65 changes: 40 additions & 25 deletions Tests/GoogleAITests/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,29 @@ final class GenerativeModelTests: XCTestCase {
}

func testGenerateContent_failure_invalidAPIKey() async throws {
let expectedStatusCode = 400
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "unary-failure-api-key",
withExtension: "json",
statusCode: 400
statusCode: expectedStatusCode
)

var responseError: Error?
var content: GenerateContentResponse?
do {
content = try await model.generateContent(testPrompt)
_ = try await model.generateContent(testPrompt)
XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
} catch let GenerateContentError.internalError(underlying: underlyingError) {
guard let rpcError = underlyingError as? RPCError else {
XCTFail("Not an RPCError: \(underlyingError)")
return
}

XCTAssertEqual(rpcError.status, .invalidArgument)
XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
XCTAssertTrue(rpcError.message.hasPrefix("API key not valid"))
} catch {
responseError = error
XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
}

XCTAssertNotNil(responseError)
XCTAssertNil(content)
// TODO: Add assertions about `responseError`.
}

func testGenerateContent_failure_emptyContent() async throws {
Expand Down Expand Up @@ -243,24 +248,29 @@ final class GenerativeModelTests: XCTestCase {
}

func testGenerateContent_failure_imageRejected() async throws {
let expectedStatusCode = 400
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "unary-failure-image-rejected",
withExtension: "json",
statusCode: 400
)

var responseError: Error?
var content: GenerateContentResponse?
do {
content = try await model.generateContent(testPrompt)
_ = try await model.generateContent(testPrompt)
XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
} catch let GenerateContentError.internalError(underlying: underlyingError) {
guard let rpcError = underlyingError as? RPCError else {
XCTFail("Not an RPCError: \(underlyingError)")
return
}

XCTAssertEqual(rpcError.status, .invalidArgument)
XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
XCTAssertEqual(rpcError.message, "Request contains an invalid argument.")
} catch {
responseError = error
XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
}

XCTAssertNotNil(responseError)
XCTAssertNil(content)
// TODO: Add assertions about `responseError`.
}

func testGenerateContent_failure_promptBlockedSafety() async throws {
Expand All @@ -281,24 +291,29 @@ final class GenerativeModelTests: XCTestCase {
}

func testGenerateContent_failure_unknownModel() async throws {
let expectedStatusCode = 404
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "unary-failure-unknown-model",
withExtension: "json",
statusCode: 404
)

var responseError: Error?
var content: GenerateContentResponse?
do {
content = try await model.generateContent(testPrompt)
_ = try await model.generateContent(testPrompt)
XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
} catch let GenerateContentError.internalError(underlying: underlyingError) {
guard let rpcError = underlyingError as? RPCError else {
XCTFail("Not an RPCError: \(underlyingError)")
return
}

XCTAssertEqual(rpcError.status, .notFound)
XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
XCTAssertTrue(rpcError.message.hasPrefix("models/unknown is not found"))
} catch {
responseError = error
XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
}

XCTAssertNotNil(responseError)
XCTAssertNil(content)
// TODO: Add assertions about `responseError`.
}

func testGenerateContent_failure_nonHTTPResponse() async throws {
Expand Down
Loading