Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman committed Nov 3, 2024
1 parent 87e9d46 commit d6e0a16
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/SIL.XForge.Scripture/Services/MachineApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -984,16 +984,23 @@ private static ServalBuildDto CreateDto(TranslationBuild translationBuild) =>
AdditionalInfo = new ServalBuildAdditionalInfo
{
BuildId = translationBuild.Id,
CorporaIds =
[
.. translationBuild.Pretranslate?.Select(t => t.Corpus?.Id).Where(id => !string.IsNullOrEmpty(id))
?? [],
.. translationBuild.Pretranslate?.SelectMany(t => t.SourceFilters ?? []).Select(f => f.Corpus.Id)
?? [],
.. translationBuild.TrainOn?.Select(t => t.Corpus?.Id).Where(id => !string.IsNullOrEmpty(id)) ?? [],
.. translationBuild.TrainOn?.SelectMany(t => t.SourceFilters ?? []).Select(f => f.Corpus.Id) ?? [],
.. translationBuild.TrainOn?.SelectMany(t => t.TargetFilters ?? []).Select(f => f.Corpus.Id) ?? [],
],
CorporaIds = new HashSet<string>(
// Use a HashSet to ensure there are no duplicate corpus ids
[
.. translationBuild
.Pretranslate?.Select(t => t.Corpus?.Id)
.Where(id => !string.IsNullOrEmpty(id)) ?? [],
.. translationBuild
.Pretranslate?.SelectMany(t => t.SourceFilters ?? [])
.Select(f => f.Corpus.Id) ?? [],
.. translationBuild.TrainOn?.Select(t => t.Corpus?.Id).Where(id => !string.IsNullOrEmpty(id))
?? [],
.. translationBuild.TrainOn?.SelectMany(t => t.SourceFilters ?? []).Select(f => f.Corpus.Id)
?? [],
.. translationBuild.TrainOn?.SelectMany(t => t.TargetFilters ?? []).Select(f => f.Corpus.Id)
?? [],
]
),
DateFinished = translationBuild.DateFinished,
Step = translationBuild.Step,
TranslationEngineId = translationBuild.Engine.Id,
Expand Down
122 changes: 119 additions & 3 deletions test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ public async Task GetBuildAsync_IncludesAdditionalInfo()
const string engineId = "engineId1";
const string corpusId1 = "corpusId1";
const string corpusId2 = "corpusId2";
const string corpusId3 = "corpusId3";
const string corpusId4 = "corpusId4";
const int step = 123;
env.TranslationEnginesClient.GetBuildAsync(
TranslationEngine01,
Expand All @@ -434,12 +436,59 @@ public async Task GetBuildAsync_IncludesAdditionalInfo()
[
new PretranslateCorpus
{
// Previous corpus format
Corpus = new ResourceLink { Id = corpusId1, Url = "https://example.com" },
},
new PretranslateCorpus
{
// Previous corpus format
Corpus = new ResourceLink { Id = corpusId2, Url = "https://example.com" },
},
new PretranslateCorpus
{
// New parallel corpus format
SourceFilters =
[
new ParallelCorpusFilter
{
Corpus = new ResourceLink { Id = corpusId1, Url = "https://example.com" }
},
new ParallelCorpusFilter
{
Corpus = new ResourceLink { Id = corpusId2, Url = "https://example.com" }
},
],
},
// Invalid corpus format
new PretranslateCorpus(),
],
TrainOn =
[
new TrainingCorpus
{
// Previous corpus format
Corpus = new ResourceLink { Id = corpusId3, Url = "https://example.com" },
},
new TrainingCorpus
{
// New parallel corpus format
SourceFilters =
[
new ParallelCorpusFilter
{
Corpus = new ResourceLink { Id = corpusId3, Url = "https://example.com" }
},
],
TargetFilters =
[
new ParallelCorpusFilter
{
Corpus = new ResourceLink { Id = corpusId4, Url = "https://example.com" }
},
],
},
// Invalid corpus format
new TrainingCorpus(),
],
}
)
Expand Down Expand Up @@ -472,9 +521,11 @@ public async Task GetBuildAsync_IncludesAdditionalInfo()
Assert.AreEqual(step, actual.AdditionalInfo.Step);
Assert.AreEqual(engineId, actual.AdditionalInfo.TranslationEngineId);
Assert.IsNotNull(actual.AdditionalInfo.CorporaIds);
Assert.AreEqual(2, actual.AdditionalInfo.CorporaIds.Count());
Assert.AreEqual(corpusId1, actual.AdditionalInfo.CorporaIds.First());
Assert.AreEqual(corpusId2, actual.AdditionalInfo.CorporaIds.Last());
Assert.AreEqual(4, actual.AdditionalInfo.CorporaIds.Count());
Assert.AreEqual(corpusId1, actual.AdditionalInfo.CorporaIds.ElementAt(0));
Assert.AreEqual(corpusId2, actual.AdditionalInfo.CorporaIds.ElementAt(1));
Assert.AreEqual(corpusId3, actual.AdditionalInfo.CorporaIds.ElementAt(2));
Assert.AreEqual(corpusId4, actual.AdditionalInfo.CorporaIds.ElementAt(3));
}

[Test]
Expand Down Expand Up @@ -1972,6 +2023,71 @@ await env.Service.StartPreTranslationBuildAsync(
);
}

[Test]
public async Task StartPreTranslationBuildAsync_SuccessWithTrainingAndTranslationScriptureRanges()
{
// Set up test environment
var env = new TestEnvironment();
const string scriptureRange1 = "GEN";
const string scriptureRange2 = "EXO";

// SUT
await env.Service.StartPreTranslationBuildAsync(
User01,
new BuildConfig
{
ProjectId = Project01,
TrainingScriptureRanges =
[
new ProjectScriptureRange { ProjectId = Project01, ScriptureRange = scriptureRange1 }
],
TranslationScriptureRanges =
[
new ProjectScriptureRange { ProjectId = Project02, ScriptureRange = scriptureRange2 }
],
},
CancellationToken.None
);

await env.ProjectService.Received(1).SyncAsync(User01, Project01);
env.BackgroundJobClient.Received(1).Create(Arg.Any<Job>(), Arg.Any<IState>());
Assert.AreEqual(JobId, env.ProjectSecrets.Get(Project01).ServalData!.PreTranslationJobId);
Assert.IsNotNull(env.ProjectSecrets.Get(Project01).ServalData?.PreTranslationQueuedAt);
Assert.IsNull(env.ProjectSecrets.Get(Project01).ServalData?.PreTranslationErrorMessage);
Assert.AreEqual(
1,
env.Projects.Get(Project01).TranslateConfig.DraftConfig.LastSelectedTrainingScriptureRanges.Count
);
Assert.AreEqual(
Project01,
env.Projects.Get(Project01)
.TranslateConfig.DraftConfig.LastSelectedTrainingScriptureRanges.First()
.ProjectId
);
Assert.AreEqual(
scriptureRange1,
env.Projects.Get(Project01)
.TranslateConfig.DraftConfig.LastSelectedTrainingScriptureRanges.First()
.ScriptureRange
);
Assert.AreEqual(
1,
env.Projects.Get(Project01).TranslateConfig.DraftConfig.LastSelectedTranslationScriptureRanges.Count
);
Assert.AreEqual(
Project02,
env.Projects.Get(Project01)
.TranslateConfig.DraftConfig.LastSelectedTranslationScriptureRanges.First()
.ProjectId
);
Assert.AreEqual(
scriptureRange2,
env.Projects.Get(Project01)
.TranslateConfig.DraftConfig.LastSelectedTranslationScriptureRanges.First()
.ScriptureRange
);
}

[Test]
public async Task StartPreTranslationBuildAsync_AlternateTrainingSource()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public async Task GetPreTranslationParametersAsync_ThrowsExceptionWhenNoPreTrans
Assert.ThrowsAsync<DataNotFoundException>(() => env.Service.GetPreTranslationParametersAsync(Project01));
}

[Test]
public async Task GetPreTranslationParametersAsync_ThrowsExceptionWhenNullServalData()
{
// Set up test environment
var env = new TestEnvironment();
await env.SetupProjectSecretAsync(servalData: null);

// SUT
Assert.ThrowsAsync<DataNotFoundException>(() => env.Service.GetPreTranslationParametersAsync(Project01));
}

[Test]
public void GetPreTranslationParametersAsync_ThrowsExceptionWhenProjectSecretMissing()
{
Expand Down

0 comments on commit d6e0a16

Please sign in to comment.