Skip to content

Commit 59b82e0

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH-3530: Separate the types into different tables to avoid failure on default values.
1 parent e66dee7 commit 59b82e0

File tree

3 files changed

+60
-89
lines changed

3 files changed

+60
-89
lines changed

src/NHibernate.Test/NHSpecificTest/GH3530/Entities.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using NHibernate.SqlCommand;
67

78
namespace NHibernate.Test.NHSpecificTest.GH3530
89
{
9-
public class LocaleEntity
10+
public abstract class Entity
1011
{
1112
public virtual Guid Id { get; set; }
12-
public virtual int? IntegerValue { get; set; }
13-
public virtual DateTime? DateTimeValue { get; set; }
14-
public virtual double? DoubleValue { get; set; }
15-
public virtual decimal? DecimalValue { get; set; }
1613
}
14+
15+
public abstract class Entity<T>:Entity where T : struct
16+
{
17+
public virtual T Value { get; set; }
18+
}
19+
20+
public class IntegerEntity : Entity<int> { }
21+
public class DateTimeEntity : Entity<DateTime> { }
22+
23+
public class DoubleEntity : Entity<double> { }
24+
public class DecimalEntity : Entity<decimal> { }
1725
}

src/NHibernate.Test/NHSpecificTest/GH3530/Fixture.cs

+33-79
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,28 @@ protected override void OnTearDown()
3737
}
3838

3939
protected override void CreateSchema()
40+
{
41+
CreateTable("Integer");
42+
CreateTable("DateTime");
43+
CreateTable("Double");
44+
CreateTable("Decimal");
45+
}
46+
47+
private void CreateTable(string name)
4048
{
4149
var sb = new StringBuilder();
4250
var guidType = Dialect.GetTypeName(SqlTypeFactory.Guid);
4351
var stringType = Dialect.GetTypeName(SqlTypeFactory.GetAnsiString(255));
4452

4553
var catalog = GetQuotedDefaultCatalog();
4654
var schema = GetQuotedDefaultSchema();
47-
var table = GetQualifiedName(catalog, schema, "LocaleEntity");
55+
var table = GetQualifiedName(catalog, schema, $"{name}Entity");
4856

4957
sb.Append($"{Dialect.CreateTableString} {table} (");
5058

5159
// Generate columns
5260
sb.Append($"Id {guidType}, ");
53-
sb.Append($"IntegerValue {stringType}, ");
54-
sb.Append($"DateTimeValue {stringType}, ");
55-
sb.Append($"DoubleValue {stringType}, ");
56-
sb.Append($"DecimalValue {stringType}");
61+
sb.Append($"Value {stringType}, ");
5762

5863
// Add the primary key contraint for the identity column
5964
sb.Append($", {Dialect.PrimaryKeyString} ( Id )");
@@ -81,7 +86,7 @@ private string GetQuotedDefaultCatalog()
8186
var t = cfg.GetType();
8287
var getQuotedDefaultCatalog = t.GetMethod("GetQuotedDefaultCatalog", BindingFlags.Instance | BindingFlags.NonPublic);
8388

84-
return (string)getQuotedDefaultCatalog.Invoke(cfg, [Dialect]);
89+
return (string) getQuotedDefaultCatalog.Invoke(cfg, [Dialect]);
8590
}
8691

8792
private string GetQuotedDefaultSchema()
@@ -97,19 +102,19 @@ private string GetQualifiedName(string catalog, string schema, string name)
97102
return Dialect.Qualify(catalog, schema, name);
98103
}
99104

100-
[Test, TestCaseSource(nameof(GetTestCases))]
101-
public void TestDateTime(CultureInfo from, CultureInfo to)
105+
private void PerformTest<T, U>(CultureInfo from, CultureInfo to, T expectedValue, Action<T, T> assert)
106+
where T : struct
107+
where U : Entity<T>, new()
102108
{
103-
DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish));
104109
object id;
105110

106111
CurrentCulture = from;
107112
using (var session = OpenSession())
108113
using (var tx = session.BeginTransaction())
109114
{
110-
var entity = new LocaleEntity()
115+
var entity = new U()
111116
{
112-
DateTimeValue = leapDay
117+
Value = expectedValue
113118
};
114119

115120
id = session.Save(entity);
@@ -120,96 +125,45 @@ public void TestDateTime(CultureInfo from, CultureInfo to)
120125
using (var session = OpenSession())
121126
using (var tx = session.BeginTransaction())
122127
{
123-
var entity = session.Get<LocaleEntity>(id);
128+
var entity = session.Get<U>(id);
124129

125-
Assert.AreEqual(leapDay, entity.DateTimeValue);
130+
assert(expectedValue, entity.Value);
126131
}
127132
}
128133

129134
[Test, TestCaseSource(nameof(GetTestCases))]
130-
public void TestDecimal(CultureInfo from, CultureInfo to)
135+
public void TestDateTime(CultureInfo from, CultureInfo to)
131136
{
132-
decimal decimalValue = 12.3m;
133-
object id;
134-
135-
CurrentCulture = from;
136-
using (var session = OpenSession())
137-
using (var tx = session.BeginTransaction())
138-
{
139-
var entity = new LocaleEntity()
140-
{
141-
DecimalValue = decimalValue
142-
};
137+
DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish));
143138

144-
id = session.Save(entity);
145-
tx.Commit();
146-
}
139+
PerformTest<DateTime, DateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual));
140+
}
147141

148-
CurrentCulture = to;
149-
using (var session = OpenSession())
150-
using (var tx = session.BeginTransaction())
151-
{
152-
var entity = session.Get<LocaleEntity>(id);
142+
[Test, TestCaseSource(nameof(GetTestCases))]
143+
public void TestDecimal(CultureInfo from, CultureInfo to)
144+
{
145+
decimal decimalValue = 12.3m;
153146

154-
Assert.AreEqual(decimalValue, entity.DecimalValue);
155-
}
147+
PerformTest<decimal, DecimalEntity>(from, to, decimalValue, (expected, actual) => Assert.AreEqual(expected, actual));
156148
}
157149

158150
[Test, TestCaseSource(nameof(GetTestCases))]
159151
public void TestDouble(CultureInfo from, CultureInfo to)
160152
{
161153
double doubleValue = 12.3d;
162-
object id;
163154

164-
CurrentCulture = from;
165-
using (var session = OpenSession())
166-
using (var tx = session.BeginTransaction())
167-
{
168-
var entity = new LocaleEntity()
169-
{
170-
DoubleValue = doubleValue
171-
};
172-
173-
id = session.Save(entity);
174-
tx.Commit();
175-
}
176-
177-
CurrentCulture = to;
178-
using (var session = OpenSession())
179-
using (var tx = session.BeginTransaction())
180-
{
181-
var entity = session.Get<LocaleEntity>(id);
182-
183-
Assert.True(Math.Abs(doubleValue - entity.DoubleValue) < double.Epsilon, $"Expected: {doubleValue}\nBut was: {entity.DoubleValue}\n");
184-
}
155+
PerformTest<double, DoubleEntity>(from, to, doubleValue,
156+
(expected, actual) => Assert.True(Math.Abs(expected - actual) < double.Epsilon, $"Expected: {expected}\nBut was: {actual}\n")
157+
);
185158
}
186159

160+
[Test, TestCaseSource(nameof(GetTestCases))]
161+
187162
public void TestInteger(CultureInfo from, CultureInfo to)
188163
{
189164
int integerValue = 123;
190-
object id;
191165

192-
CurrentCulture = from;
193-
using (var session = OpenSession())
194-
using (var tx = session.BeginTransaction())
195-
{
196-
var entity = new LocaleEntity()
197-
{
198-
IntegerValue = integerValue
199-
};
200-
201-
id = session.Save(entity);
202-
tx.Commit();
203-
}
204-
205-
CurrentCulture = to;
206-
using (var session = OpenSession())
207-
using (var tx = session.BeginTransaction())
208-
{
209-
var entity = session.Get<LocaleEntity>(id);
210-
211-
Assert.AreEqual(integerValue, entity.IntegerValue);
212-
}
166+
PerformTest<int, IntegerEntity>(from, to, integerValue, (expected, actual) => Assert.AreEqual(expected, actual));
213167
}
214168

215169
private CultureInfo CurrentCulture
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
33
namespace="NHibernate.Test.NHSpecificTest.GH3530">
4-
<class name="LocaleEntity">
4+
<class name="IntegerEntity">
55
<id name="Id" generator="guid.comb" />
6-
<property name="IntegerValue" column="IntegerValue"/>
7-
<property name="DateTimeValue" column="DateTimeValue"/>
8-
<property name="DoubleValue" column="DoubleValue"/>
9-
<property name="DecimalValue" column="DecimalValue"/>
6+
<property name="Value" column="Value"/>
7+
</class>
8+
<class name="DateTimeEntity">
9+
<id name="Id" generator="guid.comb" />
10+
<property name="Value" column="Value"/>
11+
</class>
12+
<class name="DoubleEntity">
13+
<id name="Id" generator="guid.comb" />
14+
<property name="Value" column="Value"/>
15+
</class>
16+
<class name="DecimalEntity">
17+
<id name="Id" generator="guid.comb" />
18+
<property name="Value" column="Value"/>
1019
</class>
1120
</hibernate-mapping>

0 commit comments

Comments
 (0)