123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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<TableSimple1Dbo, table_simple_1>
- {
- 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<TableSimple1Dbo, table_simple_1, luticate2_unit_testsContext>
- {
- public TableSimple1DataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
- {
- }
- }
-
- protected IServiceProvider GetServiceProvider()
- {
- var services = new ServiceCollection();
- services.AddLuObjectConverterDescriptors();
- services.AddSingleton<ILuObjectConverterDescriptor<TableSimple1Dbo, table_simple_1>, TableSimple1DboDescriptor>();
-
- services.AddLuObjectConverters();
- services.AddLuObjectConverterPoco<table_simple_1, TableSimple1Dbo>(); // DB read
-
- services.AddDbContext<luticate2_unit_testsContext>(options =>
- {
- options.UseNpgsql(@"Host=localhost;Database=luticate2_unit_tests;Username=dev;Password=dev");
- options.UseInternalServiceProvider(new ServiceCollection()
- .AddEntityFrameworkNpgsql()
- .BuildServiceProvider());
- }, ServiceLifetime.Transient);
-
- services.AddSingleton<TableSimple1DataAccess>();
- services.AddSingleton<ILuConvertersTypeConverter>(new LuConvertersTypeConverter(new Dictionary<Type, Type>
- {
- {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<TableSimple1DataAccess>();
-
- var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo
- {
- Filter = new LuFilterDbo
- {
- Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => x.Name == "test1")
- },
- Page = 0,
- OrderBy = new LuOrderByDbo
- {
- OrderByFields = new List<LuOrderByFieldDbo>()
- },
- 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<TableSimple1DataAccess>();
-
- var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo
- {
- Filter = new LuFilterDbo
- {
- Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => x.Name == "test1" || x.Name == "test2")
- },
- Page = 0,
- OrderBy = new LuOrderByDbo
- {
- OrderByFields = new List<LuOrderByFieldDbo>
- {
- new LuOrderByFieldDbo
- {
- Asc = true,
- Expression = (Expression<Func<TableSimple1Dbo, object>>) (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<TableSimple1DataAccess>();
-
- var partialRespone = new LuPartialFieldsDbo
- {
- Fields = new List<LuFieldDbo>
- {
- new LuFieldDbo
- {
- Parts = new List<string>
- {
- "*"
- }
- }
- }
- };
-
- var paginationParams = new LuPaginatedParamsDbo
- {
- Filter = new LuFilterDbo
- {
- Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => x.Name == "test1" || x.Name == "test2")
- },
- Page = 0,
- OrderBy = new LuOrderByDbo
- {
- OrderByFields = new List<LuOrderByFieldDbo>
- {
- new LuOrderByFieldDbo
- {
- Asc = false,
- Expression = (Expression<Func<TableSimple1Dbo, object>>) (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);
- }
- }
- }
|