mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 05:13:27 +08:00
Add unit test
This commit is contained in:
parent
bd3f1db4a0
commit
627827b8c2
@ -103,7 +103,12 @@
|
||||
<Compile Include="Models\Tree.cs" />
|
||||
<Compile Include="Models\ViewOrder.cs" />
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="UnitTest\UintOneToOneDto.cs" />
|
||||
<Compile Include="UnitTest\CrossDatabase02.cs" />
|
||||
<Compile Include="UnitTest\Models\SignInRecord.cs" />
|
||||
<Compile Include="UnitTest\Models\SignInRecordOutput.cs" />
|
||||
<Compile Include="UnitTest\Models\SignInResultEnum.cs" />
|
||||
<Compile Include="UnitTest\Models\SysUser.cs" />
|
||||
<Compile Include="UnitTest\UinitCustomConvert.cs" />
|
||||
<Compile Include="UnitTest\UInsert3.cs" />
|
||||
<Compile Include="UnitTest\ULock.cs" />
|
||||
|
@ -31,6 +31,7 @@ namespace OrmTest
|
||||
}
|
||||
public static void Init()
|
||||
{
|
||||
UintOneToOneDto.Init();
|
||||
Unitadfaafsd.Init();
|
||||
CrossDatabase02.Init();
|
||||
UinitCustomConvert.Init();
|
||||
|
61
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecord.cs
Normal file
61
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecord.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 签到表
|
||||
/// </summary>
|
||||
[SugarTable(null, "签到表")]
|
||||
public class SignInRecord
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 报名用户Id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "用户Id")]
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 系统用户
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
[Navigate(NavigateType.OneToOne, nameof(UserId))]
|
||||
public SysUser sysUser { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 签到日期
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "签到日期")]
|
||||
public DateTime SignInDate { get; set; }
|
||||
/// <summary>
|
||||
/// 上午签到时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "上午签到时间")]
|
||||
public DateTime? MorningSignInTime { get; set; }
|
||||
/// <summary>
|
||||
/// 上午签到地点
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "上午签到地点", Length = 64)]
|
||||
public string MorningSignInAddress { get; set; }
|
||||
/// <summary>
|
||||
/// 上午签到结果
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "上午签到结果")]
|
||||
public SignInResultEnum MorningSignInResult { get; set; } = 0;
|
||||
/// <summary>
|
||||
/// 下午签到时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "下午签到时间")]
|
||||
public DateTime? AfternoonSignInTime { get; set; }
|
||||
/// <summary>
|
||||
/// 下午签到地点
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "下午签到地点", Length = 64)]
|
||||
public string AfternoonSignInAddress { get; set; }
|
||||
/// <summary>
|
||||
/// 下午签到结果
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "下午签到结果")]
|
||||
public SignInResultEnum AfternoonSignInResult { get; set; } = 0;
|
||||
|
||||
}
|
36
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecordOutput.cs
Normal file
36
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecordOutput.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
|
||||
|
||||
public class SignInRecordOutput
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 用户Id
|
||||
/// </summary>
|
||||
public long UserId { get; set; }
|
||||
/// <summary>
|
||||
/// 用户
|
||||
/// </summary>
|
||||
public SysUser sysUser { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 需签到次数
|
||||
/// </summary>
|
||||
public long SignInNeedCount { get; set; }
|
||||
/// <summary>
|
||||
/// 签到次数
|
||||
/// </summary>
|
||||
public long SignInCount { get; set; }
|
||||
/// <summary>
|
||||
/// 迟到次数
|
||||
/// </summary>
|
||||
public long LateCount { get; set; }
|
||||
/// <summary>
|
||||
/// 请假次数
|
||||
/// </summary>
|
||||
public long LeaveCount { get; set; }
|
||||
/// <summary>
|
||||
/// 出勤率
|
||||
/// </summary>
|
||||
public double AttendanceRate { get; set; }
|
||||
}
|
32
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInResultEnum.cs
Normal file
32
Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInResultEnum.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
/// <summary>
|
||||
/// 签到结果枚举
|
||||
/// </summary>
|
||||
[Description("签到结果枚举")]
|
||||
public enum SignInResultEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// 未签到
|
||||
/// </summary>
|
||||
[Description("未签到")]
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 已签到
|
||||
/// </summary>
|
||||
[Description("已签到")]
|
||||
SignedIn = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 迟到
|
||||
/// </summary>
|
||||
[Description("迟到")]
|
||||
Late = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 请假
|
||||
/// </summary>
|
||||
[Description("请假")]
|
||||
Leave = 3,
|
||||
}
|
120
Src/Asp.Net/PgSqlTest/UnitTest/Models/SysUser.cs
Normal file
120
Src/Asp.Net/PgSqlTest/UnitTest/Models/SysUser.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 系统用户表
|
||||
/// </summary>
|
||||
[SugarTable(null, "系统用户表")]
|
||||
public class SysUser
|
||||
{
|
||||
/// <summary>
|
||||
/// 雪花Id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true, IsIdentity = false)]
|
||||
public virtual long Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 真实姓名
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "真实姓名", Length = 32)]
|
||||
public virtual string RealName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "昵称", Length = 32)]
|
||||
public string NickName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头像
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "头像", Length = 512)]
|
||||
public string Avatar { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 年龄
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "年龄")]
|
||||
public int Age { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出生日期
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "出生日期")]
|
||||
public DateTime? Birthday { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 民族
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "民族", Length = 32)]
|
||||
public string Nation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 手机号码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "手机号码", Length = 16)]
|
||||
public string Phone { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 身份证号
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "身份证号", Length = 32)]
|
||||
public string IdCardNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 邮箱
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "邮箱", Length = 64)]
|
||||
public string Email { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "地址", Length = 256)]
|
||||
public string Address { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 政治面貌
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "政治面貌", Length = 16)]
|
||||
public string PoliticalOutlook { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 毕业院校
|
||||
/// </summary>COLLEGE
|
||||
[SugarColumn(ColumnDescription = "毕业院校", Length = 128)]
|
||||
public string College { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 办公电话
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "办公电话", Length = 16)]
|
||||
public string OfficePhone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 紧急联系人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "紧急联系人", Length = 32)]
|
||||
public string EmergencyContact { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 紧急联系人电话
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "紧急联系人电话", Length = 16)]
|
||||
public string EmergencyPhone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 紧急联系人地址
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "紧急联系人地址", Length = 256)]
|
||||
public string EmergencyAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 个人简介
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "个人简介", Length = 512)]
|
||||
public string Introduction { get; set; }
|
||||
|
||||
}
|
81
Src/Asp.Net/PgSqlTest/UnitTest/UintOneToOneDto.cs
Normal file
81
Src/Asp.Net/PgSqlTest/UnitTest/UintOneToOneDto.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
internal class UintOneToOneDto
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
EntityService = (s, y) =>
|
||||
{
|
||||
if (y.IsPrimarykey != true && y.IsIdentity != true)
|
||||
{
|
||||
y.IsNullable = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
//生成表
|
||||
db.CodeFirst.SetStringDefaultLength(50).InitTables<SysUser>();
|
||||
db.CodeFirst.SetStringDefaultLength(50).InitTables<SignInRecord>();
|
||||
db.DbMaintenance.TruncateTable<SysUser, SignInRecord>();
|
||||
//生成数据
|
||||
SysUser sysuser = new SysUser()
|
||||
{
|
||||
Id = 1,
|
||||
RealName = "Test",
|
||||
Age = 20,
|
||||
Phone = "1311111111",
|
||||
IdCardNum = "1111",
|
||||
Nation = "中国"
|
||||
};
|
||||
|
||||
SignInRecord signInRecord = new SignInRecord()
|
||||
{
|
||||
UserId = 1,
|
||||
SignInDate = (DateTime.Now),
|
||||
MorningSignInTime = DateTime.Now,
|
||||
MorningSignInAddress = "Addr",
|
||||
MorningSignInResult = SignInResultEnum.SignedIn,
|
||||
AfternoonSignInTime = DateTime.Now,
|
||||
AfternoonSignInAddress = "Addr",
|
||||
AfternoonSignInResult = SignInResultEnum.Late,
|
||||
};
|
||||
|
||||
db.DbMaintenance.TruncateTable<SysUser>();
|
||||
db.DbMaintenance.TruncateTable<SignInRecord>();
|
||||
db.Insertable(sysuser).ExecuteCommand();
|
||||
db.Insertable(signInRecord).ExecuteCommand();
|
||||
|
||||
//查询测试
|
||||
var query = db.Queryable<SignInRecord>()
|
||||
//**** Bug1, 当有x => x.sysUser.ToList(it => 时,同时使用Select的话,sysUser不会赋值。去掉Select则可以。
|
||||
|
||||
// .Includes<SysUser>(x => x.sysUser)
|
||||
.Includes<SysUser>(x => x.sysUser.ToList(it=>new SysUser() { Nation=it.Nation }))
|
||||
.GroupBy(g => new { g.UserId })
|
||||
.Select(it => new SignInRecordOutput
|
||||
{
|
||||
UserId = it.UserId,
|
||||
sysUser = it.sysUser,
|
||||
|
||||
SignInCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.SignedIn || it.AfternoonSignInResult == SignInResultEnum.SignedIn).Return(1).End<int>()),
|
||||
LateCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.Late || it.AfternoonSignInResult == SignInResultEnum.Late).Return(1).End<int>()),
|
||||
LeaveCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.Leave || it.AfternoonSignInResult == SignInResultEnum.Leave).Return(1).End<int>()),
|
||||
}) .ToList();
|
||||
|
||||
if (query.First().sysUser == null)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user