OpenAuth.Net/OpenAuth.Repository/Test/TestSugarDynamicLinq.cs

107 lines
3.2 KiB
C#
Raw Normal View History

2023-08-26 23:52:30 +08:00
using System;
using System.Linq;
using Infrastructure;
using NUnit.Framework;
using Microsoft.Extensions.DependencyInjection;
2023-08-27 00:23:45 +08:00
using OpenAuth.Repository.Domain;
using SqlSugar;
2023-08-26 23:52:30 +08:00
namespace OpenAuth.Repository.Test
{
class TestSugarDynamicLinq : TestBase
{
[Test]
public void GenerateFilter()
{
2023-08-27 00:23:45 +08:00
var sugarClient = _autofacServiceProvider.GetService<ISqlSugarClient>();
2023-08-26 23:52:30 +08:00
var json = @"
{
""Operation"": ""and"",
""Filters"": [{
""Key"": ""Name"",
""Contrast"": ""=="",
""Value"": ""admin""
},
{
""Key"": ""Name"",
""Contrast"": ""=="",
""Value"": ""admin""
}
],
""Children"": [{
""Operation"": ""or"",
""Filters"": [{
""Key"": ""Name"",
""Contrast"": ""=="",
""Value"": ""admin""
},
{
""Key"": ""Name"",
""Contrast"": ""=="",
""Value"": ""admin""
}
],
""Children"": [
]
}
]
}
";
2023-08-27 00:23:45 +08:00
var query = sugarClient.Queryable<User>().GenerateFilter("c",json);
Console.WriteLine(query.ToSqlString());
2023-08-26 23:52:30 +08:00
Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
}
[Test]
public void TestGetColumns()
{
var sugarClient = _autofacServiceProvider.GetService<ISqlSugarClient>();
var tables = sugarClient.DbMaintenance.GetTableInfoList();
// Console.WriteLine(JsonHelper.Instance.Serialize(tables));
var query = sugarClient.DbMaintenance.GetColumnInfosByTableName("userv");
Console.WriteLine(JsonHelper.Instance.Serialize(query));
}
2023-08-26 23:52:30 +08:00
[Test]
public void TestDynamic()
{
2024-06-12 21:22:16 +08:00
QueryObject sub = new QueryObject
2023-08-26 23:52:30 +08:00
{
Operation = "or"
};
sub.Filters = new[]
{
new Filter {Key = "Name", Value = "name", Contrast = "=="},
new Filter {Key = "Sex", Value = "10", Contrast = "=="}
};
2024-06-12 21:22:16 +08:00
QueryObject queryObject = new QueryObject
2023-08-26 23:52:30 +08:00
{
Operation = "and"
};
2024-06-12 21:22:16 +08:00
queryObject.Filters = new[]
2023-08-26 23:52:30 +08:00
{
new Filter {Key = "Account", Value = "name", Contrast = "=="},
new Filter {Key = "Password", Value = "10", Contrast = "=="}
};
2024-06-12 21:22:16 +08:00
queryObject.Children = new[]
2023-08-26 23:52:30 +08:00
{
sub
};
2023-08-27 00:23:45 +08:00
var sugarClient = _autofacServiceProvider.GetService<ISqlSugarClient>();
2023-08-26 23:52:30 +08:00
2024-06-12 21:22:16 +08:00
var query = sugarClient.Queryable<User>().GenerateFilter("c",queryObject);
2023-08-27 00:23:45 +08:00
Console.WriteLine(query.ToSqlString());
2023-08-26 23:52:30 +08:00
}
}
}