示例#1
0
        public void uses_provided_instance()
        {
            var stream = StreamAction.Append(Guid.NewGuid())
                         .Add(new QuestStarted {
                Name = "Destroy the Ring"
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Frodo", "Sam" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Merry", "Pippin" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Strider" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Gandalf", "Boromir", "Gimli", "Legolas" }
            })
                         .Add(new MembersEscaped {
                Members = new string[] { "Frodo", "Sam" }, Location = "Mt. Doom"
            });

            var state = new QuestFinishingParty("The Eagles!");

            var party = theAggregator.Build(stream.Events, null, state);

            party.Name.ShouldBe("Destroy the Ring");

            party.Members.ShouldHaveTheSameElementsAs("Merry", "Pippin", "Strider", "Gandalf", "Boromir", "Gimli", "Legolas");
        }
示例#2
0
        public void build_a_series_of_events()
        {
            var stream = StreamAction.Append(Guid.NewGuid())
                         .Add(new QuestStarted {
                Name = "Destroy the Ring"
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Frodo", "Sam" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Merry", "Pippin" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Strider" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Gandalf", "Boromir", "Gimli", "Legolas" }
            })
                         .Add(new MembersDeparted()
            {
                Members = new string[] { "Frodo", "Sam" }
            });

            var party = theAggregator.Build(stream.Events, null);

            party.Name.ShouldBe("Destroy the Ring");

            party.Members.ShouldHaveTheSameElementsAs("Merry", "Pippin", "Strider", "Gandalf", "Boromir", "Gimli", "Legolas");
        }
示例#3
0
        public async Task find_when_stream_is_not_new_and_it_is_not_in_database_async()
        {
            var id = Guid.NewGuid();

            var finder = new AggregateFinder <QuestParty>();

            (await finder.FindAsync(StreamAction.Append(id), theSession, new CancellationToken()))
            .ShouldNotBeNull();
        }
示例#4
0
        public void find_when_stream_is_not_new_and_it_is_not_in_database()
        {
            var session = Substitute.For <IDocumentSession>();
            var id      = Guid.NewGuid();

            var finder = new AggregateFinder <QuestParty>();

            finder.Find(StreamAction.Append(id), session)
            .ShouldNotBeNull();
        }
示例#5
0
        public void explicitly_added_step_as_action()
        {
            theAggregator.Add <MonsterSlayed>((party, slayed) =>
            {
                party.Slayed.Fill(slayed.Name);
            });

            theAggregator.AppliesTo(StreamAction.Append(Guid.NewGuid()).Add(new MonsterSlayed()))
            .ShouldBeTrue();
        }
示例#6
0
        public static StreamAction[] ToStreams(StreamIdentity streamIdentity, IEnumerable <IEvent> events)
        {
            if (streamIdentity == StreamIdentity.AsGuid)
            {
                return(events.GroupBy(x => x.StreamId)
                       .Select(group => StreamAction.Append(@group.Key, @group.ToArray()))
                       .ToArray());
            }

            return(events.GroupBy(x => x.StreamKey)
                   .Select(group => StreamAction.Append(@group.Key, @group.ToArray()))
                   .ToArray());
        }
示例#7
0
        public void applies_to()
        {
            var stream = StreamAction.Append(Guid.NewGuid());

            theAggregator.AppliesTo(stream).ShouldBeFalse();

            stream.Add(new MonsterSlayed());

            theAggregator.AppliesTo(stream).ShouldBeFalse();

            stream.Add(new MembersJoined());

            theAggregator.AppliesTo(stream).ShouldBeTrue();
        }
示例#8
0
        public StreamAction Append(string stream, params object[] events)
        {
            var step = action(e => e.Append(stream, events));

            if (events.Count() > 3)
            {
                step.Description = $"Append('{stream}', events)";
            }
            else
            {
                step.Description = $"Append('{stream}', {events.Select(x => x.ToString()).Join(", ")})";
            }

            return(StreamAction.Append(_store.Events, stream, events));
        }
示例#9
0
        public StreamAction Append(Guid stream, IEnumerable <object> events)
        {
            var step = action(e => e.Append(stream, events));

            if (events.Count() > 3)
            {
                step.Description = $"Append({stream}, events)";
            }
            else
            {
                step.Description = $"Append({stream}, {events.Select(x => x.ToString()).Join(", ")})";
            }

            return(StreamAction.Append(_store.Events, stream, events));
        }
        public void can_lookup_private_apply_methods()
        {
            var theGraph = new EventGraph(new StoreOptions());

            theGraph.UseAggregatorLookup(new AggregatorLookup(type => typeof(AggregatorApplyPrivate <>).CloseAndBuildAs <IAggregator>(type)));

            var aggregator = theGraph.AggregateFor <AggregateWithPrivateEventApply>();

            var stream = StreamAction.Append(Guid.NewGuid(), new[] { new QuestStarted {
                                                                         Name = "Destroy the Ring"
                                                                     } });

            var party = aggregator.Build(stream.Events, null);

            party.Name.ShouldBe("Destroy the Ring");
        }
示例#11
0
        public async Task find_when_stream_is_not_new_async()
        {
            var id = Guid.NewGuid();

            var persisted = new QuestParty {
                Id = id
            };

            theSession.Store(persisted);
            await theSession.SaveChangesAsync();

            var finder = new AggregateFinder <QuestParty>();

            (await finder.FindAsync(StreamAction.Append(id), theSession, new CancellationToken()))
            .ShouldBeTheSameAs(persisted);
        }
示例#12
0
        public void find_when_stream_is_not_new()
        {
            var session = Substitute.For <IDocumentSession>();
            var id      = Guid.NewGuid();

            var persisted = new QuestParty {
                Id = id
            };

            var finder = new AggregateFinder <QuestParty>();

            session.Load <QuestParty>(id).Returns(persisted);

            finder.Find(StreamAction.Append(id), session)
            .ShouldBeTheSameAs(persisted);
        }
示例#13
0
        public StreamAction Append(Guid stream, long expectedVersion, params object[] events)
        {
            var step = action(e => e.Append(stream, expectedVersion, events));

            if (events.Count() > 3)
            {
                step.Description = $"Append({stream}, {expectedVersion}, events)";
            }
            else
            {
                step.Description =
                    $"Append({stream}, {expectedVersion}, {events.Select(x => x.ToString()).Join(", ")})";
            }

            return(StreamAction.Append(_store.Events, stream, events));
        }
示例#14
0
        public async Task InlineProject(Action <TestEventScenario> action)
        {
            var scenario = new TestEventScenario();

            action(scenario);

            var streams = scenario
                          .Streams
                          .ToDictionary()
                          .Select(x => StreamAction.Append(x.Key, x.Value.Events().ToArray()))
                          .ToArray();

            var inline = _projection.BuildRuntime(theStore);

            await inline.ApplyAsync(theSession, streams, CancellationToken.None);

            await theSession.SaveChangesAsync();
        }
示例#15
0
        public void ApplyServerVersion_for_existing_streams()
        {
            var action = StreamAction.Append(theEvents, Guid.NewGuid(), new AEvent(), new BEvent(), new CEvent(), new DEvent());

            var queue = new Queue<long>();
            queue.Enqueue(11);
            queue.Enqueue(12);
            queue.Enqueue(13);
            queue.Enqueue(14);

            action.PrepareEvents(5, theEvents, queue, theSession);

            action.ExpectedVersionOnServer.ShouldBe(5);

            action.Events[0].Version.ShouldBe(6);
            action.Events[1].Version.ShouldBe(7);
            action.Events[2].Version.ShouldBe(8);
            action.Events[3].Version.ShouldBe(9);
        }
        public void can_set_private_apply_aggregator_through_extension_methods_and_strategy()
        {
            var theGraph = new EventGraph(new StoreOptions());

            // SAMPLE: register-custom-aggregator-lookup
            // Registering an aggregator lookup that provides aggregator supporting private Apply([Event Type]) methods
            theGraph.UseAggregatorLookup(AggregationLookupStrategy.UsePrivateApply);
            // ENDSAMPLE

            var aggregator = theGraph.AggregateFor <AggregateWithPrivateEventApply>();

            var stream = StreamAction.Append(Guid.NewGuid(), new object[] { new QuestStarted {
                                                                                Name = "Destroy the Ring"
                                                                            } });

            var party = aggregator.Build(stream.Events, null);

            party.Name.ShouldBe("Destroy the Ring");
        }
示例#17
0
            public StreamAction StartNewStream(IMartenSessionLogger logger = null)
            {
                var events = new object[] { new AEvent(), new BEvent(), new CEvent(), new DEvent() };

                using var session = Store.Events.TenancyStyle == TenancyStyle.Conjoined
                    ? Store.LightweightSession(TenantId)
                    : Store.LightweightSession();

                session.Listeners.Add(new EventMetadataChecker());

                if (logger != null)
                {
                    session.Logger = logger;
                }

                if (Store.Events.StreamIdentity == StreamIdentity.AsGuid)
                {
                    session.Events.StartStream(StreamId, events);
                    session.SaveChanges();

                    var stream = StreamAction.Append(StreamId);
                    stream.Version  = 4;
                    stream.TenantId = TenantId;

                    return(stream);
                }
                else
                {
                    session.Events.StartStream(StreamId.ToString(), events);
                    session.SaveChanges();

                    var stream = StreamAction.Start(StreamId.ToString(), new AEvent());
                    stream.Version  = 4;
                    stream.TenantId = TenantId;

                    return(stream);
                }
            }
        public void can_set_public_and_private_apply_aggregator_through_extension_methods_and_strategy()
        {
            var theGraph = new EventGraph(new StoreOptions());

            theGraph.UseAggregatorLookup(AggregationLookupStrategy.UsePublicAndPrivateApply);

            var aggregator = theGraph.AggregateFor <AggregateWithPrivateEventApply>();

            var stream = StreamAction.Append(Guid.NewGuid(), new object[] { new QuestStarted {
                                                                                Name = "Destroy the Ring"
                                                                            } });

            var party = aggregator.Build(stream.Events, null);

            party.Name.ShouldBe("Destroy the Ring");

            stream.Add(new QuestEnded {
                Name = "Ring Destroyed"
            });
            var party2 = aggregator.Build(stream.Events, null);

            party2.Name.ShouldBe("Ring Destroyed");
        }
示例#19
0
        public void uses_default_aggregate_constructor_if_no_instance_provided()
        {
            var stream = StreamAction.Append(Guid.NewGuid())
                         .Add(new QuestStarted {
                Name = "Destroy the Ring"
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Frodo", "Sam" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Merry", "Pippin" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Strider" }
            })
                         .Add(new MembersJoined {
                Members = new string[] { "Gandalf", "Boromir", "Gimli", "Legolas" }
            })
                         .Add(new MembersEscaped {
                Members = new string[] { "Frodo", "Sam" }, Location = "Mt. Doom"
            });

            Exception <NullReferenceException> .ShouldBeThrownBy(() => theAggregator.Build(stream.Events, null));
        }