69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Commerce.Contracts.DTOs;
|
|
using Commerce.Core.Services;
|
|
using Commerce.Domain;
|
|
using Commerce.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Xunit;
|
|
|
|
namespace Commerce.Core.Tests;
|
|
|
|
public class CategoryServiceTests
|
|
{
|
|
private Context GetContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<Context>()
|
|
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
|
.Options;
|
|
return new Context(options);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllCategories_ShouldFilterByLocalizedName()
|
|
{
|
|
// Arrange
|
|
using var context = GetContext();
|
|
await context.Database.EnsureCreatedAsync();
|
|
var id1 = Guid.NewGuid();
|
|
context.Categories.AddRange(new List<Category>
|
|
{
|
|
new Category {
|
|
Id = id1,
|
|
Translations = new List<CategoryTranslation>
|
|
{
|
|
new CategoryTranslation { Language = "en", Info = new CategoryInfo { Name = "Electronics", Description = "Desc" } },
|
|
new CategoryTranslation { Language = "ar", Info = new CategoryInfo { Name = "إلكترونيات", Description = "Desc" } }
|
|
},
|
|
Photos = Array.Empty<string>()
|
|
},
|
|
new Category {
|
|
Id = Guid.NewGuid(),
|
|
Translations = new List<CategoryTranslation>
|
|
{
|
|
new CategoryTranslation { Language = "en", Info = new CategoryInfo { Name = "Fashion", Description = "Desc" } }
|
|
},
|
|
Photos = Array.Empty<string>()
|
|
}
|
|
});
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = new CategoryService(context);
|
|
|
|
// Act - Search for Arabic name
|
|
var resultsAr = await service.GetAllCategories("إلكترونيات");
|
|
|
|
// Act - Search for English name
|
|
var resultsEn = await service.GetAllCategories("Elec");
|
|
|
|
// Assert
|
|
Assert.Single(resultsAr);
|
|
Assert.Equal(id1, resultsAr.First().Id);
|
|
|
|
Assert.Single(resultsEn);
|
|
Assert.Equal(id1, resultsEn.First().Id);
|
|
}
|
|
}
|