-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Increase Firebase AI Logic unit test coverage #15126
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37273e4
Part 1
paulb777 8dcfc18
part 2
paulb777 d769436
Review
paulb777 1eef613
address review comments
paulb777 76bf729
fix tests
paulb777 7c3cb3a
[Firebase AI] Merge `+Coverage` files into existing test files (#15136)
andrewheard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import XCTest | ||
|
||
@testable import FirebaseAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension JSONValueTests { | ||
func testDecodeNestedObject() throws { | ||
let nestedObject: JSONObject = [ | ||
"nestedKey": .string("nestedValue"), | ||
] | ||
let expectedObject: JSONObject = [ | ||
"numberKey": .number(numberValue), | ||
"objectKey": .object(nestedObject), | ||
] | ||
let json = """ | ||
{ | ||
"numberKey": \(numberValue), | ||
"objectKey": { | ||
"nestedKey": "nestedValue" | ||
} | ||
} | ||
""" | ||
let jsonData = try XCTUnwrap(json.data(using: .utf8)) | ||
|
||
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData)) | ||
|
||
XCTAssertEqual(jsonObject, .object(expectedObject)) | ||
} | ||
|
||
func testDecodeNestedArray() throws { | ||
let nestedArray: [JSONValue] = [.string("a"), .string("b")] | ||
let expectedObject: JSONObject = [ | ||
"numberKey": .number(numberValue), | ||
"arrayKey": .array(nestedArray), | ||
] | ||
let json = """ | ||
{ | ||
"numberKey": \(numberValue), | ||
"arrayKey": ["a", "b"] | ||
} | ||
""" | ||
let jsonData = try XCTUnwrap(json.data(using: .utf8)) | ||
|
||
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData)) | ||
|
||
XCTAssertEqual(jsonObject, .object(expectedObject)) | ||
} | ||
|
||
func testEncodeNestedObject() throws { | ||
let nestedObject: JSONObject = [ | ||
"nestedKey": .string("nestedValue"), | ||
] | ||
let objectValue: JSONObject = [ | ||
"numberKey": .number(numberValue), | ||
"objectKey": .object(nestedObject), | ||
] | ||
|
||
let jsonData = try encoder.encode(JSONValue.object(objectValue)) | ||
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData)) | ||
XCTAssertEqual(jsonObject, .object(objectValue)) | ||
} | ||
|
||
func testEncodeNestedArray() throws { | ||
let nestedArray: [JSONValue] = [.string("a"), .string("b")] | ||
let objectValue: JSONObject = [ | ||
"numberKey": .number(numberValue), | ||
"arrayKey": .array(nestedArray), | ||
] | ||
|
||
let jsonData = try encoder.encode(JSONValue.object(objectValue)) | ||
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData)) | ||
XCTAssertEqual(jsonObject, .object(objectValue)) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
FirebaseAI/Tests/Unit/PartsRepresentableTests+Coverage.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import XCTest | ||
#if canImport(UIKit) | ||
import UIKit | ||
#elseif canImport(AppKit) | ||
import AppKit | ||
#endif | ||
|
||
@testable import FirebaseAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension PartsRepresentableTests { | ||
func testMixedParts() throws { | ||
let text = "This is a test" | ||
let data = try XCTUnwrap("This is some data".data(using: .utf8)) | ||
let inlineData = InlineDataPart(data: data, mimeType: "text/plain") | ||
|
||
let parts: [any PartsRepresentable] = [text, inlineData] | ||
let modelContent = ModelContent(parts: parts) | ||
|
||
XCTAssertEqual(modelContent.parts.count, 2) | ||
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart) | ||
XCTAssertEqual(textPart.text, text) | ||
let dataPart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart) | ||
XCTAssertEqual(dataPart, inlineData) | ||
} | ||
|
||
#if canImport(UIKit) | ||
func testMixedParts_withImage() throws { | ||
let text = "This is a test" | ||
let image = try XCTUnwrap(UIImage(systemName: "star")) | ||
let parts: [any PartsRepresentable] = [text, image] | ||
let modelContent = ModelContent(parts: parts) | ||
|
||
XCTAssertEqual(modelContent.parts.count, 2) | ||
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart) | ||
XCTAssertEqual(textPart.text, text) | ||
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart) | ||
XCTAssertEqual(imagePart.mimeType, "image/jpeg") | ||
XCTAssertFalse(imagePart.data.isEmpty) | ||
} | ||
|
||
#elseif canImport(AppKit) | ||
func testMixedParts_withImage() throws { | ||
let text = "This is a test" | ||
let coreImage = CIImage(color: CIColor.blue) | ||
.cropped(to: CGRect(origin: CGPoint.zero, size: CGSize(width: 16, height: 16))) | ||
let rep = NSCIImageRep(ciImage: coreImage) | ||
let image = NSImage(size: rep.size) | ||
image.addRepresentation(rep) | ||
|
||
let parts: [any PartsRepresentable] = [text, image] | ||
let modelContent = ModelContent(parts: parts) | ||
|
||
XCTAssertEqual(modelContent.parts.count, 2) | ||
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart) | ||
XCTAssertEqual(textPart.text, text) | ||
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart) | ||
XCTAssertEqual(imagePart.mimeType, "image/jpeg") | ||
XCTAssertFalse(imagePart.data.isEmpty) | ||
} | ||
#endif | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import XCTest | ||
|
||
@testable import FirebaseAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
final class SafetyTests: XCTestCase { | ||
let decoder = JSONDecoder() | ||
let encoder = JSONEncoder() | ||
|
||
// MARK: - SafetyRating Decoding | ||
|
||
func testDecodeSafetyRating_allFieldsPresent() throws { | ||
let json = """ | ||
{ | ||
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | ||
"probability": "NEGLIGIBLE", | ||
"probabilityScore": 0.1, | ||
"severity": "HARM_SEVERITY_LOW", | ||
"severityScore": 0.2, | ||
"blocked": true | ||
} | ||
""" | ||
let jsonData = try XCTUnwrap(json.data(using: .utf8)) | ||
let rating = try decoder.decode(SafetyRating.self, from: jsonData) | ||
|
||
XCTAssertEqual(rating.category, .dangerousContent) | ||
XCTAssertEqual(rating.probability, .negligible) | ||
XCTAssertEqual(rating.probabilityScore, 0.1) | ||
XCTAssertEqual(rating.severity, .low) | ||
XCTAssertEqual(rating.severityScore, 0.2) | ||
XCTAssertTrue(rating.blocked) | ||
} | ||
|
||
func testDecodeSafetyRating_missingOptionalFields() throws { | ||
let json = """ | ||
{ | ||
"category": "HARM_CATEGORY_HARASSMENT", | ||
"probability": "LOW" | ||
} | ||
""" | ||
let jsonData = try XCTUnwrap(json.data(using: .utf8)) | ||
let rating = try decoder.decode(SafetyRating.self, from: jsonData) | ||
|
||
XCTAssertEqual(rating.category, .harassment) | ||
XCTAssertEqual(rating.probability, .low) | ||
XCTAssertEqual(rating.probabilityScore, 0.0) | ||
XCTAssertEqual(rating.severity, .unspecified) | ||
XCTAssertEqual(rating.severityScore, 0.0) | ||
XCTAssertFalse(rating.blocked) | ||
} | ||
|
||
func testDecodeSafetyRating_unknownEnums() throws { | ||
let json = """ | ||
{ | ||
"category": "HARM_CATEGORY_UNKNOWN", | ||
"probability": "UNKNOWN_PROBABILITY", | ||
"severity": "UNKNOWN_SEVERITY" | ||
} | ||
""" | ||
let jsonData = try XCTUnwrap(json.data(using: .utf8)) | ||
let rating = try decoder.decode(SafetyRating.self, from: jsonData) | ||
|
||
XCTAssertEqual(rating.category.rawValue, "HARM_CATEGORY_UNKNOWN") | ||
XCTAssertEqual(rating.probability.rawValue, "UNKNOWN_PROBABILITY") | ||
XCTAssertEqual(rating.severity.rawValue, "UNKNOWN_SEVERITY") | ||
} | ||
|
||
// MARK: - SafetySetting Encoding | ||
|
||
func testEncodeSafetySetting_allFields() throws { | ||
let setting = SafetySetting( | ||
harmCategory: .hateSpeech, | ||
threshold: .blockMediumAndAbove, | ||
method: .severity | ||
) | ||
encoder.outputFormatting = .sortedKeys | ||
let jsonData = try encoder.encode(setting) | ||
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
|
||
XCTAssertEqual(jsonString, """ | ||
{"category":"HARM_CATEGORY_HATE_SPEECH","method":"SEVERITY","threshold":"BLOCK_MEDIUM_AND_ABOVE"} | ||
""") | ||
} | ||
|
||
func testEncodeSafetySetting_nilMethod() throws { | ||
let setting = SafetySetting( | ||
harmCategory: .sexuallyExplicit, | ||
threshold: .blockOnlyHigh | ||
) | ||
encoder.outputFormatting = .sortedKeys | ||
let jsonData = try encoder.encode(setting) | ||
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
|
||
XCTAssertEqual(jsonString, """ | ||
{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_ONLY_HIGH"} | ||
""") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.