单元测试提高到71%

This commit is contained in:
xuejiaming 2021-11-27 09:51:46 +08:00
parent 788ef79634
commit 508e930e01
9 changed files with 222 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
@ -25,6 +26,7 @@ namespace ShardingCore.Helpers
/// <summary>
/// https://github.com/Coldairarrow/EFCore.Sharding/blob/master/src/EFCore.Sharding/Migrations/MigrationHelper.cs
/// </summary>
[ExcludeFromCodeCoverage]
public class MigrationHelper
{
private MigrationHelper() { }

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShardingCore.Test.Domain.Entities
{
public class SysUserModInt
{
/// <summary>
/// 用户Id用于分表
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 用户姓名
/// </summary>
public int Age { get; set; }
public int AgeGroup { get; set; }
}
}

View File

@ -0,0 +1,23 @@
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 SysUserModIntMap:IEntityTypeConfiguration<SysUserModInt>
{
public void Configure(EntityTypeBuilder<SysUserModInt> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).ValueGeneratedNever();
builder.Property(o => o.Name).HasMaxLength(128);
builder.ToTable(nameof(SysUserModInt));
}
}
}

View File

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

View File

@ -1270,6 +1270,73 @@ namespace ShardingCore.Test
await _virtualDbContext.SaveChangesAsync();
}
[Fact]
public async Task Int_ToList_All_Route_Test()
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserModInt>("00");
var mod00s = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(333, mod00s.Count);
}
var mods = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
}
[Fact]
public async Task Int_ToList_All_Test()
{
var mods = await _virtualDbContext.Set<SysUserModInt>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
var pageResult = await _virtualDbContext.Set<SysUserModInt>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
foreach (var sysUserMod in pageResult)
{
Assert.Equal(pageDescAge, sysUserMod.Age);
pageDescAge--;
}
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -1217,6 +1217,72 @@ namespace ShardingCore.Test
}
}
}
[Fact]
public void Int_ToList_All_Route_Test()
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserModInt>("00");
var mod00s = _virtualDbContext.Set<SysUserModInt>().ToList();
Assert.Equal(333, mod00s.Count);
}
var mods = _virtualDbContext.Set<SysUserModInt>().ToList();
Assert.Equal(1000, mods.Count);
var modOrders1 = _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToList();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToList();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
}
[Fact]
public void Int_ToList_All_Test()
{
var mods = _virtualDbContext.Set<SysUserModInt>().ToList();
Assert.Equal(1000, mods.Count);
var modOrders1 = _virtualDbContext.Set<SysUserModInt>().OrderBy(o => o.Age).ToList();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = _virtualDbContext.Set<SysUserModInt>().OrderByDescending(o => o.Age).ToList();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
var pageResult = _virtualDbContext.Set<SysUserModInt>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToList();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
foreach (var sysUserMod in pageResult)
{
Assert.Equal(pageDescAge, sysUserMod.Age);
pageDescAge--;
}
}
// [Fact]
// public void Group_API_Test()
// {

View File

@ -0,0 +1,25 @@
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.Mods;
namespace ShardingCore.Test.Shardings
{
public class SysUserModIntVirtualRoute:AbstractSimpleShardingModKeyIntVirtualTableRoute<SysUserModInt>
{
protected override bool EnableHintRoute => true;
public SysUserModIntVirtualRoute() : base(2, 3)
{
}
public override void Configure(EntityMetadataTableBuilder<SysUserModInt> builder)
{
builder.ShardingProperty(o => o.Id);
}
}
}

View File

@ -70,6 +70,7 @@ namespace ShardingCore.Test
op.AddShardingTableRoute<LogYearDateTimeVirtualRoute>();
op.AddShardingTableRoute<LogMonthLongvirtualRoute>();
op.AddShardingTableRoute<LogYearLongVirtualRoute>();
op.AddShardingTableRoute<SysUserModIntVirtualRoute>();
}).AddReadWriteSeparation(sp =>
{
return new Dictionary<string, ISet<string>>()
@ -117,6 +118,7 @@ namespace ShardingCore.Test
{
var ids = Enumerable.Range(1, 1000);
var userMods = new List<SysUserMod>();
var userModInts = new List<SysUserModInt>();
var userSalaries = new List<SysUserSalary>();
var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1);
@ -129,6 +131,13 @@ namespace ShardingCore.Test
Name = $"name_{id}",
AgeGroup = Math.Abs(id % 10)
});
userModInts.Add(new SysUserModInt()
{
Id = id,
Age = id,
Name = $"name_{id}",
AgeGroup = Math.Abs(id % 10)
});
var tempTime = beginTime;
var i = 0;
while (tempTime <= endTime)
@ -252,6 +261,7 @@ namespace ShardingCore.Test
using (var tran = virtualDbContext.Database.BeginTransaction())
{
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userModInts);
await virtualDbContext.AddRangeAsync(userSalaries);
await virtualDbContext.AddRangeAsync(orders);
await virtualDbContext.AddRangeAsync(logDays);

View File

@ -1009,6 +1009,9 @@ namespace ShardingCore.Test2x
Assert.Equal(10, page1.Data.Count);
Assert.Equal(31, page1.Total);
}
// [Fact]
// public void Group_API_Test()
// {