Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support an arbitrary length for CultureInfoType #3481

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/reference/modules/basic_mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3919,7 +3919,8 @@
<row>
<entry><literal>CultureInfo</literal></entry>
<entry><literal>System.Globalization.CultureInfo</literal></entry>
<entry><literal>DbType.String</literal> - 5 chars for culture</entry>
<entry><literal>DbType.String</literal> - 5 chars for culture by default;
can be modified by the <literal>length</literal> mapping attribute.</entry>
<entry>Default when no <literal>type</literal> attribute specified.</entry>
</row>
<row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,10 @@ protected override void OnSetUp()

protected override void OnTearDown()
{
base.OnTearDown();

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
t.Commit();
}
using var s = OpenSession();
using var t = s.BeginTransaction();
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
t.Commit();
}

protected override void DropSchema()
Expand Down
105 changes: 105 additions & 0 deletions src/NHibernate.Test/Async/TypesTest/CultureInfoTypeFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Globalization;
using NHibernate.Dialect;
using NHibernate.Type;
using NUnit.Framework;

namespace NHibernate.Test.TypesTest
{
using System.Threading.Tasks;
[TestFixture]
public class CultureInfoTypeFixtureAsync : TypeFixtureBase
{
protected override string TypeName => "CultureInfo";

[Test]
public async Task ReadWriteBasicCultureAsync()
{
Guid id;
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US") };
await (s.SaveAsync(entity));
id = entity.Id;
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = await (s.GetAsync<CultureInfoClass>(id));
Assert.That(entity.BasicCulture, Is.Not.Null);
Assert.That(entity.BasicCulture.Name, Is.EqualTo("en-US"));
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US")));
entity.BasicCulture = CultureInfo.GetCultureInfo("fr-BE");
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = await (s.GetAsync<CultureInfoClass>(id));
Assert.That(entity.BasicCulture.Name, Is.EqualTo("fr-BE"));
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("fr-BE")));
entity.BasicCulture = null;
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = await (s.GetAsync<CultureInfoClass>(id));
Assert.That(entity.BasicCulture, Is.Null);
await (t.CommitAsync());
}
}

[Test]
public async Task ReadWriteExtendedCultureAsync()
{
Guid id;
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = new CultureInfoClass { ExtendedCulture = CultureInfo.GetCultureInfo("en-US-posix") };
await (s.SaveAsync(entity));
id = entity.Id;
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = await (s.GetAsync<CultureInfoClass>(id));
Assert.That(entity.ExtendedCulture, Is.Not.Null);
// Under Windows, it is named en-US-posix, but en-US-POSIX under Linux.
Assert.That(entity.ExtendedCulture.Name, Is.EqualTo("en-US-posix").IgnoreCase);
Assert.That(entity.ExtendedCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US-posix")));
await (t.CommitAsync());
}
}

[Test]
public async Task WriteTooLongCultureAsync()
{
if (Dialect is SQLiteDialect)
Assert.Ignore("SQLite has no length limited string type.");
using var s = OpenSession();
using var t = s.BeginTransaction();
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US-posix") };
await (s.SaveAsync(entity));
Assert.That(t.Commit, Throws.Exception);
}
}
}
20 changes: 8 additions & 12 deletions src/NHibernate.Test/Async/TypesTest/DateTimeOffsetTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ protected override void OnSetUp()
}
}

protected override void OnTearDown()
{
base.OnTearDown();

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from DateTimeOffsetClass").ExecuteUpdate();
t.Commit();
}
}

protected override void DropSchema()
{
(Sfi.ConnectionProvider.Driver as ClientDriverWithParamsStats)?.CleanUp();
Expand Down Expand Up @@ -354,6 +342,14 @@ public class DateTimeOffsetTypeWithScaleFixtureAsync : DateTimeOffsetTypeFixture
// The timestamp rounding in seeding does not account scale.
protected override bool RevisionCheck => false;

protected override void OnTearDown()
{
using var s = OpenSession();
using var t = s.BeginTransaction();
s.CreateQuery("delete DateTimeOffsetClass").ExecuteUpdate();
t.Commit();
}

[Test]
public async Task LowerDigitsAreIgnoredAsync()
{
Expand Down
12 changes: 0 additions & 12 deletions src/NHibernate.Test/Async/TypesTest/DecimalTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ protected override void OnSetUp()
}
}

protected override void OnTearDown()
{
base.OnTearDown();

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from DecimalClass").ExecuteUpdate();
t.Commit();
}
}

[Test]
public async Task ReadWriteAsync()
{
Expand Down
8 changes: 0 additions & 8 deletions src/NHibernate.Test/Async/TypesTest/EnumStringTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ protected override void OnSetUp()
s.Close();
}

protected override void OnTearDown()
{
ISession s = OpenSession();
s.Delete("from EnumStringClass");
s.Flush();
s.Close();
}

[Test]
public async Task ReadFromLoadAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ protected override void OnSetUp()
s.Close();
}

protected override void OnTearDown()
{
ISession s = OpenSession();
s.Delete("from GenericEnumStringClass");
s.Flush();
s.Close();
}

[Test]
public async Task ReadFromLoadAsync()
{
Expand Down
8 changes: 0 additions & 8 deletions src/NHibernate.Test/Async/TypesTest/StringTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ protected override string TypeName
get { return "String"; }
}

protected override void OnTearDown()
{
using (var s = OpenSession())
{
s.CreateQuery("delete from StringClass").ExecuteUpdate();
}
}

[Test]
public async Task InsertNullValueAsync()
{
Expand Down
12 changes: 0 additions & 12 deletions src/NHibernate.Test/Async/TypesTest/TimeAsTimeSpanTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ protected override string TypeName
get { return "TimeAsTimeSpan"; }
}

protected override void OnTearDown()
{
base.OnTearDown();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from TimeAsTimeSpanClass").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public async Task SavingAndRetrievingAsync()
{
Expand Down
12 changes: 4 additions & 8 deletions src/NHibernate.Test/TypesTest/AbstractDateTimeTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ protected override void OnSetUp()

protected override void OnTearDown()
{
base.OnTearDown();
fredericDelaporte marked this conversation as resolved.
Show resolved Hide resolved

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
t.Commit();
}
using var s = OpenSession();
using var t = s.BeginTransaction();
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
t.Commit();
}

protected override void DropSchema()
Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate.Test/TypesTest/CultureInfoClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Globalization;

namespace NHibernate.Test.TypesTest
{
public class CultureInfoClass
{
public Guid Id { get; set; }
public CultureInfo BasicCulture { get; set; }
public CultureInfo ExtendedCulture { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/TypesTest/CultureInfoClass.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="NHibernate.Test.TypesTest.CultureInfoClass, NHibernate.Test" table="nh_ci">
<id name="Id" generator="guid.comb" />

<property name="BasicCulture" />
<property name="ExtendedCulture" length="11" />
</class>
</hibernate-mapping>
102 changes: 102 additions & 0 deletions src/NHibernate.Test/TypesTest/CultureInfoTypeFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Globalization;
using NHibernate.Dialect;
using NHibernate.Type;
using NUnit.Framework;

namespace NHibernate.Test.TypesTest
{
[TestFixture]
public class CultureInfoTypeFixture : TypeFixtureBase
{
protected override string TypeName => "CultureInfo";

[Test]
public void ReadWriteBasicCulture()
{
Guid id;
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US") };
s.Save(entity);
id = entity.Id;
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = s.Get<CultureInfoClass>(id);
Assert.That(entity.BasicCulture, Is.Not.Null);
Assert.That(entity.BasicCulture.Name, Is.EqualTo("en-US"));
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US")));
entity.BasicCulture = CultureInfo.GetCultureInfo("fr-BE");
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = s.Get<CultureInfoClass>(id);
Assert.That(entity.BasicCulture.Name, Is.EqualTo("fr-BE"));
Assert.That(entity.BasicCulture, Is.EqualTo(CultureInfo.GetCultureInfo("fr-BE")));
entity.BasicCulture = null;
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = s.Get<CultureInfoClass>(id);
Assert.That(entity.BasicCulture, Is.Null);
t.Commit();
}
}

[Test]
public void ReadWriteExtendedCulture()
{
Guid id;
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = new CultureInfoClass { ExtendedCulture = CultureInfo.GetCultureInfo("en-US-posix") };
s.Save(entity);
id = entity.Id;
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity = s.Get<CultureInfoClass>(id);
Assert.That(entity.ExtendedCulture, Is.Not.Null);
// Under Windows, it is named en-US-posix, but en-US-POSIX under Linux.
Assert.That(entity.ExtendedCulture.Name, Is.EqualTo("en-US-posix").IgnoreCase);
Assert.That(entity.ExtendedCulture, Is.EqualTo(CultureInfo.GetCultureInfo("en-US-posix")));
t.Commit();
}
}

[Test]
public void WriteTooLongCulture()
{
if (Dialect is SQLiteDialect)
Assert.Ignore("SQLite has no length limited string type.");
using var s = OpenSession();
using var t = s.BeginTransaction();
var entity = new CultureInfoClass { BasicCulture = CultureInfo.GetCultureInfo("en-US-posix") };
s.Save(entity);
Assert.That(t.Commit, Throws.Exception);
}

[Test]
public void AutoDiscoverFromNetType()
{
// integration test to be 100% sure
var propertyType = Sfi.GetEntityPersister(typeof(CultureInfoClass).FullName).GetPropertyType(nameof(CultureInfoClass.BasicCulture));
Assert.That(propertyType, Is.InstanceOf<CultureInfoType>());
}
}
}
Loading
Loading