diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index a3178a4bf..e1baecf5a 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -879,8 +879,10 @@ internal void CreateSense(ILexEntry lexEntry, Sense sense, BetweenPosition? betw var lexSense = LexSenseFactory.Create(sense.Id); InsertSense(lexEntry, lexSense, between); var msa = new SandboxGenericMSA() { MsaType = lexSense.GetDesiredMsaType() }; - if (sense.PartOfSpeechId.HasValue && PartOfSpeechRepository.TryGetObject(sense.PartOfSpeechId.Value, out var pos)) + if (sense.PartOfSpeechId.HasValue) { + var found = PartOfSpeechRepository.TryGetObject(sense.PartOfSpeechId.Value, out var pos); + if (!found) throw new ArgumentException($"Part of speech must exist when creating a sense (could not find GUID {sense.PartOfSpeechId.Value})"); msa.MainPOS = pos; } lexSense.SandboxMSA = msa; diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index 22769bb61..30d9701b2 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -483,6 +483,9 @@ public async Task CreateSense(Guid entryId, Sense sense, BetweenPosition? if (sense.Order != default) // we don't anticipate this being necessary, so we'll be strict for now throw new InvalidOperationException("Order should not be provided when creating a sense"); + if (sense.PartOfSpeechId.HasValue && await GetPartOfSpeech(sense.PartOfSpeechId.Value) is null) + throw new InvalidOperationException($"Part of speech must exist when creating a sense (could not find GUID {sense.PartOfSpeechId.Value})"); + sense.Order = await OrderPicker.PickOrder(Senses.Where(s => s.EntryId == entryId), between); await dataModel.AddChanges(ClientId, await CreateSenseChanges(entryId, sense).ToArrayAsync()); return await dataModel.GetLatest(sense.Id) ?? throw new NullReferenceException(); diff --git a/backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs b/backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs index ba95dd9be..de7ea1e76 100644 --- a/backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs +++ b/backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs @@ -359,14 +359,14 @@ public async Task CreateSense_WillCreateWithExistingDomains() } [Fact] - public async Task CreateSense_WontCreateMissingPartOfSpeech() + public async Task CreateSense_WillThrowExceptionWithMissingPartOfSpeech() { var senseId = Guid.NewGuid(); var partOfSpeechId = Guid.NewGuid(); - var createdSense = await Api.CreateSense(Entry1Id, - new Sense() { Id = senseId, PartOfSpeech = null, PartOfSpeechId = partOfSpeechId, }); - createdSense.Id.Should().Be(senseId); - createdSense.PartOfSpeechId.Should().BeNull("because the part of speech does not exist (or was deleted)"); + var exception = await Assert.ThrowsAsync(async () => + await Api.CreateSense(Entry1Id, new Sense() { Id = senseId, PartOfSpeech = null, PartOfSpeechId = partOfSpeechId, }) + ); + exception.Should().NotBeNull("because the part of speech does not exist (or was deleted)"); } [Fact]