添加单元测试

This commit is contained in:
xuejiaming 2021-11-27 21:27:30 +08:00
parent 508e930e01
commit e7d59f98c2
14 changed files with 209 additions and 40 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace ShardingCore.Core
{
@ -10,6 +11,7 @@ namespace ShardingCore.Core
*/
public class Check
{
[ExcludeFromCodeCoverage]
private Check(){}
/// <summary>
/// 不能为空

View File

@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.Sharding.Abstractions;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
@ -45,16 +46,19 @@ namespace ShardingCore.EFCores
return _shardingQueryExecutor.ExecuteAsync<TResult>(_currentContext, query, cancellationToken);
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, TResult> CreateCompiledQuery<TResult>(Expression query)
{
throw new NotImplementedException();
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, Task<TResult>> CreateCompiledAsyncTaskQuery<TResult>(Expression query)
{
throw new NotImplementedException();
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, TResult> CreateCompiledAsyncQuery<TResult>(Expression query)
{
throw new NotImplementedException();
@ -74,17 +78,20 @@ namespace ShardingCore.EFCores
{
return _shardingQueryExecutor.ExecuteAsync<Task<TResult>>(_currentContext, query, cancellationToken);
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, TResult> CreateCompiledQuery<TResult>(Expression query)
{
throw new NotImplementedException();
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, IAsyncEnumerable<TResult>> CreateCompiledAsyncEnumerableQuery<TResult>(Expression query)
{
throw new NotImplementedException();
}
[ExcludeFromCodeCoverage]
public Func<QueryContext, Task<TResult>> CreateCompiledAsyncTaskQuery<TResult>(Expression query)
{
throw new NotImplementedException();

View File

@ -36,10 +36,10 @@ namespace ShardingCore.Sharding
_useReadWriteSeparation = _connectionStringManager is ReadWriteConnectionStringManager<TShardingDbContext>;
}
public bool IsUseReadWriteSeparation()
{
return _useReadWriteSeparation;
}
//public bool IsUseReadWriteSeparation()
//{
// return _useReadWriteSeparation;
//}
public string GetConnectionString(string dataSourceName, bool isWrite)
{
if (isWrite)

View File

@ -67,7 +67,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
// compile to Func<Data, Data>
return lambda.Compile();
}
[ExcludeFromCodeCoverage]
public static object Count(this IQueryable source, PropertyInfo property)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -89,6 +89,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.Provider.Execute(callExpression);
}
[ExcludeFromCodeCoverage]
public static object Count(this IQueryable source, string propertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -119,6 +120,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.Provider.Execute(callExpression);
}
[ExcludeFromCodeCoverage]
public static object Sum(this IQueryable source, string propertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -176,6 +178,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
// // do.
// return source.Provider.Execute(callExpression);
//}
[ExcludeFromCodeCoverage]
public static object Max(this IQueryable source, string propertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -209,6 +212,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
return source.Provider.Execute(callExpression);
}
[ExcludeFromCodeCoverage]
public static object Min(this IQueryable source, string propertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -249,6 +253,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="countPropertyName">聚合函数count属性名</param>
[ExcludeFromCodeCoverage]
public static object AverageWithCount(this IQueryable source, string averagePropertyName, string countPropertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
@ -279,6 +284,7 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
/// <param name="source">数据源</param>
/// <param name="averagePropertyName">聚合函数average属性名</param>
/// <param name="sumPropertyName">聚合函数sum属性名</param>
[ExcludeFromCodeCoverage]
public static object AverageWithSum(this IQueryable source, string averagePropertyName, string sumPropertyName)
{
if (source == null) throw new ArgumentNullException(nameof(source));

View File

@ -100,7 +100,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
if (virtualDataSourceRoute.EnablePagination)
{
dataSourceSequenceOrderConfig = virtualDataSourceRoute.PaginationMetadata.PaginationConfigs.OrderByDescending(o => o.AppendOrder)
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName) && PaginationMatch(o));
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName));
}
}
@ -110,7 +110,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
if (virtualTable.EnablePagination)
{
tableSequenceOrderConfig = virtualTable.PaginationMetadata.PaginationConfigs.OrderByDescending(o => o.AppendOrder)
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName) && PaginationMatch(o));
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName));
}
}
@ -224,14 +224,6 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
return paginationSequenceConfigs.Where(o => o.PaginationMatchEnum.HasFlag(PaginationMatchEnum.PrimaryMatch)).FirstOrDefault(o => PaginationPrimaryMatch(o, primaryOrder));
}
private bool PaginationMatch(PaginationSequenceConfig paginationSequenceConfig)
{
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
return false;
}
private bool PaginationPrimaryMatch(PaginationSequenceConfig paginationSequenceConfig, PropertyOrder propertyOrder)
{
if (propertyOrder.PropertyExpression != paginationSequenceConfig.PropertyName)

View File

@ -146,10 +146,10 @@ namespace ShardingCore.Sharding
return dbContext;
}
public IRouteTail Create(TableRouteResult tableRouteResult)
{
return _routeTailFactory.Create(tableRouteResult);
}
//public IRouteTail Create(TableRouteResult tableRouteResult)
//{
// return _routeTailFactory.Create(tableRouteResult);
//}
public IQueryable<T> GetReWriteQueryable()
{
@ -160,10 +160,10 @@ namespace ShardingCore.Sharding
return _source;
}
public bool HasSkipTake()
{
return Skip.HasValue || Take.HasValue;
}
//public bool HasSkipTake()
//{
// return Skip.HasValue || Take.HasValue;
//}
public bool IsPaginationQuery()
{
@ -176,10 +176,10 @@ namespace ShardingCore.Sharding
return this.GroupByContext.GroupExpression != null;
}
public bool HasAggregateQuery()
{
return this.SelectContext.HasAverage();
}
//public bool HasAggregateQuery()
//{
// return this.SelectContext.HasAverage();
//}
public IShardingDbContext GetShardingDbContext()
{

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShardingCore.Test.Domain.Entities
{
public class LogDayLong
{
public Guid Id { get; set; }
public string LogLevel { get; set; }
public string LogBody { get; set; }
public long LogTime { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test.Domain.Entities;
namespace ShardingCore.Test.Domain.Maps
{
public class LogDayLongMap:IEntityTypeConfiguration<LogDayLong>
{
public void Configure(EntityTypeBuilder<LogDayLong> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.LogLevel).IsRequired().IsUnicode(false).HasMaxLength(32);
builder.Property(o => o.LogBody).IsRequired().HasMaxLength(256);
builder.ToTable(nameof(LogDayLong));
}
}
}

View File

@ -33,6 +33,7 @@ namespace ShardingCore.Test
modelBuilder.ApplyConfiguration(new LogMonthLongMap());
modelBuilder.ApplyConfiguration(new LogYearLongMap());
modelBuilder.ApplyConfiguration(new SysUserModIntMap());
modelBuilder.ApplyConfiguration(new LogDayLongMap());
}
public IRouteTail RouteTail { get; set; }

View File

@ -1337,6 +1337,47 @@ namespace ShardingCore.Test
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);
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -1053,16 +1053,16 @@ namespace ShardingCore.Test
[Fact]
public async Task LogYearLongCountTest()
{
var countAsync = await _virtualDbContext.Set<LogYearLong>().CountAsync();
var countAsync = _virtualDbContext.Set<LogYearLong>().Count();
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();
var fourCount = _virtualDbContext.Set<LogYearLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).Count();
Assert.Equal(30, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogYearLong>("2021");
var countAsync1 = await _virtualDbContext.Set<LogYearLong>().CountAsync();
var countAsync1 = _virtualDbContext.Set<LogYearLong>().Count();
Assert.Equal(300, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
@ -1072,13 +1072,13 @@ namespace ShardingCore.Test
{
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);
var page = _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(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);
var page1 = _virtualDbContext.Set<LogWeekTimeLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
@ -1283,6 +1283,47 @@ namespace ShardingCore.Test
pageDescAge--;
}
}
[Fact]
public void LogDayLongCountTest()
{
var countAsync = _virtualDbContext.Set<LogDayLong>().Count();
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 = _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).Count();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDayLong>("20210102");
var countAsync1 = _virtualDbContext.Set<LogDayLong>().Count();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDayLong>("20210103", "20210104");
var countAsync2 = _virtualDbContext.Set<LogDayLong>().Count();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public void LogDayLongShardingPage()
{
var fourBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 4, 1).Date);
var fiveBegin = ShardingCoreHelper.ConvertDateTimeToLong(new DateTime(2021, 5, 1).Date);
var page = _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = _virtualDbContext.Set<LogDayLong>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
// [Fact]
// public void Group_API_Test()
// {

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Test.Domain.Entities;
using ShardingCore.VirtualRoutes.Days;
namespace ShardingCore.Test.Shardings
{
public class LogDayLongVirtualRoute:AbstractSimpleShardingDayKeyLongVirtualTableRoute<LogDayLong>
{
protected override bool EnableHintRoute => true;
public override void Configure(EntityMetadataTableBuilder<LogDayLong> builder)
{
builder.ShardingProperty(o => o.LogTime);
}
public override bool AutoCreateTableByTime()
{
return true;
}
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
}
}

View File

@ -9,7 +9,7 @@ namespace ShardingCore.Test.Shardings.PaginationConfigs
{
builder.PaginationSequence(o => o.LogTime)
.UseQueryMatch(PaginationMatchEnum.Named | PaginationMatchEnum.Owner |
PaginationMatchEnum.PrimaryMatch);
PaginationMatchEnum.PrimaryMatch).UseAppendIfOrderNone();
}
}
}

View File

@ -38,8 +38,8 @@ namespace ShardingCore.Test
.Begin(o =>
{
#if DEBUG
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
//o.CreateShardingTableOnStart = true;
//o.EnsureCreatedWithOutShardingTable = true;
#endif
o.AutoTrackEntity = true;
@ -71,6 +71,7 @@ namespace ShardingCore.Test
op.AddShardingTableRoute<LogMonthLongvirtualRoute>();
op.AddShardingTableRoute<LogYearLongVirtualRoute>();
op.AddShardingTableRoute<SysUserModIntVirtualRoute>();
op.AddShardingTableRoute<LogDayLongVirtualRoute>();
}).AddReadWriteSeparation(sp =>
{
return new Dictionary<string, ISet<string>>()
@ -175,6 +176,7 @@ namespace ShardingCore.Test
}
List<LogDay> logDays = new List<LogDay>(3600);
List<LogDayLong> logDayLongs = new List<LogDayLong>(3600);
var levels = new List<string>(){"info","warning","error"};
var begin1 = new DateTime(2021, 1, 1);
@ -190,6 +192,13 @@ namespace ShardingCore.Test
LogBody = $"{i}_{j}",
LogTime = ltime.AddHours(1)
});
logDayLongs.Add(new LogDayLong()
{
Id = Guid.NewGuid(),
LogLevel = levels[j%3],
LogBody = $"{i}_{j}",
LogTime = ShardingCoreHelper.ConvertDateTimeToLong(ltime.AddHours(1))
});
ltime = ltime.AddHours(1);
}
begin1 = begin1.AddDays(1);
@ -265,6 +274,7 @@ namespace ShardingCore.Test
await virtualDbContext.AddRangeAsync(userSalaries);
await virtualDbContext.AddRangeAsync(orders);
await virtualDbContext.AddRangeAsync(logDays);
await virtualDbContext.AddRangeAsync(logDayLongs);
await virtualDbContext.AddRangeAsync(logWeeks);
await virtualDbContext.AddRangeAsync(logWeekLongs);
await virtualDbContext.AddRangeAsync(logYears);