From 8305650f4a12cb4a7dd1ea4739477a3bdf4644f1 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 13 Apr 2022 22:51:50 +0800 Subject: [PATCH] Update unit test --- Src/Asp.Net/MySqlTest/MySqlTest.csproj | 3 + Src/Asp.Net/MySqlTest/UnitTest/Main.cs | 3 + Src/Asp.Net/MySqlTest/UnitTest/UCustom012.cs | 227 +++++++++++++++++++ Src/Asp.Net/MySqlTest/UnitTest/UCustom014.cs | 131 +++++++++++ Src/Asp.Net/MySqlTest/UnitTest/UCustom015.cs | 96 ++++++++ 5 files changed, 460 insertions(+) create mode 100644 Src/Asp.Net/MySqlTest/UnitTest/UCustom012.cs create mode 100644 Src/Asp.Net/MySqlTest/UnitTest/UCustom014.cs create mode 100644 Src/Asp.Net/MySqlTest/UnitTest/UCustom015.cs diff --git a/Src/Asp.Net/MySqlTest/MySqlTest.csproj b/Src/Asp.Net/MySqlTest/MySqlTest.csproj index 96c6604d0..b14daa0ae 100644 --- a/Src/Asp.Net/MySqlTest/MySqlTest.csproj +++ b/Src/Asp.Net/MySqlTest/MySqlTest.csproj @@ -88,6 +88,9 @@ + + + diff --git a/Src/Asp.Net/MySqlTest/UnitTest/Main.cs b/Src/Asp.Net/MySqlTest/UnitTest/Main.cs index dd399638a..237a114f0 100644 --- a/Src/Asp.Net/MySqlTest/UnitTest/Main.cs +++ b/Src/Asp.Net/MySqlTest/UnitTest/Main.cs @@ -31,6 +31,9 @@ namespace OrmTest } public static void Init() { + UCustom012.Init(); + UCustom014.Init(); + UCustom015.Init(); UCustom011.Init(); UnitCustom01.Init(); UCustom06.Init(); diff --git a/Src/Asp.Net/MySqlTest/UnitTest/UCustom012.cs b/Src/Asp.Net/MySqlTest/UnitTest/UCustom012.cs new file mode 100644 index 000000000..377550a48 --- /dev/null +++ b/Src/Asp.Net/MySqlTest/UnitTest/UCustom012.cs @@ -0,0 +1,227 @@ + +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom012 + { + + public static void Init() + { + var db = NewUnitTest.Db; + + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new RoomA() { RoomId = 1, RoomName = "北大001室", SchoolId = 1 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 2, RoomName = "北大002室", SchoolId = 1 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 3, RoomName = "北大003室", SchoolId = 1 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 4, RoomName = "清华001厅", SchoolId = 2 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 5, RoomName = "清华002厅", SchoolId = 2 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 6, RoomName = "清华003厅", SchoolId = 2 }).ExecuteCommand(); + + + db.Insertable(new SchoolA() { SchoolId = 1, SchoolName = "北大" }).ExecuteCommand(); + db.Insertable(new SchoolA() { SchoolId = 2, SchoolName = "清华" }).ExecuteCommand(); + + db.Insertable(new StudentA() { StudentId = 1, SchoolId = 1, Name = "北大jack" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 2, SchoolId = 1, Name = "北大tom" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 3, SchoolId = 2, Name = "清华jack" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 4, SchoolId = 2, Name = "清华tom" }).ExecuteCommand(); + + db.Insertable(new TeacherA() { SchoolId = 1, Id = 1, Name = "北大老师01" }).ExecuteCommand(); + db.Insertable(new TeacherA() { SchoolId = 1, Id = 2, Name = "北大老师02" }).ExecuteCommand(); + + db.Insertable(new TeacherA() { SchoolId = 2, Id = 3, Name = "清华老师01" }).ExecuteCommand(); + db.Insertable(new TeacherA() { SchoolId = 2, Id = 4, Name = "清华老师02" }).ExecuteCommand(); + + + db.Insertable(new BookA() { BookId=1, Name="java" , studenId=1 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 2, Name = "c#2", studenId = 2 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 3, Name = "c#1", studenId = 2 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 4, Name = "php", studenId = 3 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 5, Name = "js", studenId = 4 }).ExecuteCommand(); + + + var list2 = db.Queryable() + .Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include + .Includes(x => x.Books) + .Where(x=>x.Books.Any(z=>z.BookId==1)) + .Where(x => x.SchoolA.SchoolName == "北大") + .ToList(); + + //先用Mapper导航映射查出第二层 + var list = db.Queryable().Mapper(x => x.SchoolA, x => x.SchoolId).ToList(); + + //参数1 :将第二层对象合并成一个集合 参数2:委托 + //说明:如果2级对象是集合用SelectMany + db.ThenMapper(list.Select(it => it.SchoolA), sch => + { + //参数1: room表关联字段 参数2: school表关联字段, 参数3: school当前记录 + sch.RoomList = db.Queryable().SetContext(room => room.SchoolId, () => sch.SchoolId, sch).ToList(); + + sch.TeacherList = db.Queryable().SetContext(teachera => teachera.SchoolId, () => sch.SchoolId, sch).ToList(); + }); + + + + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new A1() { Id = 1, Name = "a1" }).ExecuteCommand(); + db.Insertable(new A1() { Id = 2, Name = "a2" }).ExecuteCommand(); + db.Insertable(new A1() { Id = 3, Name = "a3" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 1, Name = "b1" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 2, Name = "b2" }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId=1,BId=1 }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId =2, BId = 1 }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId = 2, BId = 2 }).ExecuteCommand(); + + var list3= db.Queryable() + .Includes(x => x.BList.Where(it=>it.Id==1).ToList()) + .Where(x=>x.BList.Any()).ToList(); + + var list31 = db.Queryable().Includes(x => x.BList,x=>x.AList).ToList(); + + db.CodeFirst.InitTables(typeof(Tree1)); + db.DbMaintenance.TruncateTable("Tree1"); + db.Insertable(new Tree1() { Id = 1, Name = "01" }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 2, Name = "0101", ParentId = 1 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 3, Name = "0102", ParentId = 1 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 4, Name = "02" }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 5, Name = "0201", ParentId = 2 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 6, Name = "020101", ParentId = 5 }).ExecuteCommand(); + + + var list21111 = new List(); + var xxx= db.Queryable() + .Includes(it => it.Child) + .Includes(it => it.Parent) + .Where(it=>it.Child.Any()) + .ToList(); + + db.ThenMapper(xxx, it => + { + it.Child = it.Child.OrderBy(x => x.Id).ToList(); + }); + //var json = db.Utilities.SerializeObject(list4); + + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + + db.Insertable(new UnitA001() { id = 1, name1 = "a", orgid = "1" }).ExecuteCommand(); + db.Insertable(new UnitA002() { id = 1, name2= "a2", orgid = "1" }).ExecuteCommand(); + var list5=db.Queryable().ToList(); + db.ThenMapper(list5, it => + { + it.UnitA002 = db.Queryable().SetContext(x => x.orgid, () => it.orgid, it).First(); + }); + } + + public class UnitA001 + { + public int id { get; set; } + public string name1 { get; set; } + public string orgid { get; set; } + [SugarColumn(IsIgnore =true)] + public UnitA002 UnitA002 { get; set; } + } + public class UnitA002 + { + public int id { get; set; } + public string name2{ get; set; } + public string orgid { get; set; } + } + public class Tree1 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + public int ParentId { get; set; } + [Navigat(NavigatType.OneToOne,nameof(ParentId))] + public Tree1 Parent { get; set; } + [Navigat(NavigatType.OneToMany,nameof(Tree1.ParentId))] + public List Child { get; set; } + } + public class ABMapping1 + { + [SugarColumn(IsPrimaryKey = true )] + public int AId { get; set; } + [SugarColumn(IsPrimaryKey = true)] + public int BId { get; set; } + } + public class A1 + { + [SugarColumn(IsPrimaryKey = true )] + public int Id { get; set; } + public string Name { get; set; } + [Navigat(typeof(ABMapping1),nameof(ABMapping1.AId),nameof(ABMapping1.BId))] + public List BList { get; set; } + } + public class B1 + { + [SugarColumn(IsPrimaryKey = true )] + public int Id { get; set; } + public string Name { get; set; } + [Navigat(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))] + public List AList { get; set; } + } + + public class StudentA + { + [SugarColumn(IsPrimaryKey = true)] + public int StudentId { get; set; } + public string Name { get; set; } + public int SchoolId { get; set; } + [Navigat(NavigatType.OneToOne, nameof(SchoolId))] + public SchoolA SchoolA { get; set; } + [Navigat(NavigatType.OneToMany, nameof(BookA.studenId))] + public List Books { get; set; } + + } + public class SchoolA + { + [SugarColumn(IsPrimaryKey = true)] + public int SchoolId { get; set; } + public string SchoolName { get; set; } + [Navigat(NavigatType.OneToMany,nameof(RoomA.SchoolId))] + public List RoomList { get; set; } + [SugarColumn(IsIgnore = true)] + public List TeacherList { get; set; } + } + public class TeacherA + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public int SchoolId { get; set; } + public string Name { get; set; } + } + public class RoomA + { + [SugarColumn(IsPrimaryKey = true)] + public int RoomId { get; set; } + public string RoomName { get; set; } + public int SchoolId { get; set; } + } + public class BookA + { + [SugarColumn(IsPrimaryKey = true)] + public int BookId { get; set; } + + public string Name { get; set; } + public int studenId { get; set; } + } + + } +} diff --git a/Src/Asp.Net/MySqlTest/UnitTest/UCustom014.cs b/Src/Asp.Net/MySqlTest/UnitTest/UCustom014.cs new file mode 100644 index 000000000..b374e37b1 --- /dev/null +++ b/Src/Asp.Net/MySqlTest/UnitTest/UCustom014.cs @@ -0,0 +1,131 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom014 + { + + public static void Init() + { + var db = NewUnitTest.Db; + + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable("Country_111"); + db.DbMaintenance.TruncateTable("Province_111"); + db.DbMaintenance.TruncateTable("City_111"); + db.Insertable(new List() + { + new Country111(){ + Id=1, + Name="中国", + Provinces=new List(){ + new Province111{ + Id=1001, + Name="江苏", + citys=new List(){ + new City111(){ Id=1001001, Name="南通" }, + new City111(){ Id=1001002, Name="南京" } + } + }, + new Province111{ + Id=1002, + Name="上海", + citys=new List(){ + new City111(){ Id=1002001, Name="徐汇" }, + new City111(){ Id=1002002, Name="普陀" } + } + }, + new Province111{ + Id=1003, + Name="北京", + citys=new List(){ + new City111(){ Id=1003001, Name="北京A" }, + new City111(){ Id=1003002, Name="北京B" } + } + } + } + }, + new Country111(){ + Name="美国", + Id=2, + Provinces=new List() + { + new Province111(){ + Name="美国小A", + Id=20001 + }, + new Province111(){ + Name="美国小b", + Id=20002 + } + } + }, + new Country111(){ + Name="英国", + Id=3 + } + }) + .AddSubList(it => new SubInsertTree() + { + Expression = it.Provinces.First().CountryId, + ChildExpression = new List() { + new SubInsertTree(){ + Expression=it.Provinces.First().citys.First().ProvinceId + } + } + }).ExecuteCommand(); + db.Aop.OnLogExecuted = (sq, p) => + { + Console.WriteLine(sq); + }; + var list=db.Queryable() + .Includes(x => x.Provinces.OrderByDescending(x111=>x111.Id).ToList()) + .ToList(); + + var list2 = db.Queryable() + .Includes(x => x.Provinces) + .Where(x=>x.Provinces.Count()>2) + .ToList(); + + var list3 = db.Queryable() + .Includes(x => x.Provinces,x=>x.citys) + .ToList(); + } + + [SugarTable("Country_111")] + public class Country111 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "cid")] + public int Id { get; set; } + public string Name { get; set; } + + [Navigat(NavigatType.OneToMany,nameof(Province111.CountryId))] + public List Provinces { get; set; } + } + [SugarTable("Province_111")] + public class Province111 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "pid")] + public int Id { get; set; } + public string Name { get; set; } + [SugarColumn(ColumnName = "coid")] + public int CountryId { get; set; } + [Navigat(NavigatType.OneToMany, nameof(City111.ProvinceId))] + public List citys { get; set; } + } + [SugarTable("City_111")] + public class City111 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "cid")] + public int Id { get; set; } + [SugarColumn(ColumnName="ppid")] + public int ProvinceId { get; set; } + public string Name { get; set; } + } + } +} diff --git a/Src/Asp.Net/MySqlTest/UnitTest/UCustom015.cs b/Src/Asp.Net/MySqlTest/UnitTest/UCustom015.cs new file mode 100644 index 000000000..69ebc4e58 --- /dev/null +++ b/Src/Asp.Net/MySqlTest/UnitTest/UCustom015.cs @@ -0,0 +1,96 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom015 + { + + public static void Init() + { + var db = NewUnitTest.Db; + db.DbMaintenance.CreateDatabase(); + + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable("Country_1111"); + db.DbMaintenance.TruncateTable("Province_1111"); + db.DbMaintenance.TruncateTable("Country111Info"); + var c = new Country1111() + { + Id=1, + Name="中国",InfoId=1 + }; + var ps = new List(){ + new Province1111{ + Id=1001, + Name="江苏", CountryId=1 + + }, + new Province1111{ + Id=1002, + Name="上海", CountryId=1 + + }, + new Province1111{ + Id=1003, + Name="北京", CountryId=1 + + } + }; + + db.Insertable(c).ExecuteCommand(); + db.Insertable(ps).ExecuteCommand(); + db.Insertable(new Country111Info { Id=1, Name="infoa"}).ExecuteCommand(); + db.Aop.OnLogExecuted = (sq, p) => + { + Console.WriteLine(sq); + }; + + var list = db.Queryable() + .Includes(x => x.Info) + .ToList(); + var list2 = db.Queryable() + .Includes(x => x.Provinces.OrderByDescending(x111 => x111.Id).ToList()) + .ToList(); + } + + [SugarTable("Country_1111")] + public class Country1111 + { + [SqlSugar.SugarColumn(IsPrimaryKey =true, ColumnName = "cid")] + public int Id { get; set; } + public string Name { get; set; } + public int InfoId { get; set; } + + [Navigat(NavigatType.OneToOne, nameof(InfoId))] + public Country111Info Info { get; set; } + + [Navigat(NavigatType.OneToMany,nameof(Province1111.CountryId))] + public List Provinces { get; set; } + } + + public class Country111Info + { + [SqlSugar.SugarColumn(IsPrimaryKey =true,ColumnName = "infoId")] + public int Id { get; set; } + public string Name { get; set; } + } + + [SugarTable("Province_1111")] + public class Province1111 + { + [SqlSugar.SugarColumn( ColumnName = "pid")] + public int Id { get; set; } + public string Name { get; set; } + [SugarColumn(ColumnName = "coid")] + public int CountryId { get; set; } + } + + } +}