From d8d307ff465459ca1f16f5f904684350371051b5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 8 Nov 2022 11:59:09 +0800 Subject: [PATCH] Add unit test --- Src/Asp.NetCore2/PgSqlTest/UnitTest/Main.cs | 1 + .../PgSqlTest/UnitTest/Unit001.cs | 122 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 Src/Asp.NetCore2/PgSqlTest/UnitTest/Unit001.cs diff --git a/Src/Asp.NetCore2/PgSqlTest/UnitTest/Main.cs b/Src/Asp.NetCore2/PgSqlTest/UnitTest/Main.cs index 62f2cc4cd..a8bf7142c 100644 --- a/Src/Asp.NetCore2/PgSqlTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/PgSqlTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + Unit001.Init(); Bulk(); CodeFirst(); Updateable(); diff --git a/Src/Asp.NetCore2/PgSqlTest/UnitTest/Unit001.cs b/Src/Asp.NetCore2/PgSqlTest/UnitTest/Unit001.cs new file mode 100644 index 000000000..31e3d979c --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/UnitTest/Unit001.cs @@ -0,0 +1,122 @@ + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; + +using SqlSugar; +namespace OrmTest { + public static partial class ObjectExtension + { + /// + /// 排除SqlSugar忽略的列 + /// + /// + /// + private static bool IsIgnoreColumn(PropertyInfo pi) + { + var sc = pi.GetCustomAttributes(false).FirstOrDefault(u => u.IsIgnore == true); + return sc != null; + } + /// + /// List转DataTable + /// + /// + /// + /// + public static DataTable ToDataTable(this List list) + { + DataTable result = new DataTable(); + if (list.Count > 0) + { + // result.TableName = list[0].GetType().Name; // 表名赋值 + PropertyInfo[] propertys = list[0].GetType().GetProperties(); + foreach (PropertyInfo pi in propertys) + { + Type colType = pi.PropertyType; + if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + colType = colType.GetGenericArguments()[0]; + } + if (IsIgnoreColumn(pi)) + continue; + result.Columns.Add(pi.Name, colType); + } + for (int i = 0; i < list.Count; i++) + { + ArrayList tempList = new ArrayList(); + foreach (PropertyInfo pi in propertys) + { + if (IsIgnoreColumn(pi)) + continue; + object obj = pi.GetValue(list[i], null); + tempList.Add(obj); + } + object[] array = tempList.ToArray(); + result.LoadDataRow(array, true); + } + } + return result; + } + } + + public class Unit001 + { + + public static void Init( ) + { + var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() + { + ConnectionString = "PORT=5432;DATABASE=SqlSugar4xTest;HOST=localhost;PASSWORD=haosql;USER ID=postgres", + DbType = SqlSugar.DbType.PostgreSQL, + IsAutoCloseConnection = true + }); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + db.Aop.OnError = (ex) => + { + if (ex.Parametres == null) return; + Console.ForegroundColor = ConsoleColor.Red; + var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value)); + Console.WriteLine("【" + DateTime.Now + "——错误SQL】\r\n" + UtilMethods.GetSqlString(SqlSugar.DbType.PostgreSQL, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n"); + + }; + + //建表 + if (!db.DbMaintenance.IsAnyTable("Test0011", false)) + { + db.CodeFirst.InitTables(); + } + List list = new List(); + list.Add(new Test0011() { Id = 1 }); + System.Type entityType = typeof(Test0011); + var seedDataTable = db.Utilities.ListToDataTable(list); + seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName; + var storage = db.Storageable(seedDataTable).ToStorage(); + var result = storage.AsInsertable.ExecuteCommand(); + + + + Console.WriteLine(result); + Console.WriteLine("用例跑完"); + Console.ReadKey(); + } + //建类 + public class Test0011 + { + [SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true, IsIdentity = false)] + public int Id { get; set; } + + /// + /// 更新时间 + /// + [SugarColumn(ColumnDescription = "更新时间", IsOnlyIgnoreInsert = true,IsNullable =true)] + public DateTime? UpdateTime { get; set; } + } + + + + } +} \ No newline at end of file