Skip to content

NH-3670 - Dynamic component should allow generic dictionary #305

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

Merged
merged 7 commits into from
Jun 7, 2018
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 @@ -2081,7 +2081,8 @@

<para>
The <literal>&lt;dynamic-component&gt;</literal> element allows an <literal>IDictionary</literal>
to be mapped as a component, where the property names refer to keys of the dictionary.
or <literal>IDictionary<string, object></literal>to be mapped as a component, where the property
names refer to keys of the dictionary.
</para>

</sect2>
Expand Down
68 changes: 68 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1039Generic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using NUnit.Framework;


namespace NHibernate.Test.NHSpecificTest.NH1039Generic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
public override string BugNumber
{
get { return "NH1039Generic"; }
}

protected override void OnTearDown()
{
base.OnTearDown();
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from Person");
tx.Commit();
}
}

[Test]
public async Task testAsync()
{
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Person person = new Person("1");
person.Name = "John Doe";
var set = new HashSet<object>();
set.Add("555-1234");
set.Add("555-4321");
person.Properties.Add("Phones", set);

await (s.SaveAsync(person));
await (tx.CommitAsync());
}
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Person person = (Person)await (s.CreateCriteria(typeof(Person)).UniqueResultAsync());

Assert.AreEqual("1", person.ID);
Assert.AreEqual("John Doe", person.Name);
Assert.AreEqual(1, person.Properties.Count);
Assert.That(person.Properties["Phones"], Is.InstanceOf<ISet<object>>());
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-1234"));
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-4321"));
}
}
}
}
76 changes: 76 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1796Generic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1796Generic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync: BugTestCase
{
[Test]
public async Task MergeAsync()
{
var entity = new Entity { Name = "Vinnie Luther" };
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveAsync(entity));
await (t.CommitAsync());
}

entity.DynProps = new Dictionary<string, object>();
entity.DynProps["StrProp"] = "Modified";
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.MergeAsync(entity));
await (t.CommitAsync());
}

using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.CreateQuery("delete from Entity").ExecuteUpdateAsync());
await (t.CommitAsync());
}
}

[Test]
public async Task SaveOrUpdateAsync()
{
var entity = new Entity { Name = "Vinnie Luther" };
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveOrUpdateAsync(entity));
await (t.CommitAsync());
}

entity.DynProps = new Dictionary<string, object>();
entity.DynProps["StrProp"] = "Modified";
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveOrUpdateAsync(entity));
await (t.CommitAsync());
}

using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.CreateQuery("delete from Entity").ExecuteUpdateAsync());
await (t.CommitAsync());
}
}
}
}
124 changes: 124 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH2664Generic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//------------------------------------------------------------------------------
// <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.Collections;
using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;
using System.Linq.Expressions;
using System;

namespace NHibernate.Test.NHSpecificTest.NH2664Generic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";

protected override IList Mappings => new[] {"NHSpecificTest.NH2664Generic.Mappings.hbm.xml"};

/// <summary>
/// push some data into the database
/// Really functions as a save test also
/// </summary>
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Save(
new Product
{
ProductId = "1",
Properties =
{
["Name"] = "First Product",
["Description"] = "First Description"
}
});

session.Save(
new Product
{
ProductId = "2",
Properties =
{
["Name"] = "Second Product",
["Description"] = "Second Description"
}
});

session.Save(
new Product
{
ProductId = "3",
Properties =
{
["Name"] = "val",
["Description"] = "val"
}
});

tran.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.CreateQuery("delete from Product").ExecuteUpdate();
tran.Commit();
}
}

[Test]
public async Task Query_DynamicComponentAsync()
{
using (var session = OpenSession())
{
var product = await ((
from p in session.Query<Product>()
where (string) p.Properties["Name"] == "First Product"
select p
).SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test]
public async Task Multiple_Query_Does_Not_CacheAsync()
{
using (var session = OpenSession())
{
// Query by name
var product1 = await ((
from p in session.Query<Product>()
where (string) p.Properties["Name"] == "First Product"
select p
).SingleAsync());
Assert.That(product1.ProductId, Is.EqualTo("1"));

// Query by description (this test is to verify that the dictionary
// index isn't cached from the query above.
var product2 = await ((
from p in session.Query<Product>()
where (string) p.Properties["Description"] == "Second Description"
select p
).SingleAsync());
Assert.That(product2.ProductId, Is.EqualTo("2"));
}
}
}
}
109 changes: 109 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3571Generic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//------------------------------------------------------------------------------
// <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.Collections;
using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;
using System.Linq.Expressions;
using System;

namespace NHibernate.Test.NHSpecificTest.NH3571Generic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";

protected override IList Mappings => new[] {"NHSpecificTest.NH3571Generic.Mappings.hbm.xml"};

/// <summary>
/// push some data into the database
/// Really functions as a save test also
/// </summary>
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var product = new Product {ProductId = "1"};
product.Details.Properties["Name"] = "First Product";
product.Details.Properties["Description"] = "First Description";

session.Save(product);

product = new Product {ProductId = "2"};
product.Details.Properties["Name"] = "Second Product";
product.Details.Properties["Description"] = "Second Description";

session.Save(product);

product = new Product {ProductId = "3"};
product.Details.Properties["Name"] = "val";
product.Details.Properties["Description"] = "val";

session.Save(product);

tran.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.CreateQuery("delete from Product").ExecuteUpdate();
tran.Commit();
}
}

[Test]
public async Task CanQueryDynamicComponentInComponentAsync()
{
using (var session = OpenSession())
{
var product = await ((
from p in session.Query<Product>()
where (string) p.Details.Properties["Name"] == "First Product"
select p
).SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test]
public async Task MultipleQueriesShouldNotCacheAsync()
{
using (var session = OpenSession())
{
// Query by name
var product1 = await ((
from p in session.Query<Product>()
where (string) p.Details.Properties["Name"] == "First Product"
select p
).SingleAsync());
Assert.That(product1.ProductId, Is.EqualTo("1"));

// Query by description (this test is to verify that the dictionary
// index isn't cached from the query above.
var product2 = await ((
from p in session.Query<Product>()
where (string) p.Details.Properties["Description"] == "Second Description"
select p
).SingleAsync());
Assert.That(product2.ProductId, Is.EqualTo("2"));
}
}
}
}
Loading