using System; using System.Threading.Tasks; using Util.Datas.Sql; using Util.Datas.Tests.Commons.Domains.Models; using Util.Datas.Tests.Commons.Domains.Repositories; using Util.Datas.Tests.Ef.SqlServer.UnitOfWorks; using Util.Dependency; using Util.Helpers; using Xunit; using Xunit.Abstractions; namespace Util.Datas.Tests.Sql.Queries { /// /// Sql Server查询对象测试 /// [Collection( "GlobalConfig" )] public class SqlQueryTest : IDisposable { /// /// 输出工具 /// private readonly ITestOutputHelper _output; /// /// 容器作用域 /// private readonly IScope _scope; /// /// 工作单元 /// private readonly ISqlServerUnitOfWork _unitOfWork; /// /// 客户仓储 /// private readonly ICustomerRepository _customerRepository; /// /// 查询对象 /// private readonly ISqlQuery _query; /// /// 测试初始化 /// public SqlQueryTest( ITestOutputHelper output ) { _output = output; _scope = Ioc.BeginScope(); _unitOfWork = _scope.Create(); _customerRepository = _scope.Create(); _query = _scope.Create(); _query.ClearAfterExecution( false ); } /// /// 测试清理 /// public void Dispose() { _scope.Dispose(); } /// /// 获取字符串结果 /// [Fact] public async Task TestToStringAsync() { string id = Id.ObjectId(); string name = Id.Guid().Substring( 0, 10 ); var customer = new Customer( id ) { Name = name }; await _customerRepository.AddAsync( customer ); await _unitOfWork.CommitAsync(); var result = await _query .Select( t => t.Name ) .From( "c" ) .Where( t => t.Id == id ) .ToStringAsync(); _output.WriteLine( _query.GetDebugSql() ); Assert.Equal( name, result ); } /// /// 获取分页结果 /// [Fact] public async Task TestToPagerListAsync() { string id = Id.ObjectId(); string name = Id.Guid().Substring( 0, 10 ); var customer = new Customer( id ) { Name = name }; await _customerRepository.AddAsync( customer ); await _unitOfWork.CommitAsync(); var result = await _query .Select( t => new object[] { t.Id, t.Name } ) .From( "c" ) .Where( t => t.Id == id ) .OrderBy( t => t.Name ) .ToPagerListAsync(1,20); _output.WriteLine( _query.GetDebugSql() ); Assert.Equal( 1, result.PageCount ); } } }