diff --git a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs index 0416f5d34..adf40dcdc 100644 --- a/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs +++ b/backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs @@ -103,7 +103,7 @@ public void Succeeds_WhenNonEmptyFieldHasNoContent(string fieldName) [InlineData("CitationForm")] [InlineData("LiteralMeaning")] [InlineData("Note")] - public void Failss_WhenNonEmptyFieldHasWsWithEmptyContent(string fieldName) + public void Fails_WhenNonEmptyFieldHasWsWithEmptyContent(string fieldName) { var entry = new Entry() { Id = Guid.NewGuid(), LexemeForm = new MultiString(){{"en", "lexeme"}} }; SetProperty(entry, fieldName, ""); diff --git a/backend/FwLite/MiniLcm.Tests/Validators/ExampleSentenceValidatorTests.cs b/backend/FwLite/MiniLcm.Tests/Validators/ExampleSentenceValidatorTests.cs new file mode 100644 index 000000000..c9c2223c0 --- /dev/null +++ b/backend/FwLite/MiniLcm.Tests/Validators/ExampleSentenceValidatorTests.cs @@ -0,0 +1,91 @@ +using FluentValidation.TestHelper; +using MiniLcm.Validators; + +namespace MiniLcm.Tests.Validators; + +public class ExampleSentenceValidatorTests +{ + private readonly ExampleSentenceValidator _validator = new(); + + [Fact] + public void Succeeds_WhenDeletedAtIsNull() + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}}, DeletedAt = null }; + _validator.TestValidate(example).ShouldNotHaveAnyValidationErrors(); + } + + [Fact] + public void Fails_WhenDeletedAtIsNotNull() + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}}, DeletedAt = DateTimeOffset.UtcNow }; + _validator.TestValidate(example).ShouldHaveValidationErrorFor("DeletedAt"); + } + + [Fact] + public void Succeeds_WhenSentenceIsPresent() + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}} }; + _validator.TestValidate(example).ShouldNotHaveAnyValidationErrors(); + } + + [Fact] + public void Fails_WhenSentenceIsMissing() + { + var example = new ExampleSentence() { Id = Guid.NewGuid() }; + _validator.TestValidate(example).ShouldHaveValidationErrorFor("Sentence"); + } + + [Fact] + public void Fails_WhenSentenceHasNoContent() + { + // Technically the same as Fails_WhenSentenceIsMissing -- should we combine them? + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString() }; + _validator.TestValidate(example).ShouldHaveValidationErrorFor("Sentence"); + } + + [Fact] + public void Fails_WhenSentenceHasWsWithEmptyContent() + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", ""}} }; + _validator.TestValidate(example).ShouldHaveValidationErrorFor("Sentence"); + } + + [Theory] + [InlineData("Translation")] + public void Succeeds_WhenNonEmptyFieldIsPresent(string fieldName) + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}} }; + SetProperty(example, fieldName, "content"); + _validator.TestValidate(example).ShouldNotHaveAnyValidationErrors(); + } + + [Theory] + [InlineData("Translation")] + public void Succeeds_WhenNonEmptyFieldHasNoContent(string fieldName) + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}} }; + MakePropertyEmpty(example, fieldName); + _validator.TestValidate(example).ShouldNotHaveAnyValidationErrors(); + } + + [Theory] + [InlineData("Translation")] + public void Fails_WhenNonEmptyFieldHasWsWithEmptyContent(string fieldName) + { + var example = new ExampleSentence() { Id = Guid.NewGuid(), Sentence = new MultiString(){{"en", "sentence"}} }; + SetProperty(example, fieldName, ""); + _validator.TestValidate(example).ShouldHaveValidationErrorFor(fieldName); + } + + private void SetProperty(ExampleSentence example, string propName, string content) + { + var propInfo = typeof(ExampleSentence).GetProperty(propName); + propInfo?.SetValue(example, new MultiString(){{"en", content}}); + } + + private void MakePropertyEmpty(ExampleSentence example, string propName) + { + var propInfo = typeof(ExampleSentence).GetProperty(propName); + propInfo?.SetValue(example, new MultiString()); + } +} diff --git a/backend/FwLite/MiniLcm.Tests/Validators/SenseValidatorTests.cs b/backend/FwLite/MiniLcm.Tests/Validators/SenseValidatorTests.cs new file mode 100644 index 000000000..69104e216 --- /dev/null +++ b/backend/FwLite/MiniLcm.Tests/Validators/SenseValidatorTests.cs @@ -0,0 +1,65 @@ +using FluentValidation.TestHelper; +using MiniLcm.Validators; + +namespace MiniLcm.Tests.Validators; + +public class SenseValidatorTests +{ + private readonly SenseValidator _validator = new(); + + [Fact] + public void Succeeds_WhenDeletedAtIsNull() + { + var sense = new Sense() { Id = Guid.NewGuid(), DeletedAt = null }; + _validator.TestValidate(sense).ShouldNotHaveAnyValidationErrors(); + } + + [Fact] + public void Fails_WhenDeletedAtIsNotNull() + { + var sense = new Sense() { Id = Guid.NewGuid(), DeletedAt = DateTimeOffset.UtcNow }; + _validator.TestValidate(sense).ShouldHaveValidationErrorFor("DeletedAt"); + } + + [Theory] + [InlineData("Definition")] + [InlineData("Gloss")] + public void Succeeds_WhenNonEmptyFieldIsPresent(string fieldName) + { + var sense = new Sense() { Id = Guid.NewGuid() }; + SetProperty(sense, fieldName, "content"); + _validator.TestValidate(sense).ShouldNotHaveAnyValidationErrors(); + } + + [Theory] + [InlineData("Definition")] + [InlineData("Gloss")] + public void Succeeds_WhenNonEmptyFieldHasNoContent(string fieldName) + { + var sense = new Sense() { Id = Guid.NewGuid() }; + MakePropertyEmpty(sense, fieldName); + _validator.TestValidate(sense).ShouldNotHaveAnyValidationErrors(); + } + + [Theory] + [InlineData("Definition")] + [InlineData("Gloss")] + public void Fails_WhenNonEmptyFieldHasWsWithEmptyContent(string fieldName) + { + var sense = new Sense() { Id = Guid.NewGuid() }; + SetProperty(sense, fieldName, ""); + _validator.TestValidate(sense).ShouldHaveValidationErrorFor(fieldName); + } + + private void SetProperty(Sense sense, string propName, string content) + { + var propInfo = typeof(Sense).GetProperty(propName); + propInfo?.SetValue(sense, new MultiString(){{"en", content}}); + } + + private void MakePropertyEmpty(Sense sense, string propName) + { + var propInfo = typeof(Sense).GetProperty(propName); + propInfo?.SetValue(sense, new MultiString()); + } +} diff --git a/backend/FwLite/MiniLcm/Validators/SenseValidator.cs b/backend/FwLite/MiniLcm/Validators/SenseValidator.cs index c7819224a..682e7db58 100644 --- a/backend/FwLite/MiniLcm/Validators/SenseValidator.cs +++ b/backend/FwLite/MiniLcm/Validators/SenseValidator.cs @@ -10,7 +10,6 @@ public SenseValidator() RuleFor(s => s.DeletedAt).Null(); RuleFor(s => s.Definition).NoEmptyValues(); RuleFor(s => s.Gloss).NoEmptyValues(); - RuleFor(s => s.Gloss).NoEmptyValues(); // RuleFor(s => s.PartOfSpeech).Empty(); // TODO: Comment out if we're not yet ready to move away from strings RuleFor(s => s.PartOfSpeechId).SetValidator(new PartOfSpeechIdValidator()); RuleForEach(s => s.SemanticDomains).SetValidator(new SemanticDomainValidator());