From b931d2604c18d1521a19e2beb294647cf8d9700a Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 3 Jul 2022 04:37:53 +0800 Subject: [PATCH] Add Unit test --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 1 + .../SqlServerTest/UnitTest/UOneManyMany4.cs | 170 ++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UOneManyMany4.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 898083d7d..be27f87e6 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -139,6 +139,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 5b5673394..9bd91cd54 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -32,6 +32,7 @@ namespace OrmTest public static void Init() { + UOneManyMany4.init(); UOneManyMany3.init(); UOneManyMany2.init(); UOneManyMany.init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UOneManyMany4.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UOneManyMany4.cs new file mode 100644 index 000000000..a227b45db --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UOneManyMany4.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UOneManyMany4 + { + + 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 = id1, Name = "北大jack", SchoolId = id1 }).ExecuteCommand(); + db.Insertable(new Student_004() { sid = id2, Name = "青华jack", SchoolId = id2 }).ExecuteCommand(); + + db.Insertable(new School_004() { scid = id1, schname = "北大" }).ExecuteCommand(); + db.Insertable(new School_004() { scid = id2, schname = "青华" }).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, x => x.rooms, x => x.desk) + .Where(x => x.school_001.rooms.Any(z => z.desk.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.desk.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.desk.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.desk.Any(c => c.deskName == "青华04室_01"))).ToList(); + + + if (list4.Count != 0) + { + throw new Exception("unit error"); + } + db.DbMaintenance.TruncateTable(); + foreach (var item in list) + { + item.sid = Guid.Empty; + item.SchoolId = Guid.Empty; + item.school_001.scid = Guid.Empty; + foreach (var r in item.school_001.rooms) + { + r.roomId = Guid.Empty; + r.schoolId = Guid.Empty; + foreach (var z in r.desk) + { + z.deskid = Guid.Empty; + z.roomId = Guid.Empty; + } + } + } + db.InsertNav(list.First()) + .ThenInclude(x => x.school_001) + .ThenInclude(x => x.rooms) + .ThenInclude(x => x.desk); + db.InsertNav(list.Last()) + .ThenInclude(x => x.school_001) + .ThenInclude(x => x.rooms) + .ThenInclude(x => x.desk); + + if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 + || db.Queryable().Count() != 2 || db.Queryable().Count() != 2) + { + throw new Exception("unit error"); + } + + db.DbMaintenance.TruncateTable(); + + db.InsertNav(list.First().school_001) + .ThenInclude(x => x.rooms) + .ThenInclude(x => x.desk); + db.InsertNav(list.Last().school_001) + .ThenInclude(x => x.rooms) + .ThenInclude(x => x.desk); + + if (db.Queryable().Count() != 4 || db.Queryable().Count() != 4 + || db.Queryable().Count() != 2 || db.Queryable().Count() != 0) + { + throw new Exception("unit error"); + } + } + + public class Student_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public Guid sid { get; set; } + public string Name { get; set; } + + public Guid SchoolId { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(SchoolId))] + public School_004 school_001 { get; set; } + + } + + public class School_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true )] + public Guid scid { get; set; } + public string schname { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Room_004.schoolId))] + public List rooms { get; set; } + } + + 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; } + } + + public class Desk_004 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public Guid deskid { get; set; } + public string deskName { get; set; } + public Guid roomId { get; set; } + } + } +}