public ConnectedProjectionScenarioWrapper(ConnectedProjectionScenario <TContext> inner, Func <TContext> contextFactory, Random random, Action <string> logAction)
 {
     _inner          = inner;
     _contextFactory = contextFactory;
     _random         = random;
     _logAction      = logAction;
 }
Example #2
0
 public void SetUp()
 {
     _resolver = Resolve.
                 WhenEqualToHandlerMessageType(new ConnectedProjectionHandler <object> [0]);
     _messages = new[] { new object(), new object() };
     _sut      = new ConnectedProjectionScenario <object>(_resolver).Given(_messages);
 }
        public static async Task ExpectNone(this ConnectedProjectionScenario <ProductContext> scenario)
        {
            var database = Guid.NewGuid().ToString("N");

            var specification = scenario.Verify(async context =>
            {
                var actualRecords = await context.AllRecords();
                return(actualRecords.Length == 0
                    ? VerificationResult.Pass()
                    : VerificationResult.Fail($"Expected 0 records but found {actualRecords.Length}."));
            });

            using (var context = CreateContextFor(database))
            {
                var projector = new ConnectedProjector <ProductContext>(specification.Resolver);
                foreach (var message in specification.Messages)
                {
                    var envelope = new Envelope(message, new Dictionary <string, object>()).ToGenericEnvelope();
                    await projector.ProjectAsync(context, envelope);
                }
                await context.SaveChangesAsync();
            }

            using (var context = CreateContextFor(database))
            {
                var result = await specification.Verification(context, CancellationToken.None);

                if (result.Failed)
                {
                    throw specification.CreateFailedScenarioExceptionFor(result);
                }
            }
        }
Example #4
0
        public static Task ExpectNone(this ConnectedProjectionScenario <IAsyncDocumentSession> scenario)
        {
            return(scenario
                   .Verify(async session =>
            {
                using (var streamer = await session.Advanced.StreamAsync <RavenJObject>(Etag.Empty))
                {
                    if (await streamer.MoveNextAsync())
                    {
                        var storedDocumentIdentifiers = new List <string>();
                        do
                        {
                            storedDocumentIdentifiers.Add(streamer.Current.Key);
                        } while (await streamer.MoveNextAsync());

                        return VerificationResult.Fail(
                            string.Format("Expected no documents, but found {0} document(s) ({1}).",
                                          storedDocumentIdentifiers.Count,
                                          string.Join(",", storedDocumentIdentifiers)));
                    }
                    return VerificationResult.Pass();
                }
            })
                   .Assert());
        }
        //DO NOT MOVE TO LIB
        public ConnectedProjectionScenarioWrapper <TContext> Project <T>(IGenerator <T> eventGenerator)
        {
            var @event = eventGenerator.Generate(_random);

            _logAction($"Projecting event\r\n{@event.ToLoggableString(Formatting.Indented)}");
            _inner = _inner.Given(new Envelope <T>(new Envelope(@event, new ConcurrentDictionary <string, object>())));

            return(this);
        }
Example #6
0
        public static Task Expect(this ConnectedProjectionScenario <MemoryCache> scenario, params CacheItem[] items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            if (items.Length == 0)
            {
                return(scenario.ExpectNone());
            }

            return(scenario.
                   Verify(cache =>
            {
                if (cache.GetCount() != items.Length)
                {
                    if (cache.GetCount() == 0)
                    {
                        return Task.FromResult(
                            VerificationResult.Fail(
                                string.Format("Expected {0} cache item(s), but found 0 cache items.",
                                              items.Length)));
                    }

                    return Task.FromResult(
                        VerificationResult.Fail(
                            string.Format("Expected {0} cache item(s), but found {1} cache item(s) ({2}).",
                                          items.Length,
                                          cache.GetCount(),
                                          string.Join(",", cache.Select(pair => pair.Key)))));
                }
                if (!cache.Select(pair => cache.GetCacheItem(pair.Key)).SequenceEqual(items, new CacheItemEqualityComparer()))
                {
                    var builder = new StringBuilder();
                    builder.AppendLine("Expected the following cache items:");
                    foreach (var expectedItem in items)
                    {
                        builder.AppendLine(expectedItem.Key + ": " + JToken.FromObject(expectedItem.Value).ToString());
                    }
                    builder.AppendLine();
                    builder.AppendLine("But found the following cache items:");
                    foreach (var actualItem in cache)
                    {
                        builder.AppendLine(actualItem.Key + ": " + JToken.FromObject(actualItem.Value).ToString());
                    }
                    return Task.FromResult(VerificationResult.Fail(builder.ToString()));
                }
                return Task.FromResult(VerificationResult.Pass());
            }).
                   Assert());
        }
        public static async Task Expect(
            this ConnectedProjectionScenario <ProductContext> scenario,
            params object[] records)
        {
            var database = Guid.NewGuid().ToString("N");

            var specification = scenario.Verify(async context =>
            {
                var comparisonConfig = new ComparisonConfig {
                    MaxDifferences = 5
                };
                var comparer      = new CompareLogic(comparisonConfig);
                var actualRecords = await context.AllRecords();
                var result        = comparer.Compare(
                    actualRecords,
                    records
                    );

                return(result.AreEqual
                    ? VerificationResult.Pass()
                    : VerificationResult.Fail(result.CreateDifferenceMessage(actualRecords, records)));
            });

            using (var context = CreateContextFor(database))
            {
                var projector = new ConnectedProjector <ProductContext>(specification.Resolver);
                var position  = 0L;
                foreach (var message in specification.Messages)
                {
                    var envelope = new Envelope(message, new Dictionary <string, object> {
                        { "Position", position }
                    }).ToGenericEnvelope();
                    await projector.ProjectAsync(context, envelope);

                    position++;
                }

                await context.SaveChangesAsync();
            }

            using (var context = CreateContextFor(database))
            {
                var result = await specification.Verification(context, CancellationToken.None);

                if (result.Failed)
                {
                    throw specification.CreateFailedScenarioExceptionFor(result);
                }
            }
        }
Example #8
0
 public static Task ExpectNone(this ConnectedProjectionScenario <MemoryCache> scenario)
 {
     return(scenario.
            Verify(cache =>
     {
         if (cache.GetCount() != 0)
         {
             return Task.FromResult(
                 VerificationResult.Fail(
                     string.Format("Expected no cache items, but found {0} cache item(s) ({1}).",
                                   cache.GetCount(),
                                   string.Join(",", cache.Select(pair => pair.Key)))));
         }
         return Task.FromResult(VerificationResult.Pass());
     }).
            Assert());
 }
 public static Task Expect(
     this ConnectedProjectionScenario <ProductContext> scenario,
     IEnumerable <object> records)
 {
     return(scenario.Expect(records.ToArray()));
 }
 public static Task Expect(
     this ConnectedProjectionScenario <ApiProjectionsContext> scenario,
     ITestOutputHelper testOutputHelper,
     IEnumerable <object> records) => scenario.Expect(testOutputHelper, records.ToArray());
Example #11
0
        public static Task Expect(this ConnectedProjectionScenario <IAsyncDocumentSession> scenario, params object[] documents)
        {
            if (documents == null)
            {
                throw new ArgumentNullException("documents");
            }

            if (documents.Length == 0)
            {
                return(scenario.ExpectNone());
            }
            return(scenario
                   .Verify(async session =>
            {
                using (var streamer = await session.Advanced.StreamAsync <object>(Etag.Empty))
                {
                    var storedDocuments = new List <object>();
                    var storedDocumentIdentifiers = new List <string>();
                    while (await streamer.MoveNextAsync())
                    {
                        storedDocumentIdentifiers.Add(streamer.Current.Key);
                        storedDocuments.Add(streamer.Current.Document);
                    }

                    if (documents.Length != storedDocumentIdentifiers.Count)
                    {
                        if (storedDocumentIdentifiers.Count == 0)
                        {
                            return VerificationResult.Fail(
                                string.Format("Expected {0} document(s), but found 0 documents.",
                                              documents.Length));
                        }
                        return VerificationResult.Fail(
                            string.Format("Expected {0} document(s), but found {1} document(s) ({2}).",
                                          documents.Length,
                                          storedDocumentIdentifiers.Count,
                                          string.Join(",", storedDocumentIdentifiers)));
                    }

                    var expectedDocuments = documents.Select(JToken.FromObject).ToArray();
                    var actualDocuments = storedDocuments.Select(JToken.FromObject).ToArray();

                    if (!expectedDocuments.SequenceEqual(actualDocuments, new JTokenEqualityComparer()))
                    {
                        var builder = new StringBuilder();
                        builder.AppendLine("Expected the following documents:");
                        foreach (var expectedDocument in expectedDocuments)
                        {
                            builder.AppendLine(expectedDocument.ToString());
                        }
                        builder.AppendLine();
                        builder.AppendLine("But found the following documents:");
                        foreach (var actualDocument in actualDocuments)
                        {
                            builder.AppendLine(actualDocument.ToString());
                        }
                        return VerificationResult.Fail(builder.ToString());
                    }
                    return VerificationResult.Pass();
                }
            })
                   .Assert());
        }