From 7146eeceb75eaf0b363bf2f27144aec793abca2a Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 25 Apr 2023 11:44:39 +0800 Subject: [PATCH] Add unit test --- .../SqlSeverTest/UnitTest/Main.cs | 1 + .../UnitTest/UinitCustomConvert.cs | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UinitCustomConvert.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs index 5ea2a923d..5d18f16e0 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UinitCustomConvert.Init(); UnitCustom020.Init(); UnitSubToList.Init(); UJsonsdafa.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UinitCustomConvert.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UinitCustomConvert.cs new file mode 100644 index 000000000..4764a0943 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UinitCustomConvert.cs @@ -0,0 +1,83 @@ +using SqlSugar.DbConvert; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Data; +using System.Security.Principal; +using System.Text; +using System.Linq; + +namespace OrmTest +{ + internal class UinitCustomConvert + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new Uinitadfa22122() + { + DcValue = new Dictionary() { { "1", 1 } } + } + ).ExecuteCommand(); + var data = db.Queryable().ToList(); + if (data.First().EnumValue != null) + { + throw new Exception("unit error"); + } + + db.Updateable(new Uinitadfa22122() + { + Id = 1, + DcValue = new Dictionary() { { "1", 2 } }, + EnumValue = SqlSugar.DbType.MySql + } + ).ExecuteCommand(); + data = db.Queryable().Where(it => it.EnumValue == SqlSugar.DbType.MySql).ToList(); + var data2 = db.Queryable().Where(it => SqlSugar.DbType.MySql == it.EnumValue).ToList(); + if (data.First().EnumValue != SqlSugar.DbType.MySql) + { + throw new Exception("unit error"); + } + + var dt = db.Queryable().ToDataTable(); + if (dt.Columns["EnumValue"].DataType != typeof(string)) + { + throw new Exception("unit error"); + } + if (data2.First().EnumValue != data.First().EnumValue) + { + throw new Exception("unit error"); + } + } + } + public class Uinitadfa22122 + { + + [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + public int Id { get; set; } + + [SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", SqlParameterDbType = typeof(DictionaryConvert), IsNullable = true)] + public Dictionary DcValue { get; set; } + [SqlSugar.SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert), IsNullable = true)] + public SqlSugar.DbType? EnumValue { get; set; } + } + + + public class DictionaryConvert : ISugarDataConverter + { + public SugarParameter ParameterConverter(object value, int i) + { + var name = "@myp" + i; + var str = new SerializeService().SerializeObject(value); + return new SugarParameter(name, str); + } + + public T QueryConverter(IDataRecord dr, int i) + { + var str = dr.GetValue(i) + ""; + return new SerializeService().DeserializeObject(str); + } + } +}