From 0fc946e1256da30628b7900c01237187943f78e9 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Tue, 1 Oct 2024 16:36:25 +0700 Subject: [PATCH] add support for changing ComplexForms collection directly --- .../CreateEntryTests.cs | 36 ++++++ .../UpdateComplexFormsTests.cs | 103 +++++++++++++++++- .../Api/UpdateProxy/UpdateEntryProxy.cs | 15 +++ 3 files changed, 149 insertions(+), 5 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge.Tests/CreateEntryTests.cs b/backend/FwLite/FwDataMiniLcmBridge.Tests/CreateEntryTests.cs index 7ed435e27..a699a8a36 100644 --- a/backend/FwLite/FwDataMiniLcmBridge.Tests/CreateEntryTests.cs +++ b/backend/FwLite/FwDataMiniLcmBridge.Tests/CreateEntryTests.cs @@ -82,6 +82,42 @@ public async Task CanCreate_WithComplexFormsProperty() entry!.ComplexForms.Should().ContainSingle(c => c.ComplexFormEntryId == complexForm.Id); } + [Fact] + public async Task CreateEntry_WithComponentSenseDoesNotShowOnComplexFormsList() + { + var componentSenseId = Guid.NewGuid(); + var component = await _api.CreateEntry(new() + { + LexemeForm = { { "en", "test component" } }, + Senses = [new Sense() { Id = componentSenseId, Gloss = { { "en", "test component sense" } } }] + }); + var complexFormEntryId = Guid.NewGuid(); + await _api.CreateEntry(new() + { + Id = complexFormEntryId, + LexemeForm = { { "en", "test" } }, + Components = + [ + new ComplexFormComponent() + { + ComponentEntryId = component.Id, + ComponentHeadword = component.Headword(), + ComponentSenseId = componentSenseId, + ComplexFormEntryId = complexFormEntryId, + ComplexFormHeadword = "test" + } + ] + }); + + var entry = await _api.GetEntry(component.Id); + entry.Should().NotBeNull(); + entry!.ComplexForms.Should().BeEmpty(); + + entry = await _api.GetEntry(complexFormEntryId); + entry.Should().NotBeNull(); + entry!.Components.Should().ContainSingle(c => c.ComplexFormEntryId == complexFormEntryId && c.ComponentEntryId == component.Id && c.ComponentSenseId == componentSenseId); + } + [Fact] public async Task CanCreate_WithComplexFormTypesProperty() { diff --git a/backend/FwLite/FwDataMiniLcmBridge.Tests/UpdateComplexFormsTests.cs b/backend/FwLite/FwDataMiniLcmBridge.Tests/UpdateComplexFormsTests.cs index 9d2552859..286b9f0ff 100644 --- a/backend/FwLite/FwDataMiniLcmBridge.Tests/UpdateComplexFormsTests.cs +++ b/backend/FwLite/FwDataMiniLcmBridge.Tests/UpdateComplexFormsTests.cs @@ -35,8 +35,7 @@ await _api.UpdateEntry(complexForm.Id, ComplexFormComponent.FromEntries(complexForm, component))); var entry = await _api.GetEntry(complexForm.Id); entry.Should().NotBeNull(); - entry!.Components.Should().ContainSingle(c => c.ComponentEntryId == component.Id); - entry!.Components.Should().ContainSingle(c => c.ComplexFormEntryId == complexForm.Id); + entry!.Components.Should().ContainSingle(c => c.ComponentEntryId == component.Id && c.ComplexFormEntryId == complexForm.Id); } [Fact] @@ -124,7 +123,6 @@ public async Task CanChangeComponentSenseId() public async Task CanChangeComponentSenseIdToNull() { var component2SenseId = Guid.NewGuid(); - var component1 = await _api.CreateEntry(new() { LexemeForm = { { "en", "component1" } } }); var component2 = await _api.CreateEntry(new() { LexemeForm = { { "en", "component2" } }, Senses = [new Sense() { Id = component2SenseId, Gloss = { { "en", "component2" } } }] }); var complexFormId = Guid.NewGuid(); var complexForm = await _api.CreateEntry(new() @@ -135,9 +133,9 @@ public async Task CanChangeComponentSenseIdToNull() [ new ComplexFormComponent() { - ComponentEntryId = component1.Id, + ComponentEntryId = component2.Id, ComponentSenseId = component2SenseId, - ComponentHeadword = component1.Headword(), + ComponentHeadword = component2.Headword(), ComplexFormEntryId = complexFormId, ComplexFormHeadword = "complex form" } @@ -177,4 +175,99 @@ public async Task CanChangeComplexFormId() entry.Should().NotBeNull(); entry!.Components.Should().ContainSingle(c => c.ComponentEntryId == component1.Id); } + + [Fact] + public async Task CanAddComplexFormToExistingEntry() + { + var complexForm = await _api.CreateEntry(new() { LexemeForm = { { "en", "complex form" } } }); + var component = await _api.CreateEntry(new() { LexemeForm = { { "en", "component" } } }); + + await _api.UpdateEntry(component.Id, + new UpdateObjectInput().Add(e => e.ComplexForms, + ComplexFormComponent.FromEntries(complexForm, component))); + var entry = await _api.GetEntry(component.Id); + entry.Should().NotBeNull(); + entry!.ComplexForms.Should().ContainSingle(c => c.ComponentEntryId == component.Id && c.ComplexFormEntryId == complexForm.Id); + } + + [Fact] + public async Task CanRemoveComplexFormFromExistingEntry() + { + var component = await _api.CreateEntry(new() { LexemeForm = { { "en", "component" } } }); + var complexFormId = Guid.NewGuid(); + await _api.CreateEntry(new() + { + Id = complexFormId, + LexemeForm = { { "en", "complex form" } }, + Components = [new ComplexFormComponent() + { + ComponentEntryId = component.Id, + ComponentHeadword = component.Headword(), + ComplexFormEntryId = complexFormId, + ComplexFormHeadword = "complex form" + }] + }); + + await _api.UpdateEntry(component.Id, + new UpdateObjectInput().Remove(e => e.ComplexForms, 0)); + var entry = await _api.GetEntry(component.Id); + entry.Should().NotBeNull(); + entry!.ComplexForms.Should().BeEmpty(); + } + + [Fact] + public async Task CanChangeComplexFormIdOnComplexFormsList() + { + var component1 = await _api.CreateEntry(new() { LexemeForm = { { "en", "component1" } } }); + var complexForm2 = await _api.CreateEntry(new() { LexemeForm = { { "en", "complex form 2" } } }); + var complexFormId = Guid.NewGuid(); + await _api.CreateEntry(new() + { + Id = complexFormId, + LexemeForm = { { "en", "complex form" } }, + Components = + [ + new ComplexFormComponent() + { + ComponentEntryId = component1.Id, + ComponentHeadword = component1.Headword(), + ComplexFormEntryId = complexFormId, + ComplexFormHeadword = "complex form" + } + ] + }); + + await _api.UpdateEntry(component1.Id, new UpdateObjectInput().Set(e => e.ComplexForms[0].ComplexFormEntryId, complexForm2.Id)); + var entry = await _api.GetEntry(component1.Id); + entry.Should().NotBeNull(); + entry!.ComplexForms.Should().ContainSingle(c => c.ComplexFormEntryId == complexForm2.Id); + } + + [Fact] + public async Task CanChangeComponentIdOnComplexFormsList() + { + var component1 = await _api.CreateEntry(new() { LexemeForm = { { "en", "component1" } } }); + var component2 = await _api.CreateEntry(new() { LexemeForm = { { "en", "component2" } } }); + var complexFormId = Guid.NewGuid(); + await _api.CreateEntry(new() + { + Id = complexFormId, + LexemeForm = { { "en", "complex form" } }, + Components = + [ + new ComplexFormComponent() + { + ComponentEntryId = component1.Id, + ComponentHeadword = component1.Headword(), + ComplexFormEntryId = complexFormId, + ComplexFormHeadword = "complex form" + } + ] + }); + + await _api.UpdateEntry(component1.Id, new UpdateObjectInput().Set(e => e.ComplexForms[0].ComponentEntryId, component2.Id)); + var entry = await _api.GetEntry(component2.Id); + entry.Should().NotBeNull(); + entry!.ComplexForms.Should().ContainSingle(c => c.ComponentEntryId == component2.Id && c.ComplexFormEntryId == complexFormId); + } } diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs index 5948c2ef0..b49b0a363 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs @@ -56,6 +56,21 @@ public override IList Components set => throw new NotImplementedException(); } + public override IList ComplexForms + { + get => + new UpdateListProxy( + component => lexboxLcmApi.AddComplexFormComponent(lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component), + component => lexboxLcmApi.RemoveComplexFormComponent(lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component), + //todo this does not handle complex forms which reference a sense + i => new UpdateComplexFormComponentProxy(lcmEntry.ComplexFormEntries.ElementAt(i), + lcmEntry, + lexboxLcmApi), + lcmEntry.ComplexFormEntries.Count() + ); + set => throw new NotImplementedException(); + } + public override MultiString Note { get => new UpdateMultiStringProxy(lcmEntry.Comment, lexboxLcmApi);