public async Task DeleteImage_ImageDeleted() { // Arrange var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("DeleteImage_ImageDeleted").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); var mockEnvironment = new Mock <IWebHostEnvironment>(); mockEnvironment .Setup(m => m.EnvironmentName) .Returns("Hosting:UnitTestEnvironment"); ctx.Add(new GalleryImage { Id = 1, Title = "11" }); ctx.SaveChanges(); ImageController controller = new ImageController(service, mockEnvironment.Object); // Act try { await controller.Delete(1); Assert.Fail(); } catch (Exception) { } // Assert }
public async Task UpdateImage_ModelHasCorrectData() { // Arrange var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("UpdateImage_ModelHasCorrectData").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); var mockEnvironment = new Mock <IWebHostEnvironment>(); mockEnvironment .Setup(m => m.EnvironmentName) .Returns("Hosting:UnitTestEnvironment"); ctx.Add(new GalleryImage { Id = 1, Title = "11" }); ctx.SaveChanges(); ImageController controller = new ImageController(service, mockEnvironment.Object); // Act ViewResult result = await controller.UpdateImage(1) as ViewResult; GalleryImage Image = result.ViewData.Model as GalleryImage; // Assert Assert.AreEqual("11", Image.Title); }
public async Task DeleteImage_GetViewResult() { // Arrange var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("DeleteImage_GetViewResult").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); var mockEnvironment = new Mock <IWebHostEnvironment>(); mockEnvironment .Setup(m => m.EnvironmentName) .Returns("Hosting:UnitTestEnvironment"); ctx.Add(new GalleryImage { Id = 1, Title = "11", Url = "" }); ctx.SaveChanges(); ImageController controller = new ImageController(service, mockEnvironment.Object); // Act var result = await controller.Delete(1); Assert.IsInstanceOf <ViewResult>(result); }
public async Task UpdateImagePost_GetViewResult() { // Arrange var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("Test1_ImageController").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); var mockEnvironment = new Mock <IWebHostEnvironment>(); Bitmap bitmap = new Bitmap(10, 10, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Image copy = bitmap; string newPath = Environment.CurrentDirectory + "/test/newImageName.jpg"; copy.Save(newPath); mockEnvironment .Setup(m => m.WebRootPath).Returns(Environment.CurrentDirectory + "/test/"); ImageController controller = new ImageController(service, mockEnvironment.Object); ctx.Add(new GalleryImage { Id = 1, Title = "11", Url = "newImageName.jpg" }); ctx.SaveChanges(); var fileMock = new Mock <IFormFile>(); var file = new Mock <IFormFile>(); var sourceImg = File.OpenRead("C:\\Users\\PC\\Source\\Repos\\andr1312e\\ImageGallery\\ImageGallery\\wwwroot\\gallery\\1.jpeg"); var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(sourceImg); writer.Flush(); stream.Position = 0; var fileName = "QQ.png"; file.Setup(f => f.OpenReadStream()).Returns(stream); file.Setup(f => f.FileName).Returns(fileName); file.Setup(f => f.Length).Returns(stream.Length); file.Setup(f => f.ContentType).Returns("image/jpeg"); // Act var result = await controller.UpdateImage(1, "122", file.Object); // Assert Assert.IsInstanceOf <ViewResult>(result); var IRes = (ViewResult)result; var res = IRes.ViewData.Model as GalleryImage; string a = "2"; }
public async Task <IActionResult> Create([Bind("Id,Name,Description,Url,Author")] Image images) { if (ModelState.IsValid) { images.Created = DateTime.Now; _context.Add(images); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(images)); }
public async Task SetImage(string title, string tags, Uri uri) { var image = new GalleryImage { Title = title, Tags = ParseTags(tags), Url = uri.AbsoluteUri, Created = DateTime.Now }; _context.Add(image); await _context.SaveChangesAsync(); }
public async Task SetImage(string title, string tags, Uri uri) { var image = new GalleryImage { Title = title, Tags = tags != null?ParseTags(tags) : new List <ImageTag>(), Url = uri.AbsoluteUri }; _ctx.Add(image); await _ctx.SaveChangesAsync(); }
public void SetImage(string title, string tags, string path, string user) { GalleryImage Image = new GalleryImage { Title = title, ImageCreated = DateTime.Now, UserName = user, Tags = TagsFromStringParse(DeleteSpace(tags)), Url = path, }; ImageGalleryDbContext.Add(Image); ImageGalleryDbContext.SaveChanges(); }
public void Service_Rename_Image_Correct() { var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("Rename").Options; var ctx = new ImageGalleryDbContext(options); ctx.Add(new GalleryImage { Id = 1, Title = "11" }); ctx.SaveChanges(); IImage service = new Service(ctx); service.Rename(service.GetById(1), "33"); Assert.AreEqual("33", service.GetById(1).Title); }
public void Service_Get_By_Id_Correct() { var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("Get_by_Id").Options; var ctx = new ImageGalleryDbContext(options); ctx.Add(new GalleryImage { Id = 1, Title = "11" }); ctx.SaveChanges(); IImage service = new Service(ctx); string TitleGet = service.GetById(1).Title; Assert.AreEqual("11", TitleGet); }
public void Service_Get_Last_Id_Correct() { var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("LastId").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); ctx.Add(new GalleryImage { Id = 0, Title = "11" }); ctx.SaveChanges(); int id = service.LastId(); Assert.AreEqual(1, id); }
public void Service_Get_By_UserName_Correct() { var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("UserName").Options; var ctx = new ImageGalleryDbContext(options); IImage service = new Service(ctx); ctx.Add(new GalleryImage { Id = 0, Title = "11", UserName = "******" }); ctx.SaveChanges(); GalleryImage img = service.GetByUserName("33").First(); Assert.AreEqual("11", img.Title); }
public void Service_Delete_Image_Correct() { var options = new DbContextOptionsBuilder <ImageGalleryDbContext>().UseInMemoryDatabase("Delete").Options; var ctx = new ImageGalleryDbContext(options); ctx.Add(new GalleryImage { Id = 1, Title = "11" }); ctx.SaveChanges(); IImage service = new Service(ctx); service.Delete(service.GetById(1)); int count = service.GetAll().Count(); Assert.AreEqual(0, count); }
public async Task SetImage(string title, string tags, Uri uri) { // Check tags is null or isn't List <ImageTag> listTags = null; if (tags == null) { listTags = ParseTags(tags); } var image = new GalleryImage() { Title = title, Tags = listTags, Url = uri.AbsoluteUri, Created = DateTime.Now, }; _context.Add(image); await _context.SaveChangesAsync(); }
public void AddImage(Image newImage) { _ctx.Add(newImage); _ctx.SaveChanges(); }