-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4df5f4
commit 43a11f7
Showing
14 changed files
with
398 additions
and
68 deletions.
There are no files selected for viewing
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,41 @@ | ||
using CoreDomain.Tests.Common; | ||
using CoreDomain.Tests.Models; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace CoreDomain.Tests | ||
{ | ||
public class AggregateBaseTest | ||
{ | ||
[Fact] | ||
public void InstanceTest() | ||
{ | ||
MyCustomAggregate<Guid> customAggregate = Generator.GetAggregateInstace<Guid>(); | ||
|
||
Assert.NotNull(customAggregate.ValueObject); | ||
Assert.NotNull(customAggregate.Entity); | ||
Assert.NotNull(customAggregate); | ||
Assert.True(customAggregate.Id != Guid.Empty); | ||
Assert.True(customAggregate.Version > -1); | ||
Assert.NotNull(customAggregate.GetUncommittedEvents()); | ||
Assert.True(customAggregate.GetUncommittedEvents().Any()); | ||
} | ||
|
||
[Fact] | ||
public void AddEventTest() | ||
{ | ||
MyCustomAggregate<Guid> customAggregate = Generator.GetAggregateInstace<Guid>(); | ||
|
||
customAggregate.ApplyEvent(); | ||
|
||
Assert.NotNull(customAggregate.ValueObject); | ||
Assert.NotNull(customAggregate.Entity); | ||
Assert.NotNull(customAggregate); | ||
Assert.True(customAggregate.Id != Guid.Empty); | ||
Assert.True(customAggregate.Version > -1); | ||
Assert.NotNull(customAggregate.GetUncommittedEvents()); | ||
Assert.True(customAggregate.GetUncommittedEvents().Count() == 2); | ||
} | ||
} | ||
} |
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,58 @@ | ||
using CoreDomain.Tests.Models; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace CoreDomain.Tests.Common | ||
{ | ||
public static class Generator | ||
{ | ||
public static int GetRandomInt() | ||
{ | ||
return new Random().Next(); | ||
} | ||
|
||
public static long GetRandomLong() | ||
{ | ||
return new Random().Next() * new Random().Next(); | ||
} | ||
|
||
public static string GetRandomString(int length) | ||
{ | ||
Random random = new Random(); | ||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
|
||
return new string(Enumerable.Repeat(chars, length) | ||
.Select(s => s[random.Next(s.Length)]).ToArray()); | ||
} | ||
|
||
public static Guid GetRandomGuid() | ||
{ | ||
return Guid.NewGuid(); | ||
} | ||
|
||
public static MyCustomAggregate<T> GetAggregateInstace<T>() | ||
{ | ||
MyCustomValueObject customValueObject = new MyCustomValueObject(GetRandomInt(), GetRandomString(10)); | ||
MyCustomEntity<T> customEntity = new MyCustomEntity<T>(); | ||
MyCustomAggregate<T> customAggregate = new MyCustomAggregate<T>(customEntity, customValueObject); | ||
|
||
return customAggregate; | ||
} | ||
} | ||
|
||
public static class Generator<T> | ||
{ | ||
static readonly Type type = typeof(T); | ||
|
||
public static MyCustomEntity<T> GetCustomEntityInstance(params object[] args) | ||
{ | ||
if (type.Equals(typeof(int)) | ||
|| type.Equals(typeof(long)) | ||
|| type.Equals(typeof(string)) | ||
|| type.Equals(typeof(Guid))) | ||
return (MyCustomEntity<T>)Activator.CreateInstance(typeof(MyCustomEntity<T>), args); | ||
|
||
throw new InvalidCastException($"Type {typeof(T).Name} is not supported"); | ||
} | ||
} | ||
} |
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,32 @@ | ||
using CoreDomain.Tests.Common; | ||
using CoreDomain.Tests.Models; | ||
using System; | ||
using Xunit; | ||
|
||
namespace CoreDomain.Tests | ||
{ | ||
public class DomainEventBaseTest | ||
{ | ||
[Fact] | ||
public void InstanceTest() | ||
{ | ||
MyCustomEvent customEvent = new MyCustomEvent(Generator.GetRandomGuid(), 1); | ||
|
||
Assert.NotNull(customEvent); | ||
Assert.True(customEvent.AggregateId != Guid.Empty); | ||
Assert.True(customEvent.EventId != Guid.Empty); | ||
Assert.True(customEvent.AggregateVersion > -1); | ||
Assert.True(customEvent.CreatedAt != DateTime.MinValue); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest() | ||
{ | ||
MyCustomEvent customEvent1 = new MyCustomEvent(Generator.GetRandomGuid(), 1); | ||
MyCustomEvent customEvent2 = new MyCustomEvent(Generator.GetRandomGuid(), 1); | ||
|
||
Assert.False(customEvent1.Equals(customEvent2)); | ||
Assert.NotEqual(customEvent1, customEvent2); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,49 +1,107 @@ | ||
using CoreDomain.Tests.Common; | ||
using System; | ||
using Xunit; | ||
|
||
namespace CoreDomain.Tests | ||
namespace CoreDomain.Tests.Models | ||
{ | ||
public class EntityTest | ||
{ | ||
[Fact] | ||
public void EqualityTest() | ||
public void EqualityTest_Int() | ||
{ | ||
MyIntClass myClass1 = new MyIntClass(); | ||
MyIntClass myClass2 = new MyIntClass(); | ||
int id = Generator.GetRandomInt(); | ||
|
||
Assert.True(myClass1 == myClass2); | ||
Assert.False(myClass1 != myClass2); | ||
Assert.True(myClass1.Equals(myClass2)); | ||
Assert.Equal(myClass1, myClass2); | ||
MyCustomEntity<int> myClass1 = Generator<int>.GetCustomEntityInstance(id); | ||
MyCustomEntity<int> myClass2 = Generator<int>.GetCustomEntityInstance(id); | ||
|
||
ValidateEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest_Int() | ||
{ | ||
MyCustomEntity<int> myClass1 = Generator<int>.GetCustomEntityInstance(Generator.GetRandomInt()); | ||
MyCustomEntity<int> myClass2 = Generator<int>.GetCustomEntityInstance(Generator.GetRandomInt()); | ||
|
||
ValidateNonEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void EqualityTestWithId() | ||
public void EqualityTest_Long() | ||
{ | ||
MyIntClass myClass1 = new MyIntClass(1); | ||
MyIntClass myClass2 = new MyIntClass(1); | ||
long id = Generator.GetRandomLong(); | ||
|
||
MyCustomEntity<long> myClass1 = Generator<long>.GetCustomEntityInstance(id); | ||
MyCustomEntity<long> myClass2 = Generator<long>.GetCustomEntityInstance(id); | ||
|
||
ValidateEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest_Long() | ||
{ | ||
MyCustomEntity<long> myClass1 = Generator<long>.GetCustomEntityInstance(Generator.GetRandomLong()); | ||
MyCustomEntity<long> myClass2 = Generator<long>.GetCustomEntityInstance(Generator.GetRandomLong()); | ||
|
||
ValidateNonEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void EqualityTest_String() | ||
{ | ||
string id = Generator.GetRandomString(20); | ||
|
||
MyCustomEntity<string> myClass1 = Generator<string>.GetCustomEntityInstance(id); | ||
MyCustomEntity<string> myClass2 = Generator<string>.GetCustomEntityInstance(id); | ||
|
||
ValidateEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest_String() | ||
{ | ||
MyCustomEntity<string> myClass1 = Generator<string>.GetCustomEntityInstance(Generator.GetRandomString(20)); | ||
MyCustomEntity<string> myClass2 = Generator<string>.GetCustomEntityInstance(Generator.GetRandomString(20)); | ||
|
||
ValidateNonEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void EqualityTest_Guid() | ||
{ | ||
Guid id = Generator.GetRandomGuid(); | ||
|
||
MyCustomEntity<Guid> myClass1 = Generator<Guid>.GetCustomEntityInstance(id); | ||
MyCustomEntity<Guid> myClass2 = Generator<Guid>.GetCustomEntityInstance(id); | ||
|
||
ValidateEquality(myClass1, myClass2); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest_Guid() | ||
{ | ||
MyCustomEntity<Guid> myClass1 = Generator<Guid>.GetCustomEntityInstance(Generator.GetRandomGuid()); | ||
MyCustomEntity<Guid> myClass2 = Generator<Guid>.GetCustomEntityInstance(Generator.GetRandomGuid()); | ||
|
||
ValidateNonEquality(myClass1, myClass2); | ||
} | ||
|
||
private static void ValidateEquality<T>(MyCustomEntity<T> myClass1, MyCustomEntity<T> myClass2) | ||
{ | ||
Assert.True(myClass1 == myClass2); | ||
Assert.False(myClass1 != myClass2); | ||
Assert.True(myClass1.Equals(myClass2)); | ||
Assert.Equal(myClass1, myClass2); | ||
Assert.Equal(myClass1.Id, myClass2.Id); | ||
} | ||
|
||
[Fact] | ||
public void EqualityTestWithDifferentId() | ||
private static void ValidateNonEquality<T>(MyCustomEntity<T> myClass1, MyCustomEntity<T> myClass2) | ||
{ | ||
MyIntClass myClass1 = new MyIntClass(1); | ||
MyIntClass myClass2 = new MyIntClass(2); | ||
|
||
Assert.False(myClass1 == myClass2); | ||
Assert.True(myClass1 != myClass2); | ||
Assert.False(myClass1.Equals(myClass2)); | ||
Assert.NotEqual(myClass1, myClass2); | ||
Assert.NotEqual(myClass1.Id, myClass2.Id); | ||
} | ||
} | ||
public class MyIntClass : EntityBase<int> | ||
{ | ||
public MyIntClass() { } | ||
|
||
public MyIntClass(int id) => Id = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace CoreDomain.Tests.Models | ||
{ | ||
public class MyCustomAggregate<T> : AggregateBase<Guid> | ||
{ | ||
public MyCustomEntity<T> Entity { get; set; } | ||
public MyCustomValueObject ValueObject { get; set; } | ||
|
||
public MyCustomAggregate(MyCustomEntity<T> entity, MyCustomValueObject valueObject) | ||
{ | ||
Id = Guid.NewGuid(); | ||
Entity = entity ?? throw new ArgumentNullException(nameof(entity)); | ||
ValueObject = valueObject ?? throw new ArgumentNullException(nameof(valueObject)); | ||
|
||
RaiseEvent(new MyCustomEvent(Id, Version)); | ||
} | ||
|
||
public void ApplyEvent() | ||
{ | ||
RaiseEvent(new MyCustomEvent()); | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace CoreDomain.Tests.Models | ||
{ | ||
public class MyCustomEntity<T> : EntityBase<T> | ||
{ | ||
public MyCustomEntity() { } | ||
|
||
public MyCustomEntity(T id) => Id = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using CoreDomain.Interfaces; | ||
using System; | ||
|
||
namespace CoreDomain.Tests.Models | ||
{ | ||
public class MyCustomEvent : DomainEventBase<Guid> | ||
{ | ||
public MyCustomEvent() : base() | ||
{ | ||
|
||
} | ||
|
||
public MyCustomEvent(Guid aggregateId, long version) : base(aggregateId, version) | ||
{ | ||
|
||
} | ||
|
||
public override IDomainEvent<Guid> WithAggregate(Guid aggregateId, long aggregateVersion) | ||
{ | ||
return new MyCustomEvent(aggregateId, aggregateVersion); | ||
} | ||
} | ||
} |
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,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CoreDomain.Tests.Models | ||
{ | ||
public class MyCustomValueObject : ValueObject | ||
{ | ||
public MyCustomValueObject(int foo, string bar) | ||
{ | ||
Foo = foo; | ||
Bar = bar ?? throw new ArgumentNullException(nameof(bar)); | ||
} | ||
|
||
public int Foo { get; set; } | ||
|
||
public string Bar { get; set; } | ||
|
||
protected override IEnumerable<object> GetEqualityComponents() | ||
{ | ||
yield return Foo; | ||
yield return Bar; | ||
} | ||
} | ||
} |
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,33 @@ | ||
using CoreDomain.Tests.Models; | ||
using Xunit; | ||
|
||
namespace CoreDomain.Tests | ||
{ | ||
public class ValueObjectTest | ||
{ | ||
[Theory] | ||
[InlineData(1, "test")] | ||
public void EqualityTest(int foo, string bar) | ||
{ | ||
MyCustomValueObject valueObject1 = new MyCustomValueObject(foo, bar); | ||
MyCustomValueObject valueObject2 = new MyCustomValueObject(foo, bar); | ||
|
||
Assert.True(valueObject1 == valueObject2); | ||
Assert.False(valueObject1 != valueObject2); | ||
Assert.True(valueObject1.Equals(valueObject2)); | ||
Assert.Equal(valueObject1, valueObject2); | ||
} | ||
|
||
[Fact] | ||
public void NonEqualityTest() | ||
{ | ||
MyCustomValueObject valueObject1 = new MyCustomValueObject(1, "test"); | ||
MyCustomValueObject valueObject2 = new MyCustomValueObject(2, "test"); | ||
|
||
Assert.True(valueObject1 != valueObject2); | ||
Assert.False(valueObject1 == valueObject2); | ||
Assert.False(valueObject1.Equals(valueObject2)); | ||
Assert.NotEqual(valueObject1, valueObject2); | ||
} | ||
} | ||
} |
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,13 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace CoreDomain.Interfaces | ||
{ | ||
public interface IAggregateEvent<TKey> | ||
{ | ||
long Version { get; } | ||
IReadOnlyCollection<IDomainEvent<TKey>> UncommittedEvents { get; } | ||
|
||
IEnumerable<IDomainEvent<TKey>> GetUncommittedEvents(); | ||
void ClearUncommittedEvents(); | ||
} | ||
} |
Oops, something went wrong.