public void Inserting_E2_Before_E1_Persisting_and_then_Inserting_E3_before_E1()
{
var firstMigration = Seq.Create(Before<E1>.Insert<E2>()).ToArray();
var secondMigration = Seq.Create(Before<E1>.Insert<E3>()).ToArray();
IReadOnlyList<IEventMigration> migrations = new List<IEventMigration>();
using (var container = CreateContainerForEventStoreType(() => migrations, EventStoreType))
{
container.Resolve<DummyTimeSource>().UtcNow = DateTime.Parse("2001-01-01 12:00");
var id = Guid.Parse("00000000-0000-0000-0000-000000000001");
var initialAggregate = TestAggregate.FromEvents(container.Resolve<IUtcTimeTimeSource>(), id, Seq.OfTypes<Ec1, E1>());
var expectedHistoryAfterFirstMigration = TestAggregate.FromEvents(container.Resolve<IUtcTimeTimeSource>(), id, Seq.OfTypes<Ec1, E2, E1>()).History;
var expectedHistoryAfterSecondMigration = TestAggregate.FromEvents(container.Resolve<IUtcTimeTimeSource>(), id, Seq.OfTypes<Ec1, E2, E3, E1>()).History;
Func<IEventStoreSession> session = () => container.Resolve<IEventStoreSession>();
Func<IEventStore> eventStore = () => container.Resolve<IEventStore>();
container.ExecuteUnitOfWorkInIsolatedScope(() => session().Save(initialAggregate));
migrations = firstMigration;
var historyWithFirstMigrationUnPersisted = container.ExecuteUnitOfWorkInIsolatedScope(() => session().Get<TestAggregate>(id).History);
container.ExecuteUnitOfWorkInIsolatedScope(() => eventStore().PersistMigrations());
var historyAfterPersistingFirstMigration = container.ExecuteUnitOfWorkInIsolatedScope(() => session().Get<TestAggregate>(id).History);
AssertStreamsAreIdentical(expectedHistoryAfterFirstMigration, historyWithFirstMigrationUnPersisted, nameof(historyWithFirstMigrationUnPersisted));
AssertStreamsAreIdentical(expectedHistoryAfterFirstMigration, historyAfterPersistingFirstMigration, nameof(historyAfterPersistingFirstMigration));
migrations = secondMigration;
ClearEventstoreCache(container);
var historyWithSecondMigrationUnPersisted = container.ExecuteUnitOfWorkInIsolatedScope(() => session().Get<TestAggregate>(id).History);
container.ExecuteUnitOfWorkInIsolatedScope(() => eventStore().PersistMigrations());
var historyAfterPersistingSecondMigration = container.ExecuteUnitOfWorkInIsolatedScope(() => session().Get<TestAggregate>(id).History);
AssertStreamsAreIdentical(expectedHistoryAfterSecondMigration, historyWithSecondMigrationUnPersisted, nameof(historyWithSecondMigrationUnPersisted));
AssertStreamsAreIdentical(expectedHistoryAfterSecondMigration, historyAfterPersistingSecondMigration, nameof(historyAfterPersistingSecondMigration));
}
}