public void 儲存一張主要的產品圖片而且原本沒有圖片Test() { // arrange var mockProduct = new Product { Id = 1, ItemCode = "1000000100101" }; var mockUploadImage = new UploadIamge { ItemCode = mockProduct.ItemCode, PictureType = ProductPictureType.Product, BlobPath = "", Seq = "01" }; var mockProductPicture = new ProductPicture { Id = 1, ProductId = 1, ProductPictureType = 1, IsMainPicture = true, ImageUrl = "imageUrl" }; var mockProductPictures = new List<ProductPicture>() { mockProductPicture }.AsQueryable(); var mockProductPictureDbSet = Substitute.For<IDbSet<ProductPicture>>().Initialize(mockProductPictures); var mockDbContext = Substitute.For<ICMSDbContext>(); var productService = new ProductService(mockDbContext); mockProductPictureDbSet.Add(mockProductPicture).Returns(mockProductPicture); mockDbContext.ProductPictures = mockProductPictureDbSet; // act productService.SaveProductImageData(mockProduct, mockUploadImage, mockProductPicture.ImageUrl); // assert mockDbContext.ProductPictures.Should().NotBeNull(); var productPictureResut = mockDbContext.ProductPictures.ElementAt(0); productPictureResut.Should().NotBeNull(); productPictureResut.Id.Should().Be(mockProductPicture.Id); productPictureResut.ImageUrl.Should().Be(mockProductPicture.ImageUrl); productPictureResut.IsMainPicture.Should().BeTrue(); productPictureResut.ProductPictureType.Should().Be(mockProductPicture.ProductPictureType); }
public void SaveProductImageData(Product product, UploadIamge uploadIamge, string fileName) { if (product == null) { return; } //seq == 1 表示為主圖 if (uploadIamge.Seq == "01") { var image = product.ProductPictures.FirstOrDefault( a => a.IsMainPicture == true && a.ProductPictureType == (int)uploadIamge.PictureType); if (image != null) { image.ImageUrl = fileName; image.ModifiedOn = DateTime.UtcNow; image.ModifiedBy = 1; } else { image = this.GetProductPictureInstance(product.Id, fileName, true, (int)uploadIamge.PictureType); this.db.ProductPictures.Add(image); } } else { var image = product.ProductPictures.FirstOrDefault( a => a.IsMainPicture == false && a.ImageUrl == fileName && a.ProductPictureType == (int)uploadIamge.PictureType); if (image != null) { image.ImageUrl = fileName; image.ModifiedOn = DateTime.UtcNow; image.ModifiedBy = 1; } else { image = this.GetProductPictureInstance(product.Id, fileName, false, (int)uploadIamge.PictureType); this.db.ProductPictures.Add(image); } } this.db.SaveChanges(); }
public void InputProductImagePack_新增成功_return_fileName_fileSize() { // arrange var mockFile = Substitute.For<HttpPostedFileBase>(); var mockFileName = "fileName"; var mockContentLength = 10; mockFile.ContentLength.Returns(mockContentLength); mockFile.FileName.Returns(mockFileName); var stream = Substitute.For<Stream>(); mockFile.InputStream.Returns(stream); var mockProductService = Substitute.For<IProductService>(); var product = new Product { ItemCode = "123" }; mockProductService.GetProductByItemCode(Arg.Any<string>()).Returns(product); var mockBlobService = Substitute.For<IBlobService>(); mockBlobService.SaveToBlob(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<Stream>()).Returns(true); var mockZipService = Substitute.For<IZipService>(); var mockZip = Substitute.For<ZipFile>(); mockZip.AddEntry("1000000100101_M_01.jpg", Arg.Any<Stream>()); mockZipService.Read(Arg.Any<Stream>()).Returns(mockZip); mockZipService.EntriesCount(mockZip).Returns(1); var mockMemoryStream = Arg.Any<MemoryStream>(); mockZipService.GetEntryMemoryStream(new ZipEntry()).Returns(mockMemoryStream); var uploadController = new UploadController(mockProductService, mockBlobService, mockZipService); var expected = new { fileName = mockFileName, fileSize = mockContentLength }; // act var result = uploadController.InputProductImagePack(mockFile) as JsonResult; // assert result.Should().NotBeNull(); result.Data.ShouldBeEquivalentTo(expected); }
private List<Product> GetProduct() { var result = new List<Product>(); var product = new Product { Id = 1, ItemCode = "1000000100101" }; product.ProductPictures = this.GetProductPicture(); result.Add(product); return result; }