From 913400a143e2f344c66475925cc68f9f3ad6993e Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Thu, 9 Jan 2025 10:53:00 -0500 Subject: [PATCH] Fix complex form test - this one should fail The Components property on a complex form can contain empty GUIDs for the complex form entry ID, because we can infer that (it's the entry we're looking at right now). But it cannot contain an empty GUID for the component entry ID, because that's meaningless: which component is being referenced is the whole point of the Components property. --- .../MiniLcm.Tests/Validators/EntryValidatorTests.cs | 4 ++-- backend/FwLite/MiniLcm/Validators/EntryValidator.cs | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs index 46c34ff27..63c033415 100644 --- a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs +++ b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs @@ -128,11 +128,11 @@ public void Succeeds_WhenComplexFormsContainEmptyGuid() } [Fact] - public void Succeeds_WhenComponentsContainEmptyGuid() + public void Fails_WhenComponentsContainEmptyGuid() { var entryId = Guid.NewGuid(); var entry = new Entry() { Id = entryId, LexemeForm = new MultiString(){{"en", "lexeme"}}, Components = [new ComplexFormComponent(){ ComplexFormEntryId = entryId, ComponentEntryId = Guid.Empty }] }; - _validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); + _validator.TestValidate(entry).ShouldHaveValidationErrorFor("Components[0]"); } private void SetProperty(Entry entry, string propName, string content) diff --git a/backend/FwLite/MiniLcm/Validators/EntryValidator.cs b/backend/FwLite/MiniLcm/Validators/EntryValidator.cs index e2cc980fb..986caaf86 100644 --- a/backend/FwLite/MiniLcm/Validators/EntryValidator.cs +++ b/backend/FwLite/MiniLcm/Validators/EntryValidator.cs @@ -13,6 +13,7 @@ public EntryValidator() RuleFor(e => e.LiteralMeaning).NoEmptyValues(); RuleFor(e => e.Note).NoEmptyValues(); RuleForEach(e => e.Senses).SetValidator(entry => new SenseValidator(entry)); + RuleForEach(e => e.Components).Must(NotBeEmptyComponentReference); RuleForEach(e => e.Components).Must(NotBeComponentSelfReference); RuleForEach(e => e.Components).Must(HaveCorrectComponentEntryReference); RuleForEach(e => e.ComplexForms).Must(NotBeComplexFormSelfReference); @@ -23,13 +24,19 @@ public EntryValidator() RuleForEach(e => e.ComplexFormTypes).SetValidator(new ComplexFormTypeValidator()); } + private bool NotBeEmptyComponentReference(Entry entry, ComplexFormComponent component) + { + return component.ComponentEntryId != Guid.Empty; + } + private bool NotBeComponentSelfReference(Entry entry, ComplexFormComponent component) { - return component.ComponentEntryId != entry.Id || component.ComponentEntryId == Guid.Empty; + return component.ComponentEntryId != entry.Id; } private bool HaveCorrectComponentEntryReference(Entry entry, ComplexFormComponent component) { + // Empty GUID is okay here because it can be guessed from the parent object return component.ComplexFormEntryId == entry.Id || component.ComplexFormEntryId == Guid.Empty; } @@ -40,6 +47,7 @@ private bool NotBeComplexFormSelfReference(Entry entry, ComplexFormComponent com private bool HaveCorrectComplexFormEntryReference(Entry entry, ComplexFormComponent component) { + // Empty GUID is okay here because it can be guessed from the parent object return component.ComponentEntryId == entry.Id || component.ComponentEntryId == Guid.Empty; } }