예제 #1
0
        public async Task RecipeUpdate_ClearingAllIngredients_AreDeleted()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Remove all ingredients
                    recipeToUpdate.Ingredients = new List <IngredientBase>();
                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    //Assert.IsFalse(dbrecipe.Ingredients.Any());
                    Assert.AreEqual(0, dbrecipe.Ingredients.Count());
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #2
0
        public async Task RecipeUpdate_UpdatingExistingImageBytes_UpdatesProperly()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 3;
                // Setup context with one recipe, and medias. Specifying separate media path since modifying image physically (to avoid conflict with other tests)
                SetupBasicContext(options, false, recipeId, "RecipeUpdate_UpdatingExistingImageBytes");

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID 4
                    var recipeToUpdate = await service.GetOne(recipeId);

                    // Update media with ID: 1
                    var media = recipeToUpdate.Medias.Where(m => m.Id == 3).First();
                    // overriding ingredient to remove linked/tracked objects, and modifying some properties
                    // TODO: verify why object is being tracked? When we have asNoTracking set
                    recipeToUpdate.Medias.Remove(media);

                    // Load images json and select image 1 (foodMarket.jpg) that we know isn't in Media with ID 1 (foodColor.jpeg)
                    var TestImages = LoadRecipeMediaDtoFromJson(recipeId);
                    // Update properties AND mediabytes
                    recipeToUpdate.Medias.Add(new MediaDto {
                        Id = media.Id, Recipe_Id = media.Recipe_Id, Title = "FoodMarket", Tag = media.Tag, MediaDataUrl = TestImages.First().MediaDataUrl
                    });
                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service       = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    var recipeUpdated = await service.GetOne(recipeId);

                    var image = recipeUpdated.Medias.Where(m => m.Id == 3).First();
                    Assert.AreEqual("FoodMarket", image.Title);

                    // Verify logger wrote INFO specifying that Media with Id 1 had its image overriden
                    this._loggerMock.Verify(l =>
                                            l.Log(LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString().Contains("New image provided")), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())
                                            , Times.Once);
                    //l.Log(LogLevel.Information, 0, It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()), Times.Once);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #3
0
        public async Task RecipeUpdate_AddingNewImagesToRecipeWithNoImages_WorksAndPathCreatedProperly()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 1; // TODO: why is this text conflicting with DeleteMultipleImages ? Not same path nor Recipe ID and diff contexts
                // Setup context with one recipe, no medias
                SetupBasicContext(options, true, recipeId);

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID setup for this test suite
                    var recipeToUpdate = await service.GetOne(recipeId);

                    // loading 3 mediaDtos from json
                    var mediaDtos = LoadRecipeMediaDtoFromJson(recipeId);
                    // insert into recipe that we want to update, to act like incoming media bytes
                    recipeToUpdate.Medias = mediaDtos;

                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);

                    // Get recipe again from DB with new medias
                    var recipeToUpdate = await service.GetOne(recipeId);

                    Assert.AreEqual(3, recipeToUpdate.Medias.Count());
                    string userRecipePath; // not needed so we override
                    foreach (var i in recipeToUpdate.Medias)
                    {
                        var savedImgPath = MediaLogicHelper.GenerateSingleMediaPath(this._mediaHelper.UserMediasPath, recipeToUpdate.Id, i.Id, out userRecipePath);
                        // check file exists
                        Assert.IsTrue(File.Exists(savedImgPath));

                        long savedImageSize = new FileInfo(savedImgPath).Length;
                        // verify size is under 800kb
                        Assert.Less(savedImageSize, this.MAX_IMAGESIZE);
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #4
0
        public async Task RecipeUpdate_DeletingOneImage_DeletesProperly()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 4; // choose recipe ID not used by other test to prevent conflicts on result checks
                // Setup context with one recipe, and medias. Specifying separate media path since modifying image physically (to avoid conflict with other tests)
                // TODO: Each test should have their own data to work with
                SetupBasicContext(options, false, recipeId, "RecipeUpdate_DeletingOneImage");

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID defined at class level
                    // TODO: we're doing all of this wrong... You need a moqed environment for all test to work properly... The mediaLogicHelperTest already handles testing physical loading

                    var recipeToUpdate = await service.GetOne(recipeId);

                    // Remove media with ID: 3
                    var image = recipeToUpdate.Medias.Where(m => m.Id == 3).First();
                    recipeToUpdate.Medias.Remove(image);


                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service       = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    var recipeUpdated = await service.GetOne(recipeId);

                    var image = recipeUpdated.Medias.Where(m => m.Id == 3).FirstOrDefault();
                    Assert.IsNull(image);
                    Assert.AreEqual(2, recipeUpdated.Medias.Count, "Medias count should be 2");
                    // Verify logger wrote INFO specifying that Image 3 was deleted
                    this._loggerMock.Verify(l =>
                                            l.Log(LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString().Contains("DELETED") && v.ToString().Contains("ID:3")), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())
                                            , Times.Once);
                    // verify file no longer exist
                    // File path was defined in SetupBasicContext, it's not following regular path standards (from recipe service)
                    var imgToDeletePath = this._contextMedia.Where(m => m.Id == 3).First().MediaPath;
                    Assert.IsFalse(File.Exists(imgToDeletePath));
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #5
0
        public async Task RecipeUpdate_AddingOneImageToRecipeWithImages_IsAddedProperlyAndPathCreated()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 6;
                // Setup context with one recipe, and medias
                SetupBasicContext(options, false, recipeId);

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID defined at class level
                    var recipeToUpdate = await service.GetOne(recipeId);

                    // loading mediaDtos from json (to have media with image bytes)
                    var mediaDtos  = LoadRecipeMediaDtoFromJson(recipeId);
                    var mediaToAdd = mediaDtos.First();
                    recipeToUpdate.Medias.Add(mediaToAdd);

                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service       = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    var recipeUpdated = await service.GetOne(recipeId);

                    Assert.AreEqual(4, recipeUpdated.Medias.Count);

                    string userRecipePath;
                    // We can't access image path but we can recreate it from method used by service when saving it
                    var savedImgPath = MediaLogicHelper.GenerateSingleMediaPath(this._mediaHelper.UserMediasPath, recipeUpdated.Id, 4, out userRecipePath);
                    // check file exists
                    Assert.IsTrue(File.Exists(savedImgPath));
                    long savedImageSize = new FileInfo(savedImgPath).Length;
                    // verify size is under 800kb
                    Assert.Less(savedImageSize, this.MAX_IMAGESIZE);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #6
0
        public async Task RecipeUpdate_UpdatingRecipeAndIngredient_IsUpdated()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // modifying recipe properties
                    recipeToUpdate.OriginalLink = "https://www.something.com";
                    recipeToUpdate.TitleShort   = "Banana pancakes";

                    // modifying ingredient with id 1
                    var ingredient = recipeToUpdate.Ingredients.FirstOrDefault(i => i.Id == 1);
                    // overriding ingredient to remove linked/tracked objects, and modifying some properties
                    recipeToUpdate.Ingredients.Remove(ingredient);
                    recipeToUpdate.Ingredients.Add(new IngredientBase {
                        Id = ingredient.Id, Name = "Strawberry", Quantity = 1, Unit_Id = 3, Recipe_Id = ingredient.Recipe_Id
                    });

                    await service.UpdateOne(recipeToUpdate);

                    // get recipe again
                    var newRecipe = await service.GetOne(4);

                    Assert.AreEqual("Banana pancakes", newRecipe.TitleShort);
                    Assert.AreEqual("https://www.something.com", newRecipe.OriginalLink);

                    ingredient = recipeToUpdate.Ingredients.FirstOrDefault(i => i.Id == 1);
                    Assert.AreEqual("Strawberry", ingredient.Name);
                    Assert.AreEqual(1, ingredient.Quantity);
                    Assert.AreEqual(3, ingredient.Unit_Id);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #7
0
        // TODO: bad practice. If doing integration test that use physical disk, setup different folder with pics for each test
        public async Task RecipeUpdate_DeletingMultipleImages_DeletesProperly()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 5;
                // Setup context with one recipe, and medias. Specifying separate media path since modifying image physically (to avoid conflict with other tests)
                SetupBasicContext(options, false, recipeId, "RecipeUpdate_DeletingMultipleImages");
                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID defined at class level
                    var recipeToUpdate = await service.GetOne(recipeId);

                    // Remove all medias
                    recipeToUpdate.Medias.Clear();

                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service       = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    var recipeUpdated = await service.GetOne(recipeId);

                    Assert.IsEmpty(recipeUpdated.Medias);
                    // Verify logger wrote INFO specifying that Images were deleted, for all 3 images
                    this._loggerMock.Verify(l =>
                                            l.Log(LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString().Contains("DELETED")), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())
                                            , Times.Exactly(3));
                    // Verify files no longer exist
                    foreach (var media in this._contextMedia)
                    {
                        Assert.IsFalse(File.Exists(media.MediaPath));
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #8
0
        public async Task RecipeUpdate_UpdatingExistingImageProperties_UpdatesProperly()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                var recipeId = 2;
                // Setup context with one recipe, and medias. No specific media path provided since test doesn't modify media physically, can remain in standard path
                SetupBasicContext(options, false, recipeId);

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    // get recipe with ID 4
                    var recipeToUpdate = await service.GetOne(recipeId);

                    // Update media with ID: 3
                    var media = recipeToUpdate.Medias.Where(m => m.Id == 1).First();
                    // overriding ingredient to remove linked/tracked objects, and modifying some properties
                    // TODO: verify why object is being tracked? When we have asNoTracking set
                    recipeToUpdate.Medias.Remove(media);
                    recipeToUpdate.Medias.Add(new MediaDto {
                        Id = media.Id, Recipe_Id = media.Recipe_Id, Title = "OrangeJuice", Tag = "Juice", MediaDataUrl = media.MediaDataUrl
                    });

                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    // VERIFY
                    var service       = new RecipesService(context, this._loggerMock.Object, this._mediaHelper, this._mapper);
                    var recipeUpdated = await service.GetOne(recipeId);

                    var image = recipeUpdated.Medias.Where(m => m.Id == 1).First();
                    Assert.AreEqual("OrangeJuice", image.Title);
                    Assert.AreEqual("Juice", image.Tag);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #9
0
        public async Task RecipeUpdate_UpdatingIngredient_IsUpdated()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Update ingredient with Id:1's Name and Unit
                    var ingredient = recipeToUpdate.Ingredients.FirstOrDefault(i => i.Id == 1);
                    // overriding ingredient to remove linked/tracked objects, and modifying some properties
                    recipeToUpdate.Ingredients.Remove(ingredient);
                    recipeToUpdate.Ingredients.Add(new IngredientBase {
                        Id = ingredient.Id, Name = "Strawberry", Quantity = 1, Unit_Id = 3, Recipe_Id = ingredient.Recipe_Id
                    });

                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(2, dbrecipe.Ingredients.Count());  // verify count hasn't changed
                    ingredient = dbrecipe.Ingredients.FirstOrDefault(i => i.Id == 1);
                    Assert.AreEqual("Strawberry", ingredient.Name);
                    Assert.AreEqual(1, ingredient.Quantity);
                    Assert.AreEqual(3, ingredient.Unit_Id);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #10
0
        public async Task RecipeUpdate_UpdatingRecipeTitleShort_IsUpdated()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service      = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeUpdate = await service.GetOne(4);

                    // modifying recipe properties
                    recipeUpdate.OriginalLink = "https://www.something.com";
                    recipeUpdate.TitleShort   = "Banana pancakes";


                    await service.UpdateOne(recipeUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    // get recipe again
                    var newRecipe = await service.GetOne(4);

                    Assert.AreEqual("Banana pancakes", newRecipe.TitleShort);
                    Assert.AreEqual("https://www.something.com", newRecipe.OriginalLink);
                    // TODO: Test also that auditDate and creationDate are ignored (and handled by database)
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #11
0
        public async Task RecipeUpdate_NewIngredients_AreAdded()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Add Ingredients to recipe
                    recipeToUpdate.Ingredients.Add(new IngredientBase {
                        Id = 0, Name = "Butter", Quantity = 2, Unit_Id = 3, Recipe_Id = recipeToUpdate.Id
                    });
                    recipeToUpdate.Ingredients.Add(new IngredientBase {
                        Id = 0, Name = "Flour", Quantity = 3, Unit_Id = 3, Recipe_Id = recipeToUpdate.Id
                    });
                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(4, dbrecipe.Ingredients.Count());
                    var newIngredient = dbrecipe.Ingredients.FirstOrDefault(i => i.Id == 3);
                    Assert.AreEqual("Butter", newIngredient.Name);
                    Assert.AreEqual(2, newIngredient.Quantity);
                    Assert.AreEqual(3, newIngredient.Unit_Id);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task RecipeUpdate_ClearingOneInstructions_IsDeleted()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Remove instruction with Id = 1
                    var instruction = recipeToUpdate.Instructions.FirstOrDefault(i => i.Id == 1);
                    recipeToUpdate.Instructions.Remove(instruction);
                    await service.UpdateOne(recipeToUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    var service  = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(1, dbrecipe.Instructions.Count());
                    var instruction = dbrecipe.Instructions.FirstOrDefault();
                    Assert.AreEqual(2, instruction.Id);
                    Assert.AreEqual("Start cutting stuff", instruction.Description);
                    Assert.AreEqual(2, instruction.StepNum);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task RecipeUpdate_NewInstructions_AreAdded()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Add Ingredients to recipe
                    recipeToUpdate.Instructions.Add(new Instruction {
                        Id = 0, StepNum = 3, Description = "Go do something", Recipe_Id = recipeToUpdate.Id
                    });
                    recipeToUpdate.Instructions.Add(new Instruction {
                        Id = 0, StepNum = 4, Description = "Done", Recipe_Id = recipeToUpdate.Id
                    });
                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(4, dbrecipe.Instructions.Count());
                    var instructiontThree = dbrecipe.Instructions.FirstOrDefault(i => i.Id == 3);
                    Assert.AreEqual("Go do something", instructiontThree.Description);
                    Assert.AreEqual(3, instructiontThree.StepNum);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task RecipeUpdate_NewInstructionWithGivenId_IsAddedAndAssignedNewIdByDatabase()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service        = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeToUpdate = await service.GetOne(4);

                    // Add Ingredient with ID that doesn't exist yet
                    recipeToUpdate.Instructions.Add(new Instruction {
                        Id = 75, StepNum = 3, Description = "Go do something", Recipe_Id = recipeToUpdate.Id
                    });
                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(3, dbrecipe.Instructions.Count());
                    // According to our context, DB should override provided ID and assign ID 3 to this instruction
                    var instructiontThree = dbrecipe.Instructions.FirstOrDefault(i => i.Id == 3);
                    Assert.IsNotNull(instructiontThree);
                    Assert.AreEqual("Go do something", instructiontThree.Description);
                    Assert.AreEqual(3, instructiontThree.StepNum);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #15
0
        public async Task RecipeUpdate_UpdatingRecipeLastModifier_IsUpdated()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service      = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeUpdate = await service.GetOne(4);

                    // modifying recipe properties
                    recipeUpdate.LastModifier = "mark.x";


                    await service.UpdateOne(recipeUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    // get recipe again
                    var newRecipe = await service.GetOne(4);

                    Assert.AreEqual("mark.x", newRecipe.LastModifier);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #16
0
        public async Task RecipeUpdate_UpdatingCreationDate_IsIgnored()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service      = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    var recipeUpdate = await service.GetOne(4);

                    var currentTime = DateTime.Now;
                    recipeUpdate.CreationDate = currentTime;

                    await service.UpdateOne(recipeUpdate);
                }
                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    // get recipe again in new "request" / separate context
                    var newRecipe = await service.GetOne(4);

                    Assert.AreEqual(null, newRecipe.CreationDate); // should be unchanged, aka: should be null since it can only be generated by DB
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #17
0
        public async Task RecipeUpdate_DeleteAddAndUpdateIngredients_WorkAsExpected()
        {
            // each test creates new Connection / Options / DbSchema
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <RecipesContext>()
                              .UseSqlite(connection)
                              .Options;

                SetupBasicContext(options);

                using (var context = new RecipesContext(options))
                {
                    var service = new RecipesService(context, this._loggerMock.Object, this._mediaHelper.Object, this._mapper);
                    // todo: is this bad practice? Should I just recreate an object Recipe here, with same Id? (Since we want to reproduce offline example). To avoid tracked/extra entities..
                    // get recipe with Id 4
                    var recipeToUpdate = await service.GetOne(4);

                    // Removing ingredient with id 1
                    var ingredient1 = recipeToUpdate.Ingredients.FirstOrDefault(i => i.Id == 1);
                    recipeToUpdate.Ingredients.Remove(ingredient1);

                    // Modify ingredient with Id 2
                    var ingredient2 = recipeToUpdate.Ingredients.FirstOrDefault(i => i.Id == 2);
                    recipeToUpdate.Ingredients.Remove(ingredient2);
                    recipeToUpdate.Ingredients.Add(new IngredientBase {
                        Id = ingredient2.Id, Name = "Strawberry", Quantity = 1, Unit_Id = 3, Recipe_Id = ingredient2.Recipe_Id
                    });

                    // Add new ingredient
                    var ingredient3 = new IngredientBase()
                    {
                        Id = 0, Name = "Flour", Quantity = 3, Unit_Id = 1, Recipe_Id = 4
                    };
                    recipeToUpdate.Ingredients.Add(ingredient3);

                    // Get recipe again from db and verify all changes worked
                    await service.UpdateOne(recipeToUpdate);

                    var dbrecipe = await service.GetOne(4);

                    Assert.AreEqual(2, dbrecipe.Ingredients.Count());
                    // recipe should have 2 ingredient with id:2 and id:3 (new)
                    var ingredientIds = dbrecipe.Ingredients.Select(i => i.Id).ToList();
                    Assert.AreEqual(new int[2] {
                        2, 3
                    }, ingredientIds);
                    // check modif in ingredient with Id:2
                    ingredient2 = dbrecipe.Ingredients.FirstOrDefault(i => i.Id == 2);
                    Assert.AreEqual("Strawberry", ingredient2.Name);
                    Assert.AreEqual(1, ingredient2.Quantity);
                    Assert.AreEqual(3, ingredient2.Unit_Id);
                }
            }
            finally
            {
                connection.Close();
            }
        }