Update unit test

This commit is contained in:
sunkaixuna 2021-12-26 02:05:48 +08:00
parent 9b87cbbaf8
commit ac6712f685
5 changed files with 577 additions and 3 deletions

View File

@ -31,6 +31,9 @@ namespace OrmTest
}
public static void Init()
{
UCustom01.Init();
UCustom02.Init();
UCustom03.Init();
Bulk();
Filter();
Insert();

View File

@ -0,0 +1,68 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class UCustom01
{
public static void Init()
{
var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true
});
db.Aop.OnLogExecuted = (s, p) =>
{
Console.WriteLine(s);
};
//建表
if (!db.DbMaintenance.IsAnyTable("User_Test001", false))
{
db.CodeFirst.InitTables<User_Test001>();
}
if (!db.DbMaintenance.IsAnyTable("UserRole_Test001", false))
{
db.CodeFirst.InitTables<UserRole_Test001>();
}
//用例代码
var result = db.Queryable<User_Test001, UserRole_Test001>((u, ur) => new object[] {
JoinType.Left,u.ID==ur.UserID
}).Select((u, ur) => new
{
customName = SqlFunc.Subqueryable<User_Test001>().Where(s => s.UserName == u.UserName).Select(s => s.UserName+"")
}).ToPageList(1, 10);
}
[SugarTable("unitUser_Test001")]
public class User_Test001
{
public int ID { get; set; }
public string UserName { get; set; }
}
[SugarTable("unitUserRole_Test001")]
public class UserRole_Test001
{
public int ID { get; set; }
public int UserID { get; set; }
}
}
}

View File

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace OrmTest
{
[SugarTable("UnitTest0")]
public class Test0
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
public string Title { get; set; }
}
[SugarTable("UnitTest_1")]
public class Test1
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0id")]
public long Test0Id { get; set; }
public string Title1 { get; set; }
}
[SugarTable("UnitTest_2")]
public class Test2
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0i_d")]
public long Test0Id { get; set; }
public string Title2 { get; set; }
}
[SugarTable("UnitTest_3")]
public class Test3
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0_id")]
public long Test0Id { get; set; }
public string Title3 { get; set; }
}
public class UCustom02
{
public static void Init()
{
var db = NewUnitTest.Db; ;
//用来打印Sql方便你调式
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
db.Aop.OnError = (sql) =>
{
string ss = sql.Sql;
};
db.Aop.OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
};
db.Aop.OnLogExecuted = (sql, p) =>
{
Console.WriteLine(sql);
};
db.CodeFirst.InitTables(typeof(Test0), typeof(Test1), typeof(Test2), typeof(Test3));
db.Deleteable<Test0>().ExecuteCommand();
db.Deleteable<Test1>().ExecuteCommand();
db.Deleteable<Test2>().ExecuteCommand();
db.Deleteable<Test3>().ExecuteCommand();
db.Insertable(new Test0 { Id = 1, Title = "111111" }).ExecuteCommand();
db.Insertable(new Test1 { Id = 1, Test0Id = 1, Title1 = "111111" }).ExecuteCommand();
db.Insertable(new Test2 { Id = 1, Test0Id = 1, Title2 = "222222222" }).ExecuteCommand();
db.Insertable(new Test3 { Id = 1, Test0Id = 1, Title3 = "333333333" }).ExecuteCommand();
var cacheQueryT = db.Queryable<Test0>().Select(x => new
{
id = x.Id,
title = x.Title,
title1 =
SqlFunc.Subqueryable<Test1>().Where(w => w.Test0Id == x.Id &&
SqlFunc.Subqueryable<Test2>().Where(t => t.Test0Id == w.Test0Id &&
SqlFunc.Subqueryable<Test3>().Where(t1 => t1.Test0Id == w.Test0Id).Any()).Any()).Select(w => w.Title1)
}).ToList();
}
}
}

View File

@ -0,0 +1,312 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace OrmTest
{
public class UCustom03
{
public static void Init()
{
Demo8();
Demo7();
Demo6();
Demo5();
Demo4();
Demo3();
Demo2();
Demo1();
}
private static void Demo5()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null });
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo1()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null });// id is not null
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> ( WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.And,new ConditionalModel(){
FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1"
})
}
})
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo2()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> ( WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.And,new ConditionalModel(){
FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1"
})
}
})
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo3()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> ( WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="ID", ConditionalType=ConditionalType.Equal, FieldValue="1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Or,new ConditionalModel(){
FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1"
})
}
})
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo4()
{
var db = NewUnitTest.Db;
List<ConditionalTree> conModels = new List<ConditionalTree>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.And,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> (
WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>()
{
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="price", ConditionalType=ConditionalType.Equal, FieldValue="1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.And,new ConditionalModel(){
FieldName = "CustomId", ConditionalType = ConditionalType.Equal, FieldValue = "1"
})
}
})
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo6()
{
var db = NewUnitTest.Db;
List<ConditionalTree> conModels = new List<ConditionalTree>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.And,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, IConditionalModel> (
WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>()
{
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="price", ConditionalType=ConditionalType.Equal, FieldValue="1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.And,new ConditionalModel(){
FieldName = "CustomId", ConditionalType = ConditionalType.Equal, FieldValue = "1"
})
}
})
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(it=>it.Id==1).Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo7()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Null,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "name" }),
new KeyValuePair<WhereType, IConditionalModel> ( WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Or,new ConditionalModel(){
FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Or,new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1"
}
),
}
})
}
}),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
private static void Demo8()
{
var db = NewUnitTest.Db;
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalTree()
{
ConditionalList = new List<KeyValuePair<WhereType, IConditionalModel>>()// (id=1 or id=2 and id=1)
{
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Null,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "name" }),
new KeyValuePair<WhereType, IConditionalModel> ( WhereType.And, new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.And,new ConditionalModel(){
FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1"
}),
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Or,new ConditionalTree(){
ConditionalList=new List<KeyValuePair<WhereType, IConditionalModel>>(){
new KeyValuePair<WhereType, IConditionalModel>(WhereType.Null,new ConditionalModel(){
FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1"
}
),
}
})
}
}),
new KeyValuePair<WhereType, IConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
}
});
var json = db.Context.Utilities.SerializeObject(conModels);
var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json);
var list6 = db.Queryable<Order>().Where(conditionalModels).ToList();
var json2 = db.Context.Utilities.SerializeObject(conditionalModels);
if (json != json2)
{
throw new Exception("unit error");
}
}
}
}

View File

@ -719,7 +719,7 @@ namespace SqlSugar
{
IConditionalModel conditionalModel = new ConditionalModel()
{
ConditionalType = (ConditionalType)Convert.ToInt32(item["ConditionalType"]),
ConditionalType = (ConditionalType)Convert.ToInt32(item["ConditionalType"].Value<int>()),
FieldName = item["FieldName"] + "",
FieldValue = item["FieldValue"].Value<string>()==null?null: item["FieldValue"].ToString()
};
@ -735,7 +735,7 @@ namespace SqlSugar
var values = item.Values().First();
foreach (var jToken in values)
{
WhereType type = (WhereType)Convert.ToInt32(jToken["Key"]);
WhereType type = (WhereType)Convert.ToInt32(jToken["Key"].Value<int>());
IConditionalModel conditionalModel = null;
var value = jToken["Value"];
if (value.ToString().Contains("ConditionalList"))
@ -749,7 +749,7 @@ namespace SqlSugar
{
conditionalModel = new ConditionalModel()
{
ConditionalType = (ConditionalType)Convert.ToInt32(value["ConditionalType"]),
ConditionalType = (ConditionalType)Convert.ToInt32(value["ConditionalType"].Value<int>()),
FieldName = value["FieldName"] + "",
FieldValue = value["FieldValue"].Value<string>() == null ? null : value["FieldValue"].ToString()
};