Skip to content

Commit

Permalink
When part of speech GUID not found, should throw
Browse files Browse the repository at this point in the history
Also adjust test to expect an exception for missing parts of speech.
  • Loading branch information
rmunn committed Jan 8, 2025
1 parent 212264f commit 97bf735
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ public async Task<Sense> 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>(sense.Id) ?? throw new NullReferenceException();
Expand Down
10 changes: 5 additions & 5 deletions backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(async () =>
await Api.CreateSense(Entry1Id, new Sense() { Id = senseId, PartOfSpeech = null, PartOfSpeechId = partOfSpeechId, })

Check failure on line 367 in backend/FwLite/MiniLcm.Tests/BasicApiTestsBase.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

FwDataMiniLcmBridge.Tests.MiniLcmTests.BasicApiTests.CreateSense_WillThrowExceptionWithMissingPartOfSpeech

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(System.InvalidOperationException) Actual: typeof(System.ArgumentException) ---- System.ArgumentException : Part of speech must exist when creating a sense (could not find GUID 0bb9a776-c9bf-46d5-a80b-0437fadae1cc)
);
exception.Should().NotBeNull("because the part of speech does not exist (or was deleted)");
}

[Fact]
Expand Down

0 comments on commit 97bf735

Please sign in to comment.