Skip to content

Commit 3431ec6

Browse files
committed
Separate single test into two distinct tests for serialization and deserialization
1 parent 97b84ec commit 3431ec6

File tree

1 file changed

+149
-141
lines changed

1 file changed

+149
-141
lines changed
Lines changed: 149 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,174 @@
11
using System.Text.Json;
2-
32
using AzureOpenAIProxy.ApiApp.Models;
4-
53
using FluentAssertions;
64

75
namespace AzureOpenAIProxy.ApiApp.Tests.Models;
6+
87
public class CreateChatCompletionResponseTests
98
{
10-
[Fact]
11-
public void Given_ExampleJson_When_DeserializedAndSerialized_Then_ShouldMatchExpectedJson()
9+
private readonly string exampleJson = @"
1210
{
13-
// Arrange
14-
var exampleJson = @"
15-
{
16-
""id"": ""string"",
17-
""object"": ""chat.completion"",
18-
""created"": 1620241923,
19-
""model"": ""string"",
20-
""usage"": {
21-
""prompt_tokens"": 0,
22-
""completion_tokens"": 0,
23-
""total_tokens"": 0
24-
},
25-
""system_fingerprint"": ""string"",
26-
""prompt_filter_results"": [
27-
{
28-
""prompt_index"": 0,
29-
""content_filter_results"": {
30-
""sexual"": {
31-
""filtered"": true,
32-
""severity"": ""safe""
33-
},
34-
""violence"": {
35-
""filtered"": true,
36-
""severity"": ""safe""
37-
},
38-
""hate"": {
39-
""filtered"": true,
40-
""severity"": ""safe""
41-
},
42-
""self_harm"": {
43-
""filtered"": true,
44-
""severity"": ""safe""
45-
},
46-
""profanity"": {
47-
""filtered"": true,
48-
""detected"": true
49-
},
50-
""error"": {
51-
""code"": ""string"",
52-
""message"": ""string""
53-
},
54-
""jailbreak"": {
55-
""filtered"": true,
56-
""detected"": true
57-
}
11+
""id"": ""string"",
12+
""object"": ""chat.completion"",
13+
""created"": 1620241923,
14+
""model"": ""string"",
15+
""usage"": {
16+
""prompt_tokens"": 0,
17+
""completion_tokens"": 0,
18+
""total_tokens"": 0
19+
},
20+
""system_fingerprint"": ""string"",
21+
""prompt_filter_results"": [
22+
{
23+
""prompt_index"": 0,
24+
""content_filter_results"": {
25+
""sexual"": {
26+
""filtered"": true,
27+
""severity"": ""safe""
28+
},
29+
""violence"": {
30+
""filtered"": true,
31+
""severity"": ""safe""
32+
},
33+
""hate"": {
34+
""filtered"": true,
35+
""severity"": ""safe""
36+
},
37+
""self_harm"": {
38+
""filtered"": true,
39+
""severity"": ""safe""
40+
},
41+
""profanity"": {
42+
""filtered"": true,
43+
""detected"": true
44+
},
45+
""error"": {
46+
""code"": ""string"",
47+
""message"": ""string""
48+
},
49+
""jailbreak"": {
50+
""filtered"": true,
51+
""detected"": true
5852
}
5953
}
60-
],
61-
""choices"": [
62-
{
63-
""index"": 0,
64-
""finish_reason"": ""string"",
65-
""message"": {
66-
""role"": ""assistant"",
67-
""content"": ""string"",
68-
""tool_calls"": [
69-
{
70-
""id"": ""string"",
71-
""type"": ""function"",
72-
""function"": {
73-
""name"": ""string"",
74-
""arguments"": ""string""
75-
}
54+
}
55+
],
56+
""choices"": [
57+
{
58+
""index"": 0,
59+
""finish_reason"": ""string"",
60+
""message"": {
61+
""role"": ""assistant"",
62+
""content"": ""string"",
63+
""tool_calls"": [
64+
{
65+
""id"": ""string"",
66+
""type"": ""function"",
67+
""function"": {
68+
""name"": ""string"",
69+
""arguments"": ""string""
7670
}
77-
],
78-
""function_call"": {
79-
""name"": ""string"",
80-
""arguments"": ""string""
81-
},
82-
""context"": {
83-
""citations"": [
84-
{
85-
""content"": ""string"",
86-
""title"": ""string"",
87-
""url"": ""string"",
88-
""filepath"": ""string"",
89-
""chunk_id"": ""string""
90-
}
91-
],
92-
""intent"": ""string""
9371
}
72+
],
73+
""function_call"": {
74+
""name"": ""string"",
75+
""arguments"": ""string""
9476
},
95-
""content_filter_results"": {
96-
""sexual"": {
97-
""filtered"": true,
98-
""severity"": ""safe""
99-
},
100-
""violence"": {
101-
""filtered"": true,
102-
""severity"": ""safe""
103-
},
104-
""hate"": {
105-
""filtered"": true,
106-
""severity"": ""safe""
107-
},
108-
""self_harm"": {
109-
""filtered"": true,
110-
""severity"": ""safe""
111-
},
112-
""profanity"": {
113-
""filtered"": true,
114-
""detected"": true
115-
},
116-
""error"": {
117-
""code"": ""string"",
118-
""message"": ""string""
119-
},
120-
""protected_material_text"": {
121-
""filtered"": true,
122-
""detected"": true
123-
},
124-
""protected_material_code"": {
125-
""filtered"": true,
126-
""detected"": true,
127-
""citation"": {
128-
""URL"": ""string"",
129-
""license"": ""string""
130-
}
131-
}
132-
},
133-
""logprobs"": {
134-
""content"": [
77+
""context"": {
78+
""citations"": [
13579
{
136-
""token"": ""string"",
137-
""logprob"": 0,
138-
""bytes"": [
139-
0
140-
],
141-
""top_logprobs"": [
142-
{
143-
""token"": ""string"",
144-
""logprob"": 0,
145-
""bytes"": [
146-
0
147-
]
148-
}
149-
]
80+
""content"": ""string"",
81+
""title"": ""string"",
82+
""url"": ""string"",
83+
""filepath"": ""string"",
84+
""chunk_id"": ""string""
15085
}
151-
]
86+
],
87+
""intent"": ""string""
15288
}
89+
},
90+
""content_filter_results"": {
91+
""sexual"": {
92+
""filtered"": true,
93+
""severity"": ""safe""
94+
},
95+
""violence"": {
96+
""filtered"": true,
97+
""severity"": ""safe""
98+
},
99+
""hate"": {
100+
""filtered"": true,
101+
""severity"": ""safe""
102+
},
103+
""self_harm"": {
104+
""filtered"": true,
105+
""severity"": ""safe""
106+
},
107+
""profanity"": {
108+
""filtered"": true,
109+
""detected"": true
110+
},
111+
""error"": {
112+
""code"": ""string"",
113+
""message"": ""string""
114+
},
115+
""protected_material_text"": {
116+
""filtered"": true,
117+
""detected"": true
118+
},
119+
""protected_material_code"": {
120+
""filtered"": true,
121+
""detected"": true,
122+
""citation"": {
123+
""URL"": ""string"",
124+
""license"": ""string""
125+
}
126+
}
127+
},
128+
""logprobs"": {
129+
""content"": [
130+
{
131+
""token"": ""string"",
132+
""logprob"": 0,
133+
""bytes"": [
134+
0
135+
],
136+
""top_logprobs"": [
137+
{
138+
""token"": ""string"",
139+
""logprob"": 0,
140+
""bytes"": [
141+
0
142+
]
143+
}
144+
]
145+
}
146+
]
153147
}
154-
]
155-
}";
148+
}
149+
]
150+
}";
156151

157-
// Act
152+
[Fact]
153+
public void Given_ExampleJson_When_Deserialized_Then_ShouldReturnValidObject()
154+
{
155+
// Arrange & Act
158156
var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson);
159-
var serializedJson = JsonSerializer.Serialize(deserializedResponse, new JsonSerializerOptions { WriteIndented = false });
160157

161158
// Assert
162159
deserializedResponse.Should().NotBeNull();
163-
serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", ""));
160+
}
161+
162+
[Fact]
163+
public void Given_DeserializedObject_When_Serialized_Then_ShouldMatchOriginalJson()
164+
{
165+
// Arrange
166+
var deserializedResponse = JsonSerializer.Deserialize<CreateChatCompletionResponse>(exampleJson);
167+
168+
// Act
169+
var serializedJson = JsonSerializer.Serialize(deserializedResponse, new JsonSerializerOptions { WriteIndented = false });
164170

171+
// Assert
172+
serializedJson.Should().Be(exampleJson.Replace("\r", "").Replace("\n", "").Replace(" ", ""));
165173
}
166174
}

0 commit comments

Comments
 (0)