init
This commit is contained in:
37
DouyinApi.Tests/Common_Test/CacheTest.cs
Normal file
37
DouyinApi.Tests/Common_Test/CacheTest.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Autofac;
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.Common.Caches.Interface;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace DouyinApi.Tests.Common_Test;
|
||||
|
||||
public class CacheTest
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
DI_Test dI_Test = new DI_Test();
|
||||
private readonly ICaching _cache;
|
||||
|
||||
public CacheTest(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
dI_Test.Build();
|
||||
_cache = App.GetService<ICaching>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestCaching()
|
||||
{
|
||||
_cache.Set("test", "test", new TimeSpan(0, 10, 0));
|
||||
|
||||
var result = _cache.Get<string>("test");
|
||||
Assert.Equal("test", result);
|
||||
|
||||
var caches = _cache.GetAllCacheKeys();
|
||||
_testOutputHelper.WriteLine(caches.ToJson());
|
||||
Assert.NotNull(caches);
|
||||
|
||||
var count = _cache.GetAllCacheKeys().Count;
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
}
|
||||
158
DouyinApi.Tests/Common_Test/DynamicLambdaTest.cs
Normal file
158
DouyinApi.Tests/Common_Test/DynamicLambdaTest.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac;
|
||||
using DouyinApi.Common.Helper;
|
||||
using DouyinApi.IRepository.Base;
|
||||
using DouyinApi.Model.Models;
|
||||
using SqlSugar;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace DouyinApi.Tests.Common_Test;
|
||||
|
||||
public class DynamicLambdaTest
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
private readonly IBaseRepository<BlogArticle> _baseRepository;
|
||||
DI_Test dI_Test = new DI_Test();
|
||||
|
||||
public DynamicLambdaTest(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
|
||||
var container = dI_Test.DICollections();
|
||||
|
||||
_baseRepository = container.Resolve<IBaseRepository<BlogArticle>>();
|
||||
_baseRepository.Db.Aop.OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
_testOutputHelper.WriteLine(UtilMethods.GetNativeSql(sql, p));
|
||||
};
|
||||
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
_baseRepository.Db.CodeFirst.InitTables<BlogArticle>();
|
||||
_baseRepository.Db.CodeFirst.InitTables<BlogArticleComment>();
|
||||
_baseRepository.Db.CodeFirst.InitTables<SysUserInfo>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 普通查询 例子<br/>
|
||||
/// 没有复杂链表 主要使用导航属性<br/>
|
||||
/// 推荐将条件拼接交给前端 后端只定义个接口就很方便 维护也很简单<br/>
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void Get_Blogs_DynamicTest()
|
||||
{
|
||||
//方便前端自定义条件查询
|
||||
//语法更舒服
|
||||
var data = await _baseRepository.Query();
|
||||
_testOutputHelper.WriteLine(data.ToJson());
|
||||
|
||||
await TestConditions("");
|
||||
await TestConditions("bId=1");
|
||||
await TestConditions("bId=2");
|
||||
await TestConditions("bId in (1,2,3,4,5)");
|
||||
await TestConditions("bId in (1,2,3,4,5)|| bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
await TestConditions("btitle like \" 测试数据\"");
|
||||
await TestConditions("btitle like \"测试数据\" && bId>0");
|
||||
await TestConditions("btitle like \"测试!@#$%^&*()_+|}{\":<>?LP\"数据\" && bId>0");
|
||||
await TestConditions(
|
||||
"btitle like \"测试!@+)(*()_&%^&^$^%$IUYWIQOJVLXKZM>?Z<>?<L:\"SQLitePCL{|\"CM<:\"KJLEGRTOWEJT\"#$%^&*()_+|}{\":<>?LP\"数据\" && bId>0");
|
||||
await TestConditions("IsDeleted == false");
|
||||
await TestConditions("IsDeleted == true");
|
||||
await TestConditions("IsDeleted == true && ( btitle like \"张三\" || btitle like \"李四\" )");
|
||||
await TestConditions(
|
||||
"IsDeleted == true && ( btitle like \"张三\" || btitle like \"李四\" || ( btitle StartsLike \"王五\" && btitle EndLike \"赵六\" ) )");
|
||||
|
||||
//导航属性
|
||||
|
||||
//一对一
|
||||
|
||||
//查询 老张的文章
|
||||
await TestConditions("User.RealName like \"老张\"");
|
||||
//查询 2019年后的老张文章
|
||||
await TestConditions("User.RealName like \"老张\" && bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
|
||||
//一对多
|
||||
|
||||
//查询 评论中有"写的不错"的文章
|
||||
await TestConditions("Comments.Comment like \"写的不错\"");
|
||||
//查询 2019后的 评论中有"写的不错"的文章
|
||||
await TestConditions("Comments.Comment like \"写的不错\" && bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
//查询 有老张评论的文章
|
||||
await TestConditions("Comments.User.LoginName like \"老张\"");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复杂链表 也能使用动态条件<br/>
|
||||
/// 存在复杂的链表 left join等
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void Get_Blogs_DynamicJoinTest()
|
||||
{
|
||||
//方便前端自定义条件查询
|
||||
//语法更舒服
|
||||
var data = await _baseRepository.Query();
|
||||
_testOutputHelper.WriteLine(data.ToJson());
|
||||
|
||||
await TestJoinConditions("");
|
||||
await TestJoinConditions("bId=1");
|
||||
await TestJoinConditions("bId=2");
|
||||
await TestJoinConditions("bId in (1,2,3,4,5)");
|
||||
await TestJoinConditions("bId in (1,2,3,4,5)|| bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
await TestJoinConditions("btitle like \" 测试数据\"");
|
||||
await TestJoinConditions("btitle like \"测试数据\" && bId>0");
|
||||
await TestJoinConditions("btitle like \"测试!@#$%^&*()_+|}{\":<>?LP\"数据\" && bId>0");
|
||||
await TestJoinConditions(
|
||||
"btitle like \"测试!@+)(*()_&%^&^$^%$IUYWIQOJVLXKZM>?Z<>?<L:\"SQLitePCL{|\"CM<:\"KJLEGRTOWEJT\"#$%^&*()_+|}{\":<>?LP\"数据\" && bId>0");
|
||||
await TestJoinConditions("IsDeleted == false");
|
||||
await TestJoinConditions("IsDeleted == true");
|
||||
await TestJoinConditions("IsDeleted == true && ( btitle like \"张三\" || btitle like \"李四\" )");
|
||||
await TestJoinConditions(
|
||||
"IsDeleted == true && ( btitle like \"张三\" || btitle like \"李四\" || ( btitle StartsLike \"王五\" && btitle EndLike \"赵六\" ) )");
|
||||
|
||||
//导航属性
|
||||
|
||||
//一对一
|
||||
|
||||
//查询 老张的文章
|
||||
await TestJoinConditions("User.RealName like \"老张\"");
|
||||
//查询 2019年后的老张文章
|
||||
await TestJoinConditions("User.RealName like \"老张\" && bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
|
||||
//一对多
|
||||
|
||||
//查询 评论中有"写的不错"的文章
|
||||
await TestJoinConditions("Comments.Comment like \"写的不错\"");
|
||||
//查询 2019后的 评论中有"写的不错"的文章
|
||||
await TestJoinConditions("Comments.Comment like \"写的不错\" && bUpdateTime>=\"2019-01-01 01:01:01\"");
|
||||
//查询 有老张评论的文章
|
||||
await TestJoinConditions("Comments.User.LoginName like \"老张\"");
|
||||
}
|
||||
|
||||
|
||||
private async Task TestConditions(string conditions)
|
||||
{
|
||||
var express = DynamicLinqFactory.CreateLambda<BlogArticle>(conditions);
|
||||
_testOutputHelper.WriteLine(new string('=', 100));
|
||||
var product = await _baseRepository.Query(express);
|
||||
_testOutputHelper.WriteLine($"条件:{DynamicLinqFactory.FormatString(conditions)}\r\nLambda:{express}\r\n结果:{product.Count}");
|
||||
_testOutputHelper.WriteLine(new string('=', 100));
|
||||
}
|
||||
|
||||
private async Task TestJoinConditions(string conditions)
|
||||
{
|
||||
var express = DynamicLinqFactory.CreateLambda<BlogArticle>(conditions);
|
||||
_testOutputHelper.WriteLine(new string('=', 100));
|
||||
var product = await _baseRepository.Db.Queryable<BlogArticle>()
|
||||
.LeftJoin<SysUserInfo>((b, u) => Convert.ToInt64(b.bsubmitter) == u.Id)
|
||||
.MergeTable()
|
||||
.Where(express)
|
||||
.ToListAsync();
|
||||
_testOutputHelper.WriteLine($"条件:{DynamicLinqFactory.FormatString(conditions)}\r\nLambda:{express}\r\n结果:{product.Count}");
|
||||
_testOutputHelper.WriteLine(new string('=', 100));
|
||||
}
|
||||
}
|
||||
26
DouyinApi.Tests/Common_Test/HttpHelper_Should.cs
Normal file
26
DouyinApi.Tests/Common_Test/HttpHelper_Should.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using DouyinApi.Common.Helper;
|
||||
using Xunit;
|
||||
|
||||
namespace DouyinApi.Tests.Common_Test
|
||||
{
|
||||
public class HttpHelper_Should
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void Get_Async_Test()
|
||||
{
|
||||
var responseString = HttpHelper.GetAsync("http://apk.neters.club/api/Blog").Result;
|
||||
|
||||
Assert.NotNull(responseString);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Post_Async_Test()
|
||||
{
|
||||
var responseString = HttpHelper.PostAsync("http://apk.neters.club/api/Login/swgLogin", "{\"name\":\"admin\",\"pwd\":\"admin\"}").Result;
|
||||
|
||||
Assert.NotNull(responseString);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
42
DouyinApi.Tests/Common_Test/SM4Helper_Should.cs
Normal file
42
DouyinApi.Tests/Common_Test/SM4Helper_Should.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using DouyinApi.Common.Helper;
|
||||
using DouyinApi.Common.Helper.SM;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace DouyinApi.Tests.Common_Test
|
||||
{
|
||||
public class SM4Helper_Should
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void Encrypt_ECB_Test()
|
||||
{
|
||||
var plainText = "暗号";
|
||||
|
||||
var sm4 = new SM4Helper();
|
||||
|
||||
Console.Out.WriteLine("ECB模式");
|
||||
var cipherText = sm4.Encrypt_ECB(plainText);
|
||||
Console.Out.WriteLine("密文: " + cipherText);
|
||||
|
||||
Assert.NotNull(cipherText);
|
||||
Assert.Equal("VhVDC0KzyZjAVMpwz0GyQA==", cipherText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decrypt_ECB_Test()
|
||||
{
|
||||
var cipherText = "Y9ygWexdpuLQjW/qsnZNQw==";
|
||||
|
||||
var sm4 = new SM4Helper();
|
||||
|
||||
Console.Out.WriteLine("ECB模式");
|
||||
var plainText = sm4.Decrypt_ECB(cipherText);
|
||||
Console.Out.WriteLine("明文: " + plainText);
|
||||
|
||||
Assert.NotNull(plainText);
|
||||
Assert.Equal("老张的哲学", plainText);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user