Skip to content

Commit

Permalink
Fix complex form test - this one should fail
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rmunn committed Jan 9, 2025
1 parent 7e0e604 commit 913400a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion backend/FwLite/MiniLcm/Validators/EntryValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}
}

0 comments on commit 913400a

Please sign in to comment.