Skip to content

Commit 324e2f8

Browse files
committed
Add tests for encoding GenerateContentRequest
1 parent c142fe0 commit 324e2f8

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import XCTest
17+
18+
@testable import GoogleGenerativeAI
19+
20+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
21+
final class GenerateContentRequestTests: XCTestCase {
22+
let encoder = JSONEncoder()
23+
let role = "test-role"
24+
let prompt = "test-prompt"
25+
let modelName = "test-model-name"
26+
27+
override func setUp() {
28+
encoder.outputFormatting = .init(
29+
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
30+
)
31+
}
32+
33+
// MARK: GenerateContentRequest Encoding
34+
35+
func testEncodeRequest_allFieldsIncluded() throws {
36+
let content = [ModelContent(role: role, parts: prompt)]
37+
let request = GenerateContentRequest(
38+
model: modelName,
39+
isModelEncoded: true,
40+
contents: content,
41+
generationConfig: GenerationConfig(temperature: 0.5),
42+
safetySettings: [SafetySetting(
43+
harmCategory: .dangerousContent,
44+
threshold: .blockLowAndAbove
45+
)],
46+
tools: [Tool(functionDeclarations: [FunctionDeclaration(
47+
name: "test-function-name",
48+
description: "test-function-description",
49+
parameters: nil
50+
)])],
51+
toolConfig: ToolConfig(functionCallingConfig: FunctionCallingConfig(mode: .auto)),
52+
systemInstruction: ModelContent(role: "system", parts: "test-system-instruction"),
53+
isStreaming: false,
54+
options: RequestOptions()
55+
)
56+
57+
let jsonData = try encoder.encode(request)
58+
59+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
60+
XCTAssertEqual(json, """
61+
{
62+
"contents" : [
63+
{
64+
"parts" : [
65+
{
66+
"text" : "\(prompt)"
67+
}
68+
],
69+
"role" : "\(role)"
70+
}
71+
],
72+
"generationConfig" : {
73+
"temperature" : 0.5
74+
},
75+
"model" : "\(modelName)",
76+
"safetySettings" : [
77+
{
78+
"category" : "HARM_CATEGORY_DANGEROUS_CONTENT",
79+
"threshold" : "BLOCK_LOW_AND_ABOVE"
80+
}
81+
],
82+
"systemInstruction" : {
83+
"parts" : [
84+
{
85+
"text" : "test-system-instruction"
86+
}
87+
],
88+
"role" : "system"
89+
},
90+
"toolConfig" : {
91+
"functionCallingConfig" : {
92+
"mode" : "AUTO"
93+
}
94+
},
95+
"tools" : [
96+
{
97+
"functionDeclarations" : [
98+
{
99+
"description" : "test-function-description",
100+
"name" : "test-function-name",
101+
"parameters" : {
102+
"type" : "OBJECT"
103+
}
104+
}
105+
]
106+
}
107+
]
108+
}
109+
""")
110+
}
111+
112+
func testEncodeRequest_optionalFieldsOmitted_modelNameEncoded() throws {
113+
let content = [ModelContent(role: role, parts: prompt)]
114+
let request = GenerateContentRequest(
115+
model: modelName,
116+
isModelEncoded: true,
117+
contents: content,
118+
generationConfig: nil,
119+
safetySettings: nil,
120+
tools: nil,
121+
toolConfig: nil,
122+
systemInstruction: nil,
123+
isStreaming: false,
124+
options: RequestOptions()
125+
)
126+
127+
let jsonData = try encoder.encode(request)
128+
129+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
130+
XCTAssertEqual(json, """
131+
{
132+
"contents" : [
133+
{
134+
"parts" : [
135+
{
136+
"text" : "\(prompt)"
137+
}
138+
],
139+
"role" : "\(role)"
140+
}
141+
],
142+
"model" : "\(modelName)"
143+
}
144+
""")
145+
}
146+
147+
func testEncodeRequest_optionalFieldsOmitted_modelNameNotEncoded() throws {
148+
let content = [ModelContent(role: role, parts: prompt)]
149+
let request = GenerateContentRequest(
150+
model: modelName,
151+
isModelEncoded: false,
152+
contents: content,
153+
generationConfig: nil,
154+
safetySettings: nil,
155+
tools: nil,
156+
toolConfig: nil,
157+
systemInstruction: nil,
158+
isStreaming: false,
159+
options: RequestOptions()
160+
)
161+
162+
let jsonData = try encoder.encode(request)
163+
164+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
165+
XCTAssertEqual(json, """
166+
{
167+
"contents" : [
168+
{
169+
"parts" : [
170+
{
171+
"text" : "\(prompt)"
172+
}
173+
],
174+
"role" : "\(role)"
175+
}
176+
]
177+
}
178+
""")
179+
}
180+
}

0 commit comments

Comments
 (0)