public async Task SaveRecipe_NoId()
        {
            var    moq      = new Mock <IDynamoDBContext>();
            Recipe callback = null;

            moq.SetupAllProperties();
            moq.Setup(s =>
                      s.SaveAsync(It.IsAny <Recipe>(), It.IsAny <CancellationToken>()))
            .Callback((Recipe r, CancellationToken ct) => { callback = r; });

            var recipeService = new DynamoRecipeService(null);

            DynamoRecipeService.Initialized = true;
            recipeService.SetupMockContext(moq.Object);

            var recipe = new Recipe
            {
                Name           = "Test Recipe",
                LastUpdateTime = DateTime.UtcNow,
                UserId         = "123",
                CookTimeMins   = 11,
                PrepTimeMins   = 22,
                Servings       = 99
            };

            var result = await recipeService.SaveRecipe(recipe);

            Assert.True(result);
            Assert.NotNull(callback);
            Assert.NotEqual(default, callback.EntityId);
Пример #2
0
        public async Task <IActionResult> Post(string userId, [FromBody] RecipeModel value)
        {
            Console.WriteLine(value);
            if (value == null)
            {
                Logger.LogWarning("Failed to parse a recipe on post.");
                return(new BadRequestResult());
            }

            if (value.UserId != userId)
            {
                Logger.LogWarning($"Recipe had userid of {value.UserId} and it was posted to {userId}");
                return(new BadRequestResult());
            }

            try
            {
                var result = await RecipeService.SaveRecipe(new Recipe(value));

                if (result)
                {
                    return(new OkResult());
                }
                else
                {
                    Logger.LogWarning($"Failed to save the recipe into Dynamo for some reason.");
                    return(new BadRequestResult());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new BadRequestResult());
            }
        }
        public async void TestSave_Create_Delete()
        {
            var moq = new Moq.Mock <IAmazonDynamoDB>();

            moq.SetupAllProperties();
            moq.Setup(s =>
                      s.DeleteItemAsync(It.IsAny <DeleteItemRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new DeleteItemResponse {
                HttpStatusCode = System.Net.HttpStatusCode.OK
            });

            moq.Setup(s =>
                      s.PutItemAsync(It.IsAny <PutItemRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new PutItemResponse {
                HttpStatusCode = System.Net.HttpStatusCode.OK
            });

            moq.Setup(s =>
                      s.QueryAsync(It.IsAny <QueryRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new QueryResponse {
                HttpStatusCode = System.Net.HttpStatusCode.OK,
            });

            var recipeService = new DynamoRecipeService(moq.Object);

            var recipe = new Recipe()
            {
                Name           = "Test Recipe",
                LastUpdateTime = DateTime.UtcNow,
                UserId         = "123",
                RecipeId       = 456,
                CookTimeMins   = 11,
                PrepTimeMins   = 22,
                Servings       = 99
            };

            try
            {
                var saved = await recipeService.SaveRecipe(recipe);

                Assert.True(saved);

                //var retrieved = await recipeService.RetrieveRecipe(recipe.UserId, recipe.RecipeId);
                //var equal = recipe.Equals(retrieved);
                //Assert.True(equal);
            }
            finally
            {
                //var deleted = await recipeService.DeleteRecipe(recipe);
                //Assert.True(deleted);
            }
        }