Skip to content

Commit 80f6ba7

Browse files
Strythiojeremydmiller
authored andcommitted
Added tests for FetchForWriting<T>(Guid,long,CancellationToken) to cover a bug where an InvalidCastException is thrown when the aggregate is inline and UseIdentityMapForAggregates = true.
1 parent fc16730 commit 80f6ba7

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/EventSourcingTests/FetchForWriting/fetching_inline_aggregates_for_writing.cs

+79
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,89 @@ await Should.ThrowAsync<StreamLockedException>(async () =>
375375
}
376376

377377

378+
[Fact]
379+
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map()
380+
{
381+
StoreOptions(
382+
opts =>
383+
{
384+
opts.Events.UseIdentityMapForAggregates = true;
385+
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
386+
});
378387

388+
var streamId = Guid.NewGuid();
379389

390+
theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
391+
new CEvent(), new CEvent());
392+
await theSession.SaveChangesAsync();
380393

394+
var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 6);
395+
stream.Aggregate.ShouldNotBeNull();
396+
stream.CurrentVersion.ShouldBe(6);
397+
398+
stream.AppendOne(new EEvent());
399+
await theSession.SaveChangesAsync();
381400

401+
var latest = await theSession.Events.FetchLatest<SimpleAggregate>(streamId);
402+
latest.Version.ShouldBe(7);
403+
}
404+
[Fact]
405+
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map_immediate_sad_path()
406+
{
407+
StoreOptions(
408+
opts =>
409+
{
410+
opts.Events.UseIdentityMapForAggregates = true;
411+
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
412+
});
413+
414+
415+
var streamId = Guid.NewGuid();
416+
417+
theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
418+
new CEvent(), new CEvent());
419+
await theSession.SaveChangesAsync();
420+
421+
await Should.ThrowAsync<ConcurrencyException>(async () =>
422+
{
423+
var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 5);
424+
});
425+
}
426+
427+
[Fact]
428+
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map_sad_path_on_save_changes()
429+
{
430+
StoreOptions(
431+
opts =>
432+
{
433+
opts.Events.UseIdentityMapForAggregates = true;
434+
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
435+
});
436+
437+
438+
var streamId = Guid.NewGuid();
439+
440+
theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
441+
new CEvent(), new CEvent());
442+
await theSession.SaveChangesAsync();
443+
444+
// This should be fine
445+
var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 6);
446+
stream.AppendOne(new EEvent());
447+
448+
// Get in between and run other events in a different session
449+
await using (var otherSession = theStore.LightweightSession())
450+
{
451+
otherSession.Events.Append(streamId, new EEvent());
452+
await otherSession.SaveChangesAsync();
453+
}
454+
455+
// The version is now off
456+
await Should.ThrowAsync<ConcurrencyException>(async () =>
457+
{
458+
await theSession.SaveChangesAsync();
459+
});
460+
}
382461

383462
[Fact]
384463
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version()

0 commit comments

Comments
 (0)