From 4dd403220097ed067a74d6d298b313982777a994 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Mon, 6 Jan 2025 15:08:05 -0500 Subject: [PATCH] Add entry validation tests for literal meaning, note Refactored citation form tests to be more generic so they can be resued for other similar fields. --- .../Validators/EntryValidatorTests.cs | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs index f859487a3..0416f5d34 100644 --- a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs +++ b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs @@ -76,37 +76,49 @@ public void Fails_WhenLexemeFormHasWsWithEmptyContent() var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", ""}} }; _validator.TestValidate(entry).ShouldHaveValidationErrorFor("LexemeForm"); } -// - [Fact] - public void Succeeds_WhenCitationFormIsPresent() + [Theory] + [InlineData("CitationForm")] + [InlineData("LiteralMeaning")] + [InlineData("Note")] + public void Succeeds_WhenNonEmptyFieldIsPresent(string fieldName) { - var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}}, CitationForm = new MultiString(){{"en", "citation"}} }; + var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}} }; + SetProperty(entry, fieldName, "content"); _validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); } - [Fact] - public void Succeeds_WhenCitationFormIsMissing() + [Theory] + [InlineData("CitationForm")] + [InlineData("LiteralMeaning")] + [InlineData("Note")] + public void Succeeds_WhenNonEmptyFieldHasNoContent(string fieldName) { var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}} }; + MakePropertyEmpty(entry, fieldName); _validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); } - [Fact] - public void Succeeds_WhenCitationFormHasNoContent() + [Theory] + [InlineData("CitationForm")] + [InlineData("LiteralMeaning")] + [InlineData("Note")] + public void Failss_WhenNonEmptyFieldHasWsWithEmptyContent(string fieldName) { - // Technically the same as Succeeds_WhenCitationFormIsMissing -- should we combine them? - var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}}, CitationForm = new MultiString() }; - _validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); + var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}} }; + SetProperty(entry, fieldName, ""); + _validator.TestValidate(entry).ShouldHaveValidationErrorFor(fieldName); } - [Fact] - public void Fails_WhenCitationFormHasWsWithEmptyContent() + private void SetProperty(Entry entry, string propName, string content) { - var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}}, CitationForm = new MultiString(){{"en", ""}} }; - _validator.TestValidate(entry).ShouldHaveValidationErrorFor("CitationForm"); + var propInfo = typeof(Entry).GetProperty(propName); + propInfo?.SetValue(entry, new MultiString(){{"en", content}}); } - // TODO: That's extremely similar to the LexemeForm tests except for the missing/no content tests. - // And we'll need to write similar tests for LiteralMeaning and Note. A test helper method is clearly needed here. + private void MakePropertyEmpty(Entry entry, string propName) + { + var propInfo = typeof(Entry).GetProperty(propName); + propInfo?.SetValue(entry, new MultiString()); + } }