sharding/test/ShardingCore.Test2x/ShardingTest.cs

1481 lines
71 KiB
C#
Raw Normal View History

2021-11-26 23:11:09 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.ExtensionExpressionComparer;
2021-11-26 23:11:09 +08:00
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
2021-11-26 23:11:09 +08:00
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Helpers;
using ShardingCore.Sharding;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Sharding.ShardingDbContextExecutors;
2021-11-26 23:11:09 +08:00
using ShardingCore.TableCreator;
using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Utils;
2021-11-26 23:11:09 +08:00
using Xunit;
namespace ShardingCore.Test2x
{
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 17:22:10
* @Email: 326308290@qq.com
*/
public class ShardingTest
{
private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager;
private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager;
private readonly IShardingComparer<ShardingDefaultDbContext> _shardingComparer;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager;
2021-11-27 21:41:45 +08:00
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration,
2021-11-26 23:11:09 +08:00
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
2021-11-27 21:41:45 +08:00
IShardingComparer<ShardingDefaultDbContext> shardingComparer, IVirtualDataSource<ShardingDefaultDbContext> virtualDataSource,
2021-11-26 23:11:09 +08:00
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator, IShardingReadWriteManager shardingReadWriteManager)
{
_virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager;
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>();
_configuration = configuration;
this._entityMetadataManager = entityMetadataManager;
_shardingComparer = shardingComparer;
_virtualDataSource = virtualDataSource;
_virtualTableManager = virtualTableManager;
_shardingTableCreator = shardingTableCreator;
_shardingReadWriteManager = shardingReadWriteManager;
}
[Fact]
public void RouteParseCompileCacheTest()
{
2021-12-03 13:35:08 +08:00
var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
var routeParseExpression1 = ShardingUtil.GetRouteParseExpression<int>(queryable1, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
var routeParseExpression2 = ShardingUtil.GetRouteParseExpression<int>(queryable2, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var xxxx1 = 202102;
var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
var routeParseExpression3 = ShardingUtil.GetRouteParseExpression<int>(queryable3, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
var routeParseExpression4 = ShardingUtil.GetRouteParseExpression<int>(queryable4, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
var routeParseExpression5 = ShardingUtil.GetRouteParseExpression<int>(queryable5, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
var routeParseExpression6 = ShardingUtil.GetRouteParseExpression<int>(queryable6, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
var routeParseExpression7 = ShardingUtil.GetRouteParseExpression<int>(queryable7, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
var routeParseExpression8 = ShardingUtil.GetRouteParseExpression<int>(queryable8, virtualTableRoute.EntityMetadata,
(i, op) => virtualTableRoute.GetRouteToFilter(i, op), true);
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression5));
Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression5), expressionEqualityComparer.GetHashCode(routeParseExpression6));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression4), expressionEqualityComparer.GetHashCode(routeParseExpression7));
Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
}
2021-11-26 23:11:09 +08:00
[Fact]
public async Task GenericTest()
{
2021-11-27 21:41:45 +08:00
var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>());
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>());
2021-11-26 23:11:09 +08:00
Assert.Equal(x, y);
2021-11-27 21:41:45 +08:00
var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
2021-11-26 23:11:09 +08:00
var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count);
foreach (var (key, value) in bulkShardingEnumerable)
{
Assert.Equal(100, value.Count);
}
_virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300").ShardingPrint();
2021-11-27 21:41:45 +08:00
var contains = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300").Select(o => o.Id).ContainsAsync("300");
2021-11-26 23:11:09 +08:00
Assert.True(contains);
try
{
var x1 = _virtualDataSource.GetPhysicDataSource("abc");
}
catch (Exception e)
{
2021-11-27 21:41:45 +08:00
Assert.Equal(typeof(ShardingCoreInvalidOperationException), e.GetType());
2021-11-26 23:11:09 +08:00
}
2021-11-27 21:41:45 +08:00
var queryable = new List<string>().Select(o => new SequenceClass { Id = "123", T = o }).AsQueryable();
2021-11-26 23:11:09 +08:00
var sourceType = queryable.GetType().GetSequenceType();
2021-11-27 21:41:45 +08:00
Assert.Equal(typeof(SequenceClass), sourceType);
2021-11-26 23:11:09 +08:00
try
{
_shardingTableCreator.CreateTable<Order>("A", "202105");
}
catch (Exception e)
{
2021-11-27 21:41:45 +08:00
Assert.Equal(typeof(ShardingCoreException), e.GetType());
2021-11-26 23:11:09 +08:00
}
2021-11-27 21:41:45 +08:00
var orderMetadata = _entityMetadataManager.TryGet<Order>();
2021-11-26 23:11:09 +08:00
Assert.NotNull(orderMetadata);
var isKey1 = orderMetadata.ShardingDataSourceFieldIsKey();
Assert.False(isKey1);
var isKey2 = orderMetadata.ShardingTableFieldIsKey();
Assert.False(isKey2);
var userModMetadata = _entityMetadataManager.TryGet<SysUserMod>();
Assert.NotNull(userModMetadata);
var isKey3 = userModMetadata.ShardingDataSourceFieldIsKey();
Assert.False(isKey3);
var isKey4 = userModMetadata.ShardingTableFieldIsKey();
Assert.True(isKey4);
try
{
throw new ShardingCoreParallelQueryTimeOutException("test");
}
catch (Exception e)
{
2021-11-27 21:41:45 +08:00
Assert.Equal(typeof(ShardingCoreParallelQueryTimeOutException), e.GetType());
2021-11-26 23:11:09 +08:00
}
await _virtualDbContext.AddRangeAsync(logDays);
2021-11-27 21:41:45 +08:00
var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area));
Assert.Equal(2, bulkShardingExpression.Count);
Assert.True(bulkShardingExpression.ContainsKey("A"));
Assert.True(bulkShardingExpression.ContainsKey("B"));
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
Assert.Equal(1, bulkShardingTableExpression.Count());
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
Assert.Equal(1, noShardingExpression.Count());
2021-11-26 23:11:09 +08:00
var isShardingDbContext = _virtualDbContext.IsShardingDbContext();
2021-11-27 21:41:45 +08:00
Assert.True(isShardingDbContext);
var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext();
Assert.True(isShardingTableDbContext);
var shardingDbContext = _virtualDbContext.GetType().IsShardingDbContext();
Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity();
var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity();
var dics = new SortedDictionary<string, string>(new NoShardingFirstComparer());
var dicTails = new List<string>() { emptyTailIdentity, aTailIdentity, bTailIdentity };
for (int i = 0; i < 10; i++)
{
dics.Clear();
var reOrderList = dicTails.OrderBy(o => Guid.NewGuid()).ToList();
foreach (var tail in reOrderList)
{
dics.Add(tail, null);
}
Assert.Equal($"{emptyTailIdentity},{aTailIdentity},{bTailIdentity}", string.Join(",", dics.Keys));
}
2021-11-26 23:11:09 +08:00
}
public class SequenceClass
{
public string Id { get; set; }
public string T { get; set; }
}
[Fact]
public void TestEntityMetadataManager()
{
var objMetadata0 = _entityMetadataManager.TryGet(typeof(object));
Assert.Null(objMetadata0);
var objMetadata1 = _entityMetadataManager.TryGet<object>();
Assert.Null(objMetadata1);
var objMetadata2 = _entityMetadataManager.TryGet(typeof(SysUserMod));
Assert.NotNull(objMetadata2);
var objMetadata3 = _entityMetadataManager.TryGet<SysUserMod>();
Assert.NotNull(objMetadata3);
var sysUserModIsShardingTable0 = _entityMetadataManager.IsShardingTable(typeof(SysUserMod));
Assert.True(sysUserModIsShardingTable0);
var sysUserModIsShardingTable1 = _entityMetadataManager.IsShardingTable<SysUserMod>();
Assert.True(sysUserModIsShardingTable1);
var sysUserModIsShardingDataSource0 = _entityMetadataManager.IsShardingDataSource(typeof(SysUserMod));
Assert.False(sysUserModIsShardingDataSource0);
var sysUserModIsShardingDataSource1 = _entityMetadataManager.IsShardingDataSource<SysUserMod>();
Assert.False(sysUserModIsShardingDataSource1);
}
[Fact]
public void TestShardingComparer()
{
var x = new Guid("7CDE28F8-D548-B96D-1C61-39FFE37AE492");
var y = new Guid("3425D899-291D-921B-DDE4-49FFE37AE493");
//asc y<x c# compare guid
var compare0 = x.CompareTo(y);
2021-11-27 21:41:45 +08:00
Assert.True(compare0 > 0);
2021-11-26 23:11:09 +08:00
//asc x<y db compare uniqueidentifier
var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0);
}
[Fact]
public void TestConnectionStringManager()
{
_virtualDbContext.ReadWriteSeparationReadOnly();
var connectionString1 = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName, true);
Assert.Equal(connectionString1, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;");
_virtualDbContext.ReadWriteSeparationWriteOnly();
}
//[Fact]
//public async Task Route_TEST()
//{
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
// var routeResults1 = _tableRouteRuleEngineFactory.Route(queryable1);
// Assert.Equal(1,routeResults1.Count());
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
// var ids = new[] {"339", "124","142"};
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
// var routeResult2s = _tableRouteRuleEngineFactory.Route(queryable2);
// Assert.Equal(2,routeResult2s.Count());
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
// Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count());
// Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
//}
[Fact]
public async Task ToList_All_Route_Test()
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserMod>("00");
var mod00s = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(333, mod00s.Count);
}
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
}
[Fact]
public async Task ToList_All_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
foreach (var sysUserMod in pageResult)
{
Assert.Equal(pageDescAge, sysUserMod.Age);
pageDescAge--;
}
}
[Fact]
public async Task ToList_Join_Test()
{
var list = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
u.Id,
u.Age,
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
}).ToListAsync();
2021-11-27 21:41:45 +08:00
var list2 = list.OrderBy(o => o.Age).Select(o => o.Age).Distinct().ToList();
2021-11-26 23:11:09 +08:00
Assert.Equal(24000, list.Count());
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
var list1 = await queryable.ToListAsync();
Assert.Equal(24, list1.Count());
Assert.DoesNotContain(list1, o => o.Name != "name_300");
}
[Fact]
public async Task ToList_OrderBy_Asc_Desc_Test()
{
var modascs = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToListAsync();
Assert.Equal(1000, modascs.Count);
var i = 1;
foreach (var age in modascs)
{
Assert.Equal(i, age.Age);
i++;
}
var moddescs = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(1000, moddescs.Count);
var j = 1000;
foreach (var age in moddescs)
{
Assert.Equal(j, age.Age);
j--;
}
}
[Fact]
public async Task ToList_Id_In_Test()
{
2021-11-27 21:41:45 +08:00
var ids = new[] { "1", "2", "3", "4" };
2021-11-26 23:11:09 +08:00
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "1", "2", "3", "4" }.Contains(o.Id)).ToListAsync();
foreach (var id in ids)
{
Assert.Contains(sysUserMods, o => o.Id == id);
}
Assert.DoesNotContain(sysUserMods, o => o.Age > 4);
}
[Fact]
public async Task ToList_Id_Eq_Test()
{
2021-11-27 21:41:45 +08:00
var id = 3;
2021-11-26 23:11:09 +08:00
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == id.ToString()).ToListAsync();
Assert.Single(mods);
var mods1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "4").ToListAsync();
Assert.Single(mods1);
Assert.Equal("3", mods[0].Id);
}
[Fact]
public async Task ToList_Id_Not_Eq_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync();
Assert.Equal(999, mods.Count);
Assert.DoesNotContain(mods, o => o.Id == "3");
}
[Fact]
public async Task ToList_Id_Not_Eq_Skip_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderBy(o => o.Age).Skip(2).ToListAsync();
Assert.Equal(997, mods.Count);
Assert.DoesNotContain(mods, o => o.Id == "3");
Assert.Equal(4, mods[0].Age);
Assert.Equal(5, mods[1].Age);
var modsDesc = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderByDescending(o => o.Age).Skip(13).ToListAsync();
Assert.Equal(986, modsDesc.Count);
Assert.DoesNotContain(mods, o => o.Id == "3");
Assert.Equal(987, modsDesc[0].Age);
Assert.Equal(986, modsDesc[1].Age);
}
[Fact]
public async Task ToList_Name_Eq_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_3").ToListAsync();
Assert.Single(mods);
Assert.Equal("3", mods[0].Id);
}
[Fact]
public async Task ToList_Id_Eq_Not_In_Db_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync();
Assert.Empty(mods);
}
[Fact]
public async Task ToList_Name_Eq_Not_In_Db_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").ToListAsync();
Assert.Empty(mods);
}
[Fact]
public async Task FirstOrDefault_Order_By_Id_Test()
{
var sysUserModAge = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).FirstOrDefaultAsync();
Assert.True(sysUserModAge != null && sysUserModAge.Id == "1");
var sysUserModAgeDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).FirstOrDefaultAsync();
Assert.True(sysUserModAgeDesc != null && sysUserModAgeDesc.Id == "1000");
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Id).FirstOrDefaultAsync();
Assert.True(sysUserMod != null && sysUserMod.Id == "1");
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
}
[Fact]
public async Task FirstOrDefault2()
{
var x = new Object[] { "1", "2" };
var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync();
Assert.NotNull(sysUserModab);
Assert.True(sysUserModab.Id == "1");
var sysUserModaa = await _virtualDbContext.Set<SysUserMod>().Where(o => "1".Equals(o.Id)).FirstOrDefaultAsync();
Assert.NotNull(sysUserModaa);
Assert.True(sysUserModaa.Id == "1");
2021-11-26 23:11:09 +08:00
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
Assert.True(sysUserMod.Id == "1");
var sysUserModxx = await _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefaultAsync();
Assert.NotNull(sysUserModxx);
Assert.True(x.Contains(sysUserModxx.Id));
Assert.NotNull(sysUserMod);
2021-11-26 23:11:09 +08:00
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
Assert.Equal(sysUserMod, userMod);
Assert.True(sysUserMod.Id == "1");
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
Assert.True(user198.Id == "198");
var userId198 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefaultAsync();
Assert.Equal(userId198, "198");
}
[Fact]
public async Task FirstOrDefault3()
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id);
}
[Fact]
public async Task FirstOrDefault4()
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "1").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
Assert.True(sysUserMod.Id != "1");
}
[Fact]
public async Task FirstOrDefault5()
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").FirstOrDefaultAsync();
Assert.Null(sysUserMod);
}
[Fact]
public async Task Count_Test()
{
var a = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1000").CountAsync();
Assert.Equal(1, a);
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").CountAsync();
Assert.Equal(999, b);
}
[Fact]
public async Task Sum_Test()
{
var a = await _virtualDbContext.Set<SysUserMod>().SumAsync(o => o.Age);
var expected = 0;
for (int i = 1; i <= 1000; i++)
{
expected += i;
}
Assert.Equal(expected, a);
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").SumAsync(o => o.Age);
Assert.Equal(expected - 1000, b);
}
[Fact]
public async Task Max_Test()
{
var a = await _virtualDbContext.Set<SysUserMod>().MaxAsync(o => o.Age);
Assert.Equal(1000, a);
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").MaxAsync(o => o.Age);
Assert.Equal(999, b);
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age < 500).MaxAsync(o => o.Age);
Assert.Equal(499, c);
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age <= 500).MaxAsync(o => o.Age);
Assert.Equal(500, e);
}
[Fact]
public async Task Max_Join_Test()
{
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
2021-11-27 21:41:45 +08:00
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
2021-11-26 23:11:09 +08:00
var maxSalary = await queryable.MaxAsync(o => o.Salary);
Assert.Equal(1390000, maxSalary);
}
[Fact]
public async Task Min_Test()
{
var a = await _virtualDbContext.Set<SysUserMod>().MinAsync(o => o.Age);
Assert.Equal(1, a);
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").MinAsync(o => o.Age);
Assert.Equal(2, b);
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).MinAsync(o => o.Age);
Assert.Equal(501, c);
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).MinAsync(o => o.Age);
Assert.Equal(500, e);
}
[Fact]
public async Task Any_Test()
{
var a = await _virtualDbContext.Set<SysUserMod>().AnyAsync(o => o.Age == 100);
Assert.True(a);
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").AnyAsync(o => o.Age == 1);
Assert.False(b);
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).AnyAsync(o => o.Age <= 500);
Assert.False(c);
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).AnyAsync(o => o.Age <= 500);
Assert.True(e);
}
[Fact]
public async Task Group_Test()
{
2021-11-27 21:41:45 +08:00
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
2021-11-26 23:11:09 +08:00
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
2021-11-27 21:41:45 +08:00
group u by new
{
UId = u.UserId
}
2021-11-26 23:11:09 +08:00
into g
2021-11-27 21:41:45 +08:00
select new
{
GroupUserId = g.Key.UId,
Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary)
}).ToListAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(2, group.Count);
Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task OrderCountTest()
{
var asyncCount = await _virtualDbContext.Set<Order>().CountAsync();
Assert.Equal(320, asyncCount);
2021-11-27 21:41:45 +08:00
var syncCount = _virtualDbContext.Set<Order>().Count();
2021-11-26 23:11:09 +08:00
Assert.Equal(320, syncCount);
2021-11-27 21:41:45 +08:00
var countA = await _virtualDbContext.Set<Order>().CountAsync(o => o.Area == "A");
var countB = await _virtualDbContext.Set<Order>().CountAsync(o => o.Area == "B");
var countC = await _virtualDbContext.Set<Order>().CountAsync(o => o.Area == "C");
Assert.Equal(320, countA + countB + countC);
2021-11-26 23:11:09 +08:00
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
2021-11-27 21:41:45 +08:00
var fourCount = await _virtualDbContext.Set<Order>().Where(o => o.CreateTime >= fourBegin && o.CreateTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
2021-11-26 23:11:09 +08:00
}
[Fact]
public async Task OrderFirstTest()
{
2021-11-27 21:41:45 +08:00
var threeMonth = new DateTime(2021, 3, 1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o => o.CreateTime == threeMonth);//<2F><>59<35><39> 1<><31>31<33><31>2<EFBFBD><32>28<32><38>
2021-11-26 23:11:09 +08:00
Assert.NotNull(order);
2021-11-27 21:41:45 +08:00
Assert.Equal(59, order.Money);
Assert.Equal("C", order.Area);
2021-11-26 23:11:09 +08:00
}
[Fact]
public async Task OrderOrderTest()
{
var orders = await _virtualDbContext.Set<Order>().OrderBy(o => o.CreateTime).ToListAsync();
2021-11-27 21:41:45 +08:00
Assert.Equal(320, orders.Count);
2021-11-26 23:11:09 +08:00
var i = 0;
foreach (var order in orders)
{
2021-11-27 21:41:45 +08:00
Assert.Equal(i, order.Money);
2021-11-26 23:11:09 +08:00
i++;
}
var threeMonth = new DateTime(2021, 3, 1);
2021-11-27 21:41:45 +08:00
var orderPage = await _virtualDbContext.Set<Order>().Where(o => o.CreateTime > threeMonth).OrderByDescending(o => o.CreateTime).ToShardingPageAsync(1, 20);
2021-11-26 23:11:09 +08:00
Assert.Equal(20, orderPage.Data.Count);
2021-11-27 21:41:45 +08:00
Assert.Equal(260, orderPage.Total);
2021-11-26 23:11:09 +08:00
var j = 319;
foreach (var order in orderPage.Data)
{
Assert.Equal(j, order.Money);
j--;
}
2021-11-27 21:41:45 +08:00
var orderPage1 = await _virtualDbContext.Set<Order>().Where(o => o.CreateTime > threeMonth).OrderBy(o => o.CreateTime).ToShardingPageAsync(1, 20);
2021-11-26 23:11:09 +08:00
Assert.Equal(20, orderPage1.Data.Count);
Assert.Equal(260, orderPage1.Total);
var j1 = 60;
foreach (var order in orderPage1.Data)
{
Assert.Equal(j1, order.Money);
j1++;
}
}
[Fact]
public async Task LogDayCountTest()
{
var countAsync = await _virtualDbContext.Set<LogDay>().CountAsync();
2021-11-27 21:41:45 +08:00
Assert.Equal(3000, countAsync);
2021-11-26 23:11:09 +08:00
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDay>("20210102");
var countAsync1 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDay>("20210103", "20210104");
var countAsync2 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public void LogDayTableSeparatorTest()
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName();
2021-11-27 21:41:45 +08:00
Assert.Equal(nameof(LogDay), virtualTableName);
2021-11-26 23:11:09 +08:00
var table = _virtualTableManager.GetVirtualTable(virtualTableName);
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
2021-11-27 21:41:45 +08:00
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator));
2021-11-26 23:11:09 +08:00
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable();
Assert.True(isShardingTable);
var isShardingDataSource = entityMetadata.IsShardingDataSource();
Assert.False(isShardingDataSource);
var emptySeparator = string.IsNullOrWhiteSpace(entityMetadata.TableSeparator);
Assert.True(emptySeparator);
Assert.Null(entityMetadata.AutoCreateDataSourceTable);
}
[Fact]
public async Task LogDayShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
2021-11-27 21:41:45 +08:00
var page = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
2021-11-26 23:11:09 +08:00
2021-11-27 21:41:45 +08:00
var page1 = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
2021-11-26 23:11:09 +08:00
}
[Fact]
public async Task Order_Average()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var moneyAverage = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync();
Assert.Equal(105, moneyAverage);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("C");
var sum = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).SumAsync();
Assert.Equal(0, sum);
2021-11-26 23:11:09 +08:00
var sum1 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (long?)o.Money).SumAsync();
Assert.Equal(0, sum1);
2021-11-26 23:11:09 +08:00
var sum2 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (int)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum2);
var sum3 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (int?)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum3);
var sum4 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (decimal)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum4);
var sum5 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (decimal?)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum5);
var sum6 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (double)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum6);
var sum7 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (double?)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum7);
var sum8 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (float)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum8);
var sum9 = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => (float?)o.Money).SumAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(0, sum9);
}
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintDataSource<Order>("C");
var sum = await _virtualDbContext.Set<Order>()
2021-11-27 21:41:45 +08:00
.Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).SumAsync();
Assert.Equal(0, sum);
2021-11-26 23:11:09 +08:00
}
var max = await _virtualDbContext.Set<Order>().MaxAsync(o => o.Money);
2021-11-27 21:41:45 +08:00
Assert.Equal(319, max);
var all = await _virtualDbContext.Set<Order>().AllAsync(o => o.Money <= 321);
2021-11-26 23:11:09 +08:00
Assert.True(all);
2021-11-27 21:41:45 +08:00
var longCount = await _virtualDbContext.Set<Order>().LongCountAsync();
Assert.Equal(320, longCount);
2021-11-26 23:11:09 +08:00
}
[Fact]
public async Task Order_Max()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
2021-11-27 21:41:45 +08:00
var moneyMax = await _virtualDbContext.Set<Order>()
2021-11-26 23:11:09 +08:00
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).MaxAsync();
Assert.Equal(120, moneyMax);
2021-11-27 21:41:45 +08:00
var moneyMax1 = await _virtualDbContext.Set<Order>()
2021-11-26 23:11:09 +08:00
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (long?)o.Money).MaxAsync();
Assert.Equal(120, moneyMax1);
var moneyMax2 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (int)o.Money).MaxAsync();
Assert.Equal(120, moneyMax2);
var moneyMax3 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (int?)o.Money).MaxAsync();
Assert.Equal(120, moneyMax3);
var moneyMax4 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (double)o.Money).MaxAsync();
Assert.Equal(120, moneyMax4);
var moneyMax5 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (double?)o.Money).MaxAsync();
Assert.Equal(120, moneyMax5);
var moneyMax6 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (float)o.Money).MaxAsync();
Assert.Equal(120, moneyMax6);
var moneyMax7 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (float?)o.Money).MaxAsync();
Assert.Equal(120, moneyMax7);
var moneyMax8 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (decimal)o.Money).MaxAsync();
Assert.Equal(120, moneyMax8);
var moneyMax9 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (decimal?)o.Money).MaxAsync();
Assert.Equal(120, moneyMax9);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("C");
try
{
var max = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).MaxAsync();
}
catch (Exception e)
{
Assert.True(e.Message.Contains("contains"));
}
}
}
[Fact]
public async Task Order_Min()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var moneyMin = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).MinAsync();
Assert.Equal(90, moneyMin);
var moneyMin1 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (long?)o.Money).MinAsync();
Assert.Equal(90, moneyMin1);
var moneyMin2 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (int)o.Money).MinAsync();
Assert.Equal(90, moneyMin2);
var moneyMin3 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (int?)o.Money).MinAsync();
Assert.Equal(90, moneyMin3);
var moneyMin4 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (float)o.Money).MinAsync();
Assert.Equal(90, moneyMin4);
var moneyMin5 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (float?)o.Money).MinAsync();
Assert.Equal(90, moneyMin5);
var moneyMin6 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (double)o.Money).MinAsync();
Assert.Equal(90, moneyMin6);
var moneyMin7 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (double?)o.Money).MinAsync();
Assert.Equal(90, moneyMin7);
var moneyMin8 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (decimal)o.Money).MinAsync();
Assert.Equal(90, moneyMin8);
var moneyMin9 = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => (decimal?)o.Money).MinAsync();
Assert.Equal(90, moneyMin9);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("C");
try
{
var max = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).MinAsync();
}
catch (InvalidOperationException e)
{
Assert.True(e.Message.Contains("contains"));
}
}
}
[Fact]
public async Task Order_Entity()
{
2021-11-27 21:41:45 +08:00
var x = await _virtualDbContext.Set<Order>().OrderBy(o => o.Money).LastOrDefaultAsync();
2021-11-26 23:11:09 +08:00
Assert.NotNull(x);
2021-11-27 21:41:45 +08:00
Assert.Equal(319, x.Money);
2021-11-26 23:11:09 +08:00
var x1 = await _virtualDbContext.Set<Order>().OrderBy(o => o.Money).LastAsync();
2021-11-27 21:41:45 +08:00
Assert.Equal(x, x1);
2021-11-26 23:11:09 +08:00
var y = await _virtualDbContext.Set<Order>().OrderBy(o => o.Money).FirstOrDefaultAsync();
Assert.NotNull(y);
Assert.Equal(0, y.Money);
var y1 = await _virtualDbContext.Set<Order>().OrderBy(o => o.Money).FirstAsync();
Assert.Equal(y, y1);
2021-11-27 21:41:45 +08:00
var z = await _virtualDbContext.Set<Order>().SingleOrDefaultAsync(o => o.Money == 13);
2021-11-26 23:11:09 +08:00
var z1 = await _virtualDbContext.Set<Order>().SingleAsync(o => o.Money == 13);
Assert.Equal(z, z1);
}
[Fact]
public async Task OrderReadWrite()
{
2021-11-27 21:41:45 +08:00
//<2F>л<EFBFBD><D0BB><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>ݿ⣬ֻ<E2A3AC><D6BB><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><41><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>ȡB<C8A1><42><EFBFBD><EFBFBD>Դ
2021-11-26 23:11:09 +08:00
_virtualDbContext.ReadWriteSeparationReadOnly();
2021-11-27 21:41:45 +08:00
var list = await _virtualDbContext.Set<Order>().Where(o => o.Money == 1).ToListAsync();
2021-11-26 23:11:09 +08:00
Assert.Equal(2, list.Count);
_virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
var areaB = await _virtualDbContext.Set<Order>().Where(o => o.Area == "B").FirstOrDefaultAsync();
Assert.Null(areaB);
}
_virtualDbContext.ReadWriteSeparationReadOnly();
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
var areaB = await _virtualDbContext.Set<Order>().Where(o => o.Area == "B").FirstOrDefaultAsync();
Assert.NotNull(areaB);
}
_virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>())
{
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
var areaB = await _virtualDbContext.Set<Order>().Where(o => o.Area == "B").FirstOrDefaultAsync();
Assert.NotNull(areaB);
}
}
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>())
{
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
var areaB = await _virtualDbContext.Set<Order>().Where(o => o.Area == "B").FirstOrDefaultAsync();
Assert.Null(areaB);
}
}
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
var areaB = await _virtualDbContext.Set<Order>().Where(o => o.Area == "B").FirstOrDefaultAsync();
Assert.Null(areaB);
}
}
[Fact]
public async Task LogWeekDateTimeCountTest()
{
var countAsync = await _virtualDbContext.Set<LogWeekDateTime>().CountAsync();
Assert.Equal(300, countAsync);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogWeekDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogWeekDateTime>("20210419_25");
var countAsync1 = await _virtualDbContext.Set<LogWeekDateTime>().CountAsync();
Assert.Equal(7, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogWeekDateTime>("20210419_25", "20210426_02");
var countAsync2 = await _virtualDbContext.Set<LogWeekDateTime>().CountAsync();
Assert.Equal(14, countAsync2);
}
}
[Fact]
public async Task LogWeekDateTimeShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page = await _virtualDbContext.Set<LogWeekDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(31, page.Total);
var page1 = await _virtualDbContext.Set<LogWeekDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
[Fact]
public async Task LogWeekTimeLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogWeekTimeLong>().CountAsync();
Assert.Equal(300, countAsync);
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var fourCount = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogWeekTimeLong>("20210419_25");
var countAsync1 = await _virtualDbContext.Set<LogWeekTimeLong>().CountAsync();
Assert.Equal(7, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogWeekTimeLong>("20210419_25", "20210426_02");
var countAsync2 = await _virtualDbContext.Set<LogWeekTimeLong>().CountAsync();
Assert.Equal(14, countAsync2);
}
}
[Fact]
public async Task LogWeekDateLongShardingPage()
{
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var page = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(31, page.Total);
var page1 = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
[Fact]
public async Task LogYearTimeLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogYearDateTime>().CountAsync();
Assert.Equal(600, countAsync);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogYearDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogYearDateTime>("2020");
var countAsync1 = await _virtualDbContext.Set<LogYearDateTime>().CountAsync();
Assert.Equal(366, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
2021-11-27 21:41:45 +08:00
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogYearDateTime>("2021");
2021-11-26 23:11:09 +08:00
var countAsync2 = await _virtualDbContext.Set<LogYearDateTime>().CountAsync();
Assert.Equal(234, countAsync2);
}
}
[Fact]
public async Task LogYearDateLongShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page = await _virtualDbContext.Set<LogYearDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(31, page.Total);
var page1 = await _virtualDbContext.Set<LogYearDateTime>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
2021-11-27 21:41:45 +08:00
[Fact]
public async Task LogMonthTimeLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogMonthLong>().CountAsync();
Assert.Equal(300, countAsync);
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var fourCount = await _virtualDbContext.Set<LogMonthLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogMonthLong>("202104");
var countAsync1 = await _virtualDbContext.Set<LogMonthLong>().CountAsync();
Assert.Equal(30, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogMonthLong>("202104", "202105");
var countAsync2 = await _virtualDbContext.Set<LogMonthLong>().CountAsync();
Assert.Equal(61, countAsync2);
}
}
[Fact]
public async Task LogMonthDateLongShardingPage()
{
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var page = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(31, page.Total);
var page1 = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
[Fact]
public async Task LogYearLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogYearLong>().CountAsync();
Assert.Equal(300, countAsync);
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var fourCount = await _virtualDbContext.Set<LogYearLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogYearLong>("2021");
var countAsync1 = await _virtualDbContext.Set<LogYearLong>().CountAsync();
Assert.Equal(300, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
}
[Fact]
public async Task LogYearLongShardingPage()
{
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var page = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(31, page.Total);
var page1 = await _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
[Fact]
public async Task CrudTest()
{
var logNoSharding = new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
};
var logNoShardings = new List<LogNoSharding>()
{
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
},
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
}
};
var logNoSharding1 = new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
};
var logNoSharding1s = new List<LogNoSharding>()
{
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
},
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
}
};
using (var tran = await _virtualDbContext.Database.BeginTransactionAsync())
{
try
{
await _virtualDbContext.AddAsync(logNoSharding);
await _virtualDbContext.AddRangeAsync(logNoShardings);
await _virtualDbContext.Set<LogNoSharding>().AddAsync(logNoSharding1);
await _virtualDbContext.Set<LogNoSharding>().AddRangeAsync(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync();
tran.Commit();
}
catch (Exception e)
{
tran.Rollback();
}
}
logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Update(logNoSharding);
logNoShardings.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.UpdateRange(logNoShardings);
logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1);
logNoSharding1s.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync();
_virtualDbContext.Remove(logNoSharding);
_virtualDbContext.RemoveRange(logNoShardings);
_virtualDbContext.Set<LogNoSharding>().Remove(logNoSharding1);
logNoSharding1s.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.Set<LogNoSharding>().RemoveRange(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync();
}
[Fact]
public async Task CrudTest1()
{
var logNoSharding = new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
};
var logNoShardings = new List<LogNoSharding>()
{
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
},
new LogNoSharding()
{
Id = Guid.NewGuid().ToString("n"),
Body = DateTime.Now.ToString("yyyyMMddHHmmss"),
CreationTime = DateTime.Now
}
};
using (var tran = await _virtualDbContext.Database.BeginTransactionAsync())
{
try
{
await _virtualDbContext.AddAsync((object)logNoSharding);
await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o=>(object)o).ToArray());
await _virtualDbContext.SaveChangesAsync();
tran.Commit();
}
catch (Exception e)
{
tran.Rollback();
}
}
logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Update((object)logNoSharding);
logNoShardings.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.UpdateRange(logNoShardings.Select(o => (object)o).ToArray());
await _virtualDbContext.SaveChangesAsync();
_virtualDbContext.Remove((object)logNoSharding);
_virtualDbContext.RemoveRange(logNoShardings.Select(o => (object)o).ToArray());
await _virtualDbContext.SaveChangesAsync();
}
[Fact]
public async Task Int_ToList_All_Route_Test()
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserModInt>("00");
var mod00s = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(333, mod00s.Count);
}
var mods = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
}
[Fact]
public async Task Int_ToList_All_Test()
{
var mods = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
var pageResult = await _virtualDbContext.Set<SysUserModInt>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
foreach (var sysUserMod in pageResult)
{
Assert.Equal(pageDescAge, sysUserMod.Age);
pageDescAge--;
}
}
[Fact]
public async Task LogDayLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogDayLong>().CountAsync();
Assert.Equal(3000, countAsync);
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var fourCount = await _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDayLong>("20210102");
var countAsync1 = await _virtualDbContext.Set<LogDayLong>().CountAsync();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDayLong>("20210103", "20210104");
var countAsync2 = await _virtualDbContext.Set<LogDayLong>().CountAsync();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public async Task LogDayLongShardingPage()
{
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var page = await _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = await _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
2021-11-26 23:11:09 +08:00
// [Fact]
// public async Task Group_API_Test()
// {
// var ids = new[] {"200", "300"};
// var dateOfMonths = new[] {202111, 202110};
// var group = await _virtualDbContext.Set<SysUserSalary>()
// .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
// .ShardingGroupByAsync(g => new {UId = g.UserId}, g => new
// {
//
// GroupUserId = g.Key.UId,
// Count = g.Count(),
// TotalSalary = g.Sum(o => o.Salary),
// AvgSalary = g.Average(o => o.Salary),
// AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
// MinSalary = g.Min(o => o.Salary),
// MaxSalary = g.Max(o => o.Salary)
// });
// Assert.Equal(2, group.Count);
// Assert.Equal(2, group[0].Count);
// Assert.Equal(2260000, group[0].TotalSalary);
// Assert.Equal(1130000, group[0].AvgSalary);
// Assert.Equal(11300, group[0].AvgSalaryDecimal);
// Assert.Equal(1120000, group[0].MinSalary);
// Assert.Equal(1140000, group[0].MaxSalary);
// }
}
}