Skip to content

Commit

Permalink
Refactorings. Adding permissions, supporting optional serialization, …
Browse files Browse the repository at this point in the history
…currencycodes, changes roles and features to be unique, fixed namespaces, and fixed templates
  • Loading branch information
jezzsantos committed Jun 23, 2024
1 parent e990cc6 commit 47ed531
Show file tree
Hide file tree
Showing 64 changed files with 873 additions and 119 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions src/Common.UnitTests/CurrencyCodesSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using Common.Extensions;
using FluentAssertions;
using ISO._4217;
using Xunit;

namespace Common.UnitTests;

[Trait("Category", "Unit")]
public class CurrencyCodesSpec
{
[Fact]
public void WhenExistsAndUnknown_ThenReturnsFalse()
{
var result = CurrencyCodes.Exists("notacurrencycode");

result.Should().BeFalse();
}

[Fact]
public void WhenExistsByCode_ThenReturnsTrue()
{
var result = CurrencyCodes.Exists(CurrencyCodes.Default.Code);

result.Should().BeTrue();
}

[Fact]
public void WhenExistsByNumeric_ThenReturnsTrue()
{
var result = CurrencyCodes.Exists(CurrencyCodes.Default.Numeric);

result.Should().BeTrue();
}

[Fact]
public void WhenFindAndUnknown_ThenReturnsNull()
{
var result = CurrencyCodes.Find("notacurrencycode");

result.Should().BeNull();
}

[Fact]
public void WhenFindByCode_ThenReturnsTrue()
{
var result = CurrencyCodes.Find(CurrencyCodes.Default.Code);

result.Should().Be(CurrencyCodes.Default);
}

[Fact]
public void WhenFindByNumeric_ThenReturnsTrue()
{
var result = CurrencyCodes.Find(CurrencyCodes.Default.Numeric);

result.Should().Be(CurrencyCodes.Default);
}

[Fact]
public void WhenFindForEveryCurrency_ThenReturnsCode()
{
var currencies = CurrencyCodesResolver.Codes
.Where(cur => cur.Code.HasValue())
.ToList();
foreach (var currency in currencies)
{
var result = CurrencyCodes.Find(currency.Code);

result.Should().NotBeNull($"{currency.Name} should have been found by Code");
}

foreach (var currency in currencies)
{
var result = CurrencyCodes.Find(currency.Num);

result.Should().NotBeNull($"{currency.Name} should have been found by NumericCode");
}
}

[Fact]
public void WhenCreateIso4217_ThenReturnsInstance()
{
var result = CurrencyCodeIso4217.Create("ashortname", "analpha2", "100", CurrencyDecimalKind.TwoDecimal);

result.ShortName.Should().Be("ashortname");
result.Code.Should().Be("analpha2");
result.Kind.Should().Be(CurrencyDecimalKind.TwoDecimal);
result.Numeric.Should().Be("100");
}

[Fact]
public void WhenEqualsAndNotTheSameNumeric_ThenReturnsFalse()
{
var currency1 = CurrencyCodeIso4217.Create("ashortname", "analpha2", "100", CurrencyDecimalKind.Unknown);
var currency2 = CurrencyCodeIso4217.Create("ashortname", "analpha2", "101", CurrencyDecimalKind.Unknown);

var result = currency1 == currency2;

result.Should().BeFalse();
}

[Fact]
public void WhenEqualsAndSameNumeric_ThenReturnsTrue()
{
var currency1 = CurrencyCodeIso4217.Create("ashortname1", "analpha21", "100", CurrencyDecimalKind.Unknown);
var currency2 = CurrencyCodeIso4217.Create("ashortname2", "analpha22", "100", CurrencyDecimalKind.Unknown);

var result = currency1 == currency2;

result.Should().BeTrue();
}

[Fact]
public void WhenToCurrencyWithAThousandAndOne_ThenReturnsUnitedStatesDollars()
{
var code = CurrencyCodes.UnitedStatesDollar.Code;
var result = CurrencyCodes.ToCurrency(code, 1001);

result.Should().Be(10.01M);
}

[Fact]
public void WhenToCurrencyWithAThousandAndOne_ThenReturnsKuwaitiDinars()
{
var code = CurrencyCodes.KuwaitiDinar.Code;
var result = CurrencyCodes.ToCurrency(code, 1001);

result.Should().Be(1.001M);
}

[Fact]
public void WhenToCurrencyWithAThousandAndOne_ThenReturnsChileanFomentos()
{
var code = CurrencyCodes.ChileanFomento.Code;
var result = CurrencyCodes.ToCurrency(code, 1001);

result.Should().Be(0.1001M);
}
}
55 changes: 54 additions & 1 deletion src/Common.UnitTests/Extensions/DictionaryExtensionsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void WhenFromObjectDictionaryWithMatchingNullProperties_ThenReturnsUpdate
[Fact]
public void WhenToObjectDictionaryWithNullInstance_ThenReturnsEmpty()
{
var result = ((TestMappingClass)null!).ToObjectDictionary();
var result = ((TestMappingClass?)null).ToObjectDictionary();

result.Should().BeEmpty();
}
Expand Down Expand Up @@ -223,6 +223,59 @@ public void WhenToObjectDictionaryWithInstanceWithDefaultValues_ThenReturnsPrope
result[nameof(TestMappingClass.AnOptionalDateTimeProperty)].Should().Be(Optional<DateTime>.None);
result[nameof(TestMappingClass.AnOptionalNullableDateTimeProperty)].Should().Be(Optional<DateTime?>.None);
}

[Fact]
public void WhenToStringDictionaryWithNullInstance_ThenreturnsEmpty()
{
var result = ((TestMappingClass?)null).ToStringDictionary();

result.Should().BeEmpty();
}

[Fact]
public void WhenToStringDictionaryWithInstanceWithValues_ThenReturnsProperties()
{
var datum = DateTime.UtcNow;
var result = new TestMappingClass
{
ADateTimeProperty = datum,
AnOptionalStringProperty = "avalue",
AnOptionalNullableStringProperty = "avalue",
AnOptionalDateTimeProperty = datum,
AnOptionalNullableDateTimeProperty = datum
}.ToStringDictionary();

result.Count.Should().Be(8);
result[nameof(TestMappingClass.AStringProperty)].Should().Be("adefaultvalue");
result[nameof(TestMappingClass.AnIntProperty)].Should().Be("1");
result[nameof(TestMappingClass.AnBoolProperty)].Should().Be("True");
result[nameof(TestMappingClass.ADateTimeProperty)].Should().Be(datum.ToIso8601());
result[nameof(TestMappingClass.AnOptionalStringProperty)].Should().Be("avalue");
result[nameof(TestMappingClass.AnOptionalNullableStringProperty)].Should().Be("avalue");
result[nameof(TestMappingClass.AnOptionalDateTimeProperty)].Should().Be(datum.ToIso8601());
result[nameof(TestMappingClass.AnOptionalNullableDateTimeProperty)].Should().Be(datum.ToIso8601());
}

[Fact]
public void WhenToStringDictionaryWithInstanceWithDefaultValues_ThenReturnsProperties()
{
var result = new TestMappingClass
{
AStringProperty = default!,
AnIntProperty = default,
AnBoolProperty = default,
ADateTimeProperty = default,
AnOptionalStringProperty = default,
AnOptionalNullableStringProperty = default,
AnOptionalDateTimeProperty = default,
AnOptionalNullableDateTimeProperty = default
}.ToStringDictionary();

result.Count.Should().Be(3);
result[nameof(TestMappingClass.AnIntProperty)].Should().Be("0");
result[nameof(TestMappingClass.AnBoolProperty)].Should().Be("False");
result[nameof(TestMappingClass.ADateTimeProperty)].Should().Be("0001-01-01T00:00:00");
}
}

[UsedImplicitly]
Expand Down
1 change: 1 addition & 0 deletions src/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="ISO.3166.CountryCodes" Version="1.0.3" />
<PackageReference Include="ISO.4217.CurrencyCodes" Version="1.0.10" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="NodaTime" Version="3.1.6" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 47ed531

Please sign in to comment.