Skip to content

Commit

Permalink
Merge pull request #251 from NoxOrg/types-url
Browse files Browse the repository at this point in the history
Types url
  • Loading branch information
fkucuk authored Jul 17, 2023
2 parents a32fdbe + ba39e7e commit 63ad29a
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 3 deletions.
228 changes: 228 additions & 0 deletions Nox.Generator.sln

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/Nox.Types.EntityFramework/Types/Url/UrlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Nox.Types.EntityFramework.Types;

public class UrlConverter : ValueConverter<Url, string>
{
public UrlConverter() : base(url => url.Value.AbsoluteUri,
url => Url.From(url)) { }
}
45 changes: 45 additions & 0 deletions src/Nox.Types/Types/Url/Url.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;

namespace Nox.Types;

public sealed class Url : ValueObject<System.Uri, Url>
{

public static Url From(string value)
{
if (!System.Uri.TryCreate(value, System.UriKind.Absolute, out var uriValue))
{
throw new TypeValidationException(
new List<ValidationFailure> {
new ValidationFailure("Uri", $"The string '{value}' you provided, is not a valid Uri.") });
}

var newObject = new Url
{
Value = uriValue
};

return newObject;
}

internal override ValidationResult Validate()
{
var result = base.Validate();

bool isValid = System.Uri.TryCreate(base.Value.ToString(), UriKind.Absolute, out var uriResult) &&
(uriResult.Scheme == System.Uri.UriSchemeHttps
|| uriResult.Scheme == System.Uri.UriSchemeHttp
|| uriResult.Scheme == System.Uri.UriSchemeMailto
|| uriResult.Scheme == System.Uri.UriSchemeFtp
|| uriResult.Scheme == System.Uri.UriSchemeFile);

if (!isValid)
{
result.Errors.Add(new ValidationFailure(nameof(Value),
$"Could not create a Nox Url type; as value {Value} is not a valid Url."));
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void Configure(EntityTypeBuilder<Country> builder)
builder.Property(e => e.Date).HasConversion<DateConverter>();
builder.Property(e => e.LocalTimeZone).HasConversion<TimeZoneCodeConverter>();
builder.Property(e => e.Uri).HasConversion<UriConverter>();
builder.Property(e => e.Url).HasConversion<UrlConverter>();
builder.Property(e => e.IsLandLocked).HasConversion<BooleanConverter>();
builder.Property(e => e.DateTimeDuration).HasConversion<DateTimeDurationConverter>();

Expand Down
2 changes: 2 additions & 0 deletions tests/Nox.Types.Tests/EntityFrameworkTests/Models/Country.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public sealed class Country

public Uri Uri { get; set; } = null!;

public Url Url { get; set; } = null!;

/// <summary>
/// Gets or sets the date.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Nox.Types.Tests.EntityFrameworkTests;
public class NoxTypesEntityFrameworkTests : TestWithSqlite
{
private const string Sample_Uri = "https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName";
private const string Sample_Url = "https://www.myregus.com/";

[Fact]
public async Task DatabaseIsAvailableAndCanBeConnectedTo()
{
Expand Down Expand Up @@ -50,6 +52,7 @@ public void Countries_CanRead_LatLong()
StreetAddressJson = Json.From(JsonSerializer.Serialize(streetAddress)),
LocalTimeZone = TimeZoneCode.From("CET"),
Uri = Uri.From(Sample_Uri),
Url = Url.From(Sample_Url),
IsLandLocked = Boolean.From(false),
DateTimeDuration = DateTimeDuration.From(days: 10, 5, 2, 1),
};
Expand Down Expand Up @@ -90,8 +93,9 @@ public void AddedItemShouldGetGeneratedId()
MACAddress = MacAddress.From("AE-D4-32-2C-CF-EF"),
Uri = Uri.From(Sample_Uri),
Date = Date.From(new DateTime(2023, 11, 25), new()),
StreetAddressJson = Json.From(JsonSerializer.Serialize(streetAddress, new JsonSerializerOptions { WriteIndented = true })),
LocalTimeZone = TimeZoneCode.From("CET"),
Url = Url.From(Sample_Url),
StreetAddressJson = Json.From(JsonSerializer.Serialize(streetAddress, new JsonSerializerOptions { WriteIndented = true })),
IsLandLocked = Boolean.From(true),
DateTimeDuration = DateTimeDuration.From(days: 10, 5, 2, 1),
};
Expand Down Expand Up @@ -131,6 +135,8 @@ public void AddedItemShouldGetGeneratedId()
Assert.Equal("AED4322CCFEF", item.MACAddress.Value);
Assert.Equal(new DateTime(2023, 11, 25).Date, item.Date.Value);
Assert.Equal("CET", item.LocalTimeZone.Value);
Assert.Equal(Sample_Url, item.Url.Value.AbsoluteUri);
Assert.Equal(Sample_Uri, item.Uri.Value.AbsoluteUri);
Assert.True(item.IsLandLocked.Value);
Assert.Equal(Sample_Uri, item.Uri.Value.AbsoluteUri);
Assert.Equal(Sample_Uri, item.Uri.Value.AbsoluteUri);
Expand Down
4 changes: 2 additions & 2 deletions tests/Nox.Types.Tests/Types/Percentage/PercentageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Percentage_Constructor_ThrowsException_WhenValueExceedsMaxAllowed()
var action = () => Percentage.From(testPercentage);

action.Should().Throw<TypeValidationException>()
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", "Could not create a Nox Percentage type a value 3.2 is greater than than the maximum specified value of 1") });
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", $"Could not create a Nox Percentage type a value {testPercentage} is greater than than the maximum specified value of 1") });

}

Expand All @@ -35,7 +35,7 @@ public void Percentage_Constructor_ThrowsException_WhenValueIsLessThanMinAllowed
var action = () => Percentage.From(testPercentage);

action.Should().Throw<TypeValidationException>()
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", "Could not create a Nox Percentage type as value -0.3 is less than than the minimum specified value of 0") });
.And.Errors.Should().BeEquivalentTo(new[] { new ValidationFailure("Value", $"Could not create a Nox Percentage type as value {testPercentage} is less than than the minimum specified value of 0") });
}

[Fact]
Expand Down
46 changes: 46 additions & 0 deletions tests/Nox.Types.Tests/Types/Url/UrlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentAssertions;

namespace Nox.Types.Tests.Types;

public class UrlTests
{
[Theory]
[InlineData("https://www.google.com")]
[InlineData("https://www.google.com/")]
[InlineData("https://www.google.com/test")]
[InlineData("https://www.google.com/test/test1?q=123")]
[InlineData("https://example.org/test/test1?search=test-question#part2")]
[InlineData("file://website.com/pathtofile/intro.pdf")]
[InlineData("mailto:abc@iwgplc.com")]
public void UrlFromUri_ShouldBe_Validated(string url)
{
var expected = new System.Uri(url);
var actual = Url.From(expected);
actual.Value.AbsoluteUri.Should().Be(expected.AbsoluteUri);
}

[Theory]
[InlineData("https://www.google.com/")]
[InlineData("https://www.google.com/test")]
[InlineData("https://www.google.com/test/test1?q=123")]
[InlineData("https://example.org/test/test1?search=test-question#part2")]
[InlineData("file://website.com/pathtofile/intro.pdf")]
[InlineData("mailto:abc@iwgplc.com")]
public void UrlFromString_ShouldBe_Validated(string expected)
{
var actual = Url.From(expected);
actual.Value.AbsoluteUri.Should().Be(expected);
}


[Theory]
[InlineData("abc://www.google.com/")]
public void BadUrl_Should_ThrowException(string input)
{
var uri = new System.Uri(input);
Action init = () => { var url = Url.From(uri); };

init.Should().Throw<TypeValidationException>();
}

}

0 comments on commit 63ad29a

Please sign in to comment.