Skip to content

Commit 3fcd8d4

Browse files
committed
ProjectionFixtures/Fixture.cs: Rewrite test case to use better exception asserts.
1 parent eca6002 commit 3fcd8d4

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/NHibernate.Test/ProjectionFixtures/Fixture.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NHibernate.Criterion;
33
using NHibernate.Dialect;
44
using NHibernate.Driver;
5+
using NHibernate.Exceptions;
56
using NUnit.Framework;
67

78
namespace NHibernate.Test.ProjectionFixtures
@@ -64,47 +65,48 @@ protected override void OnTearDown()
6465
}
6566

6667

67-
[Test]
68-
public void ErrorFromDBWillGiveTheActualSQLExecuted()
69-
{
70-
if (!(Dialect is MsSql2000Dialect))
71-
Assert.Ignore("Test checks for exact sql and expects an error to occur in a case which is not erroneous on all databases.");
68+
[Test]
69+
public void ErrorFromDBWillGiveTheActualSQLExecuted()
70+
{
71+
if (!(Dialect is MsSql2000Dialect))
72+
Assert.Ignore(
73+
"Test checks for exact sql and expects an error to occur in a case which is not erroneous on all databases.");
7274

73-
string pName = ((ISqlParameterFormatter) sessions.ConnectionProvider.Driver).GetParameterName(0);
74-
string expectedMessagePart0 =
75-
string.Format(
76-
@"could not execute query
75+
string pName = ((ISqlParameterFormatter) sessions.ConnectionProvider.Driver).GetParameterName(0);
76+
string expectedMessagePart0 =
77+
string.Format(
78+
@"could not execute query
7779
[ SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0} ]",
78-
pName);
79-
string expectedMessagePart1 = string.Format(@"[SQL: SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0}]",pName);
80-
81-
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
82-
.Add(Restrictions.Eq("child.Key.Id", 2))
83-
.SetProjection(
84-
Projections.ProjectionList()
85-
.Add(Projections.Property("child.Key.Id"))
86-
.Add(Projections.Count("child.Key.Area"))
87-
);
88-
try
89-
{
90-
using (var s = sessions.OpenSession())
91-
using (var tx = s.BeginTransaction())
92-
{
93-
var criteria = projection.GetExecutableCriteria(s);
94-
criteria.List();
95-
96-
tx.Commit();
97-
}
98-
Assert.Fail();
99-
}
100-
catch (Exception e)
101-
{
102-
if (!e.Message.Contains(expectedMessagePart0) || !e.Message.Contains(expectedMessagePart1))
103-
throw;
104-
}
105-
}
106-
107-
[Test]
80+
pName);
81+
string expectedMessagePart1 =
82+
string.Format(
83+
@"[SQL: SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0}]", pName);
84+
85+
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
86+
.Add(Restrictions.Eq("child.Key.Id", 2))
87+
.SetProjection(
88+
Projections.ProjectionList()
89+
.Add(Projections.Property("child.Key.Id"))
90+
.Add(Projections.Count("child.Key.Area"))
91+
);
92+
93+
94+
var e = Assert.Throws<GenericADOException>(() =>
95+
{
96+
using (var s = sessions.OpenSession())
97+
using (var tx = s.BeginTransaction())
98+
{
99+
var criteria = projection.GetExecutableCriteria(s);
100+
criteria.List();
101+
102+
tx.Commit();
103+
}
104+
});
105+
106+
Assert.That(e.Message, Is.StringContaining(expectedMessagePart0).Or.StringContaining(expectedMessagePart1));
107+
}
108+
109+
[Test]
108110
public void AggregatingHirearchyWithCount()
109111
{
110112
var root = new Key {Id = 1, Area = 2};

0 commit comments

Comments
 (0)