-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Having Entity implement IComparable allows entities to be implicitly used as equality components in ValueObjects.
- Loading branch information
1 parent
d2e833c
commit a6bdde5
Showing
3 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
CSharpFunctionalExtensions.Tests/EntityTests/IComparableTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.EntityTests | ||
{ | ||
public class IComparableTests | ||
{ | ||
[Fact] | ||
public void Can_sort_entities() | ||
{ | ||
var entity1 = new MyEntity(1); | ||
var entity2 = new MyEntity(2); | ||
var entity3 = new MyEntity(3); | ||
var entity4 = new MyEntity(4); | ||
|
||
MyEntity[] myEntities = new[] { entity3, entity1, entity4, entity2 } | ||
.OrderBy(x => x) | ||
.ToArray(); | ||
|
||
myEntities[0].Should().BeSameAs(entity1); | ||
myEntities[1].Should().BeSameAs(entity2); | ||
myEntities[2].Should().BeSameAs(entity3); | ||
myEntities[3].Should().BeSameAs(entity4); | ||
} | ||
|
||
[Fact] | ||
public void Entities_with_different_id_types_are_comparatively_null() | ||
{ | ||
var longEntity = new MyEntity(1); | ||
var guidEntity = new MyOtherEntity(new Guid("282e6fe9-a272-45da-aa16-757449ab2818")); | ||
|
||
int result1 = longEntity.CompareTo(guidEntity); | ||
int result2 = guidEntity.CompareTo(longEntity); | ||
|
||
result1.Should().Be(1); | ||
result2.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void Can_sort_entity_that_is_null() | ||
{ | ||
var entity1 = new MyEntity(1); | ||
var entity2 = new MyEntity(2); | ||
|
||
MyEntity[] entities = new[] { entity1, entity2, null } | ||
.OrderBy(x => x) | ||
.ToArray(); | ||
|
||
entities[0].Should().BeNull(); | ||
entities[1].Should().BeSameAs(entity1); | ||
entities[2].Should().BeSameAs(entity2); | ||
} | ||
|
||
private class MyEntity : Entity | ||
{ | ||
public MyEntity(long id) | ||
: base(id) | ||
{ | ||
} | ||
} | ||
|
||
private class MyOtherEntity : Entity<Guid> | ||
{ | ||
public MyOtherEntity(Guid id) | ||
: base(id) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters