public async Task ExecuteAsync_Does_Nothing_With_No_Results()
        {
            var results = Enumerable.Empty <WishResult>();
            var cmd     = new AddWishResultsCommand(results);

            await cmd.ExecuteAsync(_table.Object);

            _table.Verify(t => t.ExecuteBatchAsync(It.IsAny <TableBatchOperation>()), Times.Never());
        }
        public async Task ExecuteAsync_Throws_An_Exception_For_Any_Result_Not_Assigned_To_A_Wish()
        {
            var results = new[] { new WishResult() };
            var cmd     = new AddWishResultsCommand(results);

            var ex = await Record.ExceptionAsync(() => cmd.ExecuteAsync(_table.Object));

            Assert.NotNull(ex);
            var aex = Assert.IsType <ApplicationException>(ex);

            Assert.Equal("One or more wish results haven't been assigned to a wish!", aex.Message);
        }
        public async Task ExecuteAsync_Persists_Results_In_Batches()
        {
            _table.SetupBatch();
            var w = new Wish()
            {
                Name = "I Wish"
            };
            var wr = new WishResult();

            wr.BelongsTo(w);
            var results = new[] { wr };
            var cmd     = new AddWishResultsCommand(results);

            await cmd.ExecuteAsync(_table.Object);

            _table.VerifyBatch();
        }
        public async Task SearchOrchestrationAsync(
            [OrchestrationTrigger] DurableOrchestrationContextBase context,
            [Table(Constants.WishTableName)] CloudTable wishTable,
            [Pushover] IAsyncCollector <PushoverNotification> notifications)
        {
            var model   = context.GetInput <SearchContext>();
            var results = new List <WishResult>();

            foreach (var provider in model.Providers)
            {
                var providerResults = await context.CallSubOrchestratorAsync <IEnumerable <WishResult> >(SubOrchestrator, new SearchProviderContext
                {
                    Provider = provider,
                    Wishes   = model.Wishes
                });

                results.AddRange(providerResults);
            }

            if (results.Any())
            {
                var addResultsCmd = new AddWishResultsCommand(results);
                await wishTable.ExecuteAsync(addResultsCmd);

                var wishNames = results.GroupBy(r => r.WishName)
                                .Select(r => r.Key)
                                .Distinct();

                await notifications.AddAsync(new PushoverNotification
                {
                    Title   = "NZB Wishlist",
                    Message = $"Found new results for {string.Join(", ", wishNames)}"
                });
            }

            var updateWishesCmd = new UpdateLastSearchDateCommand(model.Wishes);
            await wishTable.ExecuteAsync(updateWishesCmd);
        }