public void GetRecipe_ParametersMatchExpectedValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipe = new FakeRecipe {
            }.Generate();

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipe);
                context.SaveChanges();

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                //Assert
                var recipeById = context.Recipes.FirstOrDefault(r => r.RecipeId == fakeRecipe.RecipeId);

                recipeById.Should().BeEquivalentTo(fakeRecipe);
                recipeById.RecipeId.Should().Be(fakeRecipe.RecipeId);
                recipeById.Title.Should().Be(fakeRecipe.Title);
                recipeById.Directions.Should().Be(fakeRecipe.Directions);
            }
        }
Exemplo n.º 2
0
        public void AddRecipe_NewRecordAddedWithProperValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <RecipeDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipe = new FakeRecipe {
            }.Generate();

            //Act
            using (var context = new RecipeDbContext(dbOptions))
            {
                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                service.AddRecipe(fakeRecipe);

                context.SaveChanges();
            }

            //Assert
            using (var context = new RecipeDbContext(dbOptions))
            {
                context.Recipes.Count().Should().Be(1);

                var recipeById = context.Recipes.FirstOrDefault(r => r.RecipeId == fakeRecipe.RecipeId);

                recipeById.Should().BeEquivalentTo(fakeRecipe);
                recipeById.RecipeId.Should().Be(fakeRecipe.RecipeId);
                recipeById.RecipeTextField1.Should().Be(fakeRecipe.RecipeTextField1);
                recipeById.RecipeTextField2.Should().Be(fakeRecipe.RecipeTextField2);
                recipeById.RecipeDateField1.Should().Be(fakeRecipe.RecipeDateField1);
            }
        }
        public async Task BadPatchRecipeReturns400BadRequest()
        {
            //Arrange
            var lookupVal = "Easily Identified Value For Test"; // don't know the id at this scope, so need to have another value to lookup
            var fakeRecipeOne = new FakeRecipe {
            }.Generate();

            var appFactory = _factory;

            using (var scope = appFactory.Services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <RecipeDbContext>();
                context.Database.EnsureCreated();

                context.Recipes.RemoveRange(context.Recipes);
                context.Recipes.AddRange(fakeRecipeOne);
                context.SaveChanges();
            }

            var client = appFactory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            var manuallyCreatedInvalidPatchDoc = "[{\"value\":\"" + lookupVal + "\",\"path\":\"/RecipeIntField1\",\"op\":\"replace\"}]";

            // Act
            // get the value i want to update. assumes I can use sieve for this field. if this is not an option, just use something else
            var getResult = await client.GetAsync($"api/v1/recipes/?filters=RecipeTextField1=={fakeRecipeOne.RecipeTextField1}")
                            .ConfigureAwait(false);

            var getResponseContent = await getResult.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false);

            var getResponse = JsonConvert.DeserializeObject <IEnumerable <RecipeDto> >(getResponseContent);
            var id          = getResponse.FirstOrDefault().RecipeId;

            // patch it
            var method       = new HttpMethod("PATCH");
            var patchRequest = new HttpRequestMessage(method, $"api/v1/recipes/{id}")
            {
                Content = new StringContent(manuallyCreatedInvalidPatchDoc,
                                            Encoding.Unicode, "application/json")
            };
            var patchResult = await client.SendAsync(patchRequest)
                              .ConfigureAwait(false);

            // Assert
            patchResult.StatusCode.Should().Be(400);
        }
        public void GetRecipes_FilterListWithExact(string filters)
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <RecipeDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipeOne = new FakeRecipe {
            }.Generate();

            fakeRecipeOne.RecipeTextField1 = "Alpha";
            fakeRecipeOne.RecipeTextField2 = "Bravo";
            fakeRecipeOne.RecipeIntField1  = 5;

            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();

            fakeRecipeTwo.RecipeTextField1 = "Charlie";
            fakeRecipeTwo.RecipeTextField2 = "Delta";
            fakeRecipeTwo.RecipeIntField1  = 6;

            var fakeRecipeThree = new FakeRecipe {
            }.Generate();

            fakeRecipeThree.RecipeTextField1 = "Echo";
            fakeRecipeThree.RecipeTextField2 = "Foxtrot";
            fakeRecipeThree.RecipeIntField1  = 7;

            //Act
            using (var context = new RecipeDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo, fakeRecipeThree);
                context.SaveChanges();

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                var recipeRepo = service.GetRecipes(new RecipeParametersDto {
                    Filters = filters
                });

                //Assert
                recipeRepo.Should()
                .HaveCount(1);

                context.Database.EnsureDeleted();
            }
        }
        public void GetRecipes_SearchQueryReturnsExpectedRecordCount(string queryString, int expectedCount)
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipeOne = new FakeRecipe {
            }.Generate();

            fakeRecipeOne.Title      = "Alpha";
            fakeRecipeOne.Directions = "Bravo";

            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();

            fakeRecipeTwo.Title      = "Hartsfield";
            fakeRecipeTwo.Directions = "White";

            var fakeRecipeThree = new FakeRecipe {
            }.Generate();

            fakeRecipeThree.Title      = "Bravehart";
            fakeRecipeThree.Directions = "Jonfav";

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo, fakeRecipeThree);
                context.SaveChanges();

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                var recipeRepo = service.GetRecipes(new RecipeParametersDto {
                    QueryString = queryString
                });

                //Assert
                recipeRepo.Should()
                .HaveCount(expectedCount);

                context.Database.EnsureDeleted();
            }
        }
        public void GetRecipes_ListSortedInDescOrder()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipeOne = new FakeRecipe {
            }.Generate();

            fakeRecipeOne.Title = "Bravo";

            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();

            fakeRecipeTwo.Title = "Alpha";

            var fakeRecipeThree = new FakeRecipe {
            }.Generate();

            fakeRecipeThree.Title = "Charlie";

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo, fakeRecipeThree);
                context.SaveChanges();

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                var recipeRepo = service.GetRecipes(new RecipeParametersDto {
                    SortOrder = "-Title"
                });

                //Assert
                recipeRepo.Should()
                .ContainInOrder(fakeRecipeThree, fakeRecipeOne, fakeRecipeTwo);

                context.Database.EnsureDeleted();
            }
        }
Exemplo n.º 7
0
        public void DeleteRecipe_ReturnsProperCount()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipeOne = new FakeRecipe {
            }.Generate();
            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();
            var fakeRecipeThree = new FakeRecipe {
            }.Generate();

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo, fakeRecipeThree);

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));
                service.DeleteRecipe(fakeRecipeTwo);

                context.SaveChanges();

                //Assert
                var recipeList = context.Recipes.ToList();

                recipeList.Should()
                .NotBeEmpty()
                .And.HaveCount(2);

                recipeList.Should().ContainEquivalentOf(fakeRecipeOne);
                recipeList.Should().ContainEquivalentOf(fakeRecipeThree);
                Assert.DoesNotContain(recipeList, r => r == fakeRecipeTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async Task GetRecipes_ReturnsSuccessCodeAndResourceWithAccurateFields()
        {
            var fakeRecipeOne = new FakeRecipe {
            }.Generate();
            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();

            var appFactory = _factory;

            using (var scope = appFactory.Services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <RecipeDbContext>();
                context.Database.EnsureCreated();

                //context.Recipes.RemoveRange(context.Recipes);
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo);
                context.SaveChanges();
            }

            var client = appFactory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            var result = await client.GetAsync($"api/v1/recipes")
                         .ConfigureAwait(false);

            var responseContent = await result.Content.ReadAsStringAsync()
                                  .ConfigureAwait(false);

            var response = JsonConvert.DeserializeObject <IEnumerable <RecipeDto> >(responseContent);

            // Assert
            result.StatusCode.Should().Be(200);
            response.Should().ContainEquivalentOf(fakeRecipeOne, options =>
                                                  options.ExcludingMissingMembers());
            response.Should().ContainEquivalentOf(fakeRecipeTwo, options =>
                                                  options.ExcludingMissingMembers());
        }
        public void GetRecipes_ReturnExpectedPageSize()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"RecipeDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeRecipeOne = new FakeRecipe {
            }.Generate();
            var fakeRecipeTwo = new FakeRecipe {
            }.Generate();
            var fakeRecipeThree = new FakeRecipe {
            }.Generate();

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Recipes.AddRange(fakeRecipeOne, fakeRecipeTwo, fakeRecipeThree);
                context.SaveChanges();

                var service = new RecipeRepository(context, new SieveProcessor(sieveOptions));

                var recipeRepo = service.GetRecipes(new RecipeParametersDto {
                    PageSize = 2
                });

                //Assert
                recipeRepo.Should()
                .NotBeEmpty()
                .And.HaveCount(2);

                recipeRepo.Should().ContainEquivalentOf(fakeRecipeOne);
                recipeRepo.Should().ContainEquivalentOf(fakeRecipeTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async Task PatchRecipeReturns204AndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <RecipeProfile>();
            }).CreateMapper();

            var lookupVal = "Easily Identified Value For Test"; // don't know the id at this scope, so need to have another value to lookup
            var fakeRecipeOne = new FakeRecipe {
            }.Generate();
            var expectedFinalObject = mapper.Map <RecipeDto>(fakeRecipeOne);

            expectedFinalObject.RecipeTextField1 = lookupVal;

            var appFactory = _factory;

            using (var scope = appFactory.Services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <RecipeDbContext>();
                context.Database.EnsureCreated();

                context.Recipes.RemoveRange(context.Recipes);
                context.Recipes.AddRange(fakeRecipeOne);
                context.SaveChanges();
            }

            var client = appFactory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            var patchDoc = new JsonPatchDocument <RecipeForUpdateDto>();

            patchDoc.Replace(r => r.RecipeTextField1, lookupVal);
            var serializedRecipeToUpdate = JsonConvert.SerializeObject(patchDoc);

            // Act
            // get the value i want to update. assumes I can use sieve for this field. if this is not an option, just use something else
            var getResult = await client.GetAsync($"api/v1/recipes/?filters=RecipeTextField1=={fakeRecipeOne.RecipeTextField1}")
                            .ConfigureAwait(false);

            var getResponseContent = await getResult.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false);

            var getResponse = JsonConvert.DeserializeObject <IEnumerable <RecipeDto> >(getResponseContent);
            var id          = getResponse.FirstOrDefault().RecipeId;

            // patch it
            var method       = new HttpMethod("PATCH");
            var patchRequest = new HttpRequestMessage(method, $"api/v1/recipes/{id}")
            {
                Content = new StringContent(serializedRecipeToUpdate,
                                            Encoding.Unicode, "application/json")
            };
            var patchResult = await client.SendAsync(patchRequest)
                              .ConfigureAwait(false);

            // get it again to confirm updates
            var checkResult = await client.GetAsync($"api/v1/recipes/{id}")
                              .ConfigureAwait(false);

            var checkResponseContent = await checkResult.Content.ReadAsStringAsync()
                                       .ConfigureAwait(false);

            var checkResponse = JsonConvert.DeserializeObject <RecipeDto>(checkResponseContent);

            // Assert
            patchResult.StatusCode.Should().Be(204);
            checkResponse.Should().BeEquivalentTo(expectedFinalObject, options =>
                                                  options.ExcludingMissingMembers());
        }