2020-10-22 14:59:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Infrastructure;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Repository.Test
|
|
|
|
|
{
|
|
|
|
|
class TestDynamicLinq : TestBase
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void GenerateFilter()
|
|
|
|
|
{
|
|
|
|
|
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
|
|
|
|
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"": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
var query = dbcontext.Users.GenerateFilter("c",json);
|
|
|
|
|
Console.WriteLine(query.Expression.ToString());
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestDynamic()
|
|
|
|
|
{
|
2024-06-12 21:22:16 +08:00
|
|
|
|
QueryObject sub = new QueryObject
|
2020-10-22 14:59:36 +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
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
|
|
|
|
Operation = "and"
|
|
|
|
|
};
|
2024-06-12 21:22:16 +08:00
|
|
|
|
queryObject.Filters = new[]
|
2020-10-22 14:59:36 +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[]
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
|
|
|
|
sub
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
|
|
|
|
|
2024-06-12 21:22:16 +08:00
|
|
|
|
var query = dbcontext.Users.GenerateFilter("c",JsonHelper.Instance.Serialize(queryObject));
|
2020-10-22 14:59:36 +08:00
|
|
|
|
Console.WriteLine(query.Expression.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|