Skip to content

Commit

Permalink
Add validation tests for senses, example sentences
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed Jan 6, 2025
1 parent 4dd4032 commit 04b8b26
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
65 changes: 65 additions & 0 deletions backend/FwLite/MiniLcm.Tests/Validators/SenseValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 0 additions & 1 deletion backend/FwLite/MiniLcm/Validators/SenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 04b8b26

Please sign in to comment.