-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-projection Feature/#89 add toggle states projection
- Loading branch information
Showing
24 changed files
with
699 additions
and
95 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
85 changes: 85 additions & 0 deletions
85
...velyn.Core.Tests/ReadModel/Projections/ToggleState/ProjectEvents/ToggleStateAddedSpecs.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,85 @@ | ||
namespace Evelyn.Core.Tests.ReadModel.Projections.ToggleState.ProjectEvents | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using Evelyn.Core.WriteModel.Project.Events; | ||
using FluentAssertions; | ||
using TestStack.BDDfy; | ||
using Xunit; | ||
using Projections = Evelyn.Core.ReadModel.Projections; | ||
|
||
public class ToggleStateAddedSpecs : ProjectionBuilderHarness<ToggleStateAdded> | ||
{ | ||
[Fact] | ||
public void ProjectionDoesNotExist() | ||
{ | ||
this.Given(_ => GivenThereIsNoProjection()) | ||
.When(_ => WhenWeHandleAToggleStateAddedEvent()) | ||
.Then(_ => ThenTheProjectionIsCreatedWithTheNewToggleState()) | ||
.Then(_ => ThenTheProjectionAuditIsSet()) | ||
.BDDfy(); | ||
} | ||
|
||
[Fact] | ||
public void Nominal() | ||
{ | ||
this.Given(_ => GivenTheProjectionExists()) | ||
.And(_ => GivenTheProjectAlreadyHasAToggleState()) | ||
.When(_ => WhenWeHandleAToggleStateAddedEvent()) | ||
.Then(_ => ThenTheNewToggleStateIsAdded()) | ||
.And(_ => ThenTheProjectionAuditIsSet()) | ||
.BDDfy(); | ||
} | ||
|
||
protected override async Task HandleEventImplementation() | ||
{ | ||
await ProjectionBuilder.Handle(StreamPosition, Event, StoppingToken); | ||
} | ||
|
||
private void GivenTheProjectAlreadyHasAToggleState() | ||
{ | ||
OriginalProjection.ToggleState.AddEnvironmentState( | ||
DataFixture.Create<Projections.EventAudit>(), | ||
DataFixture.Create<string>(), | ||
DataFixture.Create<string>()); | ||
} | ||
|
||
private async Task WhenWeHandleAToggleStateAddedEvent() | ||
{ | ||
Event = DataFixture.Build<ToggleStateAdded>() | ||
.With(pc => pc.Id, ProjectId) | ||
.With(pc => pc.EnvironmentKey, EnvironmentKey) | ||
.Create(); | ||
|
||
await WhenTheEventIsHandled(); | ||
} | ||
|
||
private void ThenTheProjectionIsCreatedWithTheNewToggleState() | ||
{ | ||
var environmentStates = UpdatedProjection.ToggleState.EnvironmentStates.ToList(); | ||
environmentStates.Count.Should().Be(1); | ||
|
||
environmentStates.Should().Contain(ts => | ||
ts.Key == Event.EnvironmentKey && | ||
ts.Value == Event.Value); | ||
} | ||
|
||
private void ThenTheNewToggleStateIsAdded() | ||
{ | ||
var environmentStates = UpdatedProjection.ToggleState.EnvironmentStates.ToList(); | ||
environmentStates.Count.Should().Be(OriginalProjection.ToggleState.EnvironmentStates.Count() + 1); | ||
|
||
foreach (var environmentState in OriginalProjection.ToggleState.EnvironmentStates) | ||
{ | ||
environmentStates.Should().Contain(ts => | ||
ts.Key == environmentState.Key && | ||
ts.Value == environmentState.Value); | ||
} | ||
|
||
environmentStates.Should().Contain(ts => | ||
ts.Key == Event.EnvironmentKey && | ||
ts.Value == Event.Value); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...lyn.Core.Tests/ReadModel/Projections/ToggleState/ProjectEvents/ToggleStateChangedSpecs.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,68 @@ | ||
namespace Evelyn.Core.Tests.ReadModel.Projections.ToggleState.ProjectEvents | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using Evelyn.Core.WriteModel.Project.Events; | ||
using FluentAssertions; | ||
using TestStack.BDDfy; | ||
using Xunit; | ||
|
||
public class ToggleStateChangedSpecs : ProjectionBuilderHarness<ToggleStateChanged> | ||
{ | ||
[Fact] | ||
public void ProjectionDoesNotExist() | ||
{ | ||
this.Given(_ => GivenThereIsNoProjection()) | ||
.When(_ => WhenWeHandleAToggleStateChangedEvent()) | ||
.Then(_ => ThenAnExceptionIsThrown()) | ||
.BDDfy(); | ||
} | ||
|
||
[Fact] | ||
public void Nominal() | ||
{ | ||
this.Given(_ => GivenTheProjectionExists()) | ||
.And(_ => GivenOurToggleStateIsOnTheProjection()) | ||
.And(_ => GivenTheProjectionHasOtherToggleStates()) | ||
.When(_ => WhenWeHandleAToggleStateChangedEvent()) | ||
.Then(_ => ThenOurToggleStateIsChanged()) | ||
.And(_ => ThenTheProjectionAuditIsSet()) | ||
.BDDfy(); | ||
} | ||
|
||
protected override async Task HandleEventImplementation() | ||
{ | ||
await ProjectionBuilder.Handle(StreamPosition, Event, StoppingToken); | ||
} | ||
|
||
private async Task WhenWeHandleAToggleStateChangedEvent() | ||
{ | ||
Event = DataFixture.Build<ToggleStateChanged>() | ||
.With(pc => pc.Id, ProjectId) | ||
.With(pc => pc.EnvironmentKey, EnvironmentKey) | ||
.With(pc => pc.ToggleKey, ToggleKey) | ||
.Create(); | ||
|
||
await WhenTheEventIsHandled(); | ||
} | ||
|
||
private void ThenOurToggleStateIsChanged() | ||
{ | ||
var updatedEnvironmentStates = UpdatedProjection.ToggleState.EnvironmentStates.ToList(); | ||
updatedEnvironmentStates.Count.Should().Be(OriginalProjection.ToggleState.EnvironmentStates.Count()); | ||
|
||
foreach (var originalEnvironmentState in OriginalProjection.ToggleState.EnvironmentStates) | ||
{ | ||
var expectedEnvironmentStateValue = | ||
(originalEnvironmentState.Key == Event.EnvironmentKey) | ||
? Event.Value | ||
: originalEnvironmentState.Value; | ||
|
||
updatedEnvironmentStates.Should().Contain(ts => | ||
ts.Key == originalEnvironmentState.Key && | ||
ts.Value == expectedEnvironmentStateValue); | ||
} | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...lyn.Core.Tests/ReadModel/Projections/ToggleState/ProjectEvents/ToggleStateDeletedSpecs.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,83 @@ | ||
namespace Evelyn.Core.Tests.ReadModel.Projections.ToggleState.ProjectEvents | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using Evelyn.Core.ReadModel.Projections.ToggleState; | ||
using Evelyn.Core.WriteModel.Project.Events; | ||
using FluentAssertions; | ||
using NSubstitute; | ||
using TestStack.BDDfy; | ||
using Xunit; | ||
|
||
public class ToggleStateDeletedSpecs : ProjectionBuilderHarness<ToggleStateDeleted> | ||
{ | ||
[Fact] | ||
public void ProjectionDoesNotExist() | ||
{ | ||
this.Given(_ => GivenThereIsNoProjection()) | ||
.When(_ => WhenWeHandleAToggleStateDeletedEvent()) | ||
.Then(_ => ThenAnExceptionIsThrown()) | ||
.BDDfy(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleToggleStatesExist() | ||
{ | ||
this.Given(_ => GivenTheProjectionExists()) | ||
.And(_ => GivenOurToggleStateIsOnTheProjection()) | ||
.And(_ => GivenTheProjectionHasOtherToggleStates()) | ||
.When(_ => WhenWeHandleAToggleStateDeletedEvent()) | ||
.Then(_ => ThenOurToggleStateIsRemoved()) | ||
.And(_ => ThenTheProjectionAuditIsSet()) | ||
.BDDfy(); | ||
} | ||
|
||
[Fact] | ||
public void LastToggleStateIsDeleted() | ||
{ | ||
this.Given(_ => GivenTheProjectionExists()) | ||
.And(_ => GivenOurToggleStateIsOnTheProjection()) | ||
.When(_ => WhenWeHandleAToggleStateDeletedEvent()) | ||
.Then(_ => ThenTheProjectionIsDeleted()) | ||
.BDDfy(); | ||
} | ||
|
||
protected override async Task HandleEventImplementation() | ||
{ | ||
await ProjectionBuilder.Handle(StreamPosition, Event, StoppingToken); | ||
} | ||
|
||
private async Task WhenWeHandleAToggleStateDeletedEvent() | ||
{ | ||
Event = DataFixture.Build<ToggleStateDeleted>() | ||
.With(pc => pc.Id, ProjectId) | ||
.With(pc => pc.EnvironmentKey, EnvironmentKey) | ||
.With(pc => pc.ToggleKey, ToggleKey) | ||
.Create(); | ||
|
||
await WhenTheEventIsHandled(); | ||
} | ||
|
||
private void ThenOurToggleStateIsRemoved() | ||
{ | ||
var updatedEnvironmentStates = UpdatedProjection.ToggleState.EnvironmentStates.ToList(); | ||
updatedEnvironmentStates.Count.Should().Be(OriginalProjection.ToggleState.EnvironmentStates.Count() - 1); | ||
|
||
foreach (var originalEnvironmentState in OriginalProjection.ToggleState.EnvironmentStates) | ||
{ | ||
if (originalEnvironmentState.Key != Event.EnvironmentKey) | ||
{ | ||
updatedEnvironmentStates.Should().Contain(ts => | ||
ts.Key == originalEnvironmentState.Key && | ||
ts.Value == originalEnvironmentState.Value); | ||
} | ||
} | ||
} | ||
|
||
private void ThenTheProjectionIsDeleted() | ||
{ | ||
ProjectionStore.Received().Delete(Projection.StoreKey(ProjectId, EnvironmentKey)); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Evelyn.Core.Tests/ReadModel/Projections/ToggleState/ProjectionBuilderHarness.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,57 @@ | ||
namespace Evelyn.Core.Tests.ReadModel.Projections.ToggleState | ||
{ | ||
using System; | ||
using AutoFixture; | ||
using Evelyn.Core.WriteModel; | ||
using NSubstitute; | ||
using Projections = Evelyn.Core.ReadModel.Projections; | ||
|
||
public abstract class ProjectionBuilderHarness<TEvent> : ProjectionBuilderHarness<Projections.ToggleState.Projection, Projections.ToggleState.ProjectionBuilder, TEvent> | ||
where TEvent : Event | ||
{ | ||
public ProjectionBuilderHarness() | ||
{ | ||
ProjectionBuilder = new Projections.ToggleState.ProjectionBuilder(ProjectionStore); | ||
} | ||
|
||
protected Guid ProjectId { get; set; } | ||
|
||
protected string ToggleKey { get; private set; } | ||
|
||
protected string EnvironmentKey { get; set; } | ||
|
||
protected void GivenThereIsNoProjection() | ||
{ | ||
ProjectId = DataFixture.Create<Guid>(); | ||
ToggleKey = DataFixture.Create<string>(); | ||
|
||
ProjectionStore.WhenForAnyArgs(ps => ps.Get(Arg.Any<string>())) | ||
.Do(ci => throw new Evelyn.Core.ReadModel.ProjectionNotFoundException()); | ||
} | ||
|
||
protected void GivenTheProjectionExists() | ||
{ | ||
OriginalProjection = DataFixture.Create<Projections.ToggleState.Projection>(); | ||
ProjectId = DataFixture.Create<Guid>(); | ||
ToggleKey = DataFixture.Create<string>(); | ||
} | ||
|
||
protected void GivenTheProjectionHasOtherToggleStates() | ||
{ | ||
OriginalProjection.ToggleState.AddEnvironmentState( | ||
DataFixture.Create<Projections.EventAudit>(), | ||
DataFixture.Create<string>(), | ||
DataFixture.Create<string>()); | ||
} | ||
|
||
protected void GivenOurToggleStateIsOnTheProjection() | ||
{ | ||
EnvironmentKey = DataFixture.Create<string>(); | ||
|
||
OriginalProjection.ToggleState.AddEnvironmentState( | ||
DataFixture.Create<Projections.EventAudit>(), | ||
EnvironmentKey, | ||
DataFixture.Create<string>()); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Evelyn.Core.Tests/ReadModel/Projections/ToggleState/ProjectionSpecs.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,16 @@ | ||
namespace Evelyn.Core.Tests.ReadModel.Projections.ToggleState | ||
{ | ||
using AutoFixture; | ||
using Xunit; | ||
using Projections = Core.ReadModel.Projections; | ||
|
||
public class ProjectionSpecs : ProjectionHarness<Projections.ToggleState.Projection> | ||
{ | ||
[Fact] | ||
public void Serialization() | ||
{ | ||
var dto = DataFixture.Create<Projections.ToggleState.Projection>(); | ||
AssertSerializationOf(dto); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.