From 3f8075d44689b17958d88ce9c8742a439a4dbb91 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 3 Jul 2022 01:20:29 +0800 Subject: [PATCH] Add unit test --- .../SqlSeverTest/UnitTest/Main.cs | 2 + .../SqlSeverTest/UnitTest/UOneManyMany.cs | 121 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs index b542c8f33..972815cf2 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,8 @@ namespace OrmTest } public static void Init() { + + UOneManyMany.init(); UNavDynamic111N.Init(); UCustom019.Init(); UCustom015.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs new file mode 100644 index 000000000..acded05f0 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UOneManyMany.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UOneManyMany + { + + public static void init() + { + + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + db.Insertable(new Student_001() { sid = 1, Name = "北大jack", SchoolId = 1 }).ExecuteCommand(); + db.Insertable(new Student_001() { sid = 2, Name = "青华jack", SchoolId = 2}).ExecuteCommand(); + + db.Insertable(new School_001() { scid = 1, schname = "北大" }).ExecuteCommand(); + db.Insertable(new School_001() { scid = 2, schname = "青华" }).ExecuteCommand(); + + + db.Insertable(new Room_001() { roomId = 1, schoolId =1, roomName= "北大01室" }).ExecuteCommand(); + db.Insertable(new Room_001() { roomId = 2, schoolId =1, roomName="北大02室" }).ExecuteCommand(); + db.Insertable(new Room_001() { roomId = 3, schoolId = 2, roomName = "青华03室" }).ExecuteCommand(); + db.Insertable(new Room_001() { roomId = 4, schoolId = 2, roomName = "青华04室" }).ExecuteCommand(); + + db.Insertable(new Desk_001() { roomId = 1, deskid = 1, deskName = "北大01室_01" }).ExecuteCommand(); + db.Insertable(new Desk_001() { roomId = 2, deskid = 2, deskName = "北大02室_01" }).ExecuteCommand(); + db.Insertable(new Desk_001() { roomId = 3, deskid = 3, deskName = "青华03室_01" }).ExecuteCommand(); + db.Insertable(new Desk_001() { roomId = 4, deskid = 4, deskName = "青华04室_01" }).ExecuteCommand(); + + + var list=db.Queryable() + .Includes(x => x.school_001, x => x.rooms) + .Where(x=>x.school_001.rooms.Any(z=>z.rooms.Any())).ToList(); + + if (list.Count() !=2) + { + throw new Exception("unit error"); + } + + var list2 = db.Queryable() + .Includes(x => x.school_001, x => x.rooms) + .Where(x => x.school_001.rooms.Any(z => + z.roomName== "北大01室" && + z.rooms.Any())).ToList(); + + + if (list2.Count() != 1) + { + throw new Exception("unit error"); + } + + var list3 = db.Queryable() + .Includes(x=>x.school_001,x=>x.rooms) + .Where(x => x.school_001.rooms.Any(z => + z.roomName == "青华03室" && + z.rooms.Any(c=>c.deskName== "青华03室_01"))).ToList(); + + if (list3.Count != 1) + { + throw new Exception("unit error"); + } + + var list4 = db.Queryable() + .Where(x => x.school_001.rooms.Any(z => + z.roomName == "青华03室" && + z.rooms.Any(c => c.deskName == "青华04室_01"))).ToList(); + + + if (list4.Count != 0) + { + throw new Exception("unit error"); + } + + } + + public class Student_001 + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public int sid { get; set; } + public string Name { get; set; } + + public int SchoolId { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(SchoolId))] + public School_001 school_001 { get; set; } + + } + + public class School_001 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int scid { get; set; } + public string schname { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Room_001.schoolId))] + public List rooms { get; set; } + } + + public class Room_001 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int roomId { get; set; } + public int schoolId { get; set; } + public string roomName { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Desk_001.roomId))] + public List rooms { get; set; } + } + + public class Desk_001 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int deskid { get; set; } + public string deskName { get; set; } + public int roomId { get; set; } + } + } +}