init
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using DouyinApi.Model.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using DouyinApi.IRepository.Base;
|
||||
using DouyinApi.Repository.MongoRepository;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace DouyinApi.Tests
|
||||
{
|
||||
public class MongoRepository_Base_Should
|
||||
{
|
||||
public class MongoTest
|
||||
{
|
||||
[BsonId]
|
||||
public ObjectId id { get; set; }
|
||||
public string name { get; set; }
|
||||
public bool isDel { get; set; }
|
||||
public DateTime time { get; set; }
|
||||
}
|
||||
|
||||
private IMongoBaseRepository<MongoTest> baseRepository;
|
||||
DI_Test dI_Test = new DI_Test();
|
||||
|
||||
public MongoRepository_Base_Should()
|
||||
{
|
||||
|
||||
var container = dI_Test.DICollections();
|
||||
|
||||
baseRepository = container.Resolve<IMongoBaseRepository<MongoTest>>();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void Add_Test()
|
||||
{
|
||||
await baseRepository.AddAsync(new MongoTest { isDel = false, name = "test", time = DateTime.UtcNow });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetObjectId_Test()
|
||||
{
|
||||
var data = await baseRepository.GetByObjectIdAsync("612b9b0be677976fa0f0cfa2");
|
||||
|
||||
Assert.NotNull(data);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void GetListFilter_Test()
|
||||
{
|
||||
var data = await baseRepository.GetListFilterAsync(new FilterDefinitionBuilder<MongoTest>().Gte("time", DateTime.Parse("2022-06-01")));
|
||||
|
||||
Assert.NotNull(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
DouyinApi.Tests/Repository_Test/OrmTest.cs
Normal file
73
DouyinApi.Tests/Repository_Test/OrmTest.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using Autofac;
|
||||
using DouyinApi.Common.Extensions;
|
||||
using DouyinApi.IRepository.Base;
|
||||
using DouyinApi.Model.Models;
|
||||
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
||||
using SqlSugar;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace DouyinApi.Tests;
|
||||
|
||||
public class OrmTest
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
private readonly IBaseRepository<BlogArticle> _baseRepository;
|
||||
DI_Test dI_Test = new DI_Test();
|
||||
|
||||
public OrmTest(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
|
||||
var container = dI_Test.DICollections();
|
||||
|
||||
_baseRepository = container.Resolve<IBaseRepository<BlogArticle>>();
|
||||
_baseRepository.Db.Aop.OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
_testOutputHelper.WriteLine("");
|
||||
_testOutputHelper.WriteLine("==================FullSql=====================", "", new string[] { sql.GetType().ToString(), GetParas(p), "【SQL语句】:" + sql });
|
||||
_testOutputHelper.WriteLine("【SQL语句】:" + sql);
|
||||
_testOutputHelper.WriteLine(GetParas(p));
|
||||
_testOutputHelper.WriteLine("==============================================");
|
||||
_testOutputHelper.WriteLine("");
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetParas(SugarParameter[] pars)
|
||||
{
|
||||
string key = "【SQL参数】:";
|
||||
foreach (var param in pars)
|
||||
{
|
||||
key += $"{param.ParameterName}:{param.Value}\n";
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiTables()
|
||||
{
|
||||
var sql = _baseRepository.Db.Queryable<BlogArticle>()
|
||||
.AS($@"{nameof(BlogArticle)}_TenantA")
|
||||
.ToSqlString();
|
||||
//_testOutputHelper.WriteLine(sql);
|
||||
|
||||
_baseRepository.Db.MappingTables.Add(nameof(BlogArticle), $@"{nameof(BlogArticle)}_TenantA");
|
||||
|
||||
var query = _baseRepository.Db.Queryable<BlogArticle>()
|
||||
.LeftJoin<BlogArticleComment>((a, c) => a.bID == c.bID);
|
||||
// query.QueryBuilder.AsTables.AddOrModify(nameof(BlogArticle), $@"{nameof(BlogArticle)}_TenantA");
|
||||
//query.QueryBuilder.AsTables.AddOrModify(nameof(BlogArticleComment), $@"{nameof(BlogArticleComment)}_TenantA");
|
||||
// query.QueryBuilder.AsTables.AddOrModify(nameof(BlogArticleComment), $@"{nameof(BlogArticleComment)}_TenantA");
|
||||
// query.QueryBuilder.AsTables.AddOrModify(nameof(SysUserInfo), $@"{nameof(SysUserInfo)}_TenantA");
|
||||
|
||||
|
||||
sql = query.ToSqlString();
|
||||
|
||||
_testOutputHelper.WriteLine(sql);
|
||||
|
||||
sql = _baseRepository.Db.Deleteable<BlogArticle>().ToSqlString();
|
||||
_testOutputHelper.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
81
DouyinApi.Tests/Repository_Test/Repository_Base_Should.cs
Normal file
81
DouyinApi.Tests/Repository_Test/Repository_Base_Should.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using DouyinApi.Model.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using DouyinApi.IRepository.Base;
|
||||
|
||||
namespace DouyinApi.Tests
|
||||
{
|
||||
public class Repository_Base_Should
|
||||
{
|
||||
private IBaseRepository<BlogArticle> baseRepository;
|
||||
DI_Test dI_Test = new DI_Test();
|
||||
|
||||
public Repository_Base_Should()
|
||||
{
|
||||
|
||||
var container = dI_Test.DICollections();
|
||||
|
||||
baseRepository = container.Resolve<IBaseRepository<BlogArticle>>();
|
||||
|
||||
//DbContext.Init(BaseDBConfig.ConnectionString,(DbType)BaseDBConfig.DbType);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void Get_Blogs_Test()
|
||||
{
|
||||
var data = await baseRepository.Query();
|
||||
|
||||
Assert.NotNull(data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Add_Blog_Test()
|
||||
{
|
||||
BlogArticle blogArticle = new BlogArticle()
|
||||
{
|
||||
bCreateTime = DateTime.Now,
|
||||
bUpdateTime = DateTime.Now,
|
||||
btitle = "xuint test title",
|
||||
bcontent = "xuint test content",
|
||||
bsubmitter = "xuint: test repositoryBase add blog",
|
||||
};
|
||||
|
||||
var BId = await baseRepository.Add(blogArticle);
|
||||
Assert.True(BId > 0);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void Update_Blog_Test()
|
||||
{
|
||||
var IsUpd = false;
|
||||
var updateModel = (await baseRepository.Query(d => d.btitle == "xuint test title")).FirstOrDefault();
|
||||
|
||||
Assert.NotNull(updateModel);
|
||||
|
||||
updateModel.bcontent = "xuint: test repositoryBase content update";
|
||||
updateModel.bCreateTime = DateTime.Now;
|
||||
updateModel.bUpdateTime = DateTime.Now;
|
||||
|
||||
IsUpd = await baseRepository.Update(updateModel);
|
||||
|
||||
Assert.True(IsUpd);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Delete_Blog_Test()
|
||||
{
|
||||
var IsDel = false;
|
||||
var deleteModel = (await baseRepository.Query(d => d.btitle == "xuint test title")).FirstOrDefault();
|
||||
|
||||
Assert.NotNull(deleteModel);
|
||||
|
||||
IsDel = await baseRepository.Delete(deleteModel);
|
||||
|
||||
Assert.True(IsDel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user