Skip to content

Commit 0a008c3

Browse files
authored
Added unit tests. (#82)
1 parent 26bbe46 commit 0a008c3

File tree

8 files changed

+551
-16
lines changed

8 files changed

+551
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
namespace Logitar.Identity.Core;
1+
using FluentValidation;
2+
using FluentValidation.Results;
3+
using Logitar.Security.Cryptography;
4+
5+
namespace Logitar.Identity.Core;
26

37
[Trait(Traits.Category, Categories.Unit)]
48
public class CustomIdentifierTests
59
{
6-
// TODO(fpion): implement
10+
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
11+
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
12+
{
13+
string value = " 1234567890 ";
14+
CustomIdentifier displayName = new(value);
15+
Assert.Equal(value.Trim(), displayName.Value);
16+
}
17+
18+
[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
19+
[InlineData("")]
20+
[InlineData(" ")]
21+
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
22+
{
23+
var exception = Assert.Throws<ValidationException>(() => new CustomIdentifier(value));
24+
25+
ValidationFailure failure = Assert.Single(exception.Errors);
26+
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
27+
Assert.Equal("Value", failure.PropertyName);
28+
}
29+
30+
[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
31+
public void Given_InvalidValue_When_ctor_Then_ValidationException()
32+
{
33+
string value = RandomStringGenerator.GetString(999);
34+
var exception = Assert.Throws<ValidationException>(() => new CustomIdentifier(value));
35+
36+
ValidationFailure failure = Assert.Single(exception.Errors);
37+
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
38+
Assert.Equal("Value", failure.PropertyName);
39+
}
40+
41+
[Fact(DisplayName = "ToString: it should return the correct string representation.")]
42+
public void Given_CustomIdentifier_When_ToString_Then_CorrectString()
43+
{
44+
CustomIdentifier displayName = new("1234567890");
45+
Assert.Equal(displayName.Value, displayName.ToString());
46+
}
47+
48+
[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
49+
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
50+
{
51+
string value = " 1234567890 ";
52+
CustomIdentifier? displayName = CustomIdentifier.TryCreate(value);
53+
Assert.NotNull(displayName);
54+
Assert.Equal(value.Trim(), displayName.Value);
55+
}
56+
57+
[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
58+
[InlineData(null)]
59+
[InlineData("")]
60+
[InlineData(" ")]
61+
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
62+
{
63+
Assert.Null(CustomIdentifier.TryCreate(value));
64+
}
65+
66+
[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
67+
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
68+
{
69+
string value = RandomStringGenerator.GetString(999);
70+
var exception = Assert.Throws<ValidationException>(() => CustomIdentifier.TryCreate(value));
71+
72+
ValidationFailure failure = Assert.Single(exception.Errors);
73+
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
74+
Assert.Equal("Value", failure.PropertyName);
75+
}
776
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
1-
namespace Logitar.Identity.Core;
1+
using FluentValidation;
2+
using FluentValidation.Results;
3+
4+
namespace Logitar.Identity.Core;
25

36
[Trait(Traits.Category, Categories.Unit)]
47
public class DescriptionTests
58
{
6-
// TODO(fpion): implement
9+
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
10+
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
11+
{
12+
string value = " Hello World! ";
13+
Description description = new(value);
14+
Assert.Equal(value.Trim(), description.Value);
15+
}
16+
17+
[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
18+
[InlineData("")]
19+
[InlineData(" ")]
20+
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
21+
{
22+
var exception = Assert.Throws<ValidationException>(() => new Description(value));
23+
24+
ValidationFailure failure = Assert.Single(exception.Errors);
25+
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
26+
Assert.Equal("Value", failure.PropertyName);
27+
}
28+
29+
[Fact(DisplayName = "ToString: it should return the correct string representation.")]
30+
public void Given_Description_When_ToString_Then_CorrectString()
31+
{
32+
Description description = new("Hello World!");
33+
Assert.Equal(description.Value, description.ToString());
34+
}
35+
36+
[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
37+
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
38+
{
39+
string value = " Hello World! ";
40+
Description? description = Description.TryCreate(value);
41+
Assert.NotNull(description);
42+
Assert.Equal(value.Trim(), description.Value);
43+
}
44+
45+
[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
46+
[InlineData(null)]
47+
[InlineData("")]
48+
[InlineData(" ")]
49+
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
50+
{
51+
Assert.Null(Description.TryCreate(value));
52+
}
753
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
namespace Logitar.Identity.Core;
1+
using FluentValidation;
2+
using FluentValidation.Results;
3+
using Logitar.Security.Cryptography;
4+
5+
namespace Logitar.Identity.Core;
26

37
[Trait(Traits.Category, Categories.Unit)]
48
public class DisplayNameTests
59
{
6-
// TODO(fpion): implement
10+
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
11+
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
12+
{
13+
string value = " Administrator ";
14+
DisplayName displayName = new(value);
15+
Assert.Equal(value.Trim(), displayName.Value);
16+
}
17+
18+
[Theory(DisplayName = "ctor: it should throw ValidationException given an empty, or white-space value.")]
19+
[InlineData("")]
20+
[InlineData(" ")]
21+
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
22+
{
23+
var exception = Assert.Throws<ValidationException>(() => new DisplayName(value));
24+
25+
ValidationFailure failure = Assert.Single(exception.Errors);
26+
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
27+
Assert.Equal("Value", failure.PropertyName);
28+
}
29+
30+
[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
31+
public void Given_InvalidValue_When_ctor_Then_ValidationException()
32+
{
33+
string value = RandomStringGenerator.GetString(999);
34+
var exception = Assert.Throws<ValidationException>(() => new DisplayName(value));
35+
36+
ValidationFailure failure = Assert.Single(exception.Errors);
37+
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
38+
Assert.Equal("Value", failure.PropertyName);
39+
}
40+
41+
[Fact(DisplayName = "ToString: it should return the correct string representation.")]
42+
public void Given_DisplayName_When_ToString_Then_CorrectString()
43+
{
44+
DisplayName displayName = new("Administrator");
45+
Assert.Equal(displayName.Value, displayName.ToString());
46+
}
47+
48+
[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
49+
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
50+
{
51+
string value = " Administrator ";
52+
DisplayName? displayName = DisplayName.TryCreate(value);
53+
Assert.NotNull(displayName);
54+
Assert.Equal(value.Trim(), displayName.Value);
55+
}
56+
57+
[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
58+
[InlineData(null)]
59+
[InlineData("")]
60+
[InlineData(" ")]
61+
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
62+
{
63+
Assert.Null(DisplayName.TryCreate(value));
64+
}
65+
66+
[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
67+
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
68+
{
69+
string value = RandomStringGenerator.GetString(999);
70+
var exception = Assert.Throws<ValidationException>(() => DisplayName.TryCreate(value));
71+
72+
ValidationFailure failure = Assert.Single(exception.Errors);
73+
Assert.Equal("MaximumLengthValidator", failure.ErrorCode);
74+
Assert.Equal("Value", failure.PropertyName);
75+
}
776
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
namespace Logitar.Identity.Core;
1+
using FluentValidation;
2+
using FluentValidation.Results;
3+
using Logitar.Security.Cryptography;
4+
5+
namespace Logitar.Identity.Core;
26

37
[Trait(Traits.Category, Categories.Unit)]
48
public class IdentifierTests
59
{
6-
// TODO(fpion): implement
10+
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
11+
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
12+
{
13+
string value = " HealthInsuranceNumber ";
14+
Identifier identifier = new(value);
15+
Assert.Equal(value.Trim(), identifier.Value);
16+
}
17+
18+
[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
19+
[InlineData("")]
20+
[InlineData(" ")]
21+
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
22+
{
23+
var exception = Assert.Throws<ValidationException>(() => new Identifier(value));
24+
25+
ValidationFailure failure = Assert.Single(exception.Errors);
26+
Assert.Equal("NotEmptyValidator", failure.ErrorCode);
27+
Assert.Equal("Value", failure.PropertyName);
28+
}
29+
30+
[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
31+
public void Given_InvalidValue_When_ctor_Then_ValidationException()
32+
{
33+
string value = RandomStringGenerator.GetString(999);
34+
var exception = Assert.Throws<ValidationException>(() => new Identifier(value));
35+
36+
Assert.Equal(2, exception.Errors.Count());
37+
Assert.Contains(exception.Errors, e => e.ErrorCode == "IdentifierValidator" && e.PropertyName == "Value");
38+
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Value");
39+
}
40+
41+
[Fact(DisplayName = "ToString: it should return the correct string representation.")]
42+
public void Given_Identifier_When_ToString_Then_CorrectString()
43+
{
44+
Identifier identifier = new("HealthInsuranceNumber");
45+
Assert.Equal(identifier.Value, identifier.ToString());
46+
}
47+
48+
[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
49+
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
50+
{
51+
string value = " HealthInsuranceNumber ";
52+
Identifier? identifier = Identifier.TryCreate(value);
53+
Assert.NotNull(identifier);
54+
Assert.Equal(value.Trim(), identifier.Value);
55+
}
56+
57+
[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
58+
[InlineData(null)]
59+
[InlineData("")]
60+
[InlineData(" ")]
61+
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
62+
{
63+
Assert.Null(Identifier.TryCreate(value));
64+
}
65+
66+
[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
67+
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
68+
{
69+
string value = RandomStringGenerator.GetString(999);
70+
var exception = Assert.Throws<ValidationException>(() => Identifier.TryCreate(value));
71+
72+
Assert.Equal(2, exception.Errors.Count());
73+
Assert.Contains(exception.Errors, e => e.ErrorCode == "IdentifierValidator" && e.PropertyName == "Value");
74+
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Value");
75+
}
776
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,78 @@
1-
namespace Logitar.Identity.Core;
1+
using FluentValidation;
2+
using Logitar.Security.Cryptography;
3+
using System.Globalization;
4+
5+
namespace Logitar.Identity.Core;
26

37
[Trait(Traits.Category, Categories.Unit)]
48
public class LocaleTests
59
{
6-
// TODO(fpion): implement
10+
[Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")]
11+
public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly()
12+
{
13+
CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA");
14+
Locale locale = new($" {culture} ");
15+
Assert.Equal(culture.Name, locale.Code);
16+
Assert.Equal(culture, locale.Culture);
17+
}
18+
19+
[Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")]
20+
[InlineData("")]
21+
[InlineData(" ")]
22+
public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value)
23+
{
24+
var exception = Assert.Throws<ValidationException>(() => new Locale(value));
25+
26+
Assert.Equal(2, exception.Errors.Count());
27+
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
28+
Assert.Contains(exception.Errors, e => e.ErrorCode == "NotEmptyValidator" && e.PropertyName == "Code");
29+
}
30+
31+
[Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")]
32+
public void Given_InvalidValue_When_ctor_Then_ValidationException()
33+
{
34+
string value = RandomStringGenerator.GetString(999);
35+
var exception = Assert.Throws<ValidationException>(() => new Locale(value));
36+
37+
Assert.Equal(2, exception.Errors.Count());
38+
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
39+
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code");
40+
}
41+
42+
[Fact(DisplayName = "ToString: it should return the correct string representation.")]
43+
public void Given_Locale_When_ToString_Then_CorrectString()
44+
{
45+
CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA");
46+
Locale locale = new(culture);
47+
Assert.Equal(string.Format("{0} ({1})", culture.DisplayName, culture.Name), locale.ToString());
48+
}
49+
50+
[Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")]
51+
public void Given_ValidValue_When_TryCreate_Then_InstanceReturned()
52+
{
53+
string value = " fr-CA ";
54+
Locale? locale = Locale.TryCreate(value);
55+
Assert.NotNull(locale);
56+
Assert.Equal(value.Trim(), locale.Culture.Name);
57+
}
58+
59+
[Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")]
60+
[InlineData(null)]
61+
[InlineData("")]
62+
[InlineData(" ")]
63+
public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value)
64+
{
65+
Assert.Null(Locale.TryCreate(value));
66+
}
67+
68+
[Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")]
69+
public void Given_InvalidValue_When_TryCreate_Then_ValidationException()
70+
{
71+
string value = RandomStringGenerator.GetString(999);
72+
var exception = Assert.Throws<ValidationException>(() => Locale.TryCreate(value));
73+
74+
Assert.Equal(2, exception.Errors.Count());
75+
Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code");
76+
Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code");
77+
}
778
}

0 commit comments

Comments
 (0)