sharding/test/ShardingCore.CommonTest/ShardingDataSourceMod.cs

152 lines
7.5 KiB
C#
Raw Normal View History

2022-06-03 23:07:44 +08:00
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<string> _allDataSources;
2022-07-18 23:52:11 +08:00
2022-06-03 23:07:44 +08:00
public ShardingDataSourceMod()
2022-07-18 23:52:11 +08:00
{
var entityMetadata = new EntityMetadata(typeof(TestEntity));
2022-07-18 23:52:11 +08:00
var entityMetadataDataSourceBuilder =
EntityMetadataDataSourceBuilder<TestEntity>.CreateEntityMetadataDataSourceBuilder(entityMetadata);
2022-06-03 23:07:44 +08:00
entityMetadataDataSourceBuilder.ShardingProperty(o => o.Id);
entityMetadata.CheckShardingDataSourceMetadata();
_testEntityMetadata = entityMetadata;
2022-07-18 23:52:11 +08:00
_allDataSources = Enumerable.Range(0, 10).Select(o => o.ToString()).ToList();
2022-06-03 23:07:44 +08:00
}
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());
2022-07-18 23:52:11 +08:00
var dataSourceName = (stringHashCode % 10).ToString();
2022-06-03 23:07:44 +08:00
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return t => t == dataSourceName;
default:
{
return t => true;
}
}
}
private void TestId(IQueryable<TestEntity> queryable, string[] dataSourceNames)
{
2022-07-18 23:52:11 +08:00
var routePredicateExpression =
ShardingUtil.GetRouteParseExpression(queryable, _testEntityMetadata, GetRouteFilter, false);
2022-06-03 23:07:44 +08:00
Assert.NotNull(routePredicateExpression);
var routePredicate = routePredicateExpression.GetRoutePredicate();
2022-07-18 23:52:11 +08:00
2022-06-03 23:07:44 +08:00
var list = _allDataSources.Where(routePredicate).ToList();
Assert.NotNull(list);
2022-07-18 23:52:11 +08:00
Assert.Equal(dataSourceNames.Length, list.Count);
2022-06-03 23:07:44 +08:00
foreach (var dataSourceName in dataSourceNames)
{
2022-07-18 23:52:11 +08:00
Assert.True(list.Any(o => o == dataSourceName));
2022-06-03 23:07:44 +08:00
}
}
private void TestFor(List<IQueryable<TestEntity>> queryables, string[] dataSourceNames)
{
foreach (var queryable in queryables)
{
TestId(queryable, dataSourceNames);
}
}
2022-07-18 23:52:11 +08:00
2022-06-03 23:07:44 +08:00
[Fact]
public void TestSingleDataSource()
{
var stringHashCode = ShardingCoreHelper.GetStringHashCode("1");
2022-07-18 23:52:11 +08:00
var dataSourceName = (stringHashCode % 10).ToString();
var dataSourceNames = new[] { dataSourceName };
var dataSourceNames1 = Enumerable.Range(0, 10).Select(o => o.ToString()).ToArray();;
2022-06-03 23:07:44 +08:00
var id = "1";
2022-07-18 23:52:11 +08:00
var ids = new[] { "1" };
var ids1 = new List<string>() { "1" };
var obj1 = new { Id = "1" };
var queryables = new List<IQueryable<TestEntity>>()
2022-06-03 23:07:44 +08:00
{
2022-07-18 23:52:11 +08:00
new List<TestEntity>().AsQueryable().Where(o => o.Id == "1"),
new List<TestEntity>().AsQueryable().Where(o => o.Id == "1" && o.Id != "2"),
new List<TestEntity>().AsQueryable().Where(o => "1" == o.Id),
new List<TestEntity>().AsQueryable().Where(o => o.Id == id),
new List<TestEntity>().AsQueryable().Where(o => id == o.Id),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals("1")),
new List<TestEntity>().AsQueryable().Where(o => "1".Equals(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals(id)),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals(id)),
new List<TestEntity>().AsQueryable().Where(o => ids.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new[] { "1" }.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => ids1.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new List<string>() { "1" }.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => o.Id == obj1.Id),
new List<TestEntity>().AsQueryable().Where(o => obj1.Id == o.Id),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals(obj1.Id)),
new List<TestEntity>().AsQueryable().Where(o => obj1.Id.Equals(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new[] { obj1.Id }.Contains(o.Id))
2022-06-03 23:07:44 +08:00
};
2022-07-18 23:52:11 +08:00
TestFor(queryables, dataSourceNames);
var queryables1 = new List<IQueryable<TestEntity>>()
{
new List<TestEntity>().AsQueryable().Where(o => o.Id.Contains("1")),
new List<TestEntity>().AsQueryable().Where(o => o.Id.StartsWith("1")),
new List<TestEntity>().AsQueryable().Where(o => o.Id.EndsWith("1"))
};
TestFor(queryables1, dataSourceNames1);
2022-06-03 23:07:44 +08:00
}
[Fact]
public void TestTwoDataSource()
{
2022-07-18 23:52:11 +08:00
var dataSourceNames = new[]
{
(ShardingCoreHelper.GetStringHashCode("1") % 10).ToString(),
(ShardingCoreHelper.GetStringHashCode("2") % 10).ToString()
};
var ids = new[] { "1", "2" };
var ids1 = new List<string>() { "1", "2" };
var obj1 = new { Id = "1" };
var obj2 = new { Id = "2" };
var queryables = new List<IQueryable<TestEntity>>()
2022-06-03 23:07:44 +08:00
{
2022-07-18 23:52:11 +08:00
new List<TestEntity>().AsQueryable().Where(o => o.Id == "1" || o.Id == "2"),
new List<TestEntity>().AsQueryable().Where(o => (o.Id == "1" || o.Id == "2") && o.Id != "3"),
new List<TestEntity>().AsQueryable().Where(o => (o.Id == "1" || o.Id == "2") && !o.Id.Equals("3")),
new List<TestEntity>().AsQueryable().Where(o => "1" == o.Id || o.Id == "2"),
new List<TestEntity>().AsQueryable().Where(o => "1" == o.Id || "2" == o.Id),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals("1") || o.Id == "2"),
new List<TestEntity>().AsQueryable().Where(o => o.Id.Equals("1") || o.Id.Equals("2")),
new List<TestEntity>().AsQueryable().Where(o => "1".Equals(o.Id) || o.Id == "2"),
new List<TestEntity>().AsQueryable().Where(o => "1".Equals(o.Id) || o.Id.Equals("2")),
new List<TestEntity>().AsQueryable().Where(o => "1".Equals(o.Id) || "2".Equals(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => ids.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new[] { "1", "2" }.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => ids1.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new List<string>() { "1", "2" }.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new List<string>() { obj1.Id, obj2.Id }.Contains(o.Id)),
new List<TestEntity>().AsQueryable().Where(o => new[] { obj1.Id, obj2.Id }.Contains(o.Id)),
2022-06-03 23:07:44 +08:00
};
2022-07-18 23:52:11 +08:00
TestFor(queryables, dataSourceNames);
2022-06-03 23:07:44 +08:00
}
}
public class TestEntity
{
public string Id { get; set; }
}
2022-07-18 23:52:11 +08:00
}