Skip to content

Commit

Permalink
chore: migrate to netstandard, add many to any unittests (#16)
Browse files Browse the repository at this point in the history
Partial coverage of many to any relation
Migrate library to netstandard
Added net8.0 to build matrix
Partial coverage of has one relation

---------

Co-authored-by: Bruno DUVAL <bruno.duval@datatunning.com>
Co-authored-by: Bruno DUVAL <bruno.duval@nineteensevenfour.com>
  • Loading branch information
3 people authored Apr 21, 2024
1 parent 24bd68e commit 3e7e33b
Show file tree
Hide file tree
Showing 64 changed files with 2,491 additions and 1,795 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
dotnet: [6.0.x, 7.0.x]
dotnet: [6.0.x, 7.0.x, 8.0.x]

steps:
- name: Checkout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,95 @@

using Xunit;

namespace NineteenSevenFour.Testing.Core.UnitTest.Extension;

public class FluentExpressionTest
namespace NineteenSevenFour.Testing.Core.UnitTest.Extension
{
[Fact]
public void MemberNameFor_Should_Return_A_String_When_Expression_IsValid()
{

// Arrange
Expression<Func<PersonModel, object>> expression = (p) => p.Surname;

// Act
var result = FluentExpression.MemberNameFor(expression);

// Assert
result.Should()
.NotBeNull()
.And
.Be(nameof(PersonModel.Surname));
}

[Fact]
public void MemberNameFor_Should_Throw_ArgumentException_When_Expression_IsNested()
public class FluentExpressionTest
{
// Arrange
#pragma warning disable CS8602 // Dereference of a possibly null reference.
Expression<Func<PersonModel, object>> expression = (p) => p.Addresses.Count;
#pragma warning restore CS8602 // Dereference of a possibly null reference.

// Act
[Fact]
public void MemberNameFor_Should_Return_A_String_When_Expression_IsValid()
{

// Arrange
Expression<Func<PersonModel, object>> expression = (p) => p.Surname;

// Act
var result = FluentExpression.MemberNameFor(expression);

// Assert
result.Should()
.NotBeNull()
.And
.Be(nameof(PersonModel.Surname));
}

[Fact]
public void MemberNameFor_Should_Throw_ArgumentException_When_Expression_IsNested()
{
// Arrange
#pragma warning disable CS8602
Expression<Func<PersonModel, object>> expression = (p) => p.Addresses.Count;
#pragma warning restore CS8602

// Act
#pragma warning disable IDE0039 // Use local function
var result = () => FluentExpression.MemberNameFor(expression);
var result = () => FluentExpression.MemberNameFor(expression);
#pragma warning restore IDE0039 // Use local function

// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message
.Should()
.Be($"Your expression 'Convert(p.Addresses.Count, Object)' cant be used. Nested accessors like 'o => o.NestedObject.Foo' at a parent level are not allowed. You should create a dedicated faker for NestedObject like new Faker<NestedObject>().RuleFor(o => o.Foo, ...) with its own rules that define how 'Foo' is generated."
);
}

[Fact]
public void MemberNameFor_Should_Throw_ArgumentException_When_Expression_HasInvalidForm()
{
// Arrange
// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message
.Should()
.Be($"Your expression 'Convert(p.Addresses.Count, Object)' cant be used. Nested accessors like 'o => o.NestedObject.Foo' at a parent level are not allowed. You should create a dedicated faker for NestedObject like new Faker<NestedObject>().RuleFor(o => o.Foo, ...) with its own rules that define how 'Foo' is generated."
);
}

[Fact]
public void MemberNameFor_Should_Throw_ArgumentException_When_Expression_HasInvalidForm()
{
// Arrange
#pragma warning disable CS8603 // Possible null reference return.
Expression<Func<object>> expression = () => null;
Expression<Func<object>> expression = () => null;
#pragma warning restore CS8603 // Possible null reference return.

// Act
// Act
#pragma warning disable IDE0039 // Use local function
var result = () => FluentExpression.MemberNameFor(expression);
var result = () => FluentExpression.MemberNameFor(expression);
#pragma warning restore IDE0039 // Use local function

// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message.Should()
.Be($"Expression was not of the form 'x => x.Property or x => x.Field'.");
}

[Fact]
public void EnsureMemberExists_Should_Throw_ArgumentException_When_PropertyNotExists()
{
// Arrange
var fqdn = "NineteenSevenFour.Testing.Example.Domain.Model.PersonModel";
var wrongPropName = "SomeProp";
#pragma warning disable CS8602 // Dereference of a possibly null reference.
Expression<Func<PersonModel, object>> expression = (p) => p.Addresses.Count;
#pragma warning restore CS8602 // Dereference of a possibly null reference.

// Act
// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message.Should()
.Be($"Expression was not of the form 'x => x.Property or x => x.Field'.");
}

[Fact]
public void EnsureMemberExists_Should_Throw_ArgumentException_When_PropertyNotExists()
{
// Arrange
var fqdn = "NineteenSevenFour.Testing.Example.Domain.Model.PersonModel";
var wrongPropName = "SomeProp";
#pragma warning disable CS8602
Expression<Func<PersonModel, object>> expression = (p) => p.Addresses.Count;
#pragma warning restore CS8602

// Act
#pragma warning disable IDE0039 // Use local function
var result = () => FluentExpression.EnsureMemberExists<PersonModel>(wrongPropName);
var result = () => FluentExpression.EnsureMemberExists<PersonModel>(wrongPropName);
#pragma warning restore IDE0039 // Use local function

// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message.Should()
.Be($"The property or field {wrongPropName} was not found on {fqdn}. " +
$"Can't create a rule for {fqdn}.SomeProp when " +
$"SomeProp cannot be found. Try creating a custom IBinder for Faker<T> with the appropriate " +
$"System.Reflection.BindingFlags that allows deeper reflection into {fqdn}.");
// Assert
var exception = Assert.Throws<ArgumentException>(result);
exception.Should()
.NotBeNull();
exception.Message.Should()
.Be($"The property or field {wrongPropName} was not found on {fqdn}. " +
$"Can't create a rule for {fqdn}.SomeProp when " +
$"SomeProp cannot be found. Try creating a custom IBinder for Faker<T> with the appropriate " +
$"System.Reflection.BindingFlags that allows deeper reflection into {fqdn}.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Loading

0 comments on commit 3e7e33b

Please sign in to comment.