Browse Source

Update Unit test

pull/2/head
skx 4 years ago
parent
commit
efc84742d5
  1. 4
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs
  2. 44
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs
  3. 91
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs
  4. 164
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs
  5. 17
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs

4
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs

@ -31,8 +31,10 @@ namespace OrmTest
}
public static void Init()
{
Tran();
Filter();
Insert();
Enum();
Tran();
Queue();
CodeFirst();
Updateable();

44
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UEnum.cs

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void Enum()
{
Db.CodeFirst.InitTables<Unit00Z11C12>();
Db.Insertable(new Unit00Z11C12() { type = UnitType.a, type2 = null }).ExecuteCommand();
var x=Db.Queryable<Unit00Z11C12>().Select(it=>new {
x=it.type,
x2=it.type2
}).ToList();
var x2 = Db.Queryable<Unit00Z11C12>().ToList();
Db.Updateable<Unit00Z11C12>().SetColumns(it => it.type2 == UnitType.b)
.Where(it=>true).ExecuteCommand();
var x3 = Db.Queryable<Unit00Z11C12>().ToList();
foreach (var item in x3)
{
item.type2 = null;
}
Db.Updateable<Unit00Z11C12>(x3).WhereColumns(it=>it.type).ExecuteCommand();
var x4 = Db.Queryable<Unit00Z11C12>().ToList();
}
public class Unit00Z11C12
{
[SqlSugar.SugarColumn(ColumnDataType = "int", IsNullable = false)]
public UnitType type { get; set; }
[SqlSugar.SugarColumn(ColumnDataType ="int",IsNullable =true)]
public UnitType? type2 { get; set; }
}
public enum UnitType {
a=-1,
b=2,
c=3
}
}
}

91
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UFilter.cs

@ -0,0 +1,91 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void Filter()
{
var db = Db;
db.QueryFilter.Add(new SqlSugar.TableFilterItem<UnitFilterClass1>(it=>it.id>0));
var s1=db.Queryable<Order>().ToSql();
if (s1.Key.Contains(">"))
{
throw new Exception("UnitFilter");
}
var s4 = db.Queryable<Order, OrderItem>((o, i) => i.OrderId == o.Id).Select("o.*").ToSql();
if (s4.Key.Contains(">"))
{
throw new Exception("UnitFilter");
}
var s5 = db.Queryable<Order, OrderItem>((o, i) => new JoinQueryInfos(JoinType.Left, o.Id == i.OrderId))
.Select("o.*").ToSql();
if (s5.Key.Contains(">"))
{
throw new Exception("UnitFilter");
}
var s2 = db.Queryable<UnitFilterClass1>().ToSql();
UValidate.Check(s2.Key,
@"SELECT [id],[name] FROM [UnitFilterClass1] WHERE ( [id] > @id0 )", "UnitFilter");
var s3= db.Queryable<UnitFilterClass1>().Where(it=>it.name!=null).ToSql();
UValidate.Check(s3.Key,
@"SELECT [id],[name] FROM [UnitFilterClass1] WHERE ( [name] IS NOT NULL ) AND ( [id] > @id1 )", "UnitFilter");
var s6 = db.Queryable<UnitFilterClass1, UnitFilterClass2>((o, i) => i.id == o.id).Select("o.*").ToSql();
UValidate.Check(s6.Key, "SELECT o.* FROM [UnitFilterClass1] o ,[UnitFilterClass2] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] > @id0 )", "UnitFilter");
var s7 = db.Queryable<UnitFilterClass1, UnitFilterClass2>((o, i) => i.id == o.id).Where(o=>o.id==1).Select("o.*").ToSql();
UValidate.Check(s7.Key, "SELECT o.* FROM [UnitFilterClass1] o ,[UnitFilterClass2] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] = @id0 ) AND ( [o].[id] > @id1 )", "UnitFilter");
var s8 = db.Queryable<UnitFilterClass2, UnitFilterClass1>((o, i) => i.id == o.id).Where(o => o.id == 1).Select("o.*").ToSql();
UValidate.Check(s8.Key, "SELECT o.* FROM [UnitFilterClass2] o ,[UnitFilterClass1] i WHERE ( [i].[id] = [o].[id] ) AND ( [o].[id] = @id0 ) AND ( [i].[id] > @id1 )", "UnitFilter");
var s9 = db.Queryable<UnitFilterClass1, UnitFilterClass2>((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id))
.Select("o.*").ToSql();
UValidate.Check(s9.Key, "SELECT o.* FROM [UnitFilterClass1] o Left JOIN [UnitFilterClass2] i ON ( [o].[id] = [i].[id] ) WHERE ( [o].[id] > @id0 )", "UnitFilter");
var s10= db.Queryable<UnitFilterClass1, UnitFilterClass2>((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id))
.Where((o,i) =>i.id==0).Select("o.*").ToSql();
UValidate.Check(s10.Key, "SELECT o.* FROM [UnitFilterClass1] o Left JOIN [UnitFilterClass2] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] = @id0 ) AND ( [o].[id] > @id1 )", "UnitFilter");
var s11 = db.Queryable<UnitFilterClass2, UnitFilterClass1>((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id))
.Select("o.*").ToSql();
UValidate.Check(s11.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] > @id0 )", "UnitFilter");
db.QueryFilter.Add(new SqlSugar.TableFilterItem<UnitFilterClass2>(it => it.id==0));
var s12 = db.Queryable<UnitFilterClass2, UnitFilterClass1>((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id))
.Select("o.*").ToSql();
UValidate.Check(s12.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [i].[id] > @id0 ) AND ( [o].[id] = @id1 )", "UnitFilter");
var s13 = db.Queryable<UnitFilterClass2, UnitFilterClass1>((o, i) => new JoinQueryInfos(JoinType.Left, o.id == i.id))
.Where(o=>o.name=="")
.Select("o.*").ToSql();
UValidate.Check(s13.Key, "SELECT o.* FROM [UnitFilterClass2] o Left JOIN [UnitFilterClass1] i ON ( [o].[id] = [i].[id] ) WHERE ( [o].[name] = @name0 ) AND ( [i].[id] > @id1 ) AND ( [o].[id] = @id2 )", "UnitFilter");
}
public class UnitFilterClass1
{
public int id { get; set; }
public string name { get; set; }
}
public class UnitFilterClass2
{
public int id { get; set; }
public string name { get; set; }
}
}
}

164
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UInsert.cs

@ -11,6 +11,7 @@ namespace OrmTest
public static void Insert()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
db.CodeFirst.InitTables<UinitBlukTable>();
db.Insertable(new List<UinitBlukTable>
{
@ -50,9 +51,172 @@ namespace OrmTest
{
throw new Exception("Unit Insert");
}
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
for (int i = 1; i <= 20; i++)
{
UinitBlukTable data = new UinitBlukTable()
{
Create=DateTime.Now.AddDays(-1),
Id=i ,
Name =i%3==0?"a":"b"
};
list2.Add(data);
}
list2.First().Name = null;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand();
var x=Db.Storageable(list2)
.SplitInsert(it => it.NotAny(y=>y.Id==it.Item.Id))
.SplitUpdate(it => it.Any(y => y.Id == it.Item.Id))
.SplitDelete(it=>it.Item.Id>10)
.SplitIgnore(it=>it.Item.Id==1)
.SplitError(it => it.Item.Id == 3,"id不能等于3")
.SplitError(it => it.Item.Id == 4, "id不能等于4")
.SplitError(it => it.Item.Id == 5, "id不能等于5")
.SplitError(it => it.Item.Name==null, "name不能等于")
.WhereColumns(it=> new { it.Id })
.ToStorage();
x.AsDeleteable.ExecuteCommand();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
foreach (var item in x.ErrorList)
{
Console.Write(item.StorageMessage+" ");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
IDemo1();
IDemo2();
IDemo3();
IDemo4();
}
private static void IDemo4()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now.AddYears(-2) });
list2.Add(new UinitBlukTable() { Id = 4, Name ="", Create = DateTime.Now.AddYears(-2) });
db.Insertable(list2.First()).ExecuteCommand();//插入第一条
var x = Db.Storageable(list2)
.SplitError(it => string.IsNullOrEmpty(it.Item.Name), "名称不能为空")
.SplitError(it => it.Item.Create<DateTime.Now.AddYears(-1),"不是今年的数据")
.SplitUpdate(it => it.Any(y=>y.Id==it.Item.Id))//存在更新
.SplitInsert(it => true)//剩余的插入
.ToStorage();
Console.WriteLine(" 插入 {0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}" ,
x.InsertList.Count,
x.UpdateList.Count,
x.ErrorList.Count,
x.IgnoreList.Count,
x.DeleteList.Count,
x.TotalList.Count
);
foreach (var item in x.ErrorList)
{
Console.WriteLine("id等于"+item.Item.Id+" : "+item.StorageMessage);
}
int i=x.AsInsertable.ExecuteCommand();
Console.WriteLine(1 + "条成功插入");
i=x.AsUpdateable.ExecuteCommand();
Console.WriteLine(1 + "条成功更新");
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
private static void IDemo3()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now });
var x = Db.Storageable(list2)
.SplitUpdate(it => it.Item.Id == 1)
.SplitInsert(it => it.Item.Id == 2)
.SplitDelete(it => it.Item.Id == 3).ToStorage();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
x.AsDeleteable.ExecuteCommand();
if (x.InsertList.Count != 1)
{
throw new Exception("Unit Insert");
}
if (x.UpdateList.Count != 1)
{
throw new Exception("Unit Insert");
}
if (x.DeleteList.Count != 1)
{
throw new Exception("Unit Insert");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
private static void IDemo1()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 0, Name = "a", Create = DateTime.Now });
var x = Db.Storageable(list2)
.SplitUpdate(it => it.Item.Id > 0)
.SplitInsert(it => it.Item.Id == 0).ToStorage();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
if (x.InsertList.Count > 1)
{
throw new Exception("Unit Insert");
}
if (x.UpdateList.Count !=2)
{
throw new Exception("Unit Insert");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
private static void IDemo2()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
list2.Add(new UinitBlukTable() { Id = 1, Name = "a1", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 2, Name = "a2", Create = DateTime.Now });
list2.Add(new UinitBlukTable() { Id = 3, Name = "a3", Create = DateTime.Now });
db.Insertable(list2[1]).ExecuteCommand();
var x = Db.Storageable(list2)
.SplitUpdate(it => it.Any(y=>y.Id==it.Item.Id))
.SplitInsert(it => it.NotAny(y => y.Id == it.Item.Id)).ToStorage();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
if (x.InsertList.Count!=2)
{
throw new Exception("Unit Insert");
}
if (x.UpdateList.Count != 1)
{
throw new Exception("Unit Insert");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
public class UinitBlukTable
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
public string Name { get; set; }
[SqlSugar.SugarColumn(IsNullable =true)]

17
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UQueryable2.cs

@ -32,7 +32,10 @@ namespace OrmTest
.Mapper(it => it.Items, it => it.Items.First().OrderId)
.Where(it => it.Items.Any())
.ToList();
var list7= Db.Queryable<Order>()
.Mapper(it => it.Items, it => it.Items.First().OrderId)
.Where(it => it.Items.Any(y => y.ItemId == 1))
.ToList();
var sql=Db.Queryable<Order>().AS("[order]").ToList();
@ -78,6 +81,18 @@ namespace OrmTest
id1=x.Id,
name=y.Name
}).ToList();
var qu3 = Db.Queryable<Order>().Select(it => new
{
id = it.Id,
name = it.Name
}).MergeTable()
.Where(it=>2>it.id).Select(it=> new Order() {
Id=SqlFunc.IIF(2>it.id,1,2)
}).ToList();
var qu4 = Db.Queryable<Order>().OrderBy(it=>it.Id+it.Id).ToList();
}
}
}

Loading…
Cancel
Save