Skip to content

Commit

Permalink
Fix wrong implementation of ISTransient for value type IDs. (#9)
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
mgernand authored Dec 17, 2021
1 parent 33cce8e commit 99f518b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Fluxera.Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public virtual bool IsTransient
{
get
{
bool isTransient = Equals(this.ID, default);
bool isTransient = Equals(this.ID, default(TKey));

if(typeof(TKey) == typeof(string))
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Fluxera.Entity.UnitTests/EmployeeAggregate/EmployeeGuid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Fluxera.Entity.UnitTests.EmployeeAggregate
{
using System;
using JetBrains.Annotations;

[PublicAPI]
public class EmployeeGuid : AggregateRoot<EmployeeGuid, Guid>
{
[DomainSignature]
public string Name { get; set; }

[DomainSignature]
public int EmployeeNumber { get; set; }

public decimal Salary { get; set; }
}
}
67 changes: 6 additions & 61 deletions tests/Fluxera.Entity.UnitTests/EntityExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,20 @@
namespace Fluxera.Entity.UnitTests
{
using System;
using EmployeeAggregate;
using FluentAssertions;
using Fluxera.Entity.UnitTests.EmployeeAggregate;
using NUnit.Framework;

[TestFixture]
public class EntityExtensionsTests
{
[Test]
public void ShouldReturnTrueForTransientEntityForDefaultValue()
{
Employee employee = new Employee
{
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForNull()
{
Employee employee = new Employee
{
ID = null,
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForEmpty()
{
Employee employee = new Employee
{
ID = string.Empty,
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForOnlyWhitespace()
[TestCase(typeof(Employee), true)]
[TestCase(typeof(PerformanceReview), false)]
[TestCase(typeof(object), false)]
public void IsAggregateRootShouldReturnExpectedValue(Type type, bool expected)
{
Employee employee = new Employee
{
ID = " ",
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
type.IsAggregateRoot().Should().Be(expected);
}

[Test]
Expand All @@ -71,14 +25,5 @@ public void IsEntityShouldReturnExpectedValue(Type type, bool expected)
{
type.IsEntity().Should().Be(expected);
}

[Test]
[TestCase(typeof(Employee), true)]
[TestCase(typeof(PerformanceReview), false)]
[TestCase(typeof(object), false)]
public void IsAggregateRootShouldReturnExpectedValue(Type type, bool expected)
{
type.IsAggregateRoot().Should().Be(expected);
}
}
}
38 changes: 38 additions & 0 deletions tests/Fluxera.Entity.UnitTests/EntityWithGuidKeyTransientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Fluxera.Entity.UnitTests
{
using System;
using FluentAssertions;
using Fluxera.Entity.UnitTests.EmployeeAggregate;
using NUnit.Framework;

[TestFixture]
public class EntityWithGuidKeyTransientTests
{
[Test]
public void ShouldReturnTrueForTransientEntityForDefaultValue()
{
EmployeeGuid employee = new EmployeeGuid
{
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForEmpty()
{
EmployeeGuid employee = new EmployeeGuid
{
ID = Guid.Empty,
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Fluxera.Entity.UnitTests
{
using FluentAssertions;
using Fluxera.Entity.UnitTests.EmployeeAggregate;
using NUnit.Framework;

[TestFixture]
public class EntityWithStringKeyTransientTests
{
[Test]
public void ShouldReturnTrueForTransientEntityForDefaultValue()
{
Employee employee = new Employee
{
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForEmpty()
{
Employee employee = new Employee
{
ID = string.Empty,
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForNull()
{
Employee employee = new Employee
{
ID = null,
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}

[Test]
public void ShouldReturnTrueForTransientEntityForOnlyWhitespace()
{
Employee employee = new Employee
{
ID = " ",
Name = "James Bond",
EmployeeNumber = 8051007,
Salary = 1_000_000,
};

employee.IsTransient.Should().BeTrue();
}
}
}

0 comments on commit 99f518b

Please sign in to comment.