Skip to content

Commit c1b3f15

Browse files
authored
Example of mocking high level client (qdrant#84)
This commit provides an example of mocking the high level client
1 parent 6233b9a commit c1b3f15

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

tests/Qdrant.Client.Tests/MockingTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ namespace Qdrant.Client;
99
public class MockingTests
1010
{
1111
[Fact]
12-
public void CanMockClientCalls()
12+
public void CanMockGrpcClientCalls()
1313
{
1414
var client = new Mock<QdrantGrpcClient>();
1515
var qdrant = new Mock<Qdrant.Client.Grpc.Qdrant.QdrantClient>();
1616

17+
var mockResponse = new HealthCheckReply { Title = "from Moq", Version = "v1.0.0" };
18+
1719
qdrant.Setup(q =>
1820
q.HealthCheck(
1921
It.IsAny<HealthCheckRequest>(),
2022
It.IsAny<CallOptions>()))
21-
.Returns(new HealthCheckReply { Title = "from Moq", Version = "v1.0.0" });
23+
.Returns(mockResponse);
2224

2325
client.SetupGet(c => c.Qdrant).Returns(qdrant.Object);
2426
var response = client.Object.Qdrant.HealthCheck(new HealthCheckRequest(), new CallOptions());
@@ -27,4 +29,35 @@ public void CanMockClientCalls()
2729
response.Title.Should().Be("from Moq");
2830
response.Version.Should().Be("v1.0.0");
2931
}
32+
33+
[Fact]
34+
public async Task CanMockHighLevelClientCalls()
35+
{
36+
var grpcClient = new Mock<QdrantGrpcClient>();
37+
var client = new QdrantClient(grpcClient.Object);
38+
var qdrant = new Mock<Qdrant.Client.Grpc.Qdrant.QdrantClient>();
39+
40+
var mockResponse = new HealthCheckReply { Title = "from Moq", Version = "v1.0.0" };
41+
42+
qdrant.Setup(q =>
43+
q.HealthCheckAsync(
44+
It.IsAny<HealthCheckRequest>(),
45+
It.IsAny<Metadata>(),
46+
It.IsAny<DateTime?>(),
47+
It.IsAny<CancellationToken>()))
48+
.Returns(new AsyncUnaryCall<HealthCheckReply>(
49+
Task.FromResult(mockResponse),
50+
null!,
51+
null!,
52+
null!,
53+
() => { }
54+
));
55+
56+
grpcClient.SetupGet(c => c.Qdrant).Returns(qdrant.Object);
57+
var response = await client.HealthAsync();
58+
59+
response.Should().NotBeNull();
60+
response.Title.Should().Be("from Moq");
61+
response.Version.Should().Be("v1.0.0");
62+
}
3063
}

0 commit comments

Comments
 (0)