mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
add ToTreeList tests #268
This commit is contained in:
@ -1848,5 +1848,90 @@ WHERE ((b.`IsFinished` OR a.`TaskType` = 3) AND b.`EnabledMark` = 1)", groupsql1
|
||||
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToTreeList()
|
||||
{
|
||||
var fsql = g.mysql;
|
||||
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
|
||||
var repo = fsql.GetRepository<VM_District_Child>();
|
||||
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
|
||||
repo.DbContextOptions.NoneParameter = true;
|
||||
repo.Insert(new VM_District_Child
|
||||
{
|
||||
Code = "100000",
|
||||
Name = "中国",
|
||||
Childs = new List<VM_District_Child>(new[] {
|
||||
new VM_District_Child
|
||||
{
|
||||
Code = "110000",
|
||||
Name = "北京市",
|
||||
Childs = new List<VM_District_Child>(new[] {
|
||||
new VM_District_Child{ Code="110100", Name = "北京市" },
|
||||
new VM_District_Child{ Code="110101", Name = "东城区" },
|
||||
})
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
var t1 = fsql.Select<VM_District_Parent>()
|
||||
.InnerJoin(a => a.ParentCode == a.Parent.Code)
|
||||
.Where(a => a.Code == "110101")
|
||||
.ToList(true);
|
||||
Assert.Single(t1);
|
||||
Assert.Equal("110101", t1[0].Code);
|
||||
Assert.NotNull(t1[0].Parent);
|
||||
Assert.Equal("110000", t1[0].Parent.Code);
|
||||
|
||||
var t2 = fsql.Select<VM_District_Parent>()
|
||||
.InnerJoin(a => a.ParentCode == a.Parent.Code)
|
||||
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
|
||||
.Where(a => a.Code == "110101")
|
||||
.ToList(true);
|
||||
Assert.Single(t2);
|
||||
Assert.Equal("110101", t2[0].Code);
|
||||
Assert.NotNull(t2[0].Parent);
|
||||
Assert.Equal("110000", t2[0].Parent.Code);
|
||||
Assert.NotNull(t2[0].Parent.Parent);
|
||||
Assert.Equal("100000", t2[0].Parent.Parent.Code);
|
||||
|
||||
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
|
||||
Assert.Single(t3);
|
||||
Assert.Equal("100000", t3[0].Code);
|
||||
Assert.Single(t3[0].Childs);
|
||||
Assert.Equal("110000", t3[0].Childs[0].Code);
|
||||
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
|
||||
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
|
||||
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
|
||||
}
|
||||
|
||||
[Table(Name = "D_District")]
|
||||
public class BaseDistrict
|
||||
{
|
||||
[Column(IsPrimary = true, StringLength = 6)]
|
||||
public string Code { get; set; }
|
||||
|
||||
[Column(StringLength = 20, IsNullable = false)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column(StringLength = 6)]
|
||||
public virtual string ParentCode { get; set; }
|
||||
}
|
||||
[Table(Name = "D_District", DisableSyncStructure = true)]
|
||||
public class VM_District_Child : BaseDistrict
|
||||
{
|
||||
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
|
||||
|
||||
[Navigate(nameof(ParentCode))]
|
||||
public List<VM_District_Child> Childs { get; set; }
|
||||
}
|
||||
[Table(Name = "D_District", DisableSyncStructure = true)]
|
||||
public class VM_District_Parent : BaseDistrict
|
||||
{
|
||||
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
|
||||
|
||||
[Navigate(nameof(ParentCode))]
|
||||
public VM_District_Parent Parent { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user