Ejemplo n.º 1
0
        public void SaveImageLocally_CanSaveOneImage(string imageName, int mediaId)
        {
            var recipeId        = 52;
            var receivedImgPath = Path.Combine(this.mediasPath, receivedDataDirectory, imageName);
            // STEP1 - Create Media to load with image path
            var media = new Media {
                MediaPath = receivedImgPath, Id = mediaId, Title = "Some media", Recipe_Id = recipeId
            };

            using (var helper = new MediaLogicHelper(this._configuration.Object, this._loggerMock.Object))
            {
                // STEP2 - Load Image from disk
                // loading image as MediaDTO (to get img bytes)
                var mediaDtos = helper.LocateAndLoadMedias(new List <Media>()
                {
                    media
                });
                // STEP3 - Save it again
                ServiceResponse savingResult;
                var             medias = helper.SaveImagesLocally(mediaDtos, out savingResult);

                // VERIFY
                Assert.IsTrue(savingResult.Success);
                string userRecipePath;
                var    savedImgPath = MediaLogicHelper.GenerateSingleMediaPath(helper.UserMediasPath, recipeId, mediaId, out userRecipePath);
                // check file exists
                Assert.IsTrue(File.Exists(savedImgPath));
                Assert.AreEqual(medias.First(m => m.Id == media.Id).MediaPath, savedImgPath);
                long savedImageSize = new FileInfo(savedImgPath).Length;
                // verify size is under 800kb
                Assert.IsTrue(savedImageSize <= this.MAX_IMAGESIZE);
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
0
        public void SaveImageLocally_CanSaveMultipleImages()
        {
            var recipeId       = 53;
            var receivedImages = new List <(string File, int MediaId)> {
                ("foodMarket.jpg", 1), ("foodColor.jpeg", 2), ("foodMarketPng.png", 3)
            };

            // STEP1 - Create list of Media to load with image path
            var medias = new List <Media>();

            foreach (var i in receivedImages)
            {
                var receivedImgPath = Path.Combine(this.mediasPath, receivedDataDirectory, i.File);
                medias.Add(new Media {
                    MediaPath = receivedImgPath, Id = i.MediaId, Title = "Some media", Recipe_Id = recipeId
                });
            }
            using (var helper = new MediaLogicHelper(this._configuration.Object, this._loggerMock.Object))
            {
                // STEP2 - Load all Images from disk
                // loading all images as MediaDTO (to get img bytes)
                var mediaDtos = helper.LocateAndLoadMedias(medias);

                // STEP3 - Save it again
                ServiceResponse savingResult;
                var             resultMedias = helper.SaveImagesLocally(mediaDtos, out savingResult);
                Assert.IsTrue(savingResult.Success);

                // VERIFY
                string userRecipePath; // not needed so we override
                foreach (var i in receivedImages)
                {
                    var savedImgPath = MediaLogicHelper.GenerateSingleMediaPath(helper.UserMediasPath, recipeId, i.MediaId, out userRecipePath);
                    // check file exists
                    Assert.IsTrue(File.Exists(savedImgPath));
                    Assert.AreEqual(resultMedias.First(m => m.Id == i.MediaId).MediaPath, savedImgPath);

                    long savedImageSize = new FileInfo(savedImgPath).Length;
                    // verify size is under 800kb
                    Assert.Less(savedImageSize, this.MAX_IMAGESIZE);
                }
            }
        }