Skip to content

Commit 6650731

Browse files
committed
Refactor test to use C# instance for serialization
1 parent b56d198 commit 6650731

File tree

1 file changed

+114
-10
lines changed

1 file changed

+114
-10
lines changed

test/AzureOpenAIProxy.ApiApp.Tests/Models/CreateChatCompletionResponseTests.cs

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,130 @@ public class CreateChatCompletionResponseTests
149149
]
150150
}";
151151

152+
private readonly CreateChatCompletionResponse exampleResponse = new CreateChatCompletionResponse
153+
{
154+
Id = "string",
155+
Object = ChatCompletionResponseObject.ChatCompletion,
156+
Created = 1620241923,
157+
Model = "string",
158+
Usage = new CompletionUsage
159+
{
160+
PromptTokens = 0,
161+
CompletionTokens = 0,
162+
TotalTokens = 0
163+
},
164+
SystemFingerprint = "string",
165+
PromptFilterResults = new List<PromptFilterResult>
166+
{
167+
new PromptFilterResult
168+
{
169+
PromptIndex = 0,
170+
ContentFilterResults = new ContentFilterPromptResults
171+
{
172+
Sexual = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
173+
Violence = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
174+
Hate = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
175+
SelfHarm = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
176+
Profanity = new ContentFilterDetectedResult { Filtered = true, Detected = true },
177+
Error = new ErrorBase { Code = "string", Message = "string" },
178+
Jailbreak = new ContentFilterDetectedResult { Filtered = true, Detected = true }
179+
}
180+
}
181+
},
182+
Choices = new List<ChatCompletionChoice>
183+
{
184+
new ChatCompletionChoice
185+
{
186+
Index = 0,
187+
FinishReason = "string",
188+
Message = new ChatCompletionResponseMessage
189+
{
190+
Role = ChatCompletionResponseMessageRole.Assistant,
191+
Content = "string",
192+
ToolCalls = new List<ChatCompletionMessageToolCall>
193+
{
194+
new ChatCompletionMessageToolCall
195+
{
196+
Id = "string",
197+
Type = ToolCallType.Function,
198+
Function = new FunctionObject
199+
{
200+
Name = "string",
201+
Arguments = "string"
202+
}
203+
}
204+
},
205+
FunctionCall = new ChatCompletionFunctionCall { Name = "string", Arguments = "string" },
206+
Context = new AzureChatExtensionsMessageContext
207+
{
208+
Citations = new List<Citation>
209+
{
210+
new Citation
211+
{
212+
Content = "string",
213+
Title = "string",
214+
Url = "string",
215+
Filepath = "string",
216+
ChunkId = "string"
217+
}
218+
},
219+
Intent = "string"
220+
}
221+
},
222+
ContentFilterResults = new ContentFilterChoiceResults
223+
{
224+
Sexual = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
225+
Violence = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
226+
Hate = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
227+
SelfHarm = new ContentFilterSeverityResult { Filtered = true, Severity = ContentFilterSeverity.Safe },
228+
Profanity = new ContentFilterDetectedResult { Filtered = true, Detected = true },
229+
Error = new ErrorBase { Code = "string", Message = "string" },
230+
ProtectedMaterialText = new ContentFilterDetectedResult { Filtered = true, Detected = true },
231+
ProtectedMaterialCode = new ContentFilterDetectedWithCitationResult
232+
{
233+
Filtered = true,
234+
Detected = true,
235+
Citation = new CitationObject { URL = "string", License = "string" }
236+
}
237+
},
238+
LogProbs = new ChatCompletionChoiceLogProbs
239+
{
240+
Content = new List<ChatCompletionTokenLogProb>
241+
{
242+
new ChatCompletionTokenLogProb
243+
{
244+
Token = "string",
245+
LogProb = 0,
246+
Bytes = new List<int> { 0 },
247+
TopLogProbs = new List<TopLogProbs>
248+
{
249+
new TopLogProbs { Token = "string", LogProb = 0, Bytes = new List<int> { 0 } }
250+
}
251+
}
252+
}
253+
}
254+
}
255+
}
256+
};
257+
152258
[Fact]
153-
public void Given_ExampleJson_When_Deserialized_Then_ShouldReturnValidObject()
259+
public void Given_ExampleResponse_When_Serialized_Then_ShouldMatchExpectedJson()
154260
{
155-
// Arrange & Act
156-
var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson);
261+
// Act
262+
var serializedJson = JsonSerializer.Serialize(exampleResponse, new JsonSerializerOptions { WriteIndented = false });
157263

158264
// Assert
159-
deserializedResponse.Should().NotBeNull();
265+
serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", ""));
160266
}
161267

162268
[Fact]
163-
public void Given_DeserializedObject_When_Serialized_Then_ShouldMatchOriginalJson()
269+
public void Given_ExampleJson_When_Deserialized_Then_ShouldReturnValidObject()
164270
{
165-
// Arrange
271+
// Arrange & Act
166272
var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson);
167273

168-
// Act
169-
var serializedJson = JsonSerializer.Serialize(deserializedResponse, new JsonSerializerOptions { WriteIndented = false });
170-
171274
// Assert
172-
serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", ""));
275+
deserializedResponse.Should().NotBeNull();
276+
deserializedResponse.Should().BeEquivalentTo(exampleResponse);
173277
}
174278
}

0 commit comments

Comments
 (0)