Skip to content

Commit

Permalink
refactor: use contract builder for domain unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilbaczek committed Mar 29, 2024
1 parent c3d1392 commit 2864e45
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.PrepareContract;

public class PreparedContractBuilder(Contract parentBuilder)
{
private DateTimeOffset _signDay;
private DateTimeOffset _fakeToday;

public PreparedContractBuilder SignedOn(DateTimeOffset signDay, DateTimeOffset fakeToday)
{
_signDay = signDay;
_fakeToday = fakeToday;
return this;
}

private BindingContract Build()
{
var bindingContract = parentBuilder.Sign(_signDay, _fakeToday);

return bindingContract;
}

public static implicit operator BindingContract(PreparedContractBuilder builder) => builder.Build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract;

using PrepareContract;

public class ContractBuilder
{
public static ContractBuilder Create() => new();

private DateTimeOffset _preparedAt;

public ContractBuilder PreparedAt(DateTimeOffset preparedAt)
{
_preparedAt = preparedAt;
return this;
}

public PreparedContractBuilder Prepared() => new(Build());

private Contract Build()
{
var prepareContractParameters = PrepareContractParameters.GetValid();
var contract = Contract.Prepare(
Guid.NewGuid(),
prepareContractParameters.MaxAge,
prepareContractParameters.MaxHeight,
_preparedAt);

return contract;
}

public static implicit operator Contract(ContractBuilder builder) => builder.Build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.SignContract;

using Common;
using Core.SignContract;
using PrepareContract;

public sealed class SignContractTests
{
Expand All @@ -15,7 +14,9 @@ internal void Given_sign_contract_Then_expiration_date_is_set_to_contract_durati
DateTimeOffset expectedExpirationDate)
{
// Arrange
var contract = PrepareContract(preparedAt);
Contract contract = ContractBuilder
.Create()
.PreparedAt(preparedAt);

// Act
var bindingContract = contract.Sign(signedAt, fakeNow);
Expand All @@ -33,7 +34,9 @@ internal void Given_sign_contract_Then_expiration_date_is_set_to_contract_durati
internal void Given_sign_contract_Then_contracts_becomes_binding_contract()
{
// Arrange
var contract = PrepareContract(PreparedAt);
Contract contract = ContractBuilder
.Create()
.PreparedAt(PreparedAt);

// Act
var bindingContract = contract.Sign(SignedAt, FakeNow);
Expand All @@ -42,16 +45,4 @@ internal void Given_sign_contract_Then_contracts_becomes_binding_contract()
var @event = bindingContract.GetPublishedEvent<ContractStartedBindingEvent>();
@event?.BindingFrom.Should().Be(SignedAt);
}

private static Contract PrepareContract(DateTimeOffset preparedAt)
{
var prepareContractParameters = PrepareContractParameters.GetValid();
var contract = Contract.Prepare(
Guid.NewGuid(),
prepareContractParameters.MaxAge,
prepareContractParameters.MaxHeight,
preparedAt);

return contract;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,28 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Core.UnitTests.TerminateBind

using Common;
using Core.TerminateBindingContract;
using PrepareContract;
using SignContract;

public sealed class TerminateBindingContractTests
{
private readonly DateTimeOffset _preparedAt = new(2022, 2, 3, 1, 1, 1, TimeSpan.Zero);
private readonly DateTimeOffset _terminatedAt = new(2023, 3, 3, 1, 1, 1, TimeSpan.Zero);
private readonly DateTimeOffset _fakeToday = new(2022, 3, 4, 1, 1, 1, TimeSpan.Zero);
private readonly DateTimeOffset _signDay = new(2022, 1, 3, 1, 1, 1, TimeSpan.Zero);

[Fact]
internal void Given_terminate_binding_contracts_Then_should_raise_binding_contracts()
{
var preparedAt = new DateTimeOffset(2022, 2, 3, 1, 1, 1, TimeSpan.Zero);
var terminatedAt = new DateTimeOffset(2023, 3, 3, 1, 1, 1, TimeSpan.Zero);
var fakeToday = new DateTimeOffset(2022, 3, 4, 1, 1, 1, TimeSpan.Zero);
var signDay = new DateTimeOffset(2022, 1, 3, 1, 1, 1, TimeSpan.Zero);
var contract = PrepareContract(preparedAt);
var bindingContract = contract.Sign(signDay, fakeToday);

bindingContract.Terminate(terminatedAt);

var @event = bindingContract.GetPublishedEvent<BindingContractTerminatedEvent>();
@event?.TerminatedAt.Should().Be(terminatedAt);
}
BindingContract bindingContract = ContractBuilder
.Create()
.PreparedAt(_preparedAt)
.Prepared()
.SignedOn(_signDay, _fakeToday);

private static Contract PrepareContract(DateTimeOffset preparedAt)
{
var prepareContractParameters = PrepareContractParameters.GetValid();
var contract = Contract.Prepare(
Guid.NewGuid(),
prepareContractParameters.MaxAge,
prepareContractParameters.MaxHeight,
preparedAt);
bindingContract.Terminate(_terminatedAt);

return contract;
var @event = bindingContract.GetPublishedEvent<BindingContractTerminatedEvent>();
@event?.TerminatedAt.Should().Be(_terminatedAt);
}
}

0 comments on commit 2864e45

Please sign in to comment.