Skip to content

Commit ec4b7b2

Browse files
authored
Merge pull request #111 from cnblogs/remove-fluent-assertion
refactor: remove FluentAssertions
2 parents 32d51c8 + 38df2c7 commit ec4b7b2

24 files changed

+203
-156
lines changed

sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.Extensions.AI" Version="9.5.0"/>
26+
<PackageReference Include="Microsoft.Extensions.AI" Version="9.7.0" />
2727
</ItemGroup>
2828

2929
</Project>

src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="JsonSchema.Net.Generation" Version="5.0.3" />
15-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.5.0" />
14+
<PackageReference Include="JsonSchema.Net.Generation" Version="5.0.4" />
15+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.7.0" />
1616
</ItemGroup>
1717

1818
</Project>

test/Cnblogs.DashScope.AI.UnitTests/ChatClientTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Text;
22
using Cnblogs.DashScope.Core;
33
using Cnblogs.DashScope.Tests.Shared.Utils;
4-
using FluentAssertions;
4+
55
using Microsoft.Extensions.AI;
66
using NSubstitute;
77
using NSubstitute.Extensions;
@@ -46,7 +46,7 @@ public async Task ChatClient_TextCompletion_SuccessAsync()
4646
Arg.Is<ModelRequest<TextGenerationInput, ITextGenerationParameters>>(
4747
m => m.IsEquivalent(testCase.RequestModel)),
4848
Arg.Any<CancellationToken>());
49-
response.Messages[0].Text.Should().Be(testCase.ResponseModel.Output.Choices?.First().Message.Content);
49+
Assert.Equal(testCase.ResponseModel.Output.Choices![0].Message.Content, response.Messages[0].Text);
5050
}
5151

5252
[Fact]
@@ -93,7 +93,7 @@ public async Task ChatClient_TextCompletionStream_SuccessAsync()
9393
Arg.Is<ModelRequest<TextGenerationInput, ITextGenerationParameters>>(
9494
m => m.IsEquivalent(testCase.RequestModel)),
9595
Arg.Any<CancellationToken>());
96-
text.ToString().Should().Be(testCase.ResponseModel.Output.Choices?.First().Message.Content);
96+
Assert.Equal(testCase.ResponseModel.Output.Choices![0].Message.Content, text.ToString());
9797
}
9898

9999
[Fact]
@@ -139,8 +139,7 @@ public async Task ChatClient_ImageRecognition_SuccessAsync()
139139
await dashScopeClient.Received().GetMultimodalGenerationAsync(
140140
Arg.Is<ModelRequest<MultimodalInput, IMultimodalParameters>>(m => m.IsEquivalent(testCase.RequestModel)),
141141
Arg.Any<CancellationToken>());
142-
response.Messages[0].Text.Should()
143-
.BeEquivalentTo(testCase.ResponseModel.Output.Choices[0].Message.Content[0].Text);
142+
Assert.Equal(testCase.ResponseModel.Output.Choices[0].Message.Content[0].Text, response.Messages[0].Text);
144143
}
145144

146145
[Fact]
@@ -191,6 +190,6 @@ public async Task ChatClient_ImageRecognitionStream_SuccessAsync()
191190
_ = dashScopeClient.Received().GetMultimodalGenerationStreamAsync(
192191
Arg.Is<ModelRequest<MultimodalInput, IMultimodalParameters>>(m => m.IsEquivalent(testCase.RequestModel)),
193192
Arg.Any<CancellationToken>());
194-
text.ToString().Should().Be(testCase.ResponseModel.Output.Choices.First().Message.Content[0].Text);
193+
Assert.Equal(testCase.ResponseModel.Output.Choices.First().Message.Content[0].Text, text.ToString());
195194
}
196195
}

test/Cnblogs.DashScope.AI.UnitTests/Cnblogs.DashScope.AI.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
16-
<PackageReference Include="xunit" Version="2.9.3" />
1716
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
1817
<PrivateAssets>all</PrivateAssets>
1918
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

test/Cnblogs.DashScope.AI.UnitTests/EmbeddingClientTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Cnblogs.DashScope.Core;
22
using Cnblogs.DashScope.Tests.Shared.Utils;
3-
using FluentAssertions;
43
using Microsoft.Extensions.AI;
54
using NSubstitute;
65
using NSubstitute.Extensions;
@@ -34,10 +33,11 @@ public async Task EmbeddingClient_Text_SuccessAsync()
3433

3534
// Assert
3635
_ = dashScopeClient.Received().GetEmbeddingsAsync(
37-
Arg.Is<ModelRequest<TextEmbeddingInput, ITextEmbeddingParameters>>(
38-
m => m.IsEquivalent(testCase.RequestModel)),
36+
Arg.Is<ModelRequest<TextEmbeddingInput, ITextEmbeddingParameters>>(m
37+
=> m.IsEquivalent(testCase.RequestModel)),
3938
Arg.Any<CancellationToken>());
40-
response.Select(x => x.Vector.ToArray()).Should()
41-
.BeEquivalentTo(testCase.ResponseModel.Output.Embeddings.Select(x => x.Embedding));
39+
Assert.Equivalent(
40+
testCase.ResponseModel.Output.Embeddings.Select(x => x.Embedding),
41+
response.Select(x => x.Vector.ToArray()));
4242
}
4343
}

test/Cnblogs.DashScope.Sdk.UnitTests/ApplicationSerializationTests.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Cnblogs.DashScope.Tests.Shared.Utils;
2-
using FluentAssertions;
32
using NSubstitute;
43

54
namespace Cnblogs.DashScope.Sdk.UnitTests;
@@ -21,7 +20,7 @@ public async Task SingleCompletion_TextNoSse_SuccessAsync()
2120
handler.Received().MockSend(
2221
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
2322
Arg.Any<CancellationToken>());
24-
response.Should().BeEquivalentTo(testCase.ResponseModel);
23+
Assert.Equivalent(testCase.ResponseModel, response);
2524
}
2625

2726
[Fact]
@@ -39,7 +38,7 @@ public async Task SingleCompletion_ThoughtNoSse_SuccessAsync()
3938
handler.Received().MockSend(
4039
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
4140
Arg.Any<CancellationToken>());
42-
response.Should().BeEquivalentTo(testCase.ResponseModel);
41+
Assert.Equivalent(testCase.ResponseModel, response);
4342
}
4443

4544
[Fact]
@@ -58,11 +57,17 @@ public async Task SingleCompletion_TextSse_SuccessAsync()
5857
handler.Received().MockSend(
5958
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
6059
Arg.Any<CancellationToken>());
61-
outputs.SkipLast(1).Should().AllSatisfy(x => x.Output.FinishReason.Should().Be("null"));
62-
outputs.Last().Should().BeEquivalentTo(
63-
testCase.ResponseModel,
64-
o => o.Excluding(y => y.Output.Text).Excluding(x => x.Output.Thoughts));
65-
text.Should().Be(testCase.ResponseModel.Output.Text);
60+
Assert.All(outputs.SkipLast(1), x => Assert.Equal("null", x.Output.FinishReason));
61+
Assert.Equal(testCase.ResponseModel.Output.Text, text);
62+
var last = outputs.Last();
63+
last = last with
64+
{
65+
Output = last.Output with
66+
{
67+
Text = testCase.ResponseModel.Output.Text, Thoughts = testCase.ResponseModel.Output.Thoughts
68+
}
69+
};
70+
Assert.Equivalent(testCase.ResponseModel, last);
6671
}
6772

6873
[Fact]
@@ -80,7 +85,7 @@ public async Task ConversationCompletion_SessionIdNoSse_SuccessAsync()
8085
handler.Received().MockSend(
8186
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
8287
Arg.Any<CancellationToken>());
83-
response.Should().BeEquivalentTo(testCase.ResponseModel);
88+
Assert.Equivalent(testCase.ResponseModel, response);
8489
}
8590

8691
[Fact]
@@ -98,7 +103,7 @@ public async Task ConversationCompletion_MessageNoSse_SuccessAsync()
98103
handler.Received().MockSend(
99104
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
100105
Arg.Any<CancellationToken>());
101-
response.Should().BeEquivalentTo(testCase.ResponseModel);
106+
Assert.Equivalent(testCase.ResponseModel, response);
102107
}
103108

104109
[Fact]
@@ -116,7 +121,7 @@ public async Task SingleCompletion_MemoryNoSse_SuccessAsync()
116121
handler.Received().MockSend(
117122
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
118123
Arg.Any<CancellationToken>());
119-
response.Should().BeEquivalentTo(testCase.ResponseModel);
124+
Assert.Equivalent(testCase.ResponseModel, response);
120125
}
121126

122127
[Fact]
@@ -134,6 +139,6 @@ public async Task SingleCompletion_WorkflowNoSse_SuccessAsync()
134139
handler.Received().MockSend(
135140
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
136141
Arg.Any<CancellationToken>());
137-
response.Should().BeEquivalentTo(testCase.ResponseModel);
142+
Assert.Equivalent(testCase.ResponseModel, response);
138143
}
139144
}

test/Cnblogs.DashScope.Sdk.UnitTests/BackgroundGenerationSerializationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Cnblogs.DashScope.Tests.Shared.Utils;
2-
using FluentAssertions;
2+
33
using NSubstitute;
44

55
namespace Cnblogs.DashScope.Sdk.UnitTests;
@@ -21,6 +21,6 @@ public async Task BackgroundGeneration_CreateTask_SuccessAsync()
2121
handler.Received().MockSend(
2222
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
2323
Arg.Any<CancellationToken>());
24-
response.Should().BeEquivalentTo(testCase.ResponseModel);
24+
Assert.Equivalent(testCase.ResponseModel, response);
2525
}
2626
}

test/Cnblogs.DashScope.Sdk.UnitTests/Cnblogs.DashScope.Sdk.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
</PackageReference>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0"/>
1515
<PackageReference Include="NSubstitute" Version="5.3.0"/>
16-
<PackageReference Include="xunit" Version="2.9.3"/>
1716
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1817
<PrivateAssets>all</PrivateAssets>
1918
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

test/Cnblogs.DashScope.Sdk.UnitTests/DashScopeClientTests.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
using System.Net.Http.Headers;
22
using System.Reflection;
33
using Cnblogs.DashScope.Core;
4-
using FluentAssertions;
4+
using Xunit.Abstractions;
55

66
namespace Cnblogs.DashScope.Sdk.UnitTests;
77

88
public class DashScopeClientTests
99
{
10+
private readonly ITestOutputHelper _output;
11+
12+
public DashScopeClientTests(ITestOutputHelper output)
13+
{
14+
_output = output;
15+
}
16+
1017
[Fact]
1118
public void DashScopeClient_Constructor_New()
1219
{
1320
// Arrange
1421
const string apiKey = "apiKey";
1522

1623
// Act
17-
var act = () => new DashScopeClient(apiKey);
18-
19-
// Assert
20-
act.Should().NotThrow();
24+
var client = new DashScopeClient(apiKey);
25+
_output.WriteLine("hash: " + client.GetHashCode()); // do something to avoid optimization
2126
}
2227

2328
[Theory]
@@ -37,7 +42,7 @@ public void DashScopeClient_Constructor_NotCacheableParams(
3742
var value2 = HttpClientAccessor.GetValue(client2);
3843

3944
// Assert
40-
value.Should().NotBe(value2);
45+
Assert.NotSame(value2, value);
4146
}
4247

4348
[Theory]
@@ -57,7 +62,7 @@ public void DashScopeClient_Constructor_CacheableParams(
5762
var value2 = HttpClientAccessor.GetValue(client2);
5863

5964
// Assert
60-
value.Should().Be(value2);
65+
Assert.Same(value2, value);
6166
}
6267

6368
[Fact]
@@ -71,8 +76,7 @@ public void DashScopeClient_Constructor_WithApiKeyHeader()
7176
var value = HttpClientAccessor.GetValue(client) as HttpClient;
7277

7378
// Assert
74-
value?.DefaultRequestHeaders.Authorization?.Should()
75-
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", apiKey));
79+
Assert.Equivalent(new AuthenticationHeaderValue("Bearer", apiKey), value?.DefaultRequestHeaders.Authorization);
7680
}
7781

7882
[Fact]
@@ -87,7 +91,7 @@ public void DashScopeClient_Constructor_WithWorkspaceId()
8791
var value = HttpClientAccessor.GetValue(client) as HttpClient;
8892

8993
// Assert
90-
value?.DefaultRequestHeaders.GetValues("X-DashScope-WorkSpace").Should().BeEquivalentTo(workspaceId);
94+
Assert.Equal(workspaceId, value?.DefaultRequestHeaders.GetValues("X-DashScope-WorkSpace").First());
9195
}
9296

9397
[Fact]
@@ -102,7 +106,7 @@ public void DashScopeClient_Constructor_WithPrivateEndpoint()
102106
var value = HttpClientAccessor.GetValue(client) as HttpClient;
103107

104108
// Assert
105-
value?.BaseAddress.Should().BeEquivalentTo(new Uri(privateEndpoint));
109+
Assert.Equivalent(new Uri(privateEndpoint), value?.BaseAddress);
106110
}
107111

108112
public static TheoryData<string, string, TimeSpan?, TimeSpan?> ParamsShouldNotCache

test/Cnblogs.DashScope.Sdk.UnitTests/ErrorTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Cnblogs.DashScope.Core;
22
using Cnblogs.DashScope.Tests.Shared.Utils;
3-
using FluentAssertions;
43
using NSubstitute;
54
using NSubstitute.ExceptionExtensions;
65
using NSubstitute.Extensions;
@@ -21,7 +20,8 @@ public async Task Error_AuthError_ExceptionAsync()
2120
var act = async () => await client.GetTextCompletionAsync(testCase.RequestModel);
2221

2322
// Assert
24-
(await act.Should().ThrowAsync<DashScopeException>()).And.Error.Should().BeEquivalentTo(testCase.ResponseModel);
23+
var ex = await Assert.ThrowsAsync<DashScopeException>(act);
24+
Assert.Equivalent(testCase.ResponseModel, ex.Error);
2525
handler.Received().MockSend(
2626
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
2727
Arg.Any<CancellationToken>());
@@ -39,7 +39,8 @@ public async Task Error_ParameterErrorNoSse_ExceptionAsync()
3939
var act = async () => await client.GetTextCompletionAsync(testCase.RequestModel);
4040

4141
// Assert
42-
(await act.Should().ThrowAsync<DashScopeException>()).And.Error.Should().BeEquivalentTo(testCase.ResponseModel);
42+
var ex = await Assert.ThrowsAsync<DashScopeException>(act);
43+
Assert.Equivalent(testCase.ResponseModel, ex.Error);
4344
handler.Received().MockSend(
4445
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
4546
Arg.Any<CancellationToken>());
@@ -58,7 +59,8 @@ public async Task Error_ParameterErrorSse_ExceptionAsync()
5859
var act = async () => await stream.LastAsync();
5960

6061
// Assert
61-
(await act.Should().ThrowAsync<DashScopeException>()).And.Error.Should().BeEquivalentTo(testCase.ResponseModel);
62+
var ex = await Assert.ThrowsAsync<DashScopeException>(act);
63+
Assert.Equivalent(testCase.ResponseModel, ex.Error);
6264
handler.Received().MockSend(
6365
Arg.Is<HttpRequestMessage>(m => Checkers.IsJsonEquivalent(m.Content!, testCase.GetRequestJson(sse))),
6466
Arg.Any<CancellationToken>());
@@ -71,13 +73,15 @@ public async Task Error_NetworkError_ExceptionAsync()
7173
var (client, handler) = Sut.GetTestClient();
7274
handler.Configure().MockSend(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
7375
.Throws(new InvalidOperationException("Network error!"));
76+
var testCase = Snapshots.TextGeneration.TextFormat.SinglePrompt;
7477

7578
// Act
7679
var act = async ()
77-
=> await client.GetTextCompletionAsync(Snapshots.TextGeneration.TextFormat.SinglePrompt.RequestModel);
80+
=> await client.GetTextCompletionAsync(testCase.RequestModel);
7881

7982
// Assert
80-
(await act.Should().ThrowAsync<DashScopeException>()).And.Error.Should().BeNull();
83+
var ex = await Assert.ThrowsAsync<DashScopeException>(act);
84+
Assert.Null(ex.Error);
8185
}
8286

8387
[Fact]
@@ -94,6 +98,7 @@ public async Task Error_OpenAiCompatibleError_ExceptionAsync()
9498
"other");
9599

96100
// Assert
97-
(await act.Should().ThrowAsync<DashScopeException>()).And.Error.Should().BeEquivalentTo(testCase.ResponseModel);
101+
var ex = await Assert.ThrowsAsync<DashScopeException>(act);
102+
Assert.Equivalent(testCase.ResponseModel, ex.Error);
98103
}
99104
}

0 commit comments

Comments
 (0)