Update Unit test

This commit is contained in:
sunkaixuan 2023-03-05 20:37:49 +08:00
parent a9cc067764
commit a0fd9c59df
2 changed files with 79 additions and 4 deletions

View File

@ -21,5 +21,35 @@ namespace Test.Model
[Navigate(NavigateType.OneToMany, nameof(Playground.PId))]
public List<Playground> Playgrounds { get; set; }
[Navigate(NavigateType.OneToMany, nameof(OtherThingOne.PId))]
public List<OtherThingOne> OtherThingOnes { get; set; }
[Navigate(NavigateType.OneToMany, nameof(OtherThingTwo.PId))]
public List<OtherThingTwo> OtherThingTwos { get; set; }
}
[SugarTable("OtherThingOne")]
public class OtherThingOne
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SugarColumn(ColumnName = "Name")]
public string Name { get; set; }
[SugarColumn(ColumnName = "PId")]
public int? PId { get; set; }
}
[SugarTable("OtherThingTwo")]
public class OtherThingTwo
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SugarColumn(ColumnName = "Name")]
public string Name { get; set; }
[SugarColumn(ColumnName = "PId")]
public int? PId { get; set; }
}
}

View File

@ -15,14 +15,18 @@ namespace OrmTest
{
var db = NewUnitTest.Db;
db.CodeFirst.InitTables<Student, School, Playground, Room, Book>();
db.CodeFirst.InitTables<OtherThingTwo, OtherThingOne>();
Demo1(db);
Demo2(db);
}
private static void Demo1(SqlSugarClient db)
{
//清空初始化数据
db.DbMaintenance.TruncateTable<Student, School, Playground, Room, Book>();
db.DbMaintenance.TruncateTable<OtherThingOne, OtherThingTwo>();
//插入测试数据
db.InsertNav(new Student()
{
Age = "11",
@ -37,25 +41,66 @@ namespace OrmTest
} ,
Rooms=new List<Room>(){
new Room() { Name= "学校ROOM" } } }
new Room() { Name= "学校ROOM" }
},
OtherThingOnes=new List<OtherThingOne>()
{
new OtherThingOne(){ Name="学校One" }
},
OtherThingTwos=new List<OtherThingTwo>()
{
new OtherThingTwo(){ Name="学校Two" }
}
}
}
})
.Include(s => s.Books)
// .Include(s => s.Schools)
.Include(s => s.Schools).ThenInclude(sc => sc.Rooms)
.Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds)
.Include(s => s.Schools).ThenInclude(sc => sc.OtherThingOnes)
.Include(s => s.Schools).ThenInclude(sc => sc.OtherThingTwos)
.ExecuteCommand();
var data = db.Queryable<Student>()
.Includes(s => s.Books)
.Includes(s => s.Schools, s => s.Rooms)
.Includes(s => s.Schools, s => s.Playgrounds)
.Includes(s => s.Schools, s => s.OtherThingOnes)
.Includes(s => s.Schools, s => s.OtherThingTwos)
.ToList();
data[0].Schools[0].OtherThingOnes[0].Name = "updateOne";
if (data.Count != 1 || data.First().Schools.Count != 1 || data.First().Schools.First().Rooms.Count() != 1 || data.First().Schools.First().Playgrounds.Count() != 1)
{
throw new Exception("unit error");
}
//更新数据
db.UpdateNav(data)
.Include(s => s.Schools).ThenInclude(sc => sc.Rooms)
.Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds)
.Include(s => s.Schools).ThenInclude(sc => sc.OtherThingOnes)
.Include(s => s.Schools).ThenInclude(sc => sc.OtherThingTwos)
.Include(s => s.Books)
.ExecuteCommand();
var data2 = db.Queryable<Student>()
.Includes(s => s.Books)
.Includes(s => s.Schools, s => s.Rooms)
.Includes(s => s.Schools, s => s.Playgrounds)
.Includes(s => s.Schools, s => s.OtherThingOnes)
.Includes(s => s.Schools, s => s.OtherThingTwos)
.ToList();
if (data2.Count != 1 || data2.First().Schools.Count != 1 || data2.First().Schools.First().Rooms.Count() != 1 || data2.First().Schools.First().Playgrounds.Count() != 1)
{
throw new Exception("unit error");
}
}
private static void Demo2(SqlSugarClient db)
{