gpt4 book ai didi

unit-testing - 单元测试 NHibernate UserTypes

转载 作者:行者123 更新时间:2023-12-04 11:32:37 26 4
gpt4 key购买 nike

有没有人对他们的 UserTypes 进行单元测试的好方法?

举例来说,我的模型中有一个名为 DateRange 的对象,它有一个 DatePoint 开始和一个 DatePoint 结束。除了使范围类型操作可用于两个 DateTime 之外,这些对象还让我可以调整手头任务的精度(即,天、小时、分钟等)。当为我正在处理的应用程序存储到数据库时,我只需要将开始和结束存储为日期时间,不允许为空值。我想不出如何在没有 UserType 的情况下映射它,所以我有:

/// <summary>User type to deal with <see cref="DateRange"/> persistence for time sheet tracking.</summary>
public class TimePeriodType : IUserType
{

public SqlType[] SqlTypes {
get {
var types = new SqlType[2];
types[0] = new SqlType(DbType.DateTime);
types[1] = new SqlType(DbType.DateTime);
return types;

}
}

public Type ReturnedType
{
get { return typeof(DateRange); }
}

/// <summary>Just return <see cref="DateRange.Equals(object)"/></summary>
public new bool Equals(object x, object y)
{
return x != null && x.Equals(y);
}

/// <summary>Just return <see cref="DateRange.GetHashCode"/></summary>
public int GetHashCode(object x)
{
return x.GetHashCode();
}

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var start = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
var end = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[1]);

return new DateRange(start, end, TimeSlice.Minute);
}

public void NullSafeSet(IDbCommand cmd, object value, int index) {
Check.RequireNotNull<DateRange>(value);
Check.RequireArgType<DateRange>(value);
var dateRange = ((DateRange)value);

NHibernateUtil.DateTime.NullSafeSet(cmd, dateRange.Start, index);
NHibernateUtil.DateTime.NullSafeSet(cmd, dateRange.End, index);
}

public object DeepCopy(object value) {
Check.RequireNotNull<DateRange>(value);
Check.RequireArgType<DateRange>(value);
var dateRange = ((DateRange) value);

return new DateRange(dateRange.Start, dateRange.End);
}

public bool IsMutable
{
get { return false; }
}

public object Replace(object original, object target, object owner) {
//because it is immutable so we can just return it as is
return original;
}

public object Assemble(object cached, object owner) {
//Used for caching, as it is immutable we can just return it as is
return cached;
}

public object Disassemble(object value) {
//Used for caching, as it is immutable we can just return it as is
return value;
}
}

}

现在我正在寻找一种方法来证明它有效。提前致谢!

干杯,
贝瑞尔

最佳答案

我为 System.Drawing.Color 创建了一个用户类型,这里是我使用 MSTest 和 Moq 对其进行单元测试的方法.

颜色用户类型.cs:

公共(public)类 ColorUserType : IUserType
{
公共(public)对象组装(对象缓存,对象所有者)
{
返回缓存;
}

公共(public)对象 DeepCopy(对象值)
{
返回值;
}

公共(public)对象反汇编(对象值)
{
返回值;
}

公共(public)新 bool 等于(对象 x,对象 y)
{
if(ReferenceEquals(x, y))
{
返回真;
}
if( x == null || y == null )
{
返回假;
}
返回 x.Equals(y);
}

public int GetHashCode(对象x)
{
返回 x == null ? typeof( Color ).GetHashCode() + 473 : x.GetHashCode();
}

public bool IsMutable
{
得到
{
返回真;
}
}

公共(public)对象 NullSafeGet( IDataReader rs, string[] 名称, 对象所有者)
{
var obj = NHibernateUtil.String.NullSafeGet( rs, names[0]);
如果(对象==空)
{
返回空;
}
返回 ColorTranslator.FromHtml( (string)obj );
}

public void NullSafeSet( IDbCommand cmd, object value, int index )
{
if( 值 == 空 )
{
( (IDataParameter)cmd.Parameters[index] ).Value = DBNull.Value;
}
别的
{
( (IDataParameter)cmd.Parameters[index] ).Value = ColorTranslator.ToHtml( (Color)value );
}
}

公共(public)对象替换(对象原始,对象目标,对象所有者)
{
退回原件;
}

公共(public)类型返回类型
{
得到
{
返回类型(颜色);
}
}

公共(public) SqlType[] SqlTypes
{
得到
{
返回 new[] { new SqlType( DbType.StringFixedLength ) };
}
}
}

ColorUserTypeTests.cs

[测试类]
公共(public)类 ColorUserTypeTests
{
公共(public) TestContext TestContext { 获取;放; }

[测试方法]
public void AssembleTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.Assemble( color, null );
Assert.AreEqual( 颜色, val );
}

[测试方法]
public void DeepCopyTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.DeepCopy( 颜色);
Assert.AreEqual( 颜色, val );
}

[测试方法]
public void DissasembleTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.Disassemble(颜色);
Assert.AreEqual( 颜色, val );
}

[测试方法]
public void EqualsTest()
{
var color1 = Color.Azure;
var color2 = Color.Bisque;
var color3 = Color.Azure;
var userType = new ColorUserType();

var obj1 = (object)color1;
var obj2 = obj1;

Assert.IsFalse( userType.Equals( color1, color2 ) );
Assert.IsTrue( userType.Equals( color1, color1 ) );
Assert.IsTrue( userType.Equals( color1, color3 ) );
Assert.IsFalse( userType.Equals( color1, null ) );
Assert.IsFalse( userType.Equals(null, color1 ) );
Assert.IsTrue( userType.Equals( null, null ) );
Assert.IsTrue(userType.Equals(obj1, obj2));
}

[测试方法]
public void GetHashCodeTest()
{
var color = Color.Azure;
var userType = new ColorUserType();

Assert.AreEqual(color.GetHashCode(), userType.GetHashCode(color));
Assert.AreEqual( typeof( Color ).GetHashCode() + 473, userType.GetHashCode( null ) );
}

[测试方法]
public void IsMutableTest()
{
var userType = new ColorUserType();
Assert.IsTrue( userType.IsMutable );
}

[测试方法]
public void NullSafeGetTest()
{
var dataReaderMock = new Mock();
dataReaderMock.Setup( m => m.GetOrdinal( "white") ).Returns( 0 );
dataReaderMock.Setup( m => m.IsDBNull( 0 ) ).Returns( false );
dataReaderMock.Setup( m => m[0] ).Returns( "#ffffff");

var userType = new ColorUserType();
var val = (Color)userType.NullSafeGet( dataReaderMock.Object, new[] { "white"}, null );

Assert.AreEqual( "ffffffff", val.Name, "返回了错误的颜色。");

dataReaderMock.Setup( m => m.IsDBNull( It.IsAny() ) ).Returns( true );
Assert.IsNull( userType.NullSafeGet( dataReaderMock.Object, new[] { "black"}, null ), "颜色不为空。");

dataReaderMock.VerifyAll();
}

[测试方法]
public void NullSafeSetTest()
{
const 字符串颜色 = "#ffffff";
const int 索引 = 0;

var mockFactory = new MockFactory( MockBehavior.Default );

var parameterMock = mockFactory.Create();
parameterMock.SetupProperty( p => p.Value, string.Empty );

var parameterCollectionMock = mockFactory.Create();
parameterCollectionMock.Setup( m => m[0] ).Returns( parameterMock.Object );

var commandMock = mockFactory.Create();
commandMock.Setup( m => m.Parameters ).Returns( parameterCollectionMock.Object );

var userType = new ColorUserType();

userType.NullSafeSet( commandMock.Object, ColorTranslator.FromHtml( color ), index );
Assert.AreEqual( 0, string.Compare( (string)( (IDataParameter)commandMock.Object.Parameters[0] ).Value, color, true ) );

userType.NullSafeSet( commandMock.Object, null, index );
Assert.AreEqual( DBNull.Value, ( (IDataParameter)commandMock.Object.Parameters[0] ).Value );

mockFactory.VerifyAll();
}

[测试方法]
public void ReplaceTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
Assert.AreEqual( color, userType.Replace( color, null, null ) );
}

[测试方法]
public void ReturnedTypeTest()
{
var userType = new ColorUserType();
Assert.AreEqual( typeof( Color ), userType.ReturnedType );
}

[测试方法]
公共(public)无效 SqlTypesTest()
{
var userType = new ColorUserType();
Assert.AreEqual( 1, userType.SqlTypes.Length );
Assert.AreEqual( new SqlType( DbType.StringFixedLength ), userType.SqlTypes[0] );
}
}

关于unit-testing - 单元测试 NHibernate UserTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1044355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com