Files
ECommerce/ECommerce.Core.Tests/LocalizationMappingTests.cs

154 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Commerce.Contracts.DTOs;
using Commerce.Domain.Entities;
using Xunit;
using TechnicalDetail = Commerce.Domain.Entities.TechnicalDetail;
namespace Commerce.Core.Tests;
public class LocalizationMappingTests
{
[Fact]
public void Product_ToVM_ShouldMapIndependentTechnicalDetails()
{
// Arrange
var product = new Product
{
Id = Guid.NewGuid(),
Translations = new List<ProductTranslation>
{
new ProductTranslation
{
Language = "en",
Info = new ProductInfo
{
Name = "Product 1",
TechnicalDetails = new List<TechnicalDetail> { new TechnicalDetail { Key = "Color", Value = "Red" } }
}
},
new ProductTranslation
{
Language = "ar",
Info = new ProductInfo
{
Name = "منتج 1",
TechnicalDetails = new List<TechnicalDetail> { new TechnicalDetail { Key = "اللون", Value = "أحمر" } }
}
}
}
};
// Act
var vm = Product.ToVM(product);
// Assert
Assert.NotNull(vm.Translations);
Assert.Equal(2, vm.Translations.Count);
var enTrans = vm.Translations.First(t => t.Language == "en");
Assert.Single(enTrans.Info.TechnicalDetails);
Assert.Equal("Color", enTrans.Info.TechnicalDetails[0].Key);
Assert.Equal("Red", enTrans.Info.TechnicalDetails[0].Value);
var arTrans = vm.Translations.First(t => t.Language == "ar");
Assert.Single(arTrans.Info.TechnicalDetails);
Assert.Equal("اللون", arTrans.Info.TechnicalDetails[0].Key);
Assert.Equal("أحمر", arTrans.Info.TechnicalDetails[0].Value);
}
[Fact]
public void Product_ToEntity_ShouldMapIndependentTechnicalDetails()
{
// Arrange
var vm = new ProductVM
{
Id = Guid.NewGuid(),
Translations = new List<ProductTranslationVM>
{
new ProductTranslationVM
{
Language = "en",
Info = new ProductInfoVM
{
Name = "Product 1",
TechnicalDetails = new List<TechnicalDetailVM> { new TechnicalDetailVM { Key = "Weight", Value = "1kg" } }
}
}
}
};
// Act
var entity = Product.ToEntity(vm);
// Assert
Assert.NotNull(entity.Translations);
Assert.Single(entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails);
Assert.Equal("Weight", entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Key);
Assert.Equal("1kg", entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Value);
}
[Fact]
public void Category_ToVM_ShouldMapIndependentTechnicalDetails()
{
// Arrange
var category = new Category
{
Id = Guid.NewGuid(),
Translations = new List<CategoryTranslation>
{
new CategoryTranslation
{
Language = "en",
Info = new CategoryInfo
{
Name = "Category 1",
TechnicalDetails = new List<TechnicalDetail> { new TechnicalDetail { Key = "Material", Value = "Leather" } }
}
}
}
};
// Act
var vm = Category.ToVM(category);
// Assert
Assert.NotNull(vm.Translations);
Assert.Single(vm.Translations.First(t => t.Language == "en").Info.TechnicalDetails);
Assert.Equal("Material", vm.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Key);
Assert.Equal("Leather", vm.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Value);
}
[Fact]
public void Category_ToEntity_ShouldMapIndependentTechnicalDetails()
{
// Arrange
var vm = new CategoryVM
{
Id = Guid.NewGuid(),
Translations = new List<CategoryTranslationVM>
{
new CategoryTranslationVM
{
Language = "en",
Info = new CategoryInfoVM
{
Name = "Category 1",
TechnicalDetails = new List<TechnicalDetailVM> { new TechnicalDetailVM { Key = "Power", Value = "100W" } }
}
}
}
};
// Act
var entity = Category.ToEntity(vm);
// Assert
Assert.NotNull(entity.Translations);
Assert.Single(entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails);
Assert.Equal("Power", entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Key);
Assert.Equal("100W", entity.Translations.First(t => t.Language == "en").Info.TechnicalDetails[0].Value);
}
}