using System;
using System.Collections.Generic;
using System.Text;
using Util.Data.Sql.Builders.Conditions;
using Util.Data.Sql.Builders.Params;
using Util.Data.Sql.Tests.Samples;
using Xunit;
namespace Util.Data.Sql.Tests.Builders.Conditions;
///
/// Not In查询条件测试
///
public class NotInConditionTest {
///
/// 参数管理器
///
private readonly IParameterManager _parameterManager;
///
/// 测试初始化
///
public NotInConditionTest() {
_parameterManager = new ParameterManager( TestDialect.Instance );
}
///
/// 获取结果
///
private string GetResult( ISqlCondition condition ) {
var result = new StringBuilder();
condition.AppendTo( result );
return result.ToString();
}
///
/// 测试创建条件 - 验证列名为空
///
[Fact]
public void TestCreate_Validate() {
var parameters = new List { "b" };
Assert.Throws( () => {
var condition = new NotInCondition( _parameterManager, "", parameters, true );
} );
}
///
/// 测试获取条件 - 参数化 - 1个字符串型参数
///
[Fact]
public void TestGetCondition_1() {
var parameters = new List { "b" };
var condition = new NotInCondition( _parameterManager, "a", parameters, true );
Assert.Equal( "a Not In (@_p_0)", GetResult( condition ) );
Assert.Equal( "b", _parameterManager.GetValue( "@_p_0" ) );
}
///
/// 测试获取条件 - 参数化 - 1个数值型参数
///
[Fact]
public void TestGetCondition_2() {
var parameters = new List { 1 };
var condition = new NotInCondition( _parameterManager, "a", parameters, true );
Assert.Equal( "a Not In (@_p_0)", GetResult( condition ) );
Assert.Equal( 1, _parameterManager.GetValue( "@_p_0" ) );
}
///
/// 测试获取条件 - 参数化 - 2个参数
///
[Fact]
public void TestGetCondition_3() {
var parameters = new List { "b", "c" };
var condition = new NotInCondition( _parameterManager, "a", parameters, true );
Assert.Equal( "a Not In (@_p_0,@_p_1)", GetResult( condition ) );
Assert.Equal( "b", _parameterManager.GetValue( "@_p_0" ) );
Assert.Equal( "c", _parameterManager.GetValue( "@_p_1" ) );
}
///
/// 测试获取条件 - 参数化 - 0个参数
///
[Fact]
public void TestGetCondition_4() {
var parameters = new List();
var condition = new NotInCondition( _parameterManager, "a", parameters, true );
Assert.Empty( GetResult( condition ) );
Assert.Equal( 0, _parameterManager.GetParams().Count );
}
///
/// 测试获取条件 - 非参数化 - 1个字符串型参数
///
[Fact]
public void TestGetCondition_5() {
var parameters = new List { "b" };
var condition = new NotInCondition( _parameterManager, "a", parameters, false );
Assert.Equal( "a Not In ('b')", GetResult( condition ) );
Assert.Equal( 0, _parameterManager.GetParams().Count );
}
///
/// 测试获取条件 - 非参数化 - 1个数值型参数
///
[Fact]
public void TestGetCondition_6() {
var parameters = new List { 1 };
var condition = new NotInCondition( _parameterManager, "a", parameters, false );
Assert.Equal( "a Not In (1)", GetResult( condition ) );
Assert.Equal( 0, _parameterManager.GetParams().Count );
}
///
/// 测试获取条件 - 非参数化 - 2个参数
///
[Fact]
public void TestGetCondition_7() {
var parameters = new List { "b", "c" };
var condition = new NotInCondition( _parameterManager, "a", parameters, false );
Assert.Equal( "a Not In ('b','c')", GetResult( condition ) );
Assert.Equal( 0, _parameterManager.GetParams().Count );
}
///
/// 测试获取条件 - 值为ISqlBuilder
///
[Fact]
public void TestGetCondition_8() {
var result = new StringBuilder();
result.Append( "a Not In " );
result.AppendLine( "(Select [a] " );
result.Append( "From [b])" );
var builder = new TestSqlBuilder().Select( "a" ).From( "b" );
var condition = new NotInCondition( _parameterManager, "a", builder, true );
Assert.Equal( result.ToString(), GetResult( condition ) );
}
}