Skip to content

Commit 403be2a

Browse files
committed
StringTypeWithLengthFixture: Be less picky about the particular exception type.
NhThrowsOnTooLong() and DbThrowsOnTooLong() were acquiring various dialect and driver exceptions (more would have been required than what was present). To avoid this, consider the prevention of the truncation/inserting the important bit, and make the test care less about which particular exception occur under which driver/dialect. Rename the test cases to reflect this.
1 parent 7cd20b8 commit 403be2a

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

src/NHibernate.Test/TypesTest/StringTypeWithLengthFixture.cs

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System;
2+
using System.Linq;
13
using NHibernate.Cfg.MappingSchema;
24
using NHibernate.Criterion;
35
using NHibernate.Dialect;
46
using NHibernate.Driver;
57
using NHibernate.Exceptions;
8+
using NHibernate.Linq;
69
using NHibernate.Mapping.ByCode;
710
using NUnit.Framework;
811

@@ -38,16 +41,22 @@ protected override HbmMapping GetMappings()
3841

3942

4043
[Test]
41-
public void NhThrowsOnTooLong()
44+
[Description("Values longer than the maximum possible string length " +
45+
"should raise an exception if they would otherwise be truncated.")]
46+
public void ShouldPreventInsertionOfVeryLongStringThatWouldBeTruncated()
4247
{
43-
if (!(Dialect is MsSql2008Dialect))
44-
Assert.Ignore(
45-
"This test only works (and is only relevant) " +
46-
"where the driver has set an explicit length " +
47-
"on the IDbDataParameter.");
48+
// This test case is for when the current driver will use a parameter size
49+
// that is significantly larger than the mapped column size (e.g. SqlClientDriver currently).
50+
51+
// Note: This test could possible be written as
52+
// "database must raise an error OR it must store and return the full value"
53+
// to avoid this dialect specific exception.
54+
if (Dialect is SQLiteDialect)
55+
Assert.Ignore("SQLite does not enforce specified string lengths.");
4856

4957
int maxStringLength = GetLongStringMappedLength();
50-
PropertyValueException ex = Assert.Throws<PropertyValueException>(
58+
59+
var ex = Assert.Catch<Exception>(
5160
() =>
5261
{
5362
using (ISession s = OpenSession())
@@ -58,21 +67,21 @@ public void NhThrowsOnTooLong()
5867
}
5968
});
6069

61-
Assert.That(ex.Message, Is.EqualTo("Error dehydrating property value for NHibernate.Test.TypesTest.StringClass.LongStringValue"));
62-
Assert.That(ex.InnerException, Is.TypeOf<HibernateException>());
63-
Assert.That(ex.InnerException.Message, Is.EqualTo("The length of the string value exceeds the length configured in the mapping/parameter."));
70+
AssertFailedInsertExceptionDetailsAndEmptyTable(ex);
6471
}
6572

6673
[Test]
67-
public void DbThrowsOnTooLong()
74+
[Description("Values longer than the mapped string length " +
75+
"should raise an exception if they would otherwise be truncated.")]
76+
public void ShouldPreventInsertionOfTooLongStringThatWouldBeTruncated()
6877
{
6978
// Note: This test could possible be written as
7079
// "database must raise an error OR it must store and return the full value"
7180
// to avoid this dialect specific exception.
7281
if (Dialect is SQLiteDialect)
7382
Assert.Ignore("SQLite does not enforce specified string lengths.");
7483

75-
Assert.Throws<GenericADOException>(
84+
var ex = Assert.Catch<Exception>(
7685
() =>
7786
{
7887
using (ISession s = OpenSession())
@@ -82,9 +91,45 @@ public void DbThrowsOnTooLong()
8291
s.Flush();
8392
}
8493
},
85-
"Database did not throw an error when trying to put too large a value into a column.");
94+
"An exception was expected when trying to put too large a value into a column.");
95+
96+
AssertFailedInsertExceptionDetailsAndEmptyTable(ex);
97+
}
98+
99+
private void AssertFailedInsertExceptionDetailsAndEmptyTable(Exception ex)
100+
{
101+
// We can get different sort of exceptions.
102+
if (ex is PropertyValueException)
103+
{
104+
// Some drivers/dialects set explicit parameter sizes, in which case we expect NH to
105+
// raise a PropertyValueException (to avoid ADO.NET from silently truncating).
106+
107+
Assert.That(
108+
ex.Message,
109+
Is.StringStarting("Error dehydrating property value for NHibernate.Test.TypesTest.StringClass."));
110+
111+
Assert.That(ex.InnerException, Is.TypeOf<HibernateException>());
112+
113+
Assert.That(
114+
ex.InnerException.Message,
115+
Is.EqualTo("The length of the string value exceeds the length configured in the mapping/parameter."));
116+
}
117+
else
118+
{
119+
// In other cases, we expect the database itself to raise an error. This case
120+
// will also happen if the driver does set an explicit parameter size, but that
121+
// size is larger than the mapped column size.
122+
Assert.That(ex, Is.TypeOf<GenericADOException>());
123+
}
124+
125+
// In any case, nothing should have been inserted.
126+
using (ISession s = OpenSession())
127+
{
128+
Assert.That(s.Query<StringClass>().ToList(), Is.Empty);
129+
}
86130
}
87131

132+
88133
[Test]
89134
public void CriteriaLikeParameterCanExceedColumnSize()
90135
{

0 commit comments

Comments
 (0)