Skip to content

Commit

Permalink
[fix]修正因跨年而带来的单元测试错误
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jan 1, 2025
1 parent 3ddb578 commit bad3cb6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
88 changes: 51 additions & 37 deletions XUnitTest.XCode/EntityTests/ShardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ public void ShardTestSQLite2()
Assert.StartsWith($"[test_{time:yyyy}] Delete From Log2_{time:yyyyMMdd} Where", sqls[^1]);

var list = Log2.Search(null, null, -1, null, -1, time.AddHours(-24), time, null, new PageParameter { PageSize = 100 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-00):yyyyMMdd} Where", sqls[^1]);

list = Log2.Search(null, null, -1, null, -1, time.AddHours(-24), time, null, new PageParameter { PageIndex = 2, PageSize = 100 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^3]);
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select Count(*) From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-00):yyyyMMdd} Where", sqls[^1]);

// 恢复现场,避免影响其它测试用例
Expand Down Expand Up @@ -218,56 +220,62 @@ public void SearchDates()
// 遍历分表查询
XTrace.WriteLine("AutoShard FindCount ({0}, {1})", start, time);
Log2.Meta.AutoShard(start, time, () => Log2.FindCount()).ToArray();
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^4]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^4]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-0):yyyyMMdd}", sqls[^1]);

// 在多表中进行分页查询
XTrace.WriteLine("Search Page");
var list = Log2.Search(null, null, -1, null, -1, start, time, null, new PageParameter { PageSize = 10000 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^4]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^4]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-0):yyyyMMdd} Where ID>=", sqls[^1]);

// 查询第二页
XTrace.WriteLine("Search Page2");
list = Log2.Search(null, null, -1, null, -1, start, time, null, new PageParameter { PageIndex = 2, PageSize = 10000 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^7]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^6]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^5]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^4]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^7]);
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^6]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^5]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^4]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-0):yyyyMMdd} Where ID>=", sqls[^1]);

// 在多表中进行分页查询(倒序)
XTrace.WriteLine("Search Page Reverse");
list = Log2.Search(null, null, -1, null, -1, start, time, null, new PageParameter { PageSize = 10000, Sort = "id", Desc = true });
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-0):yyyyMMdd} Where ID>=", sqls[^4]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^1]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where ID>=", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where ID>=", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select * From Log2_{time.AddDays(-3):yyyyMMdd} Where ID>=", sqls[^1]);

// 日期倒序
time = DateTime.Today;
start = time.AddDays(-3);
XTrace.WriteLine("AutoShard start={0} end={1}", time, start);
Log2.Meta.AutoShard(time, start, () => Log2.FindCount()).ToArray();
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^1]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^1]);

time = DateTime.Today;
start = time.AddDays(-3);
time = time.AddSeconds(1);
XTrace.WriteLine("AutoShard start={0} end={1}", time, start);
Log2.Meta.AutoShard(time, start, () => Log2.FindCount()).ToArray();
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-0):yyyyMMdd}", sqls[^4]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^1]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(-3):yyyy}] Select Count(*) From Log2_{time.AddDays(-3):yyyyMMdd}", sqls[^1]);

// 恢复现场,避免影响其它测试用例
Log2.Meta.ShardPolicy = null;
Expand Down Expand Up @@ -305,16 +313,18 @@ public void SearchAutoShard()
// 自动分表查询,在指定时间区间内执行多次查询
XTrace.WriteLine("AutoShard FindAll ({0}, {1})", start, time);
var list = Log2.Meta.AutoShard(start, time, () => Log2.FindAll(Log2._.Success == true)).SelectMany(e => e).ToList();
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(0):yyyyMMdd} Where Success=1", sqls[^1]);

// 倒序查询
XTrace.WriteLine("AutoShard FindAll ({0}, {1})", time, start);
list = Log2.Meta.AutoShard(time, start, () => Log2.FindAll(Log2._.Success == true)).SelectMany(e => e).ToList();
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(0):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);

// 枚举数查询,只有在遍历时才真正执行查询
var idx = 1;
Expand Down Expand Up @@ -353,14 +363,15 @@ public void SearchAutoShard2()
// SelectCount遍历
XTrace.WriteLine("AutoShard SelectCount({0}, {1})", start, time);
Log2.Meta.AutoShard(start, time, () => Log2.FindCount()).ToArray();
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-0):yyyyMMdd}", sqls[^1]);

// 第一个命中,不查后面
XTrace.WriteLine("FirstOrDefault");
var list = Log2.Meta.AutoShard(start, time, () => Log2.FindAll(Log2._.Success == true)).FirstOrDefault(e => e.Count > 0);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);

// 倒过来查第一个命中
XTrace.WriteLine("FirstOrDefault");
Expand All @@ -370,8 +381,9 @@ public void SearchAutoShard2()
// 查所有
XTrace.WriteLine("SelectMany");
list = Log2.Meta.AutoShard(start, time, () => Log2.FindAll(Log2._.Success == true)).SelectMany(e => e).ToList();
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-0):yyyyMMdd} Where Success=1", sqls[^1]);

// 恢复现场,避免影响其它测试用例
Expand Down Expand Up @@ -400,16 +412,18 @@ public void SearchAutoShard3()
// 遍历SelectCount
XTrace.WriteLine("AutoShard SelectCount({0}, {1})", start, time);
Log2.Meta.AutoShard(start, time, () => Log2.FindCount()).ToArray();
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select Count(*) From Log2_{time.AddDays(-2):yyyyMMdd}", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select Count(*) From Log2_{time.AddDays(-1):yyyyMMdd}", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddDays(-0):yyyyMMdd}", sqls[^1]);

// 倒序FindAll
XTrace.WriteLine("AutoShard FindAll({0}, {1})", time, start);
var list = Log2.Meta.AutoShard(time, start, () => Log2.FindAll(Log2._.Success == true)).SelectMany(e => e).ToList();
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-0):yyyyMMdd} Where Success=1", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);
Assert.StartsWith($"[test_{time.AddDays(-1):yyyy}] Select * From Log2_{time.AddDays(-1):yyyyMMdd} Where Success=1", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(-2):yyyy}] Select * From Log2_{time.AddDays(-2):yyyyMMdd} Where Success=1", sqls[^1]);

// 恢复现场,避免影响其它测试用例
Log2.Meta.ShardPolicy = null;
Expand Down
16 changes: 9 additions & 7 deletions XUnitTest.XCode/EntityTests/SqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,14 @@ public void ShardTestSQLite2()
Assert.StartsWith($"[test_{time:yyyy}] Delete From Log2_{time:yyyyMMdd} Where", sqls[^1]);

var list = Log2.Search(null, null, -1, null, -1, time.AddHours(-24), time, null, new PageParameter { PageSize = 100 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-00):yyyyMMdd} Where", sqls[^1]);

list = Log2.Search(null, null, -1, null, -1, time.AddHours(-24), time, null, new PageParameter { PageIndex = 2, PageSize = 100 });
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select Count(*) From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select * From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^3]);
Assert.StartsWith($"[test_{time.AddHours(-24):yyyy}] Select Count(*) From Log2_{time.AddHours(-24):yyyyMMdd} Where", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddHours(-00):yyyyMMdd} Where", sqls[^1]);

// logs.Delete();
Expand Down Expand Up @@ -366,9 +368,9 @@ public void ShardTestSQLite3()
var exp = new WhereExpression();
exp &= Log2._.CreateTime.Between(time.Date, time.Date.AddDays(2));
var list = Log2.FindAll(exp);

Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(0):yyyyMMdd} Where CreateTime>=", sqls[^3]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(1):yyyyMMdd} Where CreateTime>=", sqls[^2]);
Assert.StartsWith($"[test_{time:yyyy}] Select * From Log2_{time.AddDays(2):yyyyMMdd} Where CreateTime>=", sqls[^1]);
sqls.RemoveAll(e => e.EndsWith("sqlite_master"));
Assert.StartsWith($"[test_{time.AddDays(0):yyyy}] Select * From Log2_{time.AddDays(0):yyyyMMdd} Where CreateTime>=", sqls[^3]);
Assert.StartsWith($"[test_{time.AddDays(1):yyyy}] Select * From Log2_{time.AddDays(1):yyyyMMdd} Where CreateTime>=", sqls[^2]);
Assert.StartsWith($"[test_{time.AddDays(2):yyyy}] Select * From Log2_{time.AddDays(2):yyyyMMdd} Where CreateTime>=", sqls[^1]);
}
}

0 comments on commit bad3cb6

Please sign in to comment.