From 2017771513772904f37974b25117afb1c2e22487 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 4 Jul 2022 07:57:52 +0800 Subject: [PATCH] update unit test --- .../SqlSeverTest/UnitTest/UOneManyMany.cs | 4 +- .../SqlSeverTest/UnitTest/UOneManyMany2.cs | 13 +- .../SqlSeverTest/UnitTest/UOneManyMany3.cs | 8 +- .../SqlSeverTest/UnitTest/UOneManyMany4.cs | 49 +++- .../SqlSeverTest/UnitTest/UOneManyMany5.cs | 129 ++++++++++ .../SqlSeverTest/UnitTest/UnitManyToMany.cs | 222 ++++++++++++++++++ 6 files changed, 403 insertions(+), 22 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany5.cs create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitManyToMany.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs index 510ce5e41..14c656233 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs @@ -78,11 +78,11 @@ namespace OrmTest } db.DbMaintenance.TruncateTable(); db.InsertNav(list.First()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany2.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany2.cs index 252af535b..33a1013c9 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany2.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany2.cs @@ -76,7 +76,10 @@ namespace OrmTest { throw new Exception("unit error"); } - db.DbMaintenance.TruncateTable(); + db.Deleteable().ExecuteCommand(); + db.Deleteable().ExecuteCommand(); + db.Deleteable().ExecuteCommand(); + db.Deleteable().ExecuteCommand(); foreach (var item in list) { item.sid = 0; @@ -94,11 +97,11 @@ namespace OrmTest } } db.InsertNav(list.First()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); @@ -111,10 +114,10 @@ namespace OrmTest db.DbMaintenance.TruncateTable(); db.InsertNav(list.First().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany3.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany3.cs index a08a5126f..926cf924a 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany3.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany3.cs @@ -94,11 +94,11 @@ namespace OrmTest } } db.InsertNav(list.First()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last()) - .ThenInclude(x => x.school_001) + .Include(x => x.school_001) .ThenInclude(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); @@ -111,10 +111,10 @@ namespace OrmTest db.DbMaintenance.TruncateTable(); db.InsertNav(list.First().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany4.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany4.cs index 2c5f6d4b4..4c770c2dc 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany4.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany4.cs @@ -14,8 +14,8 @@ namespace OrmTest { var db = NewUnitTest.Db; - db.CodeFirst.InitTables(); - db.DbMaintenance.TruncateTable(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); @@ -26,6 +26,8 @@ namespace OrmTest db.Insertable(new School_004() { scid = id1, schname = "北大" }).ExecuteCommand(); db.Insertable(new School_004() { scid = id2, schname = "青华" }).ExecuteCommand(); + db.Insertable(new Book_004() { StudentId = id1, Name = "数学001" }).ExecuteCommand(); + db.Insertable(new Book_004() { StudentId = id2, Name = "语文002" }).ExecuteCommand(); db.Insertable(new Room_004() { roomId = id1, schoolId = id1, roomName = "北大01室" }).ExecuteCommand(); @@ -41,6 +43,7 @@ namespace OrmTest var list = db.Queryable() .Includes(x => x.school_001, x => x.rooms, x => x.desk) + .Includes(x=>x.books) .Where(x => x.school_001.rooms.Any(z => z.desk.Any())).ToList(); if (list.Count() != 2) @@ -81,7 +84,7 @@ namespace OrmTest { throw new Exception("unit error"); } - db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); foreach (var item in list) { item.sid = Guid.Empty; @@ -95,17 +98,30 @@ namespace OrmTest { z.deskid = Guid.Empty; z.roomId = Guid.Empty; + } } + foreach (var z in item.books) + { + z.bookid = Guid.Empty; + z.StudentId = Guid.Empty; + } } db.InsertNav(list.First()) - .ThenInclude(x => x.school_001) + + .Include(x => x.school_001) .ThenInclude(x => x.rooms) - .ThenInclude(x => x.desk).ExecuteCommand(); + .ThenInclude(x => x.desk) + + .Include(x=>x.books).ExecuteCommand(); + db.InsertNav(list.Last()) - .ThenInclude(x => x.school_001) + + .Include(x => x.school_001) .ThenInclude(x => x.rooms) - .ThenInclude(x => x.desk).ExecuteCommand(); + .ThenInclude(x => x.desk) + + .Include(x=>x.books).ExecuteCommand(); if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 || db.Queryable().Count() != 2 || db.Queryable().Count() != 2) @@ -113,13 +129,13 @@ namespace OrmTest throw new Exception("unit error"); } - db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); db.InsertNav(list.First().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); db.InsertNav(list.Last().school_001) - .ThenInclude(x => x.rooms) + .Include(x => x.rooms) .ThenInclude(x => x.desk).ExecuteCommand(); if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 || db.Queryable().Count() != 2 || db.Queryable().Count() != 0) @@ -136,8 +152,19 @@ namespace OrmTest public Guid SchoolId { get; set; } [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(SchoolId))] - public School_004 school_001 { get; set; } + public School_004 school_001 { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Book_004.StudentId))] + public List books { get; set; } + + } + + public class Book_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public Guid bookid { get; set; } + public string Name { get; set; } + public Guid StudentId { get; set; } } public class School_004 diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany5.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany5.cs new file mode 100644 index 000000000..980149e3c --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany5.cs @@ -0,0 +1,129 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UOneManyMany5 + { + + public static void init() + { + + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + var id1 = Guid.NewGuid(); + var id2 = Guid.NewGuid(); + var id3 = Guid.NewGuid(); + var id4 = Guid.NewGuid(); + db.Insertable(new Student_004() { sid=Guid.NewGuid(), StudentId = id1, Name = "北大jack", SchoolId = id1 }).ExecuteCommand(); + db.Insertable(new Student_004() { sid=Guid.NewGuid(), StudentId = id2, Name = "青华jack", SchoolId = id2 }).ExecuteCommand(); + + db.Insertable(new School_004() { scid=Guid.NewGuid(), scid2 = id1, schname = "北大" }).ExecuteCommand(); + db.Insertable(new School_004() { scid = Guid.NewGuid(),scid2 = id2, schname = "青华" }).ExecuteCommand(); + db.Insertable(new Book_004() { StudentId = id1, Name = "数学001" }).ExecuteCommand(); + db.Insertable(new Book_004() { StudentId = id2, Name = "语文002" }).ExecuteCommand(); + + + db.Insertable(new Room_004() { roomId = id1, schoolId = id1, roomName = "北大01室" }).ExecuteCommand(); + db.Insertable(new Room_004() { roomId = id2, schoolId = id1, roomName = "北大02室" }).ExecuteCommand(); + db.Insertable(new Room_004() { roomId = id3, schoolId = id2, roomName = "青华03室" }).ExecuteCommand(); + db.Insertable(new Room_004() { roomId = id4, schoolId = id2, roomName = "青华04室" }).ExecuteCommand(); + + db.Insertable(new Desk_004() { roomId = id1, deskid = id1, deskName = "北大01室_01" }).ExecuteCommand(); + db.Insertable(new Desk_004() { roomId = id2, deskid = id2, deskName = "北大02室_01" }).ExecuteCommand(); + db.Insertable(new Desk_004() { roomId = id3, deskid = id3, deskName = "青华03室_01" }).ExecuteCommand(); + db.Insertable(new Desk_004() { roomId = id4, deskid = id4, deskName = "青华04室_01" }).ExecuteCommand(); + + + var list = db.Queryable() + .Includes(x => x.school_001 ) + .Includes(x=>x.books) + .ToList(); + + + db.DbMaintenance.TruncateTable(); + foreach (var item in list) + { + item.sid = Guid.Empty; + item.SchoolId = Guid.Empty; + item.school_001.scid = Guid.Empty; + foreach (var z in item.books) + { + z.bookid = Guid.Empty; + z.StudentId = Guid.Empty; + } + } + db.InsertNav(list.First()) + .Include(x=>x.books) + .Include(x=>x.school_001) + .ExecuteCommand(); + + db.InsertNav(list.Last()) + .Include(x=>x.books) + .Include(x=>x.school_001) + .ExecuteCommand(); + + } + + [SugarTable("Student_005")] + public class Student_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public Guid sid { get; set; } + public Guid StudentId { get; set; } + public string Name { get; set; } + + public Guid SchoolId { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(SchoolId),nameof(School_004.scid2))] + public School_004 school_001 { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Book_004.StudentId),nameof(StudentId))] + public List books { get; set; } + + } + + [SugarTable("Book_005")] + public class Book_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public Guid bookid { get; set; } + public string Name { get; set; } + public Guid StudentId { get; set; } + } + + [SugarTable("School_005")] + public class School_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true )] + public Guid scid { get; set; } + public Guid scid2 { get; set; } + public string schname { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Room_004.schoolId))] + public List rooms { get; set; } + } + + [SugarTable("Room_005")] + public class Room_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true )] + public Guid roomId { get; set; } + public Guid schoolId { get; set; } + public string roomName { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Desk_004.roomId))] + public List desk { get; set; } + } + [SugarTable("Desk_005")] + public class Desk_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public Guid deskid { get; set; } + public string deskName { get; set; } + public Guid roomId { get; set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitManyToMany.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitManyToMany.cs new file mode 100644 index 000000000..5507de8d3 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitManyToMany.cs @@ -0,0 +1,222 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + internal class UnitManyToMany + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new OperatorInfo() + { + id="1", + createTime=DateTime.Now, + isDel=1, + isDisabled=1, + openid="", + phone="", + pwd="", + realname="a01", + remark="a", + sno="a", + username="a01" + }).ExecuteCommand(); + db.Insertable(new OperatorInfo() + { + id = "2", + createTime = DateTime.Now, + isDel = 1, + isDisabled = 1, + openid = "", + phone = "", + pwd = "", + realname = "a01", + remark = "a", + sno = "a", + username = "admin" + }).ExecuteCommand(); + db.Insertable(new OperatorInfo() + { + id = "3", + createTime = DateTime.Now, + isDel = 1, + isDisabled = 1, + openid = "", + phone = "", + pwd = "", + realname = "a01", + remark = "a", + sno = "a", + username = "admin" + }).ExecuteCommand(); + var id=db.Insertable(new Role() + { + id=1, + createTime=DateTime.Now, + name="admin" + + }).ExecuteReturnIdentity(); + var id2 = db.Insertable(new Role() + { + id = 2, + createTime = DateTime.Now, + name = "admin" + + }).ExecuteReturnIdentity(); + db.Insertable(new OptRole() { operId="1", roleId=id }).ExecuteCommand(); + db.Insertable(new OptRole() { id=2, operId = "2", roleId = id2 }).ExecuteCommand(); + db.Queryable() + .Includes(x => x.Roles).Where(x => x.Roles.Any(z=>z.id==1)) + .ToList(); + var list = db.Queryable() + .Includes(x => x.Roles).Where(x=>x.Roles.Any()).ToList(); + if (list.Count != 2) + { + throw new Exception("unit error"); + } + var list3 = db.Queryable() + .Includes(x => x.Roles).Where(x => x.Roles.Count()==0).ToList(); + if (list3.Count != 1) + { + throw new Exception("unit error"); + } + var list2=db.Queryable() + .Includes(x => x.Roles.MappingField(z=>z.name,()=>x.username).ToList()) + .ToList(); + + var list4 = db.Queryable() + .Includes(x => x.Roles.Skip(10).Take(1).ToList()) + .ToList(); + } + + /// + /// 描述: + /// 作者:synjones + /// 时间:2022-04-20 21:30:28 + /// + [SugarTable("unit_operatorinfo")] + public partial class OperatorInfo + { /// + /// 多角色 + /// + [Navigate(typeof(OptRole), nameof(OptRole.operId), nameof(OptRole.roleId))]//名字换 + public List Roles { get; set; } + /// + /// 主键 + /// + [SugarColumn(IsPrimaryKey = true)] + public string id { get; set; } + + /// + /// 姓名 + /// + public string realname { get; set; } + + /// + /// 账号 + /// + public string username { get; set; } + + /// + /// 密码 + /// + public string pwd { get; set; } + + /// + /// 学号 + /// + public string sno { get; set; } + + /// + /// openid + /// + public string openid { get; set; } + + /// + /// 手机号码 + /// + public string phone { get; set; } + + /// + /// 备注信息 + /// + public string remark { get; set; } + + /// + /// 创建日期 + /// + public DateTime createTime { get; set; } + + /// + /// 状态(1:启用,2:禁用) + /// + public int isDisabled { get; set; } + + /// + /// 是否删除(1:正常;2:删除) + /// + public int isDel { get; set; } + + } + + /// + /// 描述: + /// 作者:synjones + /// 时间:2022-04-20 21:30:28 + /// + [SugarTable("unit_role1")] + public partial class Role + { + /// + /// 角色 + /// + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int id { get; set; } + + /// + /// 角色名称 + /// + public string name { get; set; } + + /// + /// 创建时间 + /// + public DateTime createTime { get; set; } + + + } + + /// + /// 描述: + /// 作者:synjones + /// 时间:2022-04-21 14:35:09 + /// + [SugarTable("unit_operator_role")] + public partial class OptRole + { + /// + /// + /// + [SugarColumn(IsPrimaryKey = true)] + public int id { get; set; } + + /// + /// + /// + public string operId { get; set; } + + /// + /// + /// + public int roleId { get; set; } + + + } + } +}