forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpectations.cs
118 lines (103 loc) · 3.11 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
using System;
using System.Data;
using NHibernate.Engine;
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, IDbCommand 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, IDbCommand statement)
{
return reportedRowCount;
}
}
public class NoneExpectation : IExpectation
{
public void VerifyOutcomeNonBatched(int rowCount, IDbCommand 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"); }
}
} ;
// 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 == ExecuteUpdateResultCheckStyle.None)
{
return None;
}
else if (style == ExecuteUpdateResultCheckStyle.Count)
{
return Basic;
}
else
{
throw new HibernateException("unknown check style : " + style);
}
}
private Expectations()
{
}
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);
}
}
}
}