-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathExpectations.cs
147 lines (131 loc) · 3.83 KB
/
Expectations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Exceptions;
namespace NHibernate.AdoNet
{
public class Expectations
{
private const int UsualExpectedCount = 1;
public class BasicExpectation : IExpectation
{
private readonly int expectedRowCount;
public BasicExpectation(int expectedRowCount)
{
this.expectedRowCount = expectedRowCount;
if (expectedRowCount < 0)
{
throw new ArgumentException("Expected row count must be greater than zero");
}
}
public void VerifyOutcomeNonBatched(int rowCount, DbCommand statement)
{
rowCount = DetermineRowCount(rowCount, statement);
if (expectedRowCount > rowCount)
{
throw new StaleStateException(
"Unexpected row count: " + rowCount + "; expected: " + expectedRowCount
);
}
if (expectedRowCount < rowCount)
{
String msg = "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount;
throw new TooManyRowsAffectedException(msg, expectedRowCount, rowCount);
}
}
public virtual bool CanBeBatched
{
get { return true; }
}
public virtual int ExpectedRowCount
{
get { return expectedRowCount; }
}
protected virtual int DetermineRowCount(int reportedRowCount, DbCommand statement)
{
return reportedRowCount;
}
}
public class NoneExpectation : IExpectation
{
public void VerifyOutcomeNonBatched(int rowCount, DbCommand statement)
{
// explicitly perform no checking...
}
public bool CanBeBatched
{
// Change from H3! We cannot check this expectation when batching because we currently
// can only check the total row count of a batch.
get { return false; }
}
public int ExpectedRowCount
{
//get { throw new InvalidOperationException("Cannot get ExpectedRowCount of a non-batchable expectation"); }
get { return 1; }
}
} ;
// Various Expectation instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static readonly IExpectation None = new NoneExpectation();
public static readonly IExpectation Basic = new BasicExpectation(UsualExpectedCount);
public static IExpectation AppropriateExpectation(ExecuteUpdateResultCheckStyle style)
{
if (style.Equals(ExecuteUpdateResultCheckStyle.None))
{
return None;
}
else if (style.Equals(ExecuteUpdateResultCheckStyle.Count))
{
return Basic;
}
else
{
throw new HibernateException("unknown check style : " + style);
}
}
private Expectations()
{
}
// Since 5.2
[Obsolete]
public static void VerifyOutcomeBatched(int expectedRowCount, int rowCount)
{
if (expectedRowCount > rowCount)
{
throw new StaleStateException(
"Batch update returned unexpected row count from update; actual row count: " + rowCount +
"; expected: " + expectedRowCount
);
}
if (expectedRowCount < rowCount)
{
string msg = "Batch update returned unexpected row count from update; actual row count: " + rowCount +
"; expected: " + expectedRowCount;
throw new TooManyRowsAffectedException(msg, expectedRowCount, rowCount);
}
}
public static void VerifyOutcomeBatched(int expectedRowCount, int rowCount, DbCommand statement)
{
if (expectedRowCount > rowCount)
{
throw new StaleStateException(
ADOExceptionHelper.ExtendMessage(
$"Batch update returned unexpected row count from update; actual row count: {rowCount}; expected: {expectedRowCount}",
statement.CommandText,
null,
null)
);
}
if (expectedRowCount < rowCount)
{
throw new TooManyRowsAffectedException(
ADOExceptionHelper.ExtendMessage(
$"Batch update returned unexpected row count from update; actual row count: {rowCount}; expected: {expectedRowCount}",
statement.CommandText,
null,
null),
expectedRowCount,
rowCount);
}
}
}
}