Skip to content

Commit e44fffb

Browse files
authored
Create new example sentence translation if needed (#1648)
* Create new example sentence translation if needed * Add unit test reproducing the bug and fix * Address review comments
1 parent 0445af0 commit e44fffb

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

backend/FwLite/FwDataMiniLcmBridge.Tests/MiniLcmTests/UpdateEntryTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,57 @@ public async Task UpdateEntry_WithNullLexemeFormOA_CreatesNewLexemeForm()
4444
updatedEntry.LexemeForm.Should().NotBeNull();
4545
updatedEntry.LexemeForm["en"].Should().Be("updated test");
4646
}
47+
48+
[Fact]
49+
public async Task UpdateEntry_CanUpdateExampleSentenceTranslations_WhenNoTranslationObjectExists()
50+
{
51+
// Arrange
52+
var entry = await Api.CreateEntry(new Entry
53+
{
54+
LexemeForm = { { "en", "test" } },
55+
Note = { { "en", "this is a test note" } },
56+
CitationForm = { { "en", "test" } },
57+
LiteralMeaning = { { "en", "test" } },
58+
Senses =
59+
[
60+
new Sense
61+
{
62+
Gloss = { { "en", "test" } },
63+
Definition = { { "en", "test" } },
64+
ExampleSentences =
65+
[
66+
new ExampleSentence { Sentence = { { "en", "testing is good" } } }
67+
]
68+
}
69+
]
70+
});
71+
72+
var fwApi = (FwDataMiniLcmApi)Api;
73+
var lexEntry = fwApi.EntriesRepository.GetObject(entry.Id);
74+
ArgumentNullException.ThrowIfNull(entry);
75+
ArgumentNullException.ThrowIfNull(lexEntry);
76+
lexEntry.SensesOS[0].ExamplesOS[0].TranslationsOC.Should().ContainSingle();
77+
// Reproduce the bug
78+
UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("Clear TranslationsOC",
79+
"Restore TranslationsOC",
80+
fwApi.Cache.ServiceLocator.ActionHandler,
81+
() =>
82+
{
83+
lexEntry.SensesOS[0].ExamplesOS[0].TranslationsOC.Clear();
84+
});
85+
lexEntry.SensesOS[0].ExamplesOS[0].TranslationsOC.Should().BeEmpty();
86+
87+
var before = entry.Copy();
88+
var exampleSentence = entry.Senses[0].ExampleSentences[0];
89+
exampleSentence.Translation = new() { { "en", "updated" } };
90+
91+
// Act
92+
var updatedEntry = await Api.UpdateEntry(before, entry);
93+
var updatedExampleSentence = updatedEntry.Senses[0].ExampleSentences[0];
94+
95+
// Assert
96+
updatedExampleSentence.Translation.Should().ContainSingle();
97+
updatedExampleSentence.Translation["en"].Should().Be("updated");
98+
updatedEntry.Should().BeEquivalentTo(entry, options => options);
99+
}
47100
}

backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,13 +1324,18 @@ internal void CreateExampleSentence(ILexSense lexSense, ExampleSentence exampleS
13241324
var lexExampleSentence = LexExampleSentenceFactory.Create(exampleSentence.Id);
13251325
InsertExampleSentence(lexSense, lexExampleSentence, between);
13261326
UpdateLcmMultiString(lexExampleSentence.Example, exampleSentence.Sentence);
1327-
var freeTranslationType = CmPossibilityRepository.GetObject(CmPossibilityTags.kguidTranFreeTranslation);
1328-
var translation = CmTranslationFactory.Create(lexExampleSentence, freeTranslationType);
1327+
var translation = CreateExampleSentenceTranslation(lexExampleSentence);
13291328
UpdateLcmMultiString(translation.Translation, exampleSentence.Translation);
13301329
lexExampleSentence.Reference = TsStringUtils.MakeString(exampleSentence.Reference,
13311330
lexExampleSentence.Reference.get_WritingSystem(0));
13321331
}
13331332

1333+
public ICmTranslation CreateExampleSentenceTranslation(ILexExampleSentence parent)
1334+
{
1335+
var freeTranslationType = CmPossibilityRepository.GetObject(CmPossibilityTags.kguidTranFreeTranslation);
1336+
return CmTranslationFactory.Create(parent, freeTranslationType);
1337+
}
1338+
13341339
public async Task<ExampleSentence> CreateExampleSentence(Guid entryId, Guid senseId, ExampleSentence exampleSentence, BetweenPosition? between = null)
13351340
{
13361341
if (exampleSentence.Id == default) exampleSentence.Id = Guid.NewGuid();

backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateExampleSentenceProxy.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using MiniLcm.Models;
1+
using MiniLcm.Models;
22
using SIL.LCModel;
33
using SIL.LCModel.Core.Text;
44

55
namespace FwDataMiniLcmBridge.Api.UpdateProxy;
66

7-
public class UpdateExampleSentenceProxy(ILexExampleSentence sentence, FwDataMiniLcmApi lexboxLcmApi): ExampleSentence
7+
public class UpdateExampleSentenceProxy(ILexExampleSentence sentence, FwDataMiniLcmApi lexboxLcmApi) : ExampleSentence
88
{
99
public override Guid Id
1010
{
@@ -23,7 +23,13 @@ public override RichMultiString Translation
2323
get
2424
{
2525
var firstTranslation = sentence.TranslationsOC.FirstOrDefault()?.Translation;
26-
return firstTranslation is null ? new RichMultiString() : new UpdateRichMultiStringProxy(firstTranslation, lexboxLcmApi);
26+
if (firstTranslation is null)
27+
{
28+
var translation = lexboxLcmApi.CreateExampleSentenceTranslation(sentence);
29+
sentence.TranslationsOC.Add(translation);
30+
firstTranslation = translation.Translation;
31+
}
32+
return new UpdateRichMultiStringProxy(firstTranslation, lexboxLcmApi);
2733
}
2834
set => throw new NotImplementedException();
2935
}

0 commit comments

Comments
 (0)