From efc84742d5d6874274cacb9cb5117e0459a6bc84 Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Sun, 31 Jan 2021 18:54:56 +0800 Subject: [PATCH] Update Unit test --- .../SqlSeverTest/UnitTest/Main.cs | 4 +- .../SqlSeverTest/UnitTest/UEnum.cs | 44 +++++ .../SqlSeverTest/UnitTest/UFilter.cs | 91 ++++++++++ .../SqlSeverTest/UnitTest/UInsert.cs | 164 ++++++++++++++++++ .../SqlSeverTest/UnitTest/UQueryable2.cs | 17 +- 5 files changed, 318 insertions(+), 2 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs index 21243c692..28814164c 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs @@ -31,8 +31,10 @@ namespace OrmTest } public static void Init() { - Tran(); + Filter(); Insert(); + Enum(); + Tran(); Queue(); CodeFirst(); Updateable(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs new file mode 100644 index 000000000..9123ae299 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Enum() + { + Db.CodeFirst.InitTables(); + Db.Insertable(new Unit00Z11C12() { type = UnitType.a, type2 = null }).ExecuteCommand(); + var x=Db.Queryable().Select(it=>new { + x=it.type, + x2=it.type2 + }).ToList(); + + var x2 = Db.Queryable().ToList(); + Db.Updateable().SetColumns(it => it.type2 == UnitType.b) + .Where(it=>true).ExecuteCommand(); + var x3 = Db.Queryable().ToList(); + foreach (var item in x3) + { + item.type2 = null; + } + Db.Updateable(x3).WhereColumns(it=>it.type).ExecuteCommand(); + var x4 = Db.Queryable().ToList(); + } + public class Unit00Z11C12 + { + [SqlSugar.SugarColumn(ColumnDataType = "int", IsNullable = false)] + public UnitType type { get; set; } + [SqlSugar.SugarColumn(ColumnDataType ="int",IsNullable =true)] + public UnitType? type2 { get; set; } + } + public enum UnitType { + a=-1, + b=2, + c=3 + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs new file mode 100644 index 000000000..af789f914 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs @@ -0,0 +1,91 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Filter() + { + var db = Db; + db.QueryFilter.Add(new SqlSugar.TableFilterItem(it=>it.id>0)); + + var s1=db.Queryable().ToSql(); + if (s1.Key.Contains(">")) + { + throw new Exception("UnitFilter"); + } + var s4 = db.Queryable((o, i) => i.OrderId == o.Id).Select("o.*").ToSql(); + if (s4.Key.Contains(">")) + { + throw new Exception("UnitFilter"); + } + + var s5 = db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.Id == i.OrderId)) + .Select("o.*").ToSql(); + if (s5.Key.Contains(">")) + { + throw new Exception("UnitFilter"); + } + + + var s2 = db.Queryable().ToSql(); + UValidate.Check(s2.Key, + @"SELECT [id],[name] FROM [UnitFilterClass1] WHERE ( [id] > @id0 )", "UnitFilter"); + + var s3= db.Queryable().Where(it=>it.name!=null).ToSql(); + UValidate.Check(s3.Key, + @"SELECT [id],[name] FROM [UnitFilterClass1] WHERE ( [name] IS NOT NULL ) AND ( [id] > @id1 )", "UnitFilter"); + + + + var s6 = db.Queryable((o, i) => i.id == o.id).Select("o.*").ToSql(); + UValidate.Check(s6.Key, "SELECT o.* FROM [UnitFilterClass1] o ,[UnitFilterClass2] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] > @id0 )", "UnitFilter"); + + var s7 = db.Queryable((o, i) => i.id == o.id).Where(o=>o.id==1).Select("o.*").ToSql(); + UValidate.Check(s7.Key, "SELECT o.* FROM [UnitFilterClass1] o ,[UnitFilterClass2] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] = @id0 ) AND ( [o].[id] > @id1 )", "UnitFilter"); + + var s8 = db.Queryable((o, i) => i.id == o.id).Where(o => o.id == 1).Select("o.*").ToSql(); + UValidate.Check(s8.Key, "SELECT o.* FROM [UnitFilterClass2] o ,[UnitFilterClass1] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] = @id0 ) AND ( [i].[id] > @id1 )", "UnitFilter"); + + + var s9 = db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id)) + .Select("o.*").ToSql(); + UValidate.Check(s9.Key, "SELECT o.* FROM [UnitFilterClass1] o Left JOIN [UnitFilterClass2] i ON ( [o].[id] = [i].[id] ) WHERE ( [o].[id] > @id0 )", "UnitFilter"); + + + var s10= db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id)) + .Where((o,i) =>i.id==0).Select("o.*").ToSql(); + UValidate.Check(s10.Key, "SELECT o.* FROM [UnitFilterClass1] o Left JOIN [UnitFilterClass2] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] = @id0 ) AND ( [o].[id] > @id1 )", "UnitFilter"); + + var s11 = db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id)) + .Select("o.*").ToSql(); + UValidate.Check(s11.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] > @id0 )", "UnitFilter"); + + db.QueryFilter.Add(new SqlSugar.TableFilterItem(it => it.id==0)); + + var s12 = db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id)) + .Select("o.*").ToSql(); + UValidate.Check(s12.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] > @id0 ) AND ( [o].[id] = @id1 )", "UnitFilter"); + + var s13 = db.Queryable((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id)) + .Where(o=>o.name=="") + .Select("o.*").ToSql(); + UValidate.Check(s13.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [o].[name] = @name0 ) AND ( [i].[id] > @id1 ) AND ( [o].[id] = @id2 )", "UnitFilter"); + } + public class UnitFilterClass1 + { + public int id { get; set; } + public string name { get; set; } + } + public class UnitFilterClass2 + { + public int id { get; set; } + public string name { get; set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs index f0b20032f..6bd6c405d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs @@ -11,6 +11,7 @@ namespace OrmTest public static void Insert() { var db = Db; + db.DbMaintenance.TruncateTable(); db.CodeFirst.InitTables(); db.Insertable(new List { @@ -50,9 +51,172 @@ namespace OrmTest { throw new Exception("Unit Insert"); } + List list2 = new List(); + for (int i = 1; i <= 20; i++) + { + UinitBlukTable data = new UinitBlukTable() + { + Create=DateTime.Now.AddDays(-1), + Id=i , + Name =i%3==0?"a":"b" + }; + list2.Add(data); + } + list2.First().Name = null; + db.DbMaintenance.TruncateTable(); + db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand(); + var x=Db.Storageable(list2) + .SplitInsert(it => it.NotAny(y=>y.Id==it.Item.Id)) + .SplitUpdate(it => it.Any(y => y.Id == it.Item.Id)) + .SplitDelete(it=>it.Item.Id>10) + .SplitIgnore(it=>it.Item.Id==1) + .SplitError(it => it.Item.Id == 3,"id不能等于3") + .SplitError(it => it.Item.Id == 4, "id不能等于4") + .SplitError(it => it.Item.Id == 5, "id不能等于5") + .SplitError(it => it.Item.Name==null, "name不能等于") + .WhereColumns(it=> new { it.Id }) + .ToStorage(); + x.AsDeleteable.ExecuteCommand(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + foreach (var item in x.ErrorList) + { + Console.Write(item.StorageMessage+" "); + } + db.DbMaintenance.TruncateTable(); + IDemo1(); + IDemo2(); + IDemo3(); + IDemo4(); } + private static void IDemo4() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now.AddYears(-2) }); + list2.Add(new UinitBlukTable() { Id = 4, Name ="", Create = DateTime.Now.AddYears(-2) }); + + db.Insertable(list2.First()).ExecuteCommand();//插入第一条 + + var x = Db.Storageable(list2) + .SplitError(it => string.IsNullOrEmpty(it.Item.Name), "名称不能为空") + .SplitError(it => it.Item.Create it.Any(y=>y.Id==it.Item.Id))//存在更新 + .SplitInsert(it => true)//剩余的插入 + .ToStorage(); + Console.WriteLine(" 插入 {0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}" , + x.InsertList.Count, + x.UpdateList.Count, + x.ErrorList.Count, + x.IgnoreList.Count, + x.DeleteList.Count, + x.TotalList.Count + ); + foreach (var item in x.ErrorList) + { + Console.WriteLine("id等于"+item.Item.Id+" : "+item.StorageMessage); + } + + int i=x.AsInsertable.ExecuteCommand(); + Console.WriteLine(1 + "条成功插入"); + i=x.AsUpdateable.ExecuteCommand(); + Console.WriteLine(1 + "条成功更新"); + + db.DbMaintenance.TruncateTable(); + } + private static void IDemo3() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now }); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Item.Id == 1) + .SplitInsert(it => it.Item.Id == 2) + .SplitDelete(it => it.Item.Id == 3).ToStorage(); + + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + x.AsDeleteable.ExecuteCommand(); + + if (x.InsertList.Count != 1) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count != 1) + { + throw new Exception("Unit Insert"); + } + if (x.DeleteList.Count != 1) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + + private static void IDemo1() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 0, Name = "a", Create = DateTime.Now }); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Item.Id > 0) + .SplitInsert(it => it.Item.Id == 0).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + + if (x.InsertList.Count > 1) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count !=2) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + private static void IDemo2() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a1", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a2", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a3", Create = DateTime.Now }); + db.Insertable(list2[1]).ExecuteCommand(); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Any(y=>y.Id==it.Item.Id)) + .SplitInsert(it => it.NotAny(y => y.Id == it.Item.Id)).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + + + if (x.InsertList.Count!=2) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count != 1) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + public class UinitBlukTable { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] public int Id { get; set; } public string Name { get; set; } [SqlSugar.SugarColumn(IsNullable =true)] diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs index 769b68afb..1afaca31d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs @@ -32,7 +32,10 @@ namespace OrmTest .Mapper(it => it.Items, it => it.Items.First().OrderId) .Where(it => it.Items.Any()) .ToList(); - + var list7= Db.Queryable() + .Mapper(it => it.Items, it => it.Items.First().OrderId) + .Where(it => it.Items.Any(y => y.ItemId == 1)) + .ToList(); var sql=Db.Queryable().AS("[order]").ToList(); @@ -78,6 +81,18 @@ namespace OrmTest id1=x.Id, name=y.Name }).ToList(); + + var qu3 = Db.Queryable().Select(it => new + { + id = it.Id, + name = it.Name + }).MergeTable() + .Where(it=>2>it.id).Select(it=> new Order() { + Id=SqlFunc.IIF(2>it.id,1,2) + }).ToList(); + + + var qu4 = Db.Queryable().OrderBy(it=>it.Id+it.Id).ToList(); } } }