Skip to content

Commit

Permalink
Added unit tests. (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 authored Dec 24, 2024
1 parent 26bbe46 commit 0a008c3
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 16 deletions.
73 changes: 71 additions & 2 deletions tests/Logitar.Identity.Core.UnitTests/CustomIdentifierTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
namespace Logitar.Identity.Core;
using FluentValidation;
using FluentValidation.Results;
using Logitar.Security.Cryptography;

namespace Logitar.Identity.Core;

[Trait(Traits.Category, Categories.Unit)]
public class CustomIdentifierTests
{
// TODO(fpion): implement
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
{
string value = " 1234567890 ";
CustomIdentifier displayName = new(value);
Assert.Equal(value.Trim(), displayName.Value);
}

[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
[InlineData("")]
[InlineData(" ")]
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
{
var exception = Assert.Throws<ValidationException>(() => new CustomIdentifier(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_ctor_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => new CustomIdentifier(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ToString: it should return the correct string representation.")]
public void Given_CustomIdentifier_When_ToString_Then_CorrectString()
{
CustomIdentifier displayName = new("1234567890");
Assert.Equal(displayName.Value, displayName.ToString());
}

[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
{
string value = " 1234567890 ";
CustomIdentifier? displayName = CustomIdentifier.TryCreate(value);
Assert.NotNull(displayName);
Assert.Equal(value.Trim(), displayName.Value);
}

[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
{
Assert.Null(CustomIdentifier.TryCreate(value));
}

[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => CustomIdentifier.TryCreate(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}
}
50 changes: 48 additions & 2 deletions tests/Logitar.Identity.Core.UnitTests/DescriptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
namespace Logitar.Identity.Core;
using FluentValidation;
using FluentValidation.Results;

namespace Logitar.Identity.Core;

[Trait(Traits.Category, Categories.Unit)]
public class DescriptionTests
{
// TODO(fpion): implement
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
{
string value = " Hello World! ";
Description description = new(value);
Assert.Equal(value.Trim(), description.Value);
}

[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
[InlineData("")]
[InlineData(" ")]
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
{
var exception = Assert.Throws<ValidationException>(() => new Description(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ToString: it should return the correct string representation.")]
public void Given_Description_When_ToString_Then_CorrectString()
{
Description description = new("Hello World!");
Assert.Equal(description.Value, description.ToString());
}

[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
{
string value = " Hello World! ";
Description? description = Description.TryCreate(value);
Assert.NotNull(description);
Assert.Equal(value.Trim(), description.Value);
}

[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
{
Assert.Null(Description.TryCreate(value));
}
}
73 changes: 71 additions & 2 deletions tests/Logitar.Identity.Core.UnitTests/DisplayNameTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
namespace Logitar.Identity.Core;
using FluentValidation;
using FluentValidation.Results;
using Logitar.Security.Cryptography;

namespace Logitar.Identity.Core;

[Trait(Traits.Category, Categories.Unit)]
public class DisplayNameTests
{
// TODO(fpion): implement
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
{
string value = " Administrator ";
DisplayName displayName = new(value);
Assert.Equal(value.Trim(), displayName.Value);
}

[Theory(DisplayName = "ctor: it should throw ValidationException given an empty, or white-space value.")]
[InlineData("")]
[InlineData(" ")]
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
{
var exception = Assert.Throws<ValidationException>(() => new DisplayName(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_ctor_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => new DisplayName(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ToString: it should return the correct string representation.")]
public void Given_DisplayName_When_ToString_Then_CorrectString()
{
DisplayName displayName = new("Administrator");
Assert.Equal(displayName.Value, displayName.ToString());
}

[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
{
string value = " Administrator ";
DisplayName? displayName = DisplayName.TryCreate(value);
Assert.NotNull(displayName);
Assert.Equal(value.Trim(), displayName.Value);
}

[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
{
Assert.Null(DisplayName.TryCreate(value));
}

[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => DisplayName.TryCreate(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}
}
73 changes: 71 additions & 2 deletions tests/Logitar.Identity.Core.UnitTests/IdentifierTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
namespace Logitar.Identity.Core;
using FluentValidation;
using FluentValidation.Results;
using Logitar.Security.Cryptography;

namespace Logitar.Identity.Core;

[Trait(Traits.Category, Categories.Unit)]
public class IdentifierTests
{
// TODO(fpion): implement
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
{
string value = " HealthInsuranceNumber ";
Identifier identifier = new(value);
Assert.Equal(value.Trim(), identifier.Value);
}

[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
[InlineData("")]
[InlineData(" ")]
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
{
var exception = Assert.Throws<ValidationException>(() => new Identifier(value));

ValidationFailure failure = Assert.Single(exception.Errors);
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
Assert.Equal("Value", failure.PropertyName);
}

[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_ctor_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => new Identifier(value));

Assert.Equal(2, exception.Errors.Count());
Assert.Contains(exception.Errors, e => e.ErrorCode == "IdentifierValidator" && e.PropertyName == "Value");
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Value");
}

[Fact(DisplayName = "ToString: it should return the correct string representation.")]
public void Given_Identifier_When_ToString_Then_CorrectString()
{
Identifier identifier = new("HealthInsuranceNumber");
Assert.Equal(identifier.Value, identifier.ToString());
}

[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
{
string value = " HealthInsuranceNumber ";
Identifier? identifier = Identifier.TryCreate(value);
Assert.NotNull(identifier);
Assert.Equal(value.Trim(), identifier.Value);
}

[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
{
Assert.Null(Identifier.TryCreate(value));
}

[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => Identifier.TryCreate(value));

Assert.Equal(2, exception.Errors.Count());
Assert.Contains(exception.Errors, e => e.ErrorCode == "IdentifierValidator" && e.PropertyName == "Value");
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Value");
}
}
75 changes: 73 additions & 2 deletions tests/Logitar.Identity.Core.UnitTests/LocaleTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
namespace Logitar.Identity.Core;
using FluentValidation;
using Logitar.Security.Cryptography;
using System.Globalization;

namespace Logitar.Identity.Core;

[Trait(Traits.Category, Categories.Unit)]
public class LocaleTests
{
// TODO(fpion): implement
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
{
CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA");
Locale locale = new($" {culture} ");
Assert.Equal(culture.Name, locale.Code);
Assert.Equal(culture, locale.Culture);
}

[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
[InlineData("")]
[InlineData(" ")]
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
{
var exception = Assert.Throws<ValidationException>(() => new Locale(value));

Assert.Equal(2, exception.Errors.Count());
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
Assert.Contains(exception.Errors, e => e.ErrorCode == "NotEmptyValidator" && e.PropertyName == "Code");
}

[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_ctor_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => new Locale(value));

Assert.Equal(2, exception.Errors.Count());
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code");
}

[Fact(DisplayName = "ToString: it should return the correct string representation.")]
public void Given_Locale_When_ToString_Then_CorrectString()
{
CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA");
Locale locale = new(culture);
Assert.Equal(string.Format("{0} ({1})", culture.DisplayName, culture.Name), locale.ToString());
}

[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
{
string value = " fr-CA ";
Locale? locale = Locale.TryCreate(value);
Assert.NotNull(locale);
Assert.Equal(value.Trim(), locale.Culture.Name);
}

[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
{
Assert.Null(Locale.TryCreate(value));
}

[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
{
string value = RandomStringGenerator.GetString(999);
var exception = Assert.Throws<ValidationException>(() => Locale.TryCreate(value));

Assert.Equal(2, exception.Errors.Count());
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code");
}
}
Loading

0 comments on commit 0a008c3

Please sign in to comment.