diff --git a/test/ShardingCore.CommonTest/ShardingDataSource.cs b/test/ShardingCore.CommonTest/ShardingDataSource.cs deleted file mode 100644 index b4742bd0..00000000 --- a/test/ShardingCore.CommonTest/ShardingDataSource.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System.Collections.ObjectModel; -using System.Reflection; -using ShardingCore.Core.EntityMetadatas; -using ShardingCore.Core.VirtualRoutes; -using ShardingCore.Helpers; -using ShardingCore.Utils; -using Xunit; - -namespace ShardingCore.CommonTest -{ - public class ShardingDataSource - { - private readonly EntityMetadata _testEntityMetadata; - private readonly List _allDataSources; - public ShardingDataSource() - { - var entityMetadata = new EntityMetadata(typeof(TestEntity), nameof(TestEntity), typeof(ShardingDataSource), - new ReadOnlyCollection(typeof(TestEntity).GetProperties().ToList()), null); - var entityMetadataDataSourceBuilder = EntityMetadataDataSourceBuilder.CreateEntityMetadataDataSourceBuilder(entityMetadata); - entityMetadataDataSourceBuilder.ShardingProperty(o => o.Id); - entityMetadata.CheckShardingDataSourceMetadata(); - _testEntityMetadata = entityMetadata; - _allDataSources = Enumerable.Range(0,10).Select(o=>o.ToString()).ToList(); - } - - public static Func GetRouteFilter(object shardingValue, ShardingOperatorEnum shardingOperator, - string propertyName) - { - if (propertyName != nameof(TestEntity.Id)) - { - throw new Exception($"{nameof(propertyName)}:[{propertyName}] error"); - } - - var stringHashCode = ShardingCoreHelper.GetStringHashCode(shardingValue.ToString()); - var dataSourceName = (stringHashCode%10).ToString(); - switch (shardingOperator) - { - case ShardingOperatorEnum.Equal: return t => t == dataSourceName; - default: - { - return t => true; - } - } - } - - private void TestId1(IQueryable queryable) - { - var routePredicateExpression = ShardingUtil.GetRouteParseExpression(queryable,_testEntityMetadata,GetRouteFilter,false); - Assert.NotNull(routePredicateExpression); - var routePredicate = routePredicateExpression.GetRoutePredicate(); - - var list = _allDataSources.Where(routePredicate).ToList(); - Assert.NotNull(list); - Assert.Equal(1,list.Count); - var stringHashCode = ShardingCoreHelper.GetStringHashCode("1"); - var dataSourceName = (stringHashCode%10).ToString(); - Assert.Equal(dataSourceName,list[0]); - } - [Fact] - public void TestSingleDataSource() - { - var constantQueryable1 = new List().AsQueryable().Where(o=>o.Id=="1"); - TestId1(constantQueryable1); - var constantQueryable2 = new List().AsQueryable().Where(o=>"1"==o.Id); - TestId1(constantQueryable2); - var id = "1"; - var constantQueryable3 = new List().AsQueryable().Where(o=>o.Id==id); - TestId1(constantQueryable3); - var constantQueryable4 = new List().AsQueryable().Where(o=>id==o.Id); - TestId1(constantQueryable4); - var constantQueryable5 = new List().AsQueryable().Where(o=>o.Id.Equals("1")); - TestId1(constantQueryable5); - var constantQueryable6 = new List().AsQueryable().Where(o=>"1".Equals(o.Id)); - TestId1(constantQueryable6); - var constantQueryable7 = new List().AsQueryable().Where(o=>o.Id.Equals(id)); - TestId1(constantQueryable7); - var constantQueryable8 = new List().AsQueryable().Where(o=>o.Id.Equals(id)); - TestId1(constantQueryable8); - var ids = new []{"1"}; - var constantQueryable9 = new List().AsQueryable().Where(o=>ids.Contains(o.Id)); - TestId1(constantQueryable9); - var constantQueryable10 = new List().AsQueryable().Where(o=>new []{"1"}.Contains(o.Id)); - TestId1(constantQueryable10); - var ids1 = new List(){"1"}; - var constantQueryable11 = new List().AsQueryable().Where(o=>ids1.Contains(o.Id)); - TestId1(constantQueryable11); - var constantQueryable12 = new List().AsQueryable().Where(o=>new List(){"1"}.Contains(o.Id)); - TestId1(constantQueryable12); - var obj1 = new {Id="1"}; - var constantQueryable13 = new List().AsQueryable().Where(o=>o.Id==obj1.Id); - TestId1(constantQueryable13); - var constantQueryable14 = new List().AsQueryable().Where(o=>obj1.Id==o.Id); - TestId1(constantQueryable14); - var constantQueryable15 = new List().AsQueryable().Where(o=>o.Id.Equals(obj1.Id)); - TestId1(constantQueryable15); - var constantQueryable16 = new List().AsQueryable().Where(o=>obj1.Id.Equals(o.Id)); - TestId1(constantQueryable16); - var constantQueryable17 = new List().AsQueryable().Where(o=>new []{obj1.Id}.Contains(o.Id)); - TestId1(constantQueryable17); - } - - [Fact] - public void TestTwoDataSource() - { - - } - } - - public class TestEntity - { - public string Id { get; set; } - } -} diff --git a/test/ShardingCore.CommonTest/ShardingDataSourceMod.cs b/test/ShardingCore.CommonTest/ShardingDataSourceMod.cs new file mode 100644 index 00000000..7c6cd19b --- /dev/null +++ b/test/ShardingCore.CommonTest/ShardingDataSourceMod.cs @@ -0,0 +1,137 @@ +using System.Collections.ObjectModel; +using System.Reflection; +using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.VirtualRoutes; +using ShardingCore.Helpers; +using ShardingCore.Utils; +using Xunit; + +namespace ShardingCore.CommonTest +{ + public class ShardingDataSourceMod + { + private readonly EntityMetadata _testEntityMetadata; + private readonly List _allDataSources; + public ShardingDataSourceMod() + { + var entityMetadata = new EntityMetadata(typeof(TestEntity), nameof(TestEntity), typeof(ShardingDataSourceMod), + new ReadOnlyCollection(typeof(TestEntity).GetProperties().ToList()), null); + var entityMetadataDataSourceBuilder = EntityMetadataDataSourceBuilder.CreateEntityMetadataDataSourceBuilder(entityMetadata); + entityMetadataDataSourceBuilder.ShardingProperty(o => o.Id); + entityMetadata.CheckShardingDataSourceMetadata(); + _testEntityMetadata = entityMetadata; + _allDataSources = Enumerable.Range(0,10).Select(o=>o.ToString()).ToList(); + } + + public static Func GetRouteFilter(object shardingValue, ShardingOperatorEnum shardingOperator, + string propertyName) + { + if (propertyName != nameof(TestEntity.Id)) + { + throw new Exception($"{nameof(propertyName)}:[{propertyName}] error"); + } + + var stringHashCode = ShardingCoreHelper.GetStringHashCode(shardingValue.ToString()); + var dataSourceName = (stringHashCode%10).ToString(); + switch (shardingOperator) + { + case ShardingOperatorEnum.Equal: return t => t == dataSourceName; + default: + { + return t => true; + } + } + } + + private void TestId(IQueryable queryable, string[] dataSourceNames) + { + var routePredicateExpression = ShardingUtil.GetRouteParseExpression(queryable,_testEntityMetadata,GetRouteFilter,false); + Assert.NotNull(routePredicateExpression); + var routePredicate = routePredicateExpression.GetRoutePredicate(); + + var list = _allDataSources.Where(routePredicate).ToList(); + Assert.NotNull(list); + Assert.Equal(dataSourceNames.Length,list.Count); + foreach (var dataSourceName in dataSourceNames) + { + Assert.True(list.Any(o=>o==dataSourceName)); + } + } + + private void TestFor(List> queryables, string[] dataSourceNames) + { + foreach (var queryable in queryables) + { + TestId(queryable, dataSourceNames); + } + } + [Fact] + public void TestSingleDataSource() + { + var stringHashCode = ShardingCoreHelper.GetStringHashCode("1"); + var dataSourceName = (stringHashCode%10).ToString(); + var dataSourceNames = new []{dataSourceName}; + var id = "1"; + var ids = new []{"1"}; + var ids1 = new List(){"1"}; + var obj1 = new {Id="1"}; + var queryables=new List>() + { + new List().AsQueryable().Where(o=>o.Id=="1"), + new List().AsQueryable().Where(o=>o.Id=="1"&&o.Id!="2"), + new List().AsQueryable().Where(o=>"1"==o.Id), + new List().AsQueryable().Where(o=>o.Id==id), + new List().AsQueryable().Where(o=>id==o.Id), + new List().AsQueryable().Where(o=>o.Id.Equals("1")), + new List().AsQueryable().Where(o=>"1".Equals(o.Id)), + new List().AsQueryable().Where(o=>o.Id.Equals(id)), + new List().AsQueryable().Where(o=>o.Id.Equals(id)), + new List().AsQueryable().Where(o=>ids.Contains(o.Id)), + new List().AsQueryable().Where(o=>new []{"1"}.Contains(o.Id)), + new List().AsQueryable().Where(o=>ids1.Contains(o.Id)), + new List().AsQueryable().Where(o=>new List(){"1"}.Contains(o.Id)), + new List().AsQueryable().Where(o=>o.Id==obj1.Id), + new List().AsQueryable().Where(o=>obj1.Id==o.Id), + new List().AsQueryable().Where(o=>o.Id.Equals(obj1.Id)), + new List().AsQueryable().Where(o=>obj1.Id.Equals(o.Id)), + new List().AsQueryable().Where(o=>new []{obj1.Id}.Contains(o.Id)) + }; + TestFor(queryables,dataSourceNames); + } + + [Fact] + public void TestTwoDataSource() + { + var dataSourceNames = new []{( ShardingCoreHelper.GetStringHashCode("1")%10).ToString(),( ShardingCoreHelper.GetStringHashCode("2")%10).ToString()}; + var ids = new []{"1","2"}; + var ids1 = new List(){"1","2"}; + var obj1 = new {Id="1"}; + var obj2 = new {Id="2"}; + var queryables=new List>() + { + new List().AsQueryable().Where(o=>o.Id=="1"||o.Id=="2"), + new List().AsQueryable().Where(o=>(o.Id=="1"||o.Id=="2")&&o.Id!="3"), + new List().AsQueryable().Where(o=>(o.Id=="1"||o.Id=="2")&&!o.Id.Equals("3")), + new List().AsQueryable().Where(o=>"1"==o.Id||o.Id=="2"), + new List().AsQueryable().Where(o=>"1"==o.Id||"2"==o.Id), + new List().AsQueryable().Where(o=>o.Id.Equals("1")||o.Id=="2"), + new List().AsQueryable().Where(o=>o.Id.Equals("1")||o.Id.Equals("2")), + new List().AsQueryable().Where(o=>"1".Equals(o.Id)||o.Id=="2"), + new List().AsQueryable().Where(o=>"1".Equals(o.Id)||o.Id.Equals("2")), + new List().AsQueryable().Where(o=>"1".Equals(o.Id)||"2".Equals(o.Id)), + new List().AsQueryable().Where(o=>ids.Contains(o.Id)), + new List().AsQueryable().Where(o=>new []{"1","2"}.Contains(o.Id)), + new List().AsQueryable().Where(o=>ids1.Contains(o.Id)), + new List().AsQueryable().Where(o=>new List(){"1","2"}.Contains(o.Id)), + new List().AsQueryable().Where(o=>new List(){obj1.Id,obj2.Id}.Contains(o.Id)), + new List().AsQueryable().Where(o=>new []{obj1.Id,obj2.Id}.Contains(o.Id)), + }; + TestFor(queryables,dataSourceNames); + } + } + + public class TestEntity + { + public string Id { get; set; } + } +} diff --git a/test/ShardingCore.CommonTest/ShardingTableTime.cs b/test/ShardingCore.CommonTest/ShardingTableTime.cs new file mode 100644 index 00000000..d67b98bb --- /dev/null +++ b/test/ShardingCore.CommonTest/ShardingTableTime.cs @@ -0,0 +1,165 @@ +using System.Collections.ObjectModel; +using System.Reflection; +using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.VirtualRoutes; +using ShardingCore.Helpers; +using ShardingCore.Utils; +using Xunit; + +namespace ShardingCore.CommonTest +{ + + public class ShardingTableTime + { + private readonly EntityMetadata _testEntityMetadata; + private readonly List _allTables; + public ShardingTableTime() + { + var entityMetadata = new EntityMetadata(typeof(TestTimeEntity), nameof(TestTimeEntity), typeof(ShardingDataSourceMod), + new ReadOnlyCollection(typeof(TestTimeEntity).GetProperties().ToList()), null); + var entityMetadataTableBuilder = EntityMetadataTableBuilder.CreateEntityMetadataTableBuilder(entityMetadata); + entityMetadataTableBuilder.ShardingProperty(o => o.Time); + entityMetadata.CheckShardingTableMetadata(); + _testEntityMetadata = entityMetadata; + var dateTime = new DateTime(2022,1,1); + //[20220101....20220120] + _allTables = Enumerable.Range(0,20).Select(o=>dateTime.AddDays(o).ToString("yyyyMMdd")).ToList(); + } + public static Func GetRouteFilter(object shardingValue, ShardingOperatorEnum shardingOperator, + string propertyName) + { + if (propertyName != nameof(TestTimeEntity.Time)) + { + throw new Exception($"{nameof(propertyName)}:[{propertyName}] error"); + } + + var shardingKey = (DateTime)shardingValue; + var t =$"{shardingKey:yyyyMMdd}"; + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return tail =>String.Compare(tail, t, StringComparison.Ordinal) >= 0; + case ShardingOperatorEnum.LessThan: + { + var shardingKeyDate = shardingKey.Date; + //处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回 + if (shardingKeyDate == shardingKey) + return tail =>String.Compare(tail, t, StringComparison.Ordinal) < 0; + return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; + } + case ShardingOperatorEnum.LessThanOrEqual: + return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; + case ShardingOperatorEnum.Equal: return tail => tail == t; + default: + { + return tail => true; + } + } + } + + private void TestId(IQueryable queryable, string[] tables) + { + var routePredicateExpression = ShardingUtil.GetRouteParseExpression(queryable,_testEntityMetadata,GetRouteFilter,true); + Assert.NotNull(routePredicateExpression); + var routePredicate = routePredicateExpression.GetRoutePredicate(); + + var list = _allTables.Where(routePredicate).ToList(); + Assert.NotNull(list); + Assert.Equal(tables.Length,list.Count); + foreach (var table in tables) + { + Assert.True(list.Any(o=>o==table)); + } + } + + private void TestFor(List> queryables, string[] tables) + { + foreach (var queryable in queryables) + { + TestId(queryable, tables); + } + } + [Fact] + public void TestSingleTable() + { + var queryTime = new DateTime(2022, 1, 2); + var queryTime1 = queryTime.AddHours(1); + var queryTime2 = new DateTime(2022, 1, 3); + var queryTime3 = new DateTime(2022, 1, 3).AddSeconds(-1); + var table = queryTime.ToString("yyyyMMdd"); + var tables = new []{table}; + var id = "1"; + var times = new []{queryTime}; + var times1 = new List(){queryTime}; + var obj1 = new {time=new DateTime(2022, 1, 2)}; + var queryables=new List>() + { + new List().AsQueryable().Where(o=>o.Time==queryTime), + new List().AsQueryable().Where(o=>o.Time>=queryTime&&o.Time().AsQueryable().Where(o=>o.Time>=queryTime&&o.Time().AsQueryable().Where(o=>o.Time>=queryTime&&o.Time<=queryTime3), + new List().AsQueryable().Where(o=>o.Time==queryTime1), + new List().AsQueryable().Where(o=>queryTime==o.Time), + new List().AsQueryable().Where(o=>o.Time.Equals(queryTime)), + new List().AsQueryable().Where(o=>queryTime.Equals(o.Time)), + new List().AsQueryable().Where(o=>o.Time==obj1.time), + new List().AsQueryable().Where(o=>o.Time.Equals(obj1.time)), + new List().AsQueryable().Where(o=>obj1.time.Equals(o.Time)), + new List().AsQueryable().Where(o=>times.Contains(o.Time)), + new List().AsQueryable().Where(o=>new []{queryTime}.Contains(o.Time)), + new List().AsQueryable().Where(o=>times1.Contains(o.Time)), + new List().AsQueryable().Where(o=> new List(){queryTime}.Contains(o.Time)) + }; + TestFor(queryables,tables); + } + //[20220101....20220120] + [Fact] + public void TestMultiTable() + { + var tables = new []{"20220101","20220102","20220119","20220120"}; + var begin = new DateTime(2022,01,03); + var begin1 = new DateTime(2022,01,02); + var end1 = new DateTime(2022,01,19); + var begin2 = new DateTime(2022,01,01); + var end2 = new DateTime(2022,01,20); + var queryables=new List>() + { + new List().AsQueryable().Where(o=>o.Time=end1), + new List().AsQueryable().Where(o=>begin>o.Time||end1<=o.Time), + new List().AsQueryable().Where(o=>o.Time<=begin1||o.Time>=end1), + new List().AsQueryable().Where(o=>begin1>=o.Time||o.Time>=end1), + new List().AsQueryable().Where(o=>begin1>=o.Time||end1<=o.Time), + new List().AsQueryable().Where(o=>(o.Time>=begin2&&o.Time<=begin1)||(o.Time>=end1&&o.Time<=end2)), + }; + TestFor(queryables,tables); + } + [Fact] + public void TestMultiTable1() + { + var tables = new []{"20220105","20220106","20220107"}; + var time1 = new DateTime(2022,01,05); + var time2 = new DateTime(2022,01,07); + var time3 = new DateTime(2022,01,08); + var time4 = new DateTime(2022,01,06); + var dateTimes = new []{time1,time2,time4}; + var queryables=new List>() + { + new List().AsQueryable().Where(o=>o.Time>=time1&&o.Time<=time2), + new List().AsQueryable().Where(o=>o.Time<=time2&&o.Time>=time1), + new List().AsQueryable().Where(o=>o.Time>=time1.AddHours(1)&&o.Time<=time2), + new List().AsQueryable().Where(o=>o.Time>=time1&&o.Time().AsQueryable().Where(o=>o.Time>=time1&&o.Time().AsQueryable().Where(o=>o.Time>=time1&&o.Time<=time3.AddHours(-1)), + new List().AsQueryable().Where(o=>dateTimes.Contains(o.Time)), + }; + TestFor(queryables,tables); + } + + } + public class TestTimeEntity + { + public string Id { get; set; } + public DateTime Time { get; set; } + } +}