-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...val.Machine.Shared.Tests/Services/ServalWordAlignmentPlatformOutboxMessageHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using Google.Protobuf.WellKnownTypes; | ||
using Serval.WordAlignment.V1; | ||
|
||
namespace Serval.Machine.Shared.Services; | ||
|
||
[TestFixture] | ||
public class ServalWordAlignmentPlatformOutboxMessageHandlerTests | ||
{ | ||
[Test] | ||
public async Task HandleMessageAsync_BuildStarted() | ||
{ | ||
TestEnvironment env = new(); | ||
|
||
await env.Handler.HandleMessageAsync( | ||
"groupId", | ||
ServalWordAlignmentPlatformOutboxConstants.BuildStarted, | ||
JsonSerializer.Serialize( | ||
new BuildStartedRequest { BuildId = "C" }, | ||
MessageOutboxOptions.JsonSerializerOptions | ||
), | ||
null | ||
); | ||
|
||
_ = env.Client.Received(1).BuildStartedAsync(Arg.Is<BuildStartedRequest>(x => x.BuildId == "C")); | ||
} | ||
|
||
[Test] | ||
public async Task HandleMessageAsync_InsertInferenceResults() | ||
{ | ||
TestEnvironment env = new(); | ||
|
||
await using (MemoryStream stream = new()) | ||
{ | ||
await JsonSerializer.SerializeAsync( | ||
stream, | ||
new[] | ||
{ | ||
new JsonObject | ||
{ | ||
{ "corpusId", "corpus1" }, | ||
{ "textId", "MAT" }, | ||
{ "refs", new JsonArray(["MAT 1:1"]) }, | ||
{ "sourceTokens", new JsonArray(["sourceToken1"]) }, | ||
{ "targetTokens", new JsonArray(["targetToken1"]) }, | ||
{ "alignment", "0-0:1.0:1.0" } | ||
} | ||
}, | ||
MessageOutboxOptions.JsonSerializerOptions | ||
); | ||
stream.Seek(0, SeekOrigin.Begin); | ||
await env.Handler.HandleMessageAsync( | ||
"engine1", | ||
ServalWordAlignmentPlatformOutboxConstants.InsertWordAlignmentResults, | ||
"engine1", | ||
stream | ||
); | ||
} | ||
|
||
_ = env.Client.Received(1).InsertWordAlignments(); | ||
_ = env.WordAlignmentsWriter.Received(1) | ||
.WriteAsync( | ||
new InsertWordAlignmentsRequest | ||
{ | ||
EngineId = "engine1", | ||
CorpusId = "corpus1", | ||
TextId = "MAT", | ||
Refs = { "MAT 1:1" }, | ||
SourceTokens = { "sourceToken1" }, | ||
TargetTokens = { "targetToken1" }, | ||
Alignment = | ||
{ | ||
new WordAlignment.V1.AlignedWordPair() | ||
{ | ||
SourceIndex = 0, | ||
TargetIndex = 0, | ||
Score = 1.0 | ||
} | ||
} | ||
}, | ||
Arg.Any<CancellationToken>() | ||
); | ||
} | ||
|
||
private class TestEnvironment | ||
{ | ||
public TestEnvironment() | ||
{ | ||
Client = Substitute.For<WordAlignmentPlatformApi.WordAlignmentPlatformApiClient>(); | ||
Client.BuildStartedAsync(Arg.Any<BuildStartedRequest>()).Returns(CreateEmptyUnaryCall()); | ||
Client.BuildCanceledAsync(Arg.Any<BuildCanceledRequest>()).Returns(CreateEmptyUnaryCall()); | ||
Client.BuildFaultedAsync(Arg.Any<BuildFaultedRequest>()).Returns(CreateEmptyUnaryCall()); | ||
Client.BuildCompletedAsync(Arg.Any<BuildCompletedRequest>()).Returns(CreateEmptyUnaryCall()); | ||
Client | ||
.IncrementEngineCorpusSizeAsync(Arg.Any<IncrementEngineCorpusSizeRequest>()) | ||
.Returns(CreateEmptyUnaryCall()); | ||
WordAlignmentsWriter = Substitute.For<IClientStreamWriter<InsertWordAlignmentsRequest>>(); | ||
Client | ||
.InsertWordAlignments(cancellationToken: Arg.Any<CancellationToken>()) | ||
.Returns( | ||
TestCalls.AsyncClientStreamingCall( | ||
WordAlignmentsWriter, | ||
Task.FromResult(new Empty()), | ||
Task.FromResult(new Metadata()), | ||
() => Status.DefaultSuccess, | ||
() => new Metadata(), | ||
() => { } | ||
) | ||
); | ||
|
||
Handler = new ServalWordAlignmentPlatformOutboxMessageHandler(Client); | ||
} | ||
|
||
public WordAlignmentPlatformApi.WordAlignmentPlatformApiClient Client { get; } | ||
public ServalWordAlignmentPlatformOutboxMessageHandler Handler { get; } | ||
public IClientStreamWriter<InsertWordAlignmentsRequest> WordAlignmentsWriter { get; } | ||
|
||
private static AsyncUnaryCall<Empty> CreateEmptyUnaryCall() | ||
{ | ||
return new AsyncUnaryCall<Empty>( | ||
Task.FromResult(new Empty()), | ||
Task.FromResult(new Metadata()), | ||
() => Status.DefaultSuccess, | ||
() => new Metadata(), | ||
() => { } | ||
); | ||
} | ||
} | ||
} |