-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation tests for senses, example sentences
- Loading branch information
Showing
4 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
backend/FwLite/MiniLcm.Tests/Validators/ExampleSentenceValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
backend/FwLite/MiniLcm.Tests/Validators/SenseValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters