mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add unit test
This commit is contained in:
parent
e436e76d6f
commit
11d3540270
@ -31,6 +31,7 @@ namespace OrmTest
|
|||||||
}
|
}
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
|
UNavQuery01.Init();
|
||||||
UCustom012.Init();
|
UCustom012.Init();
|
||||||
Bulk();
|
Bulk();
|
||||||
CodeFirst();
|
CodeFirst();
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
public class CustomerMainInfomation:ERPEntityParentBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 客户编号
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "CustomerNo")]
|
||||||
|
public string CustomerNo { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 客户名称
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "CustomerCorpName")]
|
||||||
|
public string CustomerName { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
public class CustomerTranslatorBindingRelationship:ERPEntitySubBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 译员ID
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "gysbh")]
|
||||||
|
public string TranslatorID { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "zt")]
|
||||||
|
public string Status { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 客户的主信息
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[Navigate(NavigateType.OneToOne, nameof(CustomerTranslatorBindingRelationship.ParentID), nameof(CustomerMainInfomation.RecordID))]
|
||||||
|
public CustomerMainInfomation CustomerInfo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
public class DEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 主键Id
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "Id主键", IsPrimaryKey = true)]
|
||||||
|
public virtual long Id { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 关联ERP系统的数据库
|
||||||
|
/// </summary>
|
||||||
|
public abstract class ERPEntityBaseId
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ID
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "SerialID", IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
public abstract class ERPEntityBase : ERPEntityBaseId
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// RecordID
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "RecordID")]
|
||||||
|
public string RecordID { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class ERPEntityParentBase : ERPEntityBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class ERPEntitySubBase: ERPEntityBase
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ParentID
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "ParentID")]
|
||||||
|
public string ParentID { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace TranslateTool
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 处理 Lambda 参数不一致问题
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class ParameterReplaceExpressionVisitor : ExpressionVisitor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 参数表达式映射集合
|
||||||
|
/// </summary>
|
||||||
|
private readonly Dictionary<ParameterExpression, ParameterExpression> parameterExpressionSetter;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameterExpressionSetter">参数表达式映射集合</param>
|
||||||
|
public ParameterReplaceExpressionVisitor(Dictionary<ParameterExpression, ParameterExpression> parameterExpressionSetter)
|
||||||
|
{
|
||||||
|
this.parameterExpressionSetter = parameterExpressionSetter ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 替换表达式参数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameterExpressionSetter">参数表达式映射集合</param>
|
||||||
|
/// <param name="expression">表达式</param>
|
||||||
|
/// <returns>新的表达式</returns>
|
||||||
|
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> parameterExpressionSetter, Expression expression)
|
||||||
|
{
|
||||||
|
return new ParameterReplaceExpressionVisitor(parameterExpressionSetter).Visit(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重写基类参数访问器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameterExpression"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected override Expression VisitParameter(ParameterExpression parameterExpression)
|
||||||
|
{
|
||||||
|
if (parameterExpressionSetter.TryGetValue(parameterExpression, out var replacement))
|
||||||
|
{
|
||||||
|
parameterExpression = replacement;
|
||||||
|
}
|
||||||
|
|
||||||
|
return base.VisitParameter(parameterExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
[SugarTable("ProjectDetailInfomation")]
|
||||||
|
public class ProjectDetailInfomation : DEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
//public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 项目信息主键, ProjectMainInfomation表
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "MainId")]
|
||||||
|
public long MainId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 项目的主信息
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[Navigate(NavigateType.OneToOne, nameof(MainId))]
|
||||||
|
public ProjectMainInfomation ProjectMainInfo { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 项目信息表
|
||||||
|
///</summary>
|
||||||
|
[SugarTable("ProjectMainInfomation")]
|
||||||
|
public class ProjectMainInfomation : DEntityBase
|
||||||
|
{
|
||||||
|
//public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 项目调研状态
|
||||||
|
/// </summary>
|
||||||
|
public ProjectResearchStateDto ResearchState { get; set; } = ProjectResearchStateDto.RESEARCHING;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
public enum ProjectResearchStateDto : int
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 正在调研
|
||||||
|
/// </summary>
|
||||||
|
[Description("调研中")]
|
||||||
|
RESEARCHING = 0,
|
||||||
|
/// <summary>
|
||||||
|
/// 完成调研
|
||||||
|
/// </summary>
|
||||||
|
[Description("调研完成")]
|
||||||
|
FINISH_RESEARCH = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum RessearchReplyType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 接受
|
||||||
|
/// </summary>
|
||||||
|
[Description("接受")]
|
||||||
|
Accept,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 拒绝
|
||||||
|
/// </summary>
|
||||||
|
[Description("拒绝")]
|
||||||
|
Reject,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 未回复
|
||||||
|
/// </summary>
|
||||||
|
[Description("未回复")]
|
||||||
|
NO_REPLY,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已过期
|
||||||
|
/// </summary>
|
||||||
|
[Description("已过期")]
|
||||||
|
EXPIRED,
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
[SugarTable("ResearchRecorder")]
|
||||||
|
public class ResearchRecorder : DEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
//public string Name { get; set; }
|
||||||
|
public long TranslaterId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 关联ProjectDetailInfomation表,对应这个调研属于哪个项目哪个语言对下面
|
||||||
|
/// 因为某一个译员如果会多个语言对,同一个项目是可以做不同的任务的
|
||||||
|
/// </summary>
|
||||||
|
public long ProjectDetailInfomationId { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
#region 翻译员回复
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 回复结果,如果为空表示未回复
|
||||||
|
/// </summary>
|
||||||
|
public RessearchReplyType ReplyReseult { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 该调研是针对哪一个项目详细信息的
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[Navigate(NavigateType.OneToOne, nameof(ResearchRecorder.ProjectDetailInfomationId))]
|
||||||
|
public ProjectDetailInfomation ProjectDetailInfo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlugarDemo
|
||||||
|
{
|
||||||
|
public abstract class TranslatorMainInfomation : ERPEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 译员ID
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "SupplierNo", IsNullable = true)]
|
||||||
|
public string TranslatorID { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ERPTranslatorMainInfomation : TranslatorMainInfomation
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 客户绑定关系
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[Navigate(NavigateType.OneToMany, nameof(CustomerTranslatorBindingRelationship.TranslatorID), nameof(ERPTranslatorMainInfomation.TranslatorID))]
|
||||||
|
public List<CustomerTranslatorBindingRelationship> CustomerBindingRelationships { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
113
Src/Asp.NetCore2/SqliteTest/UnitTest/UNavQuery01.cs
Normal file
113
Src/Asp.NetCore2/SqliteTest/UnitTest/UNavQuery01.cs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using SqlugarDemo;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OrmTest
|
||||||
|
{
|
||||||
|
public class UNavQuery01
|
||||||
|
{
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||||
|
{
|
||||||
|
ConnectionString = @"DataSource=..\TestDB.sqlite",
|
||||||
|
DbType = DbType.Sqlite,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
,AopEvents = new AopEvents
|
||||||
|
{
|
||||||
|
OnLogExecuting = (sql, p) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine(sql);
|
||||||
|
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
bool bDataHaveInited = false;
|
||||||
|
|
||||||
|
if (!bDataHaveInited)
|
||||||
|
{
|
||||||
|
#region 创建表和初始化模拟数据
|
||||||
|
db.Context.DbMaintenance.CreateDatabase();
|
||||||
|
db.CodeFirst.InitTables<CustomerMainInfomation>();
|
||||||
|
db.CodeFirst.InitTables<CustomerTranslatorBindingRelationship>();
|
||||||
|
db.CodeFirst.InitTables<ERPTranslatorMainInfomation>();
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
CustomerMainInfomation pm = new CustomerMainInfomation()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
RecordID = "DFBAFACE-5E9D-4BC5-84E4-5451C404D5CD",
|
||||||
|
CustomerName = "客户名字",
|
||||||
|
CustomerNo = "11090032"
|
||||||
|
};
|
||||||
|
|
||||||
|
db.Insertable(pm).ExecuteCommand();
|
||||||
|
}
|
||||||
|
long pdiId = 21;
|
||||||
|
{
|
||||||
|
CustomerTranslatorBindingRelationship pdi = new CustomerTranslatorBindingRelationship()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
// Name = "PDI1 Name",
|
||||||
|
RecordID = "A224B2AA-927B-4071-8271-31FA32867DBE",
|
||||||
|
ParentID = "DFBAFACE-5E9D-4BC5-84E4-5451C404D5CD",
|
||||||
|
TranslatorID = "1405023",
|
||||||
|
Status = "有效"
|
||||||
|
};
|
||||||
|
db.Insertable(pdi).ExecuteCommand();
|
||||||
|
}
|
||||||
|
long rsId1 = 31;
|
||||||
|
{
|
||||||
|
ERPTranslatorMainInfomation rr = new ERPTranslatorMainInfomation()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
//Name = "RR1 Name",
|
||||||
|
RecordID = "AA8E30BA-A6BE-44F9-A4CE-CAEDFCA6E3A8",
|
||||||
|
TranslatorID = "1405023"
|
||||||
|
};
|
||||||
|
db.Insertable(rr).ExecuteCommand();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
string strCustomerNo = "11090032";
|
||||||
|
string strTranslatorID = "1405023";
|
||||||
|
#region 不加过滤条件,正常
|
||||||
|
{
|
||||||
|
var vTranslatoreList =
|
||||||
|
db.Queryable<ERPTranslatorMainInfomation>()
|
||||||
|
.Includes(s => s.CustomerBindingRelationships.Where(s1 => s1.Status == "有效"
|
||||||
|
//&& (s1.CustomerInfo.CustomerNo == strCustomerNo)
|
||||||
|
).ToList(),
|
||||||
|
s => s.CustomerInfo)
|
||||||
|
.Where(s => s.TranslatorID == strTranslatorID)
|
||||||
|
.ToList()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 加过滤条件,报错
|
||||||
|
{
|
||||||
|
var list = db.Queryable<CustomerTranslatorBindingRelationship>()
|
||||||
|
.Where(x => x.CustomerInfo.CustomerNo == "")
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
|
||||||
|
var vTranslatoreList =
|
||||||
|
db.Queryable<ERPTranslatorMainInfomation>()
|
||||||
|
.Includes(s => s.CustomerBindingRelationships.Where(s1 => s1.Status == "有效"
|
||||||
|
&& (s1.CustomerInfo.CustomerNo == strCustomerNo)
|
||||||
|
).ToList(),
|
||||||
|
s => s.CustomerInfo)
|
||||||
|
.Where(s => s.TranslatorID == strTranslatorID)
|
||||||
|
.ToList()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user