Skip to content

Commit e65ab36

Browse files
Support an arbitrary length for CultureInfoType (#3481)
1 parent a958ff1 commit e65ab36

22 files changed

+274
-159
lines changed

doc/reference/modules/basic_mapping.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3919,7 +3919,8 @@
39193919
<row>
39203920
<entry><literal>CultureInfo</literal></entry>
39213921
<entry><literal>System.Globalization.CultureInfo</literal></entry>
3922-
<entry><literal>DbType.String</literal> - 5 chars for culture</entry>
3922+
<entry><literal>DbType.String</literal> - 5 chars for culture by default;
3923+
can be modified by the <literal>length</literal> mapping attribute.</entry>
39233924
<entry>Default when no <literal>type</literal> attribute specified.</entry>
39243925
</row>
39253926
<row>

src/NHibernate.Test/Async/TypesTest/AbstractDateTimeTypeFixture.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,10 @@ protected override void OnSetUp()
6969

7070
protected override void OnTearDown()
7171
{
72-
base.OnTearDown();
73-
74-
using (var s = OpenSession())
75-
using (var t = s.BeginTransaction())
76-
{
77-
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
78-
t.Commit();
79-
}
72+
using var s = OpenSession();
73+
using var t = s.BeginTransaction();
74+
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
75+
t.Commit();
8076
}
8177

8278
protected override void DropSchema()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Globalization;
13+
using NHibernate.Dialect;
14+
using NHibernate.Type;
15+
using NUnit.Framework;
16+
17+
namespace NHibernate.Test.TypesTest
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture]
21+
public class CultureInfoTypeFixtureAsync : TypeFixtureBase
22+
{
23+
protected override string TypeName => "CultureInfo";
24+
25+
[Test]
26+
public async Task ReadWriteBasicCultureAsync()
27+
{
28+
Guid id;
29+
using (var s = OpenSession())
30+
using (var t = s.BeginTransaction())
31+
{
32+
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US") };
33+
await (s.SaveAsync(entity));
34+
id = entity.Id;
35+
await (t.CommitAsync());
36+
}
37+
38+
using (var s = OpenSession())
39+
using (var t = s.BeginTransaction())
40+
{
41+
var entity = await (s.GetAsync<CultureInfoClass>(id));
42+
Assert.That(entity.BasicCulture, Is.Not.Null);
43+
Assert.That(entity.BasicCulture.Name, Is.EqualTo("en-US"));
44+
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US")));
45+
entity.BasicCulture = CultureInfo.GetCultureInfo("fr-BE");
46+
await (t.CommitAsync());
47+
}
48+
49+
using (var s = OpenSession())
50+
using (var t = s.BeginTransaction())
51+
{
52+
var entity = await (s.GetAsync<CultureInfoClass>(id));
53+
Assert.That(entity.BasicCulture.Name, Is.EqualTo("fr-BE"));
54+
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("fr-BE")));
55+
entity.BasicCulture = null;
56+
await (t.CommitAsync());
57+
}
58+
59+
using (var s = OpenSession())
60+
using (var t = s.BeginTransaction())
61+
{
62+
var entity = await (s.GetAsync<CultureInfoClass>(id));
63+
Assert.That(entity.BasicCulture, Is.Null);
64+
await (t.CommitAsync());
65+
}
66+
}
67+
68+
[Test]
69+
public async Task ReadWriteExtendedCultureAsync()
70+
{
71+
Guid id;
72+
using (var s = OpenSession())
73+
using (var t = s.BeginTransaction())
74+
{
75+
var entity = new CultureInfoClass { ExtendedCulture = CultureInfo.GetCultureInfo("en-US-posix") };
76+
await (s.SaveAsync(entity));
77+
id = entity.Id;
78+
await (t.CommitAsync());
79+
}
80+
81+
using (var s = OpenSession())
82+
using (var t = s.BeginTransaction())
83+
{
84+
var entity = await (s.GetAsync<CultureInfoClass>(id));
85+
Assert.That(entity.ExtendedCulture, Is.Not.Null);
86+
// Under Windows, it is named en-US-posix, but en-US-POSIX under Linux.
87+
Assert.That(entity.ExtendedCulture.Name, Is.EqualTo("en-US-posix").IgnoreCase);
88+
Assert.That(entity.ExtendedCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US-posix")));
89+
await (t.CommitAsync());
90+
}
91+
}
92+
93+
[Test]
94+
public async Task WriteTooLongCultureAsync()
95+
{
96+
if (Dialect is SQLiteDialect)
97+
Assert.Ignore("SQLite has no length limited string type.");
98+
using var s = OpenSession();
99+
using var t = s.BeginTransaction();
100+
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US-posix") };
101+
await (s.SaveAsync(entity));
102+
Assert.That(t.Commit, Throws.Exception);
103+
}
104+
}
105+
}

src/NHibernate.Test/Async/TypesTest/DateTimeOffsetTypeFixture.cs

+8-12
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ protected override void OnSetUp()
6969
}
7070
}
7171

72-
protected override void OnTearDown()
73-
{
74-
base.OnTearDown();
75-
76-
using (var s = OpenSession())
77-
using (var t = s.BeginTransaction())
78-
{
79-
s.CreateQuery("delete from DateTimeOffsetClass").ExecuteUpdate();
80-
t.Commit();
81-
}
82-
}
83-
8472
protected override void DropSchema()
8573
{
8674
(Sfi.ConnectionProvider.Driver as ClientDriverWithParamsStats)?.CleanUp();
@@ -354,6 +342,14 @@ public class DateTimeOffsetTypeWithScaleFixtureAsync : DateTimeOffsetTypeFixture
354342
// The timestamp rounding in seeding does not account scale.
355343
protected override bool RevisionCheck => false;
356344

345+
protected override void OnTearDown()
346+
{
347+
using var s = OpenSession();
348+
using var t = s.BeginTransaction();
349+
s.CreateQuery("delete DateTimeOffsetClass").ExecuteUpdate();
350+
t.Commit();
351+
}
352+
357353
[Test]
358354
public async Task LowerDigitsAreIgnoredAsync()
359355
{

src/NHibernate.Test/Async/TypesTest/DecimalTypeFixture.cs

-12
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ protected override void OnSetUp()
5454
}
5555
}
5656

57-
protected override void OnTearDown()
58-
{
59-
base.OnTearDown();
60-
61-
using (var s = OpenSession())
62-
using (var t = s.BeginTransaction())
63-
{
64-
s.CreateQuery("delete from DecimalClass").ExecuteUpdate();
65-
t.Commit();
66-
}
67-
}
68-
6957
[Test]
7058
public async Task ReadWriteAsync()
7159
{

src/NHibernate.Test/Async/TypesTest/EnumStringTypeFixture.cs

-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ protected override void OnSetUp()
4040
s.Close();
4141
}
4242

43-
protected override void OnTearDown()
44-
{
45-
ISession s = OpenSession();
46-
s.Delete("from EnumStringClass");
47-
s.Flush();
48-
s.Close();
49-
}
50-
5143
[Test]
5244
public async Task ReadFromLoadAsync()
5345
{

src/NHibernate.Test/Async/TypesTest/GenericEnumStringTypeFixture.cs

-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ protected override void OnSetUp()
4242
s.Close();
4343
}
4444

45-
protected override void OnTearDown()
46-
{
47-
ISession s = OpenSession();
48-
s.Delete("from GenericEnumStringClass");
49-
s.Flush();
50-
s.Close();
51-
}
52-
5345
[Test]
5446
public async Task ReadFromLoadAsync()
5547
{

src/NHibernate.Test/Async/TypesTest/StringTypeFixture.cs

-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ protected override string TypeName
2626
get { return "String"; }
2727
}
2828

29-
protected override void OnTearDown()
30-
{
31-
using (var s = OpenSession())
32-
{
33-
s.CreateQuery("delete from StringClass").ExecuteUpdate();
34-
}
35-
}
36-
3729
[Test]
3830
public async Task InsertNullValueAsync()
3931
{

src/NHibernate.Test/Async/TypesTest/TimeAsTimeSpanTypeFixture.cs

-12
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ protected override string TypeName
5050
get { return "TimeAsTimeSpan"; }
5151
}
5252

53-
protected override void OnTearDown()
54-
{
55-
base.OnTearDown();
56-
57-
using (var s = OpenSession())
58-
using (var tx = s.BeginTransaction())
59-
{
60-
s.CreateQuery("delete from TimeAsTimeSpanClass").ExecuteUpdate();
61-
tx.Commit();
62-
}
63-
}
64-
6553
[Test]
6654
public async Task SavingAndRetrievingAsync()
6755
{

src/NHibernate.Test/TypesTest/AbstractDateTimeTypeFixture.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ protected override void OnSetUp()
5757

5858
protected override void OnTearDown()
5959
{
60-
base.OnTearDown();
61-
62-
using (var s = OpenSession())
63-
using (var t = s.BeginTransaction())
64-
{
65-
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
66-
t.Commit();
67-
}
60+
using var s = OpenSession();
61+
using var t = s.BeginTransaction();
62+
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
63+
t.Commit();
6864
}
6965

7066
protected override void DropSchema()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace NHibernate.Test.TypesTest
5+
{
6+
public class CultureInfoClass
7+
{
8+
public Guid Id { get; set; }
9+
public CultureInfo BasicCulture { get; set; }
10+
public CultureInfo ExtendedCulture { get; set; }
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
3+
<class name="NHibernate.Test.TypesTest.CultureInfoClass, NHibernate.Test" table="nh_ci">
4+
<id name="Id" generator="guid.comb" />
5+
6+
<property name="BasicCulture" />
7+
<property name="ExtendedCulture" length="11" />
8+
</class>
9+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Globalization;
3+
using NHibernate.Dialect;
4+
using NHibernate.Type;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.TypesTest
8+
{
9+
[TestFixture]
10+
public class CultureInfoTypeFixture : TypeFixtureBase
11+
{
12+
protected override string TypeName => "CultureInfo";
13+
14+
[Test]
15+
public void ReadWriteBasicCulture()
16+
{
17+
Guid id;
18+
using (var s = OpenSession())
19+
using (var t = s.BeginTransaction())
20+
{
21+
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US") };
22+
s.Save(entity);
23+
id = entity.Id;
24+
t.Commit();
25+
}
26+
27+
using (var s = OpenSession())
28+
using (var t = s.BeginTransaction())
29+
{
30+
var entity = s.Get<CultureInfoClass>(id);
31+
Assert.That(entity.BasicCulture, Is.Not.Null);
32+
Assert.That(entity.BasicCulture.Name, Is.EqualTo("en-US"));
33+
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US")));
34+
entity.BasicCulture = CultureInfo.GetCultureInfo("fr-BE");
35+
t.Commit();
36+
}
37+
38+
using (var s = OpenSession())
39+
using (var t = s.BeginTransaction())
40+
{
41+
var entity = s.Get<CultureInfoClass>(id);
42+
Assert.That(entity.BasicCulture.Name, Is.EqualTo("fr-BE"));
43+
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("fr-BE")));
44+
entity.BasicCulture = null;
45+
t.Commit();
46+
}
47+
48+
using (var s = OpenSession())
49+
using (var t = s.BeginTransaction())
50+
{
51+
var entity = s.Get<CultureInfoClass>(id);
52+
Assert.That(entity.BasicCulture, Is.Null);
53+
t.Commit();
54+
}
55+
}
56+
57+
[Test]
58+
public void ReadWriteExtendedCulture()
59+
{
60+
Guid id;
61+
using (var s = OpenSession())
62+
using (var t = s.BeginTransaction())
63+
{
64+
var entity = new CultureInfoClass { ExtendedCulture = CultureInfo.GetCultureInfo("en-US-posix") };
65+
s.Save(entity);
66+
id = entity.Id;
67+
t.Commit();
68+
}
69+
70+
using (var s = OpenSession())
71+
using (var t = s.BeginTransaction())
72+
{
73+
var entity = s.Get<CultureInfoClass>(id);
74+
Assert.That(entity.ExtendedCulture, Is.Not.Null);
75+
// Under Windows, it is named en-US-posix, but en-US-POSIX under Linux.
76+
Assert.That(entity.ExtendedCulture.Name, Is.EqualTo("en-US-posix").IgnoreCase);
77+
Assert.That(entity.ExtendedCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US-posix")));
78+
t.Commit();
79+
}
80+
}
81+
82+
[Test]
83+
public void WriteTooLongCulture()
84+
{
85+
if (Dialect is SQLiteDialect)
86+
Assert.Ignore("SQLite has no length limited string type.");
87+
using var s = OpenSession();
88+
using var t = s.BeginTransaction();
89+
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US-posix") };
90+
s.Save(entity);
91+
Assert.That(t.Commit, Throws.Exception);
92+
}
93+
94+
[Test]
95+
public void AutoDiscoverFromNetType()
96+
{
97+
// integration test to be 100% sure
98+
var propertyType = Sfi.GetEntityPersister(typeof(CultureInfoClass).FullName).GetPropertyType(nameof(CultureInfoClass.BasicCulture));
99+
Assert.That(propertyType, Is.InstanceOf<CultureInfoType>());
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)