Example #1
0
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back(DocumentTracking sessionType)
        {
            var store = InitStore();

            var id = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.OpenSession(sessionType))
            {
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();
            }

            using (var session = store.OpenSession(sessionType))
            {
                var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                session.Events.Append(id, joined);
                session.Events.Append(id, departed);

                session.SaveChanges();

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(3);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<QuestStarted>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
                streamEvents.ElementAt(2).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(2).Version.ShouldBe(3);
            }
        }
        public async Task capture_events_to_a_new_stream_and_fetch_the_events_back_async(DocumentTracking sessionType)
        {
            var store = InitStore();


            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: start-stream-with-aggregate-type
                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                var id = session.Events.StartStream<Quest>(joined, departed);
                await session.SaveChangesAsync();
                // ENDSAMPLE

                var streamEvents = await session.Events.FetchStreamAsync(id);

                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);

                streamEvents.Each(e => e.Timestamp.ShouldNotBe(default(DateTimeOffset)));
            }
        }
        public void capture_events_to_a_new_stream_and_fetch_the_events_back()
        {
            var schema = _container.GetInstance<IDocumentSchema>();
            var quest = schema.Events.StreamMappingFor<Quest>();

            quest.AddEvent(typeof(MembersJoined));
            quest.AddEvent(typeof(MembersDeparted));


            _container.GetInstance<IDocumentCleaner>().CompletelyRemoveAll();

            _container.GetInstance<IManagedConnection>().Execute(SchemaBuilder.GetText("mt_stream"));

            var events = _container.GetInstance<Marten.Events.EventStore>();


            var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
            var departed = new MembersDeparted { Members = new[] { "Thom" } };

            var id = events.StartStream<Quest>(joined, departed);

            var streamEvents = events.FetchStream<Quest>(id);

            streamEvents.Count().ShouldBe(2);
            streamEvents.ElementAt(0).ShouldBeOfType<MembersJoined>();
            streamEvents.ElementAt(1).ShouldBeOfType<MembersDeparted>();
        }
        public async Task running_asynchronously()
        {
            Guid streamId = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.StartStream <Quest>(streamId, joined, departed);
                await session.SaveChangesAsync().ConfigureAwait(false);

                session.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(1, 2);
            }

            using (var session = theStore.OpenSession())
            {
                var joined2 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined2, departed2);
                await session.SaveChangesAsync().ConfigureAwait(false);

                session.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(3, 4);
            }

            using (var session = theStore.OpenSession())
            {
                var joined3 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed3 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined3, departed3);
                await session.SaveChangesAsync().ConfigureAwait(false);

                var events = session.LastCommit.GetEvents().ToArray();
                events.Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(5, 6);

                events.Each(x => x.Sequence.ShouldBeGreaterThan(0L));

                events.Select(x => x.Sequence).Distinct().Count().ShouldBe(2);
            }
        }
        public void running_synchronously()
        {
            var logger = new RecordingSessionLogger();

            Guid streamId = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                session.Events.StartStream<Quest>(streamId, joined, departed);
                session.SaveChanges();

                var events = logger.LastCommit.GetEvents().ToArray();
                events.Select(x => x.Version)
                    .ShouldHaveTheSameElementsAs(1, 2);

                events.Each(x => x.Sequence.ShouldBeGreaterThan(0L));

                events.Select(x => x.Sequence).Distinct().Count().ShouldBe(2);
            }

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined2 = new MembersJoined { Members = new[] { "Egwene" } };
                var departed2 = new MembersDeparted { Members = new[] { "Perrin" } };

                session.Events.Append(streamId, joined2, departed2);
                session.SaveChanges();

                logger.LastCommit.GetEvents().Select(x => x.Version)
                    .ShouldHaveTheSameElementsAs(3, 4);
            }

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined3 = new MembersJoined { Members = new[] { "Egwene" } };
                var departed3 = new MembersDeparted { Members = new[] { "Perrin" } };

                session.Events.Append(streamId, joined3, departed3);
                session.SaveChanges();

                logger.LastCommit.GetEvents().Select(x => x.Version)
                    .ShouldHaveTheSameElementsAs(5, 6);
            }
        }
Example #6
0
        public fetching_stream_state(DefaultStoreFixture fixture) : base(fixture)
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            theStreamId = theSession.Events.StartStream <Quest>(joined, departed).Id;
            theSession.SaveChanges();
        }
Example #7
0
        public async Task running_asynchronously()
        {
            Guid streamId = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.StartStream <Quest>(streamId, joined, departed);
                await session.SaveChangesAsync();

                session.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(1, 2);
            }

            using (var session = theStore.OpenSession())
            {
                var joined2 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined2, departed2);
                await session.SaveChangesAsync();

                session.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(3, 4);
            }

            using (var session = theStore.OpenSession())
            {
                var joined3 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed3 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined3, departed3);
                await session.SaveChangesAsync();

                session.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(5, 6);
            }
        }
Example #8
0
        public void capture_events_to_a_new_stream_and_fetch_the_events_back_in_another_database_schema()
        {
            var store = InitStore("event_store");


            Guid stream = Guid.NewGuid();

            using (var session = store.OpenSession(DocumentTracking.IdentityOnly))
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.StartStream <Quest>(stream, joined, departed);
                session.SaveChanges();

                var streamEvents = session.Events.FetchStream(stream);

                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).Data.ShouldBeOfType <MembersJoined>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType <MembersDeparted>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
            }

            var events = new List <Guid>();

            using (var conn = store.Advanced.OpenConnection())
            {
                conn.Execute(cmd =>
                {
                    cmd.CommandText = "select event_id, stream_id from event_store.mt_rolling_buffer where reference_count = 1 and stream_id = :stream";
                    cmd.With("stream", stream);

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var evt = reader.GetGuid(0);
                            events.Add(evt);

                            reader.GetGuid(1).ShouldBe(stream);
                        }
                    }
                });
            }

            events.Count.ShouldBe(2);
        }
        public void isnew_on_start_stream_2()
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            theSession.Events.StartStream <Quest>(Guid.NewGuid(), joined, departed);

            theSession.PendingChanges.Streams().Single().IsNew.ShouldBeTrue();
        }
Example #10
0
        public void isnew_on_start_stream()
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            var id = theSession.Events.StartStream <Quest>(joined, departed);

            theSession.PendingChanges.Streams().Single().ActionType.ShouldBe(StreamActionType.Start);
        }
        public void should_be_existing_stream_on_append_event()
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            theSession.Events.Append(Guid.NewGuid(), joined, departed);

            theSession.PendingChanges.Streams().Single().IsNew
            .ShouldBeFalse();
        }
Example #12
0
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back_in_another_database_schema(
            DocumentTracking sessionType, TenancyStyle tenancyStyle, string[] tenants)
        {
            var store = InitStore(tenancyStyle);

            var id = Guid.NewGuid();

            When.CalledForEach(tenants, (tenantId, index) =>
            {
                var started = new QuestStarted();

                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    session.Events.StartStream <Quest>(id, started);
                    session.SaveChanges();
                }

                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    // SAMPLE: append-events
                    var joined = new MembersJoined {
                        Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                    };
                    var departed = new MembersDeparted {
                        Members = new[] { "Thom" }
                    };

                    session.Events.Append(id, joined, departed);

                    session.SaveChanges();
                    // ENDSAMPLE

                    var streamEvents = session.Events.FetchStream(id);

                    streamEvents.Count().ShouldBe(3);
                    streamEvents.ElementAt(0).Data.ShouldBeOfType <QuestStarted>();
                    streamEvents.ElementAt(0).Version.ShouldBe(1);
                    streamEvents.ElementAt(1).Data.ShouldBeOfType <MembersJoined>();
                    streamEvents.ElementAt(1).Version.ShouldBe(2);
                    streamEvents.ElementAt(2).Data.ShouldBeOfType <MembersDeparted>();
                    streamEvents.ElementAt(2).Version.ShouldBe(3);
                }
            }).ShouldThrowIf(
                (tenancyStyle == TenancyStyle.Single && tenants.Length > 1) || (tenancyStyle == TenancyStyle.Conjoined && tenants.SequenceEqual(SameTenants))
                );
        }
Example #13
0
        public void get_correct_events_from_single_stream()
        {
            var listener = new StubDocumentSessionListener();
            var store    = InitStore(listener);

            var id      = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.LightweightSession())
            {
                session.Events.StartStream <Quest>(id, started);
                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                             .GetEvents()
                             .ToList();

                events.Count.ShouldBe(1);
                events.ElementAt(0).Data.ShouldBeOfType <QuestStarted>();
            }

            using (var session = store.LightweightSession())
            {
                var joined = new MembersJoined {
                    Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(id, joined, departed);

                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                             .GetEvents()
                             .ToList();

                events.Count.ShouldBe(2);
                events.ElementAt(0).Data.ShouldBeOfType <MembersJoined>();
                events.ElementAt(1).Data.ShouldBeOfType <MembersDeparted>();
            }
        }
Example #14
0
        public void apply_a_simple_transformation()
        {
            var joined = new MembersJoined
            {
                Day      = 3,
                Location = "Baerlon",
                Members  = new[] { "Min" },
                Id       = Guid.NewGuid()
            };

            var stream      = Guid.NewGuid();
            var json        = theEvents.Transforms.Transform("location", stream, joined);
            var expectation = "{'Day':3,'Location':'Baerlon','Id':'EVENT','Quest':'STREAM'}"
                              .Replace("EVENT", joined.Id.ToString())
                              .Replace("STREAM", stream.ToString())
                              .Replace('\'', '"');

            json.ShouldBe(expectation);
        }
        public void apply_a_simple_transformation()
        {
            var joined = new MembersJoined
            {
                Day = 3,
                Location = "Baerlon",
                Members = new[] {"Min"},
                Id = Guid.NewGuid()
            };

            var stream = Guid.NewGuid();
            var json = theEvents.Transforms.Transform("location", stream, joined);
            var expectation = "{'Day':3,'Location':'Baerlon','Id':'EVENT','Quest':'STREAM'}"
                .Replace("EVENT", joined.Id.ToString())
                .Replace("STREAM", stream.ToString())
                .Replace('\'', '"');

            json.ShouldBe(expectation);
        }
        public void delete_stream_by_string_key(TenancyStyle tenancyStyle)
        {
            StoreOptions(_ =>
            {
                _.Events.StreamIdentity = StreamIdentity.AsString;
                _.Events.TenancyStyle   = tenancyStyle;
            });

            var stream1 = "one";
            var stream2 = "two";

            using (var session = theStore.LightweightSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream1, joined, departed);

                var joined2 = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream2, joined2, departed2);

                session.SaveChanges();
            }

            theStore.Advanced.Clean.DeleteSingleEventStream(stream1);

            using (var session = theStore.LightweightSession())
            {
                session.Events.QueryAllRawEvents().ToList().All(x => x.StreamKey == stream2)
                .ShouldBeTrue();
            }
        }
Example #17
0
        public void should_check_max_event_id_on_append()
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            var stream = theSession.Events.StartStream <Quest>(joined).Id;

            theSession.Events.Append(stream, 2, departed);

            theSession.SaveChanges();

            var state = theSession.Events.FetchStreamState(stream);

            state.Id.ShouldBe(stream);
            state.Version.ShouldBe(2);
        }
Example #18
0
        public void should_not_append_events_when_unexpected_max_version_with_string_identifier()
        {
            StoreOptions(_ => _.Events.StreamIdentity = StreamIdentity.AsString);

            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            var stream = "Another";

            theSession.Events.Append(stream, joined);
            theSession.SaveChanges();

            theSession.Events.Append(stream, 2, departed);

            using (var session = theStore.OpenSession())
            {
                var joined3 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed3 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(stream, joined3, departed3);
                session.SaveChanges();
            }

            Assert.Throws <EventStreamUnexpectedMaxEventIdException>(() => theSession.SaveChanges());

            using (var session = theStore.OpenSession())
            {
                var state = session.Events.FetchStreamState(stream);

                state.Key.ShouldBe(stream);
                state.Version.ShouldBe(3);
            }
        }
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back_in_another_database_schema(
            DocumentTracking sessionType)
        {
            var store = InitStore("event_store");

            var id      = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.OpenSession(sessionType))
            {
                session.Events.StartStream <Quest>(id, started);
                session.SaveChanges();
            }

            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: append-events
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(id, joined, departed);

                session.SaveChanges();
                // ENDSAMPLE

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(3);
                streamEvents.ElementAt(0).Data.ShouldBeOfType <QuestStarted>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType <MembersJoined>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
                streamEvents.ElementAt(2).Data.ShouldBeOfType <MembersDeparted>();
                streamEvents.ElementAt(2).Version.ShouldBe(3);
            }
        }
Example #20
0
        public void apply_a_simple_transformation()
        {
            var joined = new MembersJoined
            {
                Day = 3,
                Location = "Baerlon",
                Members = new[] { "Min" },
                //Id = Guid.NewGuid()
            };

            var stream = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                var json = session.Events.Transforms.Transform("location", stream, new Event<MembersJoined>(joined));
                var expectation = "{'Day':3,'Location':'Baerlon','Quest':'STREAM'}"
                    .Replace("STREAM", stream.ToString())
                    .Replace('\'', '"');

                json.ShouldBe(expectation);
            }
        }
        public void apply_a_simple_transformation()
        {
            var joined = new MembersJoined
            {
                Day      = 3,
                Location = "Baerlon",
                Members  = new[] { "Min" },
                //Id = Guid.NewGuid()
            };

            var stream = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                var json        = session.Events.Transforms.Transform("location", stream, new Event <MembersJoined>(joined));
                var expectation = "{'Day':3,'Location':'Baerlon','Quest':'STREAM'}"
                                  .Replace("STREAM", stream.ToString())
                                  .Replace('\'', '"');

                json.ShouldBe(expectation);
            }
        }
        public async Task delete_stream_by_guid_id_async(TenancyStyle tenancyStyle)
        {
            StoreOptions(_ => _.Events.TenancyStyle = tenancyStyle);

            var stream1 = Guid.NewGuid();
            var stream2 = Guid.NewGuid();

            using (var session = theStore.LightweightSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream1, joined, departed);

                var joined2 = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream2, joined2, departed2);

                await session.SaveChangesAsync();
            }

            await theStore.Advanced.Clean.DeleteSingleEventStreamAsync(stream1);

            using (var session = theStore.LightweightSession())
            {
                session.Events.QueryAllRawEvents().ToList().All(x => x.StreamId == stream2)
                .ShouldBeTrue();
            }
        }
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back(DocumentTracking sessionType)
        {
            var store = InitStore();

            var id      = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.OpenSession(sessionType))
            {
                session.Events.StartStream(id, started);
                session.SaveChanges();
            }

            using (var session = store.OpenSession(sessionType))
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(id, joined);
                session.Events.Append(id, departed);

                session.SaveChanges();

                var streamEvents = session.Events.FetchStream(id);

                Enumerable.Count <IEvent>(streamEvents).ShouldBe(3);
                Enumerable.ElementAt <IEvent>(streamEvents, 0).Data.ShouldBeOfType <QuestStarted>();
                Enumerable.ElementAt <IEvent>(streamEvents, 0).Version.ShouldBe(1);
                Enumerable.ElementAt <IEvent>(streamEvents, 1).Data.ShouldBeOfType <MembersJoined>();
                Enumerable.ElementAt <IEvent>(streamEvents, 1).Version.ShouldBe(2);
                Enumerable.ElementAt <IEvent>(streamEvents, 2).Data.ShouldBeOfType <MembersDeparted>();
                Enumerable.ElementAt <IEvent>(streamEvents, 2).Version.ShouldBe(3);
            }
        }
Example #24
0
        public void capture_events_to_an_existing_stream_and_fetch_the_events_back()
        {
            var store = InitStore();

            var id      = "Twelth";
            var started = new QuestStarted();

            using (var session = store.OpenSession())
            {
                session.Events.StartStream <Quest>(id, started);
                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(id, joined);
                session.Events.Append(id, departed);

                session.SaveChanges();

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(3);
                streamEvents.ElementAt(0).Data.ShouldBeOfType <QuestStarted>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType <MembersJoined>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
                streamEvents.ElementAt(2).Data.ShouldBeOfType <MembersDeparted>();
                streamEvents.ElementAt(2).Version.ShouldBe(3);
            }
        }
Example #25
0
        public void should_not_append_events_when_unexpected_max_version()
        {
            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            var stream = theSession.Events.StartStream <Quest>(joined).Id;

            theSession.SaveChanges();

            theSession.Events.Append(stream, 2, departed);

            using (var session = theStore.OpenSession())
            {
                var joined3 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed3 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(stream, joined3, departed3);
                session.SaveChanges();
            }

            Assert.Throws <EventStreamUnexpectedMaxEventIdException>(() => theSession.SaveChanges());

            using (var session = theStore.OpenSession())
            {
                var state = session.Events.FetchStreamState(stream);

                state.Id.ShouldBe(stream);
                state.Version.ShouldBe(3);
            }
        }
        public void live_aggregate_equals_inlined_aggregate_without_hidden_contracts(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note Id = questId, is we remove it from first message then AggregateStream will return party.Id=default(Guid) that is not equals to Load<QuestParty> result
                var started = new QuestStarted { /*Id = questId,*/ Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var liveAggregate = session.Events.AggregateStream<QuestParty>(questId);
                var inlinedAggregate = session.Load<QuestParty>(questId);
                liveAggregate.Id.ShouldBe(inlinedAggregate.Id);
                inlinedAggregate.ToString().ShouldBe(liveAggregate.ToString());
            }
        }
Example #27
0
        public void should_check_max_event_id_on_append_with_string_identifier()
        {
            StoreOptions(_ => _.Events.StreamIdentity = StreamIdentity.AsString);

            var joined = new MembersJoined {
                Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
            };
            var departed = new MembersDeparted {
                Members = new[] { "Thom" }
            };

            var stream = "First";

            theSession.Events.Append(stream, joined);
            theSession.Events.Append(stream, 2, departed);

            theSession.SaveChanges();

            var state = theSession.Events.FetchStreamState(stream);

            state.Key.ShouldBe(stream);
            state.Version.ShouldBe(2);
        }
        public void delete_stream_by_guid_id()
        {
            var stream1 = Guid.NewGuid();
            var stream2 = Guid.NewGuid();

            using (var session = theStore.LightweightSession())
            {
                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream1, joined, departed);

                var joined2 = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.Append(stream2, joined2, departed2);

                session.SaveChanges();
            }

            theStore.Advanced.Clean.DeleteSingleEventStream(stream1);


            using (var session = theStore.LightweightSession())
            {
                session.Events.QueryAllRawEvents().ToList().All(x => x.StreamId == stream2)
                .ShouldBeTrue();
            }
        }
        public void capture_events_to_a_new_stream_and_fetch_the_events_back()
        {
            var store = DocumentStore.For(_ =>
            {
                _.AutoCreateSchemaObjects = true;

                _.Connection(ConnectionSource.ConnectionString);

                _.Events.EventMappingFor(typeof(MembersJoined));
                _.Events.EventMappingFor(typeof(MembersDeparted));
            });

            store.Advanced.Clean.CompletelyRemoveAll();

            // TODO -- REMOVE THIS WHEN IT IS SMARTER!
            store.EventStore.RebuildEventStoreSchema();

            using (var session = store.OpenSession())
            {
                var joined = new MembersJoined {
                    Members = new string[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                var id = session.Events.StartStream <Quest>(joined, departed);
                session.SaveChanges();

                var streamEvents = session.Events.FetchStream <Quest>(id);

                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).ShouldBeOfType <MembersJoined>();
                streamEvents.ElementAt(1).ShouldBeOfType <MembersDeparted>();
            }
        }
        public void capture_events_to_a_new_stream_and_fetch_the_events_back(DocumentTracking sessionType)
        {
            var store = InitStore();


            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: start-stream-with-aggregate-type
                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                var id = session.Events.StartStream<Quest>(joined, departed);
                session.SaveChanges();
                // ENDSAMPLE

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
            }
        }
        public void capture_events_to_a_new_stream_and_fetch_the_events_back_sync_with_linq(DocumentTracking sessionType)
        {
            var store = InitStore();


            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: start-stream-with-aggregate-type
                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                var id = session.Events.StartStream<Quest>(joined, departed);
                session.SaveChanges();
                // ENDSAMPLE

                var streamEvents = session.Events.QueryAllRawEvents()
                    .Where(x => x.StreamId == id).OrderBy(x => x.Version).ToList();


                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);

                streamEvents.Each(e => e.Timestamp.ShouldNotBe(default(DateTimeOffset)));
            }
        }
Example #32
0
        public void capture_events_to_a_new_stream_and_fetch_the_events_back_with_stream_id_provided_in_another_database_schema(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");

            using (var session = store.OpenSession(sessionType))
            {
                var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                var id = Guid.NewGuid();
                session.Events.StartStream<Quest>(id, joined, departed);
                session.SaveChanges();

                var streamEvents = session.Events.FetchStream(id);

                streamEvents.Count().ShouldBe(2);
                streamEvents.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                streamEvents.ElementAt(0).Version.ShouldBe(1);
                streamEvents.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
                streamEvents.ElementAt(1).Version.ShouldBe(2);
            }
        }
Example #33
0
 protected bool Equals(MembersJoined other)
 {
     return(QuestId.Equals(other.QuestId) && Day == other.Day && Location == other.Location && Members.SequenceEqual(other.Members));
 }
Example #34
0
        public void open_persisted_stream_in_new_store_with_same_settings(DocumentTracking sessionType, TenancyStyle tenancyStyle, string[] tenants)
        {
            var store   = InitStore(tenancyStyle);
            var questId = Guid.NewGuid();

            When.CalledForEach(tenants, (tenantId, index) =>
            {
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                    var started = new QuestStarted {
                        Id = questId, Name = "Destroy the One Ring"
                    };
                    var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                    session.Events.StartStream <Quest>(questId, started, joined1);
                    session.SaveChanges();
                }

                // events-aggregate-on-the-fly - works with same store
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    // questId is the id of the stream
                    var party = session.Events.AggregateStream <QuestParty>(questId);

                    party.Id.ShouldBe(questId);
                    party.ShouldNotBeNull();

                    var party_at_version_3 = session.Events
                                             .AggregateStream <QuestParty>(questId, 3);

                    party_at_version_3.ShouldNotBeNull();

                    var party_yesterday = session.Events
                                          .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                    party_yesterday.ShouldBeNull();
                }

                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var party = session.Load <QuestParty>(questId);
                    party.Id.ShouldBe(questId);
                }

                var newStore = InitStore(tenancyStyle, false);

                //Inline is working
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var party = session.Load <QuestParty>(questId);
                    SpecificationExtensions.ShouldNotBeNull(party);
                }
                //GetAll
                using (var session = store.OpenSession(tenantId, sessionType))
                {
                    var parties = session.Events.QueryRawEventDataOnly <QuestParty>().ToArray();
                    foreach (var party in parties)
                    {
                        SpecificationExtensions.ShouldNotBeNull(party);
                    }
                }
                //This AggregateStream fail with NPE
                using (var session = newStore.OpenSession(tenantId, sessionType))
                {
                    // questId is the id of the stream
                    var party = session.Events.AggregateStream <QuestParty>(questId);//Here we get NPE
                    party.Id.ShouldBe(questId);

                    var party_at_version_3 = session.Events
                                             .AggregateStream <QuestParty>(questId, 3);
                    party_at_version_3.Id.ShouldBe(questId);

                    var party_yesterday = session.Events
                                          .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                    party_yesterday.ShouldBeNull();
                }
            }).ShouldThrowIf(
                (tenancyStyle == TenancyStyle.Single && tenants.Length > 1) || (tenancyStyle == TenancyStyle.Conjoined && tenants.SequenceEqual(SameTenants))
                );
        }
        public void capture_events()
        {
// SAMPLE: event-store-quickstart            
var store = DocumentStore.For(_ =>
{
    _.Connection(ConnectionSource.ConnectionString);
    _.Events.InlineProjections.AggregateStreamsWith<QuestParty>();
});

var questId = Guid.NewGuid();

using (var session = store.OpenSession())
{
    var started = new QuestStarted {Name = "Destroy the One Ring"};
    var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

    // Start a brand new stream and commit the new events as 
    // part of a transaction
    session.Events.StartStream<Quest>(questId, started, joined1);
    session.SaveChanges();

    // Append more events to the same stream
    var joined2 = new MembersJoined(3, "Buckland", "Merry", "Pippen");
    var joined3 = new MembersJoined(10, "Bree", "Aragorn");
    var arrived = new ArrivedAtLocation { Day = 15, Location = "Rivendell" };
    session.Events.Append(questId, joined2, joined3, arrived);
    session.SaveChanges();
}
// ENDSAMPLE

// SAMPLE: events-fetching-stream
using (var session = store.OpenSession())
{
    var events = session.Events.FetchStream(questId);
    events.Each(evt =>
    {
        Console.WriteLine($"{evt.Version}.) {evt.Data}");
    });
}
// ENDSAMPLE

// SAMPLE: events-aggregate-on-the-fly
using (var session = store.OpenSession())
{
    // questId is the id of the stream
    var party = session.Events.AggregateStream<QuestParty>(questId);
    Console.WriteLine(party);

    var party_at_version_3 = session.Events
        .AggregateStream<QuestParty>(questId, 3);


    var party_yesterday = session.Events
        .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
}
// ENDSAMPLE


using (var session = store.OpenSession())
{
    var party = session.Load<QuestParty>(questId);
    Console.WriteLine(party);
}


        }
        // ENDSAMPLE

        // SAMPLE: using_live_transformed_events
        public void using_live_transformed_events(IDocumentSession session)
        {
            var started = new QuestStarted { Name = "Find the Orb" };
            var joined = new MembersJoined { Day = 2, Location = "Faldor's Farm", Members = new string[] { "Garion", "Polgara", "Belgarath" } };
            var slayed1 = new MonsterSlayed { Name = "Troll" };
            var slayed2 = new MonsterSlayed { Name = "Dragon" };

            MembersJoined joined2 = new MembersJoined { Day = 5, Location = "Sendaria", Members = new string[] { "Silk", "Barak" } };


            session.Events.StartStream<Quest>(started, joined, slayed1, slayed2);
            session.SaveChanges();

            // Our MonsterDefeated documents are created inline
            // with the SaveChanges() call above and are available
            // for querying
            session.Query<MonsterDefeated>().Count()
                .ShouldBe(2);
        }
Example #37
0
 public void MembersJoinedAt(string[] names, int day, string location)
 {
     var @event = new MembersJoined {Day = day, Members = names, Location = location};
     _events.Add(@event);
 }
Example #38
0
        public void get_correct_events_from_single_stream()
        {
            var listener = new StubDocumentSessionListener();
            var store = InitStore(listener);

            var id = Guid.NewGuid();
            var started = new QuestStarted();

            using (var session = store.LightweightSession())
            {
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                    .GetEvents()
                    .ToList();

                events.Count.ShouldBe(1);
                events.ElementAt(0).Data.ShouldBeOfType<QuestStarted>();
            }

            using (var session = store.LightweightSession())
            {
                var joined = new MembersJoined { Members = new string[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                session.Events.Append(id, joined, departed);

                session.SaveChanges();

                var events = listener.AfterCommitSession.LastCommit
                    .GetEvents()
                    .ToList();

                events.Count.ShouldBe(2);
                events.ElementAt(0).Data.ShouldBeOfType<MembersJoined>();
                events.ElementAt(1).Data.ShouldBeOfType<MembersDeparted>();
            }
        }
Example #39
0
        public void open_persisted_stream_in_new_store_with_same_settings()
        {
            var store   = InitStore("event_store");
            var questId = "Sixth";

            using (var session = store.OpenSession())
            {
                //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                var started = new QuestStarted {
                    Name = "Destroy the One Ring"
                };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream <Quest>(questId, started, joined1);
                session.SaveChanges();
            }

            // events-aggregate-on-the-fly - works with same store
            using (var session = store.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestPartyWithStringIdentifier>(questId);

                party.ShouldNotBeNull();

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestPartyWithStringIdentifier>(questId, 3);

                party_at_version_3.ShouldNotBeNull();

                var party_yesterday = session.Events
                                      .AggregateStream <QuestPartyWithStringIdentifier>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldNotBeNull();
            }

            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestPartyWithStringIdentifier>(questId);
                party.ShouldNotBeNull();
            }

            var newStore = InitStore("event_store", false);

            //Inline is working
            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestPartyWithStringIdentifier>(questId);
                party.ShouldNotBeNull();
            }
            //GetAll
            using (var session = store.OpenSession())
            {
                var parties = session.Events.QueryRawEventDataOnly <QuestPartyWithStringIdentifier>().ToArray();
                foreach (var party in parties)
                {
                    party.ShouldNotBeNull();
                }
            }
            //This AggregateStream fail with NPE
            using (var session = newStore.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestPartyWithStringIdentifier>(questId);//Here we get NPE
                party.ShouldNotBeNull();

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestPartyWithStringIdentifier>(questId, 3);
                party_at_version_3.ShouldNotBeNull();

                var party_yesterday = session.Events
                                      .AggregateStream <QuestPartyWithStringIdentifier>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldNotBeNull();
            }
        }
        public void assert_on_max_event_id_on_event_stream_append(
            DocumentTracking sessionType)
        {
            var store = InitStore("event_store");

            var id = Guid.NewGuid();
            var started = new QuestStarted();
            
            using (var session = store.OpenSession(sessionType))
            {
                // SAMPLE: append-events-assert-on-eventid
                session.Events.StartStream<Quest>(id, started);
                session.SaveChanges();

                var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } };
                var departed = new MembersDeparted { Members = new[] { "Thom" } };

                // Events are appended into the stream only if the maximum event id for the stream
                // would be 3 after the append operation.
                session.Events.Append(id, 3, joined, departed);

                session.SaveChanges();
                // ENDSAMPLE
            }
        }
        public void open_persisted_stream_in_new_store_with_same_settings(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                var started = new QuestStarted { Id = questId, Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();
            }

            // events-aggregate-on-the-fly - works with same store
            using (var session = store.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream<QuestParty>(questId);

                party.Id.ShouldBe(questId);
                party.ShouldNotBeNull();

                var party_at_version_3 = session.Events
                    .AggregateStream<QuestParty>(questId, 3);

                party_at_version_3.ShouldNotBeNull();

                var party_yesterday = session.Events
                    .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.ShouldNotBeNull();
            }

            using (var session = store.OpenSession())
            {
                var party = session.Load<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }

            var newStore = InitStore("event_store", false);

            //Inline is working
            using (var session = store.OpenSession())
            {
                var party = session.Load<QuestParty>(questId);
                party.ShouldNotBeNull();
            }
            //GetAll
            using (var session = store.OpenSession())
            {
                var parties = session.Events.QueryRawEventDataOnly<QuestParty>().ToArray();
                foreach (var party in parties)
                {
                    party.ShouldNotBeNull();
                }
            }
            //This AggregateStream fail with NPE
            using (var session = newStore.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream<QuestParty>(questId);//Here we get NPE
                party.Id.ShouldBe(questId);

                var party_at_version_3 = session.Events
                    .AggregateStream<QuestParty>(questId, 3);
                party_at_version_3.Id.ShouldBe(questId);

                var party_yesterday = session.Events
                    .AggregateStream<QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                party_yesterday.Id.ShouldBe(questId);
            }
        }
        public void query_before_saving(DocumentTracking sessionType)
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                var parties = session.Query<QuestParty>().ToArray();
                parties.Length.ShouldBeLessThanOrEqualTo(0);
            }

            //This SaveChanges will fail with missing method (ro collection configured?)
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted { Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                session.SaveChanges();

                var party = session.Events.AggregateStream<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }
        }
        public async Task aggregate_stream_async_has_the_id()
        {
            var store = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                var parties = await session.Query<QuestParty>().ToListAsync();
                parties.Count.ShouldBeLessThanOrEqualTo(0);
            }

            //This SaveChanges will fail with missing method (ro collection configured?)
            using (var session = store.OpenSession())
            {
                var started = new QuestStarted { Name = "Destroy the One Ring" };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream<Quest>(questId, started, joined1);
                await session.SaveChangesAsync();

                var party = await session.Events.AggregateStreamAsync<QuestParty>(questId);
                party.Id.ShouldBe(questId);
            }
        }
        public void open_persisted_stream_in_new_store_with_same_settings(DocumentTracking sessionType)
        {
            var store   = InitStore("event_store");
            var questId = Guid.NewGuid();

            using (var session = store.OpenSession())
            {
                //Note "Id = questId" @see live_aggregate_equals_inlined_aggregate...
                var started = new QuestStarted {
                    Id = questId, Name = "Destroy the One Ring"
                };
                var joined1 = new MembersJoined(1, "Hobbiton", "Frodo", "Merry");

                session.Events.StartStream(questId, started, joined1);
                session.SaveChanges();
            }

            // events-aggregate-on-the-fly - works with same store
            using (var session = store.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestParty>(questId);

                ShouldBeTestExtensions.ShouldBe(party.Id, questId);
                SpecificationExtensions.ShouldNotBeNull(party);

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestParty>(questId, 3);

                SpecificationExtensions.ShouldNotBeNull(party_at_version_3);

                var party_yesterday = session.Events
                                      .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                SpecificationExtensions.ShouldNotBeNull(party_yesterday);
            }

            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestParty>(questId);
                ShouldBeTestExtensions.ShouldBe(party.Id, questId);
            }

            var newStore = InitStore("event_store", false);

            //Inline is working
            using (var session = store.OpenSession())
            {
                var party = session.Load <QuestParty>(questId);
                SpecificationExtensions.ShouldNotBeNull(party);
            }
            //GetAll
            using (var session = store.OpenSession())
            {
                var parties = Enumerable.ToArray <QuestParty>(session.Events.QueryRawEventDataOnly <QuestParty>());
                foreach (var party in parties)
                {
                    party.ShouldNotBeNull();
                }
            }
            //This AggregateStream fail with NPE
            using (var session = newStore.OpenSession())
            {
                // questId is the id of the stream
                var party = session.Events.AggregateStream <QuestParty>(questId);//Here we get NPE
                ShouldBeTestExtensions.ShouldBe(party.Id, questId);

                var party_at_version_3 = session.Events
                                         .AggregateStream <QuestParty>(questId, 3);
                ShouldBeTestExtensions.ShouldBe(party_at_version_3.Id, questId);

                var party_yesterday = session.Events
                                      .AggregateStream <QuestParty>(questId, timestamp: DateTime.UtcNow.AddDays(-1));
                ShouldBeTestExtensions.ShouldBe(party_yesterday.Id, questId);
            }
        }
        public void running_synchronously()
        {
            var logger = new RecordingSessionLogger();

            Guid streamId = Guid.NewGuid();

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined = new MembersJoined {
                    Members = new[] { "Rand", "Matt", "Perrin", "Thom" }
                };
                var departed = new MembersDeparted {
                    Members = new[] { "Thom" }
                };

                session.Events.StartStream <Quest>(streamId, joined, departed);
                session.SaveChanges();

                var events = logger.LastCommit.GetEvents().ToArray();
                events.Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(1, 2);

                events.Each(x => SpecificationExtensions.ShouldBeGreaterThan(x.Sequence, 0L));

                events.Select(x => x.Sequence).Distinct().Count().ShouldBe(2);
            }

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined2 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed2 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined2, departed2);
                session.SaveChanges();

                logger.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(3, 4);
            }

            using (var session = theStore.OpenSession())
            {
                session.Logger = logger;

                var joined3 = new MembersJoined {
                    Members = new[] { "Egwene" }
                };
                var departed3 = new MembersDeparted {
                    Members = new[] { "Perrin" }
                };

                session.Events.Append(streamId, joined3, departed3);
                session.SaveChanges();

                logger.LastCommit.GetEvents().Select(x => x.Version)
                .ShouldHaveTheSameElementsAs(5, 6);
            }
        }