mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Add unit test
This commit is contained in:
parent
c2cc1f064a
commit
4e832ed484
@ -97,6 +97,9 @@
|
||||
<Compile Include="Models\OrderItem.cs" />
|
||||
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
|
||||
<Compile Include="Models\ViewOrder.cs" />
|
||||
<Compile Include="UnitTest\CrossDatabase01.cs" />
|
||||
<Compile Include="UnitTest\CrossDatabase02.cs" />
|
||||
<Compile Include="UnitTest\CrossDatabase03.cs" />
|
||||
<Compile Include="UnitTest\UInsertNav0adsf.cs" />
|
||||
<Compile Include="UnitTest\ITestExo.cs" />
|
||||
<Compile Include="UnitTest\UExp.cs" />
|
||||
|
14
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs
Normal file
14
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class CrossDatabase01
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
118
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase02.cs
Normal file
118
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase02.cs
Normal file
@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class CrossDatabase02
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
|
||||
var db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){ConfigId="A",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",IsAutoCloseConnection=true},
|
||||
new ConnectionConfig(){ConfigId="B",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST2",IsAutoCloseConnection=true },
|
||||
new ConnectionConfig(){ConfigId="AB",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST3",IsAutoCloseConnection=true }
|
||||
});
|
||||
|
||||
db.GetConnection("A").CodeFirst.InitTables<OperatorInfo>();
|
||||
db.GetConnection("B").CodeFirst.InitTables<Role>();
|
||||
db.GetConnection("AB").CodeFirst.InitTables<OptRole>();
|
||||
|
||||
db.GetConnection("A").DbMaintenance.TruncateTable<OperatorInfo>();
|
||||
db.GetConnection("B").DbMaintenance.TruncateTable<Role>();
|
||||
db.GetConnection("AB").DbMaintenance.TruncateTable<OptRole>();
|
||||
|
||||
db.GetConnection("A").Insertable(new OperatorInfo() { id=10, realname="A"}).ExecuteCommand();
|
||||
db.GetConnection("B").Insertable(new Role() { id=101, name="B"}).ExecuteCommand();
|
||||
db.GetConnection("AB").Insertable(new OptRole() { id=1, operId=10, roleId=101}).ExecuteCommand();
|
||||
|
||||
var x=db.Queryable<OperatorInfo>()
|
||||
.CrossQueryWithAttr().Includes(z => z.Roles).ToList();
|
||||
|
||||
if (x.First().Roles.Count == 0)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 描述:
|
||||
/// 作者:synjones
|
||||
/// 时间:2022-04-20 21:30:28
|
||||
/// </summary>
|
||||
[SugarTable("unit_operatorinfo8")]
|
||||
[Tenant("A")]
|
||||
public partial class OperatorInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public int id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名
|
||||
/// </summary>
|
||||
public string realname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 多角色
|
||||
/// </summary>
|
||||
[Navigate(typeof(OptRole), nameof(OptRole.operId), nameof(OptRole.roleId))]//名字换
|
||||
public List<Role> Roles { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 描述:
|
||||
/// 作者:synjones
|
||||
/// 时间:2022-04-20 21:30:28
|
||||
/// </summary>
|
||||
[SugarTable("unit_role18")]
|
||||
[Tenant("B")]
|
||||
public partial class Role
|
||||
{
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true )]
|
||||
public int id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色名称
|
||||
/// </summary>
|
||||
public string name { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 描述:
|
||||
/// 作者:synjones
|
||||
/// 时间:2022-04-21 14:35:09
|
||||
/// </summary>
|
||||
[SugarTable("unit_operator_role8")]
|
||||
[Tenant("AB")]
|
||||
public partial class OptRole
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public int id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int operId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int roleId { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
88
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase03.cs
Normal file
88
Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase03.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
using System.Linq;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class CrossDatabase03
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){ConfigId="OrderDb",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",IsAutoCloseConnection=true},
|
||||
new ConnectionConfig(){ConfigId="OrderItemDb",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST2",IsAutoCloseConnection=true }
|
||||
});
|
||||
|
||||
db.GetConnection("OrderDb").CodeFirst.InitTables<Order>();
|
||||
db.GetConnection("OrderItemDb").CodeFirst.InitTables<OrderItem>();
|
||||
|
||||
db.GetConnection("OrderDb").DbMaintenance.TruncateTable<Order>();
|
||||
db.GetConnection("OrderItemDb").DbMaintenance.TruncateTable<OrderItem>();
|
||||
|
||||
db.GetConnection("OrderDb").Insertable(new Order() { Id=1, CreateTime=DateTime.Now, Name="a", Price=10, CustomId=1 }).ExecuteCommand();
|
||||
db.GetConnection("OrderItemDb").Insertable(new OrderItem() { OrderId = 1, CreateTime = DateTime.Now , Price=10 }).ExecuteCommand();
|
||||
|
||||
var list=db.QueryableWithAttr<OrderItem>()
|
||||
.CrossQueryWithAttr()
|
||||
.Includes(z => z.Order)
|
||||
.ToList();
|
||||
|
||||
|
||||
var list2 = db.QueryableWithAttr<Order>()
|
||||
.CrossQueryWithAttr()
|
||||
.Includes(z => z.Items)
|
||||
.ToList();
|
||||
|
||||
|
||||
if (list.First().Order == null)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (list2.First().Items.Count == 0)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
Console.WriteLine("OrderDb");
|
||||
foreach (var item in db.GetConnection("OrderDb").DbMaintenance.GetTableInfoList(false))
|
||||
{
|
||||
Console.WriteLine(item.Name);
|
||||
}
|
||||
Console.WriteLine("OrderItemDb");
|
||||
foreach (var item in db.GetConnection("OrderItemDb").DbMaintenance.GetTableInfoList(false))
|
||||
{
|
||||
Console.WriteLine(item.Name);
|
||||
}
|
||||
}
|
||||
[Tenant("OrderDb")]
|
||||
[SqlSugar.SugarTable("Order8")]
|
||||
public class Order
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int CustomId { get; set; }
|
||||
[Navigate(NavigateType.OneToMany, nameof(OrderItem.OrderId))]
|
||||
public List<OrderItem> Items { get; set; }
|
||||
}
|
||||
[SqlSugar.SugarTable("OrderDetail8")]
|
||||
[Tenant("OrderItemDb")]
|
||||
public class OrderItem
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int ItemId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public decimal? Price { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true)]
|
||||
public DateTime? CreateTime { get; set; }
|
||||
[Navigate(NavigateType.OneToOne,nameof(OrderId))]
|
||||
public Order Order { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,9 @@ namespace OrmTest
|
||||
}
|
||||
public static void Init()
|
||||
{
|
||||
CrossDatabase01.Init();
|
||||
CrossDatabase02.Init();
|
||||
CrossDatabase03.Init();
|
||||
UInsertNav0adsf.Init();
|
||||
UExp.Init();
|
||||
USelectSinleDTO.Init();
|
||||
|
Loading…
Reference in New Issue
Block a user