Skip to content

Commit

Permalink
VP-2440: Fix CatalogImage.itemId empty field after import products (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliveMen authored Sep 1, 2020
1 parent ab70ad5 commit 94764d0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/VirtoCommerce.CatalogCsvImportModule.Core/Model/CsvProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,18 @@ public void MergeFrom(CatalogProduct product)
Vendor = product.Vendor;
}

var imgComparer = AnonymousComparer.Create((Image x) => x.Url);
Images = Images.Concat(product.Images).Distinct(imgComparer).ToList();
foreach (var image in product.Images)
{
var existedImage = Images.FirstOrDefault(x => x.Url.Equals(image.Url, StringComparison.InvariantCultureIgnoreCase));
if (existedImage != null)
{
existedImage.Id = image.Id;
}
else
{
Images.Add(image);
}
}

var assetComparer = AnonymousComparer.Create((Asset x) => x.Url);
Assets = Assets.Concat(product.Assets).Distinct(assetComparer).ToList();
Expand Down
62 changes: 62 additions & 0 deletions tests/VirtoCommerce.CatalogCsvImportModule.Test/MergingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using VirtoCommerce.CatalogCsvImportModule.Core.Model;
using VirtoCommerce.CatalogModule.Core.Model;
using VirtoCommerce.CoreModule.Core.Seo;
using Xunit;

namespace VirtoCommerce.CatalogCsvImportModule.Test
{
public class MergingTests
{
[Fact]
public void CsvProductMergeTest_ProductHasSameImages_ImagesUpdated()
{
//Arrange
var catalogProduct = GetCatalogProductWithImage();
var csvProduct = new CsvProduct()
{
Images = new List<Image>() {new Image() {Id = "", Url = "SameURL"}}
};
//Act
csvProduct.MergeFrom(catalogProduct);

//Assets
Assert.Equal(1, csvProduct.Images.Count);
Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == "1"));
}

[Fact]
public void CsvProductMergeTest_ProductHasAnotherImages_ImagesAdded()
{
//Arrange
var catalogProduct = GetCatalogProductWithImage();

var csvProduct = new CsvProduct()
{
Images = new List<Image>() { new Image() { Id = "", Url = "AnotherUrl" } }
};

//Act
csvProduct.MergeFrom(catalogProduct);

//Assert
Assert.Equal(2, csvProduct.Images.Count);
Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == "1"));
Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == ""));
}

private CatalogProduct GetCatalogProductWithImage()
{
return new CatalogProduct()
{
Images = new List<Image>() { new Image() { Id = "1", Url = "SameURL" } },
Assets = new List<Asset>(),
Reviews = new List<EditorialReview>(),
Properties = new List<Property>(),
SeoInfos = new List<SeoInfo>()
};
}

}
}

0 comments on commit 94764d0

Please sign in to comment.