From c7ecc4a5ef0471149ccb7d79f2922fc9c871f570 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 12 Sep 2022 06:44:15 +0800 Subject: [PATCH] Add unit test --- Src/Asp.Net/PgSqlTest/PgSqlTest.csproj | 5 ++ Src/Asp.Net/PgSqlTest/UnitTest/Main.cs | 1 + Src/Asp.Net/PgSqlTest/UnitTest/UJsonFunc.cs | 79 +++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Src/Asp.Net/PgSqlTest/UnitTest/UJsonFunc.cs diff --git a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj index ac2a53a47..f4f22899e 100644 --- a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj +++ b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj @@ -35,6 +35,10 @@ + + False + ..\..\..\..\..\Demo\DmTest\DmTest\References\Newtonsoft.Json.dll + ..\packages\System.Buffers.4.4.0\lib\netstandard1.1\System.Buffers.dll @@ -99,6 +103,7 @@ + diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs index 52564733c..f65cd9e25 100644 --- a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UJsonFunc.Init(); UnitTestReturnPkList.Init(); UCustom07.Init(); UCustom016.Init(); diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UJsonFunc.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UJsonFunc.cs new file mode 100644 index 000000000..a172e8a0e --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UJsonFunc.cs @@ -0,0 +1,79 @@ +using Newtonsoft.Json.Linq; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UJsonFunc + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + + db.Insertable(new UnitJsonTest22() { A = JObject.Parse(db.Utilities.SerializeObject(new { Id = 1 }))}).ExecuteCommand(); + + + db.Insertable(new UnitJsonTest22() { A = JObject.Parse(db.Utilities.SerializeObject(new { Id = 2,hh=new string[] { "1","2"},zz=new { b=111} })) }).ExecuteCommand(); + + db.Insertable(new UnitJsonTest22() { A = JObject.Parse(db.Utilities.SerializeObject(new { Id = 2, zz = new { b = 222 } })) }).ExecuteCommand(); + + + + var list = db.Queryable() + .Where(x =>Convert.ToInt32(SqlFunc.JsonField(x.A,"zz","b")) == 111) + .ToList(); + + if (list.Count != 1) { throw new Exception("unit error"); } + + var list2 = db.Queryable() + .Where(x => Convert.ToInt32(SqlFunc.JsonField(x.A, "Id")) == 1) + .ToList(); + + if (list2.Count != 1) { throw new Exception("unit error"); } + + + var list3 = db.Queryable().Select(it=>new { x = SqlFunc.JsonParse(it.A)}).ToList(); + + + db.CodeFirst.InitTables(); + db.Insertable(new UnitJsonTest221() { A = JArray.Parse("[1,2]") }).ExecuteCommand(); + var list4 = db.Queryable().Select(it => new { x = SqlFunc.JsonArrayLength(it.A) }).ToList(); + + if (list4.First().x != 2) { throw new Exception("unit error"); } + + var list5 = db.Queryable().Select(it => new { x = SqlFunc.JsonContainsFieldName(it.A,"id") }).ToList(); + if (list5.First().x != false) { throw new Exception("unit error"); } + + var list6 = db.Queryable().Select(it => new { x = SqlFunc.JsonContainsFieldName(it.A, "Id") }).ToList(); + if (list6.First().x != true) { throw new Exception("unit error"); } + + + var list7 = db.Queryable() + .Where(x => SqlFunc.JsonField(x.A, "hh","{int:1}") == "2") + .ToList(); + + var list8= db.Queryable() + .Where(x => SqlFunc.JsonLike(x.A,"hh")) + .ToList(); + + db.DbMaintenance.DropTable(); + } + public class UnitJsonTest22 + { + [SqlSugar.SugarColumn(IsJson =true)] + public JObject A { get; set; } + } + public class UnitJsonTest221 + { + [SqlSugar.SugarColumn(IsJson = true)] + public JArray A { get; set; } + } + } +}