添加对象属性.equals未走索引的bug,添加更多单元测试

This commit is contained in:
xuejiaming 2022-06-02 22:32:05 +08:00
parent 8d20fe2b0a
commit fa84d756cd
7 changed files with 150 additions and 6 deletions

View File

@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MultiConfig", "sampl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.AutoCreateIfPresent", "samples\Sample.AutoCreateIfPresent\Sample.AutoCreateIfPresent.csproj", "{40C83D48-0614-4651-98C6-2ABBE857D83D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.CommonTest", "test\ShardingCore.CommonTest\ShardingCore.CommonTest.csproj", "{3E895438-E609-4860-8391-6897ED55DA06}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -161,6 +163,10 @@ Global
{40C83D48-0614-4651-98C6-2ABBE857D83D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40C83D48-0614-4651-98C6-2ABBE857D83D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40C83D48-0614-4651-98C6-2ABBE857D83D}.Release|Any CPU.Build.0 = Release|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -189,6 +195,7 @@ Global
{DCEBAC86-E62B-4B6C-A352-B8C1C2C6F734} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{D839D632-4AE4-4F75-8A2C-49EE029B0787} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{40C83D48-0614-4651-98C6-2ABBE857D83D} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{3E895438-E609-4860-8391-6897ED55DA06} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}

View File

@ -16,7 +16,7 @@ namespace Sample.SqlServer.Domain.Entities
/// 每月的金额
/// </summary>
[ShardingTableKey]
public int? DateOfMonth { get; set; }
public int DateOfMonth { get; set; }
/// <summary>
/// 工资
/// </summary>

View File

@ -16,7 +16,7 @@ namespace Sample.SqlServer.Shardings
* @Date: Monday, 01 February 2021 15:54:55
* @Email: 326308290@qq.com
*/
public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute<SysUserSalary,int?>
public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute<SysUserSalary,int>
{
public override string ShardingKeyToTail(object shardingKey)
@ -46,13 +46,13 @@ namespace Sample.SqlServer.Shardings
}
protected string TimeFormatToTail(int? time)
protected string TimeFormatToTail(int time)
{
var dateOfMonth=DateTime.ParseExact($"{time}","yyyyMM",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.AdjustToUniversal);
return $"{dateOfMonth:yyyyMM}";
}
public override Func<string, bool> GetRouteToFilter(int? shardingKey, ShardingOperatorEnum shardingOperator)
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)

View File

@ -24,6 +24,12 @@ namespace ShardingCore.Core.VirtualRoutes
Equal,
[Description("!=")]
NotEqual,
// [Description("%w%")]
// AllLike,
// [Description("%w")]
// StartLike,
// [Description("w%")]
// EndLike
// [Description("Contains")]
// BeContains
}

View File

@ -274,9 +274,9 @@ namespace ShardingCore.Core.Internal.Visitors
var shardingPredicateResult = IsMethodWrapShardingKey(methodCallExpression);
if (shardingPredicateResult.IsShardingKey)
{
if (methodCallExpression.Object is ConstantExpression constantExpression)
var shardingValue = GetExpressionValue(methodCallExpression.Object);
if (shardingValue != null)
{
var shardingValue = constantExpression.Value;
var keyToTailWithFilter = _keyToTailWithFilter(shardingValue, ShardingOperatorEnum.Equal, shardingPredicateResult.ShardingPropertyName);
return new RoutePredicateExpression(keyToTailWithFilter);
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ShardingCore\ShardingCore.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,113 @@
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<string> _allDataSources;
public ShardingDataSource()
{
var entityMetadata = new EntityMetadata(typeof(TestEntity), nameof(TestEntity), typeof(ShardingDataSource),
new ReadOnlyCollection<PropertyInfo>(typeof(TestEntity).GetProperties().ToList()), null);
var entityMetadataDataSourceBuilder = EntityMetadataDataSourceBuilder<TestEntity>.CreateEntityMetadataDataSourceBuilder(entityMetadata);
entityMetadataDataSourceBuilder.ShardingProperty(o => o.Id);
entityMetadata.CheckShardingDataSourceMetadata();
_testEntityMetadata = entityMetadata;
_allDataSources = Enumerable.Range(0,10).Select(o=>o.ToString()).ToList();
}
public static Func<string, bool> 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<TestEntity> 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<TestEntity>().AsQueryable().Where(o=>o.Id=="1");
TestId1(constantQueryable1);
var constantQueryable2 = new List<TestEntity>().AsQueryable().Where(o=>"1"==o.Id);
TestId1(constantQueryable2);
var id = "1";
var constantQueryable3 = new List<TestEntity>().AsQueryable().Where(o=>o.Id==id);
TestId1(constantQueryable3);
var constantQueryable4 = new List<TestEntity>().AsQueryable().Where(o=>id==o.Id);
TestId1(constantQueryable4);
var constantQueryable5 = new List<TestEntity>().AsQueryable().Where(o=>o.Id.Equals("1"));
TestId1(constantQueryable5);
var constantQueryable6 = new List<TestEntity>().AsQueryable().Where(o=>"1".Equals(o.Id));
TestId1(constantQueryable6);
var constantQueryable7 = new List<TestEntity>().AsQueryable().Where(o=>o.Id.Equals(id));
TestId1(constantQueryable7);
var constantQueryable8 = new List<TestEntity>().AsQueryable().Where(o=>o.Id.Equals(id));
TestId1(constantQueryable8);
var ids = new []{"1"};
var constantQueryable9 = new List<TestEntity>().AsQueryable().Where(o=>ids.Contains(o.Id));
TestId1(constantQueryable9);
var constantQueryable10 = new List<TestEntity>().AsQueryable().Where(o=>new []{"1"}.Contains(o.Id));
TestId1(constantQueryable10);
var ids1 = new List<string>(){"1"};
var constantQueryable11 = new List<TestEntity>().AsQueryable().Where(o=>ids1.Contains(o.Id));
TestId1(constantQueryable11);
var constantQueryable12 = new List<TestEntity>().AsQueryable().Where(o=>new List<string>(){"1"}.Contains(o.Id));
TestId1(constantQueryable12);
var obj1 = new {Id="1"};
var constantQueryable13 = new List<TestEntity>().AsQueryable().Where(o=>o.Id==obj1.Id);
TestId1(constantQueryable13);
var constantQueryable14 = new List<TestEntity>().AsQueryable().Where(o=>obj1.Id==o.Id);
TestId1(constantQueryable14);
var constantQueryable15 = new List<TestEntity>().AsQueryable().Where(o=>o.Id.Equals(obj1.Id));
TestId1(constantQueryable15);
var constantQueryable16 = new List<TestEntity>().AsQueryable().Where(o=>obj1.Id.Equals(o.Id));
TestId1(constantQueryable16);
var constantQueryable17 = new List<TestEntity>().AsQueryable().Where(o=>new []{obj1.Id}.Contains(o.Id));
TestId1(constantQueryable17);
}
[Fact]
public void TestTwoDataSource()
{
}
}
public class TestEntity
{
public string Id { get; set; }
}
}