using System; using System.Collections.Generic; using System.Linq.Expressions; using Luticate2.Auth.Tests.DataAccess.Crud.Models; using Luticate2.Auth.Utils.Business.Converters; using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor; using Luticate2.Auth.Utils.Business.Fields; using Luticate2.Auth.Utils.DataAccess.Crud; using Luticate2.Auth.Utils.Dbo; using Luticate2.Auth.Utils.Dbo.Fields; using Luticate2.Auth.Utils.Dbo.Pagination; using Luticate2.Auth.Utils.Dbo.Result; using Luticate2.Auth.Utils.Interfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Luticate2.Auth.Tests.DataAccess.Crud { public class LuEfCrudTests { public class TableSimple1Dbo { public Guid Id { get; set; } public string Name { get; set; } public int Priority { get; set; } public string Comment { get; set; } public int? OptionalInt { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } } public class TableSimple1DboDescriptor : LuObjectConverterDescriptor { public TableSimple1DboDescriptor() { AddStaticMemberConverter(dbo => dbo.Id, model => model.id); AddStaticMemberConverter(dbo => dbo.Name, model => model.name); AddStaticMemberConverter(dbo => dbo.Priority, model => model.priority); AddStaticMemberConverter(dbo => dbo.Comment, model => model.comment); AddStaticMemberConverter(dbo => dbo.OptionalInt, model => model.optional_int); AddStaticMemberConverter(dbo => dbo.CreatedAt, model => model.created_at); AddStaticMemberConverter(dbo => dbo.UpdatedAt, model => model.updated_at); } } public class TableSimple1DataAccess : LuEfCrudDataAccess { public TableSimple1DataAccess(IServiceProvider serviceProvider) : base(serviceProvider) { } } protected IServiceProvider GetServiceProvider() { var services = new ServiceCollection(); services.AddLuObjectConverterDescriptors(); services.AddSingleton, TableSimple1DboDescriptor>(); services.AddLuObjectConverters(); services.AddLuObjectConverterPoco(); // DB read services.AddDbContext(options => { options.UseNpgsql(@"Host=localhost;Database=luticate2_unit_tests;Username=dev;Password=dev"); options.UseInternalServiceProvider(new ServiceCollection() .AddEntityFrameworkNpgsql() .BuildServiceProvider()); }, ServiceLifetime.Transient); services.AddSingleton(); services.AddSingleton(new LuConvertersTypeConverter(new Dictionary { {typeof(TableSimple1Dbo), typeof(table_simple_1)} // DB read })); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; } [Fact] public void TestSimpleReadFilter1() { var serviceProvider = GetServiceProvider(); var crudDataAccess = serviceProvider.GetRequiredService(); var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo { Filter = new LuFilterDbo { Expression = (Expression>) (x => x.Name == "test1") }, Page = 0, OrderBy = new LuOrderByDbo { OrderByFields = new List() }, PerPage = 10 }); Assert.NotNull(result); Assert.Equal(LuStatus.Success.ToInt(), result.Status); var items = result.Data; Assert.NotNull(items); Assert.Equal(1, items.Count); Assert.NotNull(items.Data); Assert.Equal(1, items.Data.Count); var item = items.Data[0]; Assert.NotNull(item); Assert.Equal("test1", item.Name); Assert.Equal(42, item.Priority); Assert.Null(item.Comment); Assert.Null(item.OptionalInt); } [Fact] public void TestSimpleReadOrderBy1() { var serviceProvider = GetServiceProvider(); var crudDataAccess = serviceProvider.GetRequiredService(); var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo { Filter = new LuFilterDbo { Expression = (Expression>) (x => x.Name == "test1" || x.Name == "test2") }, Page = 0, OrderBy = new LuOrderByDbo { OrderByFields = new List { new LuOrderByFieldDbo { Asc = true, Expression = (Expression>) (x => x.Priority) } } }, PerPage = 10 }); Assert.NotNull(result); Assert.Equal(LuStatus.Success.ToInt(), result.Status); var items = result.Data; Assert.NotNull(items); Assert.Equal(2, items.Count); Assert.NotNull(items.Data); Assert.Equal(2, items.Data.Count); var item = items.Data[0]; Assert.NotNull(item); Assert.Equal("test2", item.Name); Assert.Equal(24, item.Priority); Assert.Null(item.Comment); Assert.Equal(1, item.OptionalInt); item = items.Data[1]; Assert.NotNull(item); Assert.Equal("test1", item.Name); Assert.Equal(42, item.Priority); Assert.Null(item.Comment); Assert.Null(item.OptionalInt); } [Fact] public void TestSimpleReadOrderBy2() { var serviceProvider = GetServiceProvider(); var crudDataAccess = serviceProvider.GetRequiredService(); var partialRespone = new LuPartialFieldsDbo { Fields = new List { new LuFieldDbo { Parts = new List { "*" } } } }; var paginationParams = new LuPaginatedParamsDbo { Filter = new LuFilterDbo { Expression = (Expression>) (x => x.Name == "test1" || x.Name == "test2") }, Page = 0, OrderBy = new LuOrderByDbo { OrderByFields = new List { new LuOrderByFieldDbo { Asc = false, Expression = (Expression>) (x => x.Priority) } } }, PerPage = 10 }; var result = crudDataAccess.Read(partialRespone, paginationParams); Assert.NotNull(result); Assert.Equal(LuStatus.Success.ToInt(), result.Status); var items = result.Data; Assert.NotNull(items); Assert.Equal(2, items.Count); Assert.NotNull(items.Data); Assert.Equal(2, items.Data.Count); var item = items.Data[0]; Assert.NotNull(item); Assert.Equal("test1", item.Name); Assert.Equal(42, item.Priority); Assert.Null(item.Comment); Assert.Null(item.OptionalInt); item = items.Data[1]; Assert.NotNull(item); Assert.Equal("test2", item.Name); Assert.Equal(24, item.Priority); Assert.Null(item.Comment); Assert.Equal(1, item.OptionalInt); } } }