|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using JasperFx.Events; |
| 4 | +using Marten.Testing.Harness; |
| 5 | +using Shouldly; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace EventSourcingTests.Aggregation; |
| 9 | + |
| 10 | +public class live_aggregation_without_an_aggregate_identifier : OneOffConfigurationsContext |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task live_aggregation_with_guid_identifiers() |
| 14 | + { |
| 15 | + StoreOptions(opts => |
| 16 | + { |
| 17 | + opts.Events.StreamIdentity = StreamIdentity.AsGuid; |
| 18 | + }); |
| 19 | + |
| 20 | + var streamId = Guid.NewGuid(); |
| 21 | + theSession.Events.StartStream(streamId, new AEvent(), new BEvent(), new BEvent(), new CEvent()); |
| 22 | + await theSession.SaveChangesAsync(); |
| 23 | + |
| 24 | + var version1 = await theSession.Events.AggregateStreamAsync<CountOfLetters>(streamId); |
| 25 | + |
| 26 | + var version2 = await theSession.Events.FetchLatest<CountOfLetters>(streamId); |
| 27 | + |
| 28 | + var version3 = await theSession.Events.FetchForWriting<CountOfLetters>(streamId); |
| 29 | + |
| 30 | + var version4 = await theSession.Events.AggregateStreamToLastKnownAsync<CountOfLetters>(streamId); |
| 31 | + |
| 32 | + version1.ACount.ShouldBe(1); |
| 33 | + version1.BCount.ShouldBe(2); |
| 34 | + version1.CCount.ShouldBe(1); |
| 35 | + version1.DCount.ShouldBe(0); |
| 36 | + |
| 37 | + version2.ACount.ShouldBe(1); |
| 38 | + version2.BCount.ShouldBe(2); |
| 39 | + version2.CCount.ShouldBe(1); |
| 40 | + version2.DCount.ShouldBe(0); |
| 41 | + |
| 42 | + version3.Aggregate.ACount.ShouldBe(1); |
| 43 | + version3.Aggregate.BCount.ShouldBe(2); |
| 44 | + version3.Aggregate.CCount.ShouldBe(1); |
| 45 | + version3.Aggregate.DCount.ShouldBe(0); |
| 46 | + |
| 47 | + version4.ACount.ShouldBe(1); |
| 48 | + version4.BCount.ShouldBe(2); |
| 49 | + version4.CCount.ShouldBe(1); |
| 50 | + version4.DCount.ShouldBe(0); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task live_aggregation_with_string_identifiers() |
| 55 | + { |
| 56 | + StoreOptions(opts => |
| 57 | + { |
| 58 | + opts.Events.StreamIdentity = StreamIdentity.AsString; |
| 59 | + }); |
| 60 | + |
| 61 | + var streamId = Guid.NewGuid().ToString(); |
| 62 | + theSession.Events.StartStream(streamId, new AEvent(), new BEvent(), new BEvent(), new CEvent()); |
| 63 | + await theSession.SaveChangesAsync(); |
| 64 | + |
| 65 | + var version1 = await theSession.Events.AggregateStreamAsync<CountOfLetters>(streamId); |
| 66 | + |
| 67 | + var version2 = await theSession.Events.FetchLatest<CountOfLetters>(streamId); |
| 68 | + |
| 69 | + var version3 = await theSession.Events.FetchForWriting<CountOfLetters>(streamId); |
| 70 | + |
| 71 | + var version4 = await theSession.Events.AggregateStreamToLastKnownAsync<CountOfLetters>(streamId); |
| 72 | + |
| 73 | + version1.ACount.ShouldBe(1); |
| 74 | + version1.BCount.ShouldBe(2); |
| 75 | + version1.CCount.ShouldBe(1); |
| 76 | + version1.DCount.ShouldBe(0); |
| 77 | + |
| 78 | + version2.ACount.ShouldBe(1); |
| 79 | + version2.BCount.ShouldBe(2); |
| 80 | + version2.CCount.ShouldBe(1); |
| 81 | + version2.DCount.ShouldBe(0); |
| 82 | + |
| 83 | + version3.Aggregate.ACount.ShouldBe(1); |
| 84 | + version3.Aggregate.BCount.ShouldBe(2); |
| 85 | + version3.Aggregate.CCount.ShouldBe(1); |
| 86 | + version3.Aggregate.DCount.ShouldBe(0); |
| 87 | + |
| 88 | + version4.ACount.ShouldBe(1); |
| 89 | + version4.BCount.ShouldBe(2); |
| 90 | + version4.CCount.ShouldBe(1); |
| 91 | + version4.DCount.ShouldBe(0); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +public class CountOfLetters |
| 97 | +{ |
| 98 | + public int ACount { get; set; } |
| 99 | + public int BCount { get; set; } |
| 100 | + public int CCount { get; set; } |
| 101 | + public int DCount { get; set; } |
| 102 | + public int ECount { get; set; } |
| 103 | + |
| 104 | + public void Apply(AEvent _) |
| 105 | + { |
| 106 | + ACount++; |
| 107 | + } |
| 108 | + |
| 109 | + public void Apply(BEvent _) |
| 110 | + { |
| 111 | + BCount++; |
| 112 | + } |
| 113 | + |
| 114 | + public void Apply(CEvent _) |
| 115 | + { |
| 116 | + CCount++; |
| 117 | + } |
| 118 | + |
| 119 | + public void Apply(DEvent _) |
| 120 | + { |
| 121 | + DCount++; |
| 122 | + } |
| 123 | + |
| 124 | + public void Apply(EEvent _) |
| 125 | + { |
| 126 | + ECount++; |
| 127 | + } |
| 128 | + |
| 129 | +} |
| 130 | + |
0 commit comments