mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
update ToTreeList tests
This commit is contained in:
parent
5f98749652
commit
a958a64bd0
111
FreeSql.Tests/FreeSql.Tests/UnitTest4.cs
Normal file
111
FreeSql.Tests/FreeSql.Tests/UnitTest4.cs
Normal file
@ -0,0 +1,111 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NpgsqlTypes;
|
||||
using Npgsql.LegacyPostgis;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading;
|
||||
using System.Data.SqlClient;
|
||||
using kwlib;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Tests
|
||||
{
|
||||
public class UnitTest4
|
||||
{
|
||||
[Fact]
|
||||
public void Test04()
|
||||
{
|
||||
g.sqlite.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
|
||||
var repo = g.sqlite.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 = g.sqlite.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 = g.sqlite.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 = g.sqlite.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; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user