Skip to content

Commit 14218a7

Browse files
committed
Add unary generate content test
1 parent 82bb5b5 commit 14218a7

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"candidates": [
3+
{
4+
"content": {
5+
"parts": [
6+
{
7+
"text": "To print strings in Python, you use the `print()` function. Here's how you can print \"Hello, world!\":\n\n"
8+
},
9+
{
10+
"executableCode": {
11+
"language": "PYTHON",
12+
"code": "\nprint(\"Hello, world!\")\n"
13+
}
14+
},
15+
{
16+
"codeExecutionResult": {
17+
"outcome": "OUTCOME_OK",
18+
"output": "Hello, world!\n"
19+
}
20+
},
21+
{
22+
"text": "The code successfully prints the string \"Hello, world!\". \n"
23+
}
24+
],
25+
"role": "model"
26+
},
27+
"finishReason": "STOP",
28+
"index": 0,
29+
"safetyRatings": [
30+
{
31+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
32+
"probability": "NEGLIGIBLE"
33+
},
34+
{
35+
"category": "HARM_CATEGORY_HATE_SPEECH",
36+
"probability": "NEGLIGIBLE"
37+
},
38+
{
39+
"category": "HARM_CATEGORY_HARASSMENT",
40+
"probability": "NEGLIGIBLE"
41+
},
42+
{
43+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
44+
"probability": "NEGLIGIBLE"
45+
}
46+
]
47+
}
48+
],
49+
"usageMetadata": {
50+
"promptTokenCount": 21,
51+
"candidatesTokenCount": 11,
52+
"totalTokenCount": 32
53+
}
54+
}

Tests/GoogleAITests/GenerativeModelTests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,60 @@ final class GenerativeModelTests: XCTestCase {
288288
XCTAssertEqual(text, "The sum of [1, 2,\n3] is")
289289
}
290290

291+
func testGenerateContent_success_codeExecution() async throws {
292+
MockURLProtocol
293+
.requestHandler = try httpRequestHandler(
294+
forResource: "unary-success-code-execution",
295+
withExtension: "json"
296+
)
297+
let expectedText1 = """
298+
To print strings in Python, you use the `print()` function. \
299+
Here's how you can print \"Hello, world!\":\n\n
300+
"""
301+
let expectedText2 = "The code successfully prints the string \"Hello, world!\". \n"
302+
let expectedLanguage = "PYTHON"
303+
let expectedCode = "\nprint(\"Hello, world!\")\n"
304+
let expectedOutput = "Hello, world!\n"
305+
306+
let response = try await model.generateContent(testPrompt)
307+
308+
XCTAssertEqual(response.candidates.count, 1)
309+
let candidate = try XCTUnwrap(response.candidates.first)
310+
XCTAssertEqual(candidate.content.parts.count, 4)
311+
guard case let .text(text1) = candidate.content.parts[0] else {
312+
XCTFail("Expected first part to be text.")
313+
return
314+
}
315+
XCTAssertEqual(text1, expectedText1)
316+
guard case let .executableCode(executableCode) = candidate.content.parts[1] else {
317+
XCTFail("Expected second part to be executable code.")
318+
return
319+
}
320+
XCTAssertEqual(executableCode.language, expectedLanguage)
321+
XCTAssertEqual(executableCode.code, expectedCode)
322+
guard case let .codeExecutionResult(codeExecutionResult) = candidate.content.parts[2] else {
323+
XCTFail("Expected second part to be a code execution result.")
324+
return
325+
}
326+
XCTAssertEqual(codeExecutionResult.outcome, .ok)
327+
XCTAssertEqual(codeExecutionResult.output, expectedOutput)
328+
guard case let .text(text2) = candidate.content.parts[3] else {
329+
XCTFail("Expected fourth part to be text.")
330+
return
331+
}
332+
XCTAssertEqual(text2, expectedText2)
333+
XCTAssertEqual(try XCTUnwrap(response.text), """
334+
\(expectedText1)
335+
```\(expectedLanguage.lowercased())
336+
\(expectedCode)
337+
```
338+
```
339+
\(expectedOutput)
340+
```
341+
\(expectedText2)
342+
""")
343+
}
344+
291345
func testGenerateContent_usageMetadata() async throws {
292346
MockURLProtocol
293347
.requestHandler = try httpRequestHandler(

0 commit comments

Comments
 (0)