From 34c38445e68c7c4c7618c35363891058db7eba56 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Tue, 9 Aug 2022 14:13:52 +0800 Subject: [PATCH] x.6.0.27 --- nuget-publish.bat | 8 +- test/ShardingCore.Test/ShardingTest.cs | 226 +++++++++++------- test/ShardingCore.Test/ShardingTestSync.cs | 109 +++++++-- .../SysUserSalaryVirtualTableRoute.cs | 1 + test/ShardingCore.Test2x/ShardingTest.cs | 109 +++++++-- test/ShardingCore.Test2x/ShardingTestSync.cs | 109 +++++++-- test/ShardingCore.Test3x/ShardingTest.cs | 109 +++++++-- test/ShardingCore.Test3x/ShardingTestSync.cs | 109 +++++++-- test/ShardingCore.Test5x/ShardingTest.cs | 109 +++++++-- test/ShardingCore.Test5x/ShardingTestSync.cs | 109 +++++++-- 10 files changed, 731 insertions(+), 267 deletions(-) diff --git a/nuget-publish.bat b/nuget-publish.bat index 18e5379f..a9b07ec2 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.6.0.26 -set EFCORE3=3.6.0.26 -set EFCORE5=5.6.0.26 -set EFCORE6=6.6.0.26 +set EFCORE2=2.6.0.27 +set EFCORE3=3.6.0.27 +set EFCORE5=5.6.0.27 +set EFCORE6=6.6.0.27 ::删除所有bin与obj下的文件 @echo off diff --git a/test/ShardingCore.Test/ShardingTest.cs b/test/ShardingCore.Test/ShardingTest.cs index d8e48343..4a2c89bf 100644 --- a/test/ShardingCore.Test/ShardingTest.cs +++ b/test/ShardingCore.Test/ShardingTest.cs @@ -11,8 +11,10 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualRoutes.Abstractions; +using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Extensions.ShardingPageExtensions; @@ -47,6 +49,7 @@ public class ShardingTest private readonly IRouteTailFactory _routeTailFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; + private readonly ITableRouteRuleEngineFactory _tableRouteRuleEngineFactory; private readonly IShardingComparer _shardingComparer; public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration) @@ -55,7 +58,7 @@ public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeC _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager, _virtualDataSource); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); @@ -63,8 +66,9 @@ public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeC _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); _shardingComparer = shardingRuntimeContext.GetShardingComparer(); _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory(); + _tableRouteRuleEngineFactory = shardingRuntimeContext.GetService(); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); - _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory); + _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), _readWriteConnectorFactory); } // [Fact] // public void RouteParseCompileCacheTest() @@ -211,7 +215,7 @@ public async Task GenericTest() { new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), }); Assert.Equal(x1x1, x2x2); Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode()); - var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;",null); + var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", null); Assert.True(succeedAddConnectionString); var connectionString = _shardingConnectionStringResolver.GetConnectionString("X", null); Assert.Equal("Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", connectionString); @@ -361,7 +365,7 @@ public async Task ToList_All_Test() Assert.Equal(pageDescAge, sysUserMod.Age); pageDescAge--; } - var skip10First= await _virtualDbContext.Set().Skip(10).OrderByDescending(o => o.Age).FirstOrDefaultAsync(); + var skip10First = await _virtualDbContext.Set().Skip(10).OrderByDescending(o => o.Age).FirstOrDefaultAsync(); Assert.Equal(skip10First, pageResult[0]); } @@ -524,9 +528,9 @@ public async Task ToList_Name_Eq_Test() [Fact] public async Task ToList_Id_Eq_Not_In_Db_Test() { - var mod1s = await _virtualDbContext.Set().Where(o =>string.Compare(((IId)o).Id,"1001") ==0).ToListAsync(); + var mod1s = await _virtualDbContext.Set().Where(o => string.Compare(((IId)o).Id, "1001") == 0).ToListAsync(); var mod2s = await _virtualDbContext.Set().Where(o => string.Compare("1001", o.Id) == 0).ToListAsync(); - var mod3s = await _virtualDbContext.Set().Where(o => "1001".CompareTo(o.Id)==0).ToListAsync(); + var mod3s = await _virtualDbContext.Set().Where(o => "1001".CompareTo(o.Id) == 0).ToListAsync(); var mod4s = await _virtualDbContext.Set().Where(o => o.Id.CompareTo("1001") == 0).ToListAsync(); var mods = await _virtualDbContext.Set().Where(o => o.Id == "1001").ToListAsync(); Assert.Empty(mods); @@ -603,9 +607,9 @@ public async Task FirstOrDefault3() Assert.Equal("2", sysUserMod.Id); var sysUserMod2 = await _virtualDbContext.Set().Where(o => o.Name == "name_2").Select(o => new { - Name=o.Name + Name = o.Name }).FirstOrDefaultAsync(); - var sysUserMod1 = await _virtualDbContext.Set().Where(o => o.Name == "name_2").Select(o => new TestClass(o.Name,""){Id = o.Id}).FirstOrDefaultAsync(); + var sysUserMod1 = await _virtualDbContext.Set().Where(o => o.Name == "name_2").Select(o => new TestClass(o.Name, "") { Id = o.Id }).FirstOrDefaultAsync(); Assert.NotNull(sysUserMod1); } @@ -615,7 +619,7 @@ public class TestClass public string Aa { get; } public string Id { get; set; } - public TestClass(string name,string aa) + public TestClass(string name, string aa) { Name = name; Aa = aa; @@ -761,7 +765,7 @@ into g select new { UI = g.Key, - x=g.Sum(o=>o.SalaryDecimal), + x = g.Sum(o => o.SalaryDecimal), Now = dateTime }; var listAsync = await sql.ToListAsync(); @@ -790,47 +794,6 @@ into g Assert.Equal(1120000, group[0].MinSalary); Assert.Equal(1140000, group[0].MaxSalary); } - //[Fact] - //public async Task Group_Recently_Test() - //{ - // //var list =(from us in _virtualDbContext.Set().Where(o => ids.Contains(o.UserId)) - // // group us by new - // // { - // // UserId=us.UserId - // // } - // // into g - // // select new - // // { - // // UserId=g.Key.UserId, - // // DateOfMonth = g.Max(o=>o.DateOfMonth) - // // }).ToList(); - // //var y = list; - - // var ids = new List(){ "200", "300" }; - // List result = new List(ids.Count); - // var routeFilter = new List().AsQueryable().Where(o => ids.Contains(o.UserId)); - // //��ȡ��·��ʱ�䵹�� - // var tableRouteResults = _tableRouteRuleEngineFactory.Route(routeFilter) - // .Select(o => o.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList(); - // foreach (var tableRouteResult in tableRouteResults) - // { - // if(ids.IsEmpty()) - // break; - // using (_shardingRouteManager.CreateScope()) - // { - // _shardingRouteManager.Current.TryCreateOrAddMustTail(tableRouteResult); - // var queryable = _virtualDbContext.Set().Where(o => ids.Contains(o.UserId)) - // .GroupBy(o => new { o.UserId }, i => i, - // (i, u) => new { - // Data = u.OrderByDescending(o => o.DateOfMonth).FirstOrDefault() - // }); - // var r =await queryable.ToListAsync(); - // result.AddRange(r.Select(o=>o.Data)); - // var removeUserIds = result.Select(u => u.UserId).ToHashSet(); - // ids.RemoveAll(o => removeUserIds.Contains(o)); - // } - // } - //} [Fact] public async Task OrderCountTest() @@ -924,7 +887,7 @@ public async Task LogDayCountTest() public void LogDayTableSeparatorTest() { var tableRoute = _tableRouteManager.GetRoute(typeof(LogDay)); - var virtualTableName =tableRoute.EntityMetadata.LogicTableName; + var virtualTableName = tableRoute.EntityMetadata.LogicTableName; Assert.Equal(nameof(LogDay), virtualTableName); Assert.True(string.IsNullOrWhiteSpace(tableRoute.EntityMetadata.TableSeparator)); @@ -961,7 +924,7 @@ 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() - .Where(o => o.CreateTime >= fourBegin&& o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync(); + .Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync(); Assert.Equal(105, moneyAverage); using (_shardingRouteManager.CreateScope()) @@ -1693,35 +1656,136 @@ public async Task AsRouteTest() [Fact] public async Task QueryInner_Test() { - var sysUserMods = _virtualDbContext.Set().Select(o=>o); - var sysUserModInts = await _virtualDbContext.Set().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync(); + var sysUserMods = _virtualDbContext.Set().Select(o => o); + var sysUserModInts = await _virtualDbContext.Set().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToListAsync(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public async Task Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = await _virtualDbContext.Set() - // .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); - // } + [Fact] + public async Task Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToListAsync(); + 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 Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o=>o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o=>o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } + + [Fact] + public async Task Group_Recently_Test() + { + //var list =(from us in _virtualDbContext.Set().Where(o => ids.Contains(o.UserId)) + // group us by new + // { + // UserId=us.UserId + // } + // into g + // select new + // { + // UserId=g.Key.UserId, + // DateOfMonth = g.Max(o=>o.DateOfMonth) + // }).ToList(); + //var y = list; + + var ids = new List() { "200", "300" }; + List result = new List(ids.Count); + var routeFilter = new List().AsQueryable().Where(o => ids.Contains(o.UserId)); + var dataSourceRouteResult = new DataSourceRouteResult(new HashSet() { _virtualDataSource.DefaultDataSourceName }); + var tableRouteResults = _tableRouteRuleEngineFactory.Route(dataSourceRouteResult, routeFilter,new Dictionary(){{typeof(SysUserSalary),null}}) + .RouteUnits + .Select(o => o.TableRouteResult.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList(); + foreach (var tableRouteResult in tableRouteResults) + { + if (ids.IsEmpty()) + break; + using (_shardingRouteManager.CreateScope()) + { + _shardingRouteManager.Current.TryCreateOrAddMustTail(tableRouteResult); + var queryable = _virtualDbContext.Set().Where(o => ids.Contains(o.UserId)) + .GroupBy(o => new { o.UserId }, i => i, + (i, u) => new + { + Data = u.OrderByDescending(o => o.DateOfMonth).FirstOrDefault() + }); + var r = await queryable.ToListAsync(); + result.AddRange(r.Select(o => o.Data)); + var removeUserIds = result.Select(u => u.UserId).ToHashSet(); + ids.RemoveAll(o => removeUserIds.Contains(o)); + } + } + } } } \ No newline at end of file diff --git a/test/ShardingCore.Test/ShardingTestSync.cs b/test/ShardingCore.Test/ShardingTestSync.cs index 4f51f4ce..75fde0d8 100644 --- a/test/ShardingCore.Test/ShardingTestSync.cs +++ b/test/ShardingCore.Test/ShardingTestSync.cs @@ -1454,31 +1454,88 @@ public void QueryInner_Test() var sysUserModInts = _virtualDbContext.Set().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToList(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public void Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = _virtualDbContext.Set() - // .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) - // .ShardingGroupBy(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); - // } + [Fact] + public void Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToList(); + 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 void Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 void Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } diff --git a/test/ShardingCore.Test/Shardings/SysUserSalaryVirtualTableRoute.cs b/test/ShardingCore.Test/Shardings/SysUserSalaryVirtualTableRoute.cs index d2c437ce..20e42b59 100644 --- a/test/ShardingCore.Test/Shardings/SysUserSalaryVirtualTableRoute.cs +++ b/test/ShardingCore.Test/Shardings/SysUserSalaryVirtualTableRoute.cs @@ -17,6 +17,7 @@ namespace ShardingCore.Test.Shardings public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute { //public override bool? EnableRouteParseCompileCache => true; + protected override bool EnableHintRoute => true; public override string ShardingKeyToTail(object shardingKey) { diff --git a/test/ShardingCore.Test2x/ShardingTest.cs b/test/ShardingCore.Test2x/ShardingTest.cs index 3d2c7941..0695d151 100644 --- a/test/ShardingCore.Test2x/ShardingTest.cs +++ b/test/ShardingCore.Test2x/ShardingTest.cs @@ -1681,31 +1681,88 @@ public async Task QueryInner_Test() var sysUserModInts = await _virtualDbContext.Set().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public async Task Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = await _virtualDbContext.Set() - // .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); - // } + [Fact] + public async Task Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToListAsync(); + 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 Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } \ No newline at end of file diff --git a/test/ShardingCore.Test2x/ShardingTestSync.cs b/test/ShardingCore.Test2x/ShardingTestSync.cs index 9d76680c..d3a760ad 100644 --- a/test/ShardingCore.Test2x/ShardingTestSync.cs +++ b/test/ShardingCore.Test2x/ShardingTestSync.cs @@ -1454,31 +1454,88 @@ public void QueryInner_Test() var sysUserModInts = _virtualDbContext.Set().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToList(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public void Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = _virtualDbContext.Set() - // .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) - // .ShardingGroupBy(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); - // } + [Fact] + public void Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToList(); + 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 void Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 void Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } diff --git a/test/ShardingCore.Test3x/ShardingTest.cs b/test/ShardingCore.Test3x/ShardingTest.cs index f05e8388..6d6b2425 100644 --- a/test/ShardingCore.Test3x/ShardingTest.cs +++ b/test/ShardingCore.Test3x/ShardingTest.cs @@ -1681,31 +1681,88 @@ public async Task QueryInner_Test() var sysUserModInts = await _virtualDbContext.Set().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public async Task Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = await _virtualDbContext.Set() - // .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); - // } + [Fact] + public async Task Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToListAsync(); + 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 Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } \ No newline at end of file diff --git a/test/ShardingCore.Test3x/ShardingTestSync.cs b/test/ShardingCore.Test3x/ShardingTestSync.cs index 7855ec4d..b018b40e 100644 --- a/test/ShardingCore.Test3x/ShardingTestSync.cs +++ b/test/ShardingCore.Test3x/ShardingTestSync.cs @@ -1454,31 +1454,88 @@ public void QueryInner_Test() var sysUserModInts = _virtualDbContext.Set().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToList(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public void Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = _virtualDbContext.Set() - // .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) - // .ShardingGroupBy(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); - // } + [Fact] + public void Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToList(); + 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 void Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 void Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } diff --git a/test/ShardingCore.Test5x/ShardingTest.cs b/test/ShardingCore.Test5x/ShardingTest.cs index b654b19c..36d3d1eb 100644 --- a/test/ShardingCore.Test5x/ShardingTest.cs +++ b/test/ShardingCore.Test5x/ShardingTest.cs @@ -1681,31 +1681,88 @@ public async Task QueryInner_Test() var sysUserModInts = await _virtualDbContext.Set().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public async Task Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = await _virtualDbContext.Set() - // .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); - // } + [Fact] + public async Task Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToListAsync(); + 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 Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = await _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToListAsync(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } } \ No newline at end of file diff --git a/test/ShardingCore.Test5x/ShardingTestSync.cs b/test/ShardingCore.Test5x/ShardingTestSync.cs index dedb704f..5e14e836 100644 --- a/test/ShardingCore.Test5x/ShardingTestSync.cs +++ b/test/ShardingCore.Test5x/ShardingTestSync.cs @@ -1454,31 +1454,88 @@ public void QueryInner_Test() var sysUserModInts = _virtualDbContext.Set().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToList(); Assert.Equal(1000, sysUserModInts.Count); } - // [Fact] - // public void Group_API_Test() - // { - // var ids = new[] {"200", "300"}; - // var dateOfMonths = new[] {202111, 202110}; - // var group = _virtualDbContext.Set() - // .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) - // .ShardingGroupBy(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); - // } + [Fact] + public void Group_API_Test() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).ToList(); + 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 void Group_API_Test1() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderBy(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("200", group[0].GroupUserId); + 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 void Group_API_Test2() + { + var ids = new[] { "200", "300" }; + var dateOfMonths = new[] { 202111, 202110 }; + var group = _virtualDbContext.Set() + .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) + .GroupBy(g => new { UId = g.UserId }) + .Select(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) + }).OrderByDescending(o => o.TotalSalary).ToList(); + Assert.Equal(2, group.Count); + Assert.Equal(2, group[0].Count); + Assert.Equal("300", group[0].GroupUserId); + Assert.Equal(2690000, group[0].TotalSalary); + Assert.Equal(1345000, group[0].AvgSalary); + Assert.Equal(13450, group[0].AvgSalaryDecimal); + Assert.Equal(1330000, group[0].MinSalary); + Assert.Equal(1360000, group[0].MaxSalary); + } } }