Пример #1
0
        public EventStoreTests()
        {
            inMemoryCrudRepository = Substitute.ForPartsOf <InMemoryCrudRepository>();

            eventStreams = new[]
            {
                new EventStream(Guid.NewGuid()),
                new EventStream(Guid.NewGuid()),
                new EventStream(Guid.NewGuid())
            };
            inMemoryCrudRepository.AttachRange(eventStreams);

            eventSerializer = Substitute.For <IEventSerializer>();

            eventSerializer.SerializeEvent(null)
            .ReturnsForAnyArgs(ci => ("{\"bar\":" + ci.ArgAt <Event1>(0).Foo + "}", new VersionedTypeId("EventName", 5)));
            eventSerializer.DeserializeEvent(Arg.Any <string>(), new VersionedTypeId("EventName", 5))
            .Returns(ci => new Event1((int)JObject.Parse(ci.ArgAt <string>(0))["bar"]));
            eventSerializer.SerializeEventMetadata(null)
            .ReturnsForAnyArgs(ci => JsonConvert.SerializeObject(ci.Arg <IReadOnlyDictionary <string, string> >()
                                                                 .Append(new KeyValuePair <string, string>("fakeSer", "true"))
                                                                 .ToDictionary(x => x.Key, x => x.Value)));
            eventSerializer.DeserializeEventMetadata(null)
            .ReturnsForAnyArgs(ci =>
            {
                var json          = JObject.Parse(ci.Arg <string>());
                json["fakeDeser"] = "true";
                return(new JsonMetadata(json));
            });

            FakeClock.Setup();

            eventStreamRows = new[]
            {
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":1}", "EventName", 5, eventStreams[0].Id, 1, Clock.Current.Now, "{\"doh\":\"1\"}"),
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":2}", "EventName", 5, eventStreams[0].Id, 2, Clock.Current.Now, "{\"doh\":\"2\"}"),
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":3}", "EventName", 5, eventStreams[1].Id, 3, Clock.Current.Now, "{\"doh\":\"3\"}"),
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":4}", "EventName", 5, eventStreams[1].Id, 2, Clock.Current.Now, "{\"doh\":\"4\"}"),
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":5}", "EventName", 5, eventStreams[1].Id, 4, Clock.Current.Now, "{\"doh\":\"5\"}"),
                new EventStreamRow(Guid.NewGuid(), "{\"bar\":6}", "EventName", 5, eventStreams[1].Id, 5, Clock.Current.Now, "{\"doh\":\"6\"}")
            };

            expectedStoreRecords = eventStreamRows.Select((x, i) =>
                                                          new FakeEventStoreRecord()
            {
                Event = eventSerializer.DeserializeEvent(x.EventJson, new VersionedTypeId(x.EventName, x.EventVersion)),
                AdditionalMetadata = new Dictionary <string, string>()
                {
                    { "doh", (i + 1).ToString() }, { "fakeDeser", "true" }
                },
                EventId = eventStreamRows[i].Id,
                StreamSequenceNumber = eventStreamRows[i].StreamSequenceNumber
            })
                                   .ToArray();

            inMemoryCrudRepository.AttachRange(eventStreamRows);

            sut = new EventStore(inMemoryCrudRepository, eventSerializer);
        }
Пример #2
0
        private ExternalEventRecord CreateExternalEventRecord(IEventMessage eventMessage)
        {
            Guid eventId = eventMessage.Metadata.GetEventId() ?? throw new InvalidOperationException($"Cannot store an external event ({eventMessage}) without ID");

            (string eventJson, VersionedTypeId typeId) = eventSerializer.SerializeEvent(eventMessage.Event);
            string metadataJson = eventSerializer.SerializeEventMetadata(eventMessage.Metadata);

            return(new ExternalEventRecord(eventId, eventJson, typeId.Name, typeId.Version, metadataJson));
        }