From ad15381b1737d8c883fe364982dfc313423fc0bb Mon Sep 17 00:00:00 2001
From: sunkaixuan <610262374@qq.com>
Date: Thu, 25 Aug 2022 22:02:43 +0800
Subject: [PATCH] Add unit test
---
.../SqlServerTest/SqlServerTest.csproj | 1 +
Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 1 +
.../UnitTest/UnitUpdateSubQuery.cs | 95 +++++++++++++++++++
3 files changed, 97 insertions(+)
create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UnitUpdateSubQuery.cs
diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
index edba3b0c7..8f8a42958 100644
--- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
+++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
@@ -97,6 +97,7 @@
+
diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
index 9f25f290d..5ea60b0c0 100644
--- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
+++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
@@ -31,6 +31,7 @@ namespace OrmTest
}
public static void Init()
{
+ UnitUpdateSubQuery.Init();
UnitManyToManyDeleteNav.Init();
UnitTestReturnPkList.Init();
UnitCustom12312.Init();
diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UnitUpdateSubQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UnitUpdateSubQuery.cs
new file mode 100644
index 000000000..cc157067a
--- /dev/null
+++ b/Src/Asp.Net/SqlServerTest/UnitTest/UnitUpdateSubQuery.cs
@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SqlSugar;
+
+namespace OrmTest
+{
+ public class UnitUpdateSubQuery
+ {
+ public static void Init()
+ {
+ var db = NewUnitTest.Db;
+
+ //建表
+ if (!db.DbMaintenance.IsAnyTable("ClassEntity", false))
+ {
+ db.CodeFirst.InitTables();
+ }
+ if (!db.DbMaintenance.IsAnyTable("ClassStudentEntity", false))
+ {
+ db.CodeFirst.InitTables();
+ }
+ if (!db.DbMaintenance.IsAnyTable("StudentEntity", false))
+ {
+ db.CodeFirst.InitTables();
+ }
+ db.DbMaintenance.TruncateTable("ClassEntity");
+ db.DbMaintenance.TruncateTable("ClassStudentEntity");
+ db.DbMaintenance.TruncateTable("StudentEntity");
+
+ //插入数据
+ var @class = new ClassEntity
+ {
+ StudentQty = 0
+ };
+ db.Insertable(@class).ExecuteCommand();
+ var classStudentList = new List
+ {
+ new ClassStudentEntity{ ClassID = 1, StudentID = 1}
+ };
+ db.Insertable(classStudentList).ExecuteCommand();
+ var studentList = new List
+ {
+ new StudentEntity{ Name = "张三" }
+ };
+ db.Insertable(studentList).ExecuteCommand();
+ var result = new SimpleClient(db).AsQueryable().Select(t => new ClassEntity
+ {
+ StudentQty = SqlFunc.Subqueryable()
+ .InnerJoin((tt, tt1) => tt.StudentID == tt1.StudentID)
+ .Where(tt => tt.ClassID == t.ClassID)
+ .Count()
+ }).ToList();
+ //用例代码
+ var result2 = new SimpleClient(db).Update(t => new ClassEntity
+ {
+ StudentQty = SqlFunc.Subqueryable()
+ .InnerJoin((tt, tt1) => tt.StudentID == tt1.StudentID)
+ .Where(tt => tt.ClassID == t.ClassID)
+ .Count()
+ }, tt => tt.ClassID == 1);
+ db.DbMaintenance.DropTable("ClassEntity");
+ db.DbMaintenance.DropTable("ClassStudentEntity");
+ db.DbMaintenance.DropTable("StudentEntity");
+ Console.WriteLine(result);
+ Console.WriteLine("用例跑完");
+ Console.ReadKey();
+ }
+
+ internal class ClassEntity
+ {
+ [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
+ public int ClassID { get; set; }
+
+ public int StudentQty { get; set; }
+ }
+ internal class ClassStudentEntity
+ {
+ [SugarColumn(IsPrimaryKey = true)]
+ public int ClassID { get; set; }
+
+ [SugarColumn(IsPrimaryKey = true)]
+ public int StudentID { get; set; }
+ }
+ internal class StudentEntity
+ {
+ [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
+ public int StudentID { get; set; }
+
+ public string Name { get; set; }
+ }
+ }
+}