Skip to content

Commit 05858b6

Browse files
committed
NH-3670
Created additional tests with generic properties for NH-1796, NH-1039, NH-3571 and NH-2664
1 parent 8f5d3c2 commit 05858b6

File tree

20 files changed

+645
-9
lines changed

20 files changed

+645
-9
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH1039Generic
7+
{
8+
[TestFixture]
9+
public class Fixture : BugTestCase
10+
{
11+
public override string BugNumber
12+
{
13+
get { return "NH1039Generic"; }
14+
}
15+
16+
protected override void OnTearDown()
17+
{
18+
base.OnTearDown();
19+
using (ISession s = OpenSession())
20+
using (ITransaction tx = s.BeginTransaction())
21+
{
22+
s.Delete("from Person");
23+
tx.Commit();
24+
}
25+
}
26+
27+
[Test]
28+
public void test()
29+
{
30+
using (ISession s = OpenSession())
31+
using (ITransaction tx = s.BeginTransaction())
32+
{
33+
Person person = new Person("1");
34+
person.Name = "John Doe";
35+
var set = new HashSet<object>();
36+
set.Add("555-1234");
37+
set.Add("555-4321");
38+
person.Properties.Add("Phones", set);
39+
40+
s.Save(person);
41+
tx.Commit();
42+
}
43+
using (ISession s = OpenSession())
44+
using (ITransaction tx = s.BeginTransaction())
45+
{
46+
Person person = (Person)s.CreateCriteria(typeof(Person)).UniqueResult();
47+
48+
Assert.AreEqual("1", person.ID);
49+
Assert.AreEqual("John Doe", person.Name);
50+
Assert.AreEqual(1, person.Properties.Count);
51+
Assert.That(person.Properties["Phones"], Is.InstanceOf<ISet<object>>());
52+
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-1234"));
53+
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-4321"));
54+
}
55+
}
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH1039Generic"
4+
assembly="NHibernate.Test">
5+
6+
<class name="Person" table="NH1039_Person">
7+
<id name="ID" type="string" length="32">
8+
<generator class="assigned"/>
9+
</id>
10+
11+
<property name="Name"/>
12+
13+
<dynamic-component name="Properties">
14+
<set name="Phones" table="NH1039_Phone">
15+
<key column="PersonId"/>
16+
<element column="`Number`" type="string"/>
17+
</set>
18+
</dynamic-component>
19+
</class>
20+
21+
</hibernate-mapping>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH1039Generic
7+
{
8+
public class Person
9+
{
10+
public Person() { }
11+
public Person(string id) { this._ID = id; }
12+
13+
private string _ID;
14+
public virtual string ID
15+
{
16+
get { return _ID; }
17+
set { _ID = value; }
18+
}
19+
20+
private string _Name;
21+
public virtual string Name
22+
{
23+
get { return _Name; }
24+
set { _Name = value; }
25+
}
26+
27+
private IDictionary<string, object> _Properties = new Dictionary<string, object>();
28+
public virtual IDictionary<string, object> Properties
29+
{
30+
get { return _Properties; }
31+
set { _Properties = value; }
32+
}
33+
}
34+
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH1796Generic
5+
{
6+
public class Entity
7+
{
8+
public virtual int Id { get; set; }
9+
public virtual string Name { get; set; }
10+
public virtual IDictionary<string, object> DynProps { get; set; }
11+
}
12+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH1796Generic
5+
{
6+
[TestFixture]
7+
public class Fixture: BugTestCase
8+
{
9+
[Test]
10+
public void Merge()
11+
{
12+
var entity = new Entity { Name = "Vinnie Luther" };
13+
using (ISession s = OpenSession())
14+
using (ITransaction t = s.BeginTransaction())
15+
{
16+
s.Save(entity);
17+
t.Commit();
18+
}
19+
20+
entity.DynProps = new Dictionary<string, object>();
21+
entity.DynProps["StrProp"] = "Modified";
22+
using (ISession s = OpenSession())
23+
using (ITransaction t = s.BeginTransaction())
24+
{
25+
s.Merge(entity);
26+
t.Commit();
27+
}
28+
29+
using (ISession s = OpenSession())
30+
using (ITransaction t = s.BeginTransaction())
31+
{
32+
s.CreateQuery("delete from Entity").ExecuteUpdate();
33+
t.Commit();
34+
}
35+
}
36+
37+
[Test]
38+
public void SaveOrUpdate()
39+
{
40+
var entity = new Entity { Name = "Vinnie Luther" };
41+
using (ISession s = OpenSession())
42+
using (ITransaction t = s.BeginTransaction())
43+
{
44+
s.SaveOrUpdate(entity);
45+
t.Commit();
46+
}
47+
48+
entity.DynProps = new Dictionary<string, object>();
49+
entity.DynProps["StrProp"] = "Modified";
50+
using (ISession s = OpenSession())
51+
using (ITransaction t = s.BeginTransaction())
52+
{
53+
s.SaveOrUpdate(entity);
54+
t.Commit();
55+
}
56+
57+
using (ISession s = OpenSession())
58+
using (ITransaction t = s.BeginTransaction())
59+
{
60+
s.CreateQuery("delete from Entity").ExecuteUpdate();
61+
t.Commit();
62+
}
63+
}
64+
}
65+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH1796Generic">
5+
6+
<class name="Entity">
7+
<id name="Id">
8+
<generator class="native" />
9+
</id>
10+
<property name="Name"/>
11+
<dynamic-component name="DynProps">
12+
<property name="StrProp" type="string" />
13+
<property name="ValueProp" type="int" />
14+
</dynamic-component>
15+
</class>
16+
</hibernate-mapping>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System.Collections;
2+
using System.Linq;
3+
using NHibernate.Linq;
4+
using NUnit.Framework;
5+
using System.Linq.Expressions;
6+
using System;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH2664Generic
9+
{
10+
[TestFixture]
11+
public class Fixture : TestCase
12+
{
13+
protected override string MappingsAssembly
14+
{
15+
get { return "NHibernate.Test"; }
16+
}
17+
18+
protected override IList Mappings
19+
{
20+
get
21+
{
22+
return new[]
23+
{
24+
"NHSpecificTest.NH2664Generic.Mappings.hbm.xml"
25+
};
26+
}
27+
}
28+
29+
/// <summary>
30+
/// push some data into the database
31+
/// Really functions as a save test also
32+
/// </summary>
33+
protected override void OnSetUp()
34+
{
35+
base.OnSetUp();
36+
37+
using (var session = OpenSession())
38+
{
39+
using (var tran = session.BeginTransaction())
40+
{
41+
Product product = new Product();
42+
product.ProductId = "1";
43+
product.Properties["Name"] = "First Product";
44+
product.Properties["Description"] = "First Description";
45+
46+
session.Save(product);
47+
48+
product = new Product();
49+
product.ProductId = "2";
50+
product.Properties["Name"] = "Second Product";
51+
product.Properties["Description"] = "Second Description";
52+
53+
session.Save(product);
54+
55+
product = new Product();
56+
product.ProductId = "3";
57+
product.Properties["Name"] = "val";
58+
product.Properties["Description"] = "val";
59+
60+
session.Save(product);
61+
62+
tran.Commit();
63+
}
64+
}
65+
}
66+
67+
protected override void OnTearDown()
68+
{
69+
base.OnTearDown();
70+
71+
using (var session = OpenSession())
72+
{
73+
using (var tran = session.BeginTransaction())
74+
{
75+
session.Delete("from Product");
76+
tran.Commit();
77+
}
78+
}
79+
80+
}
81+
82+
[Test]
83+
public void Query_DynamicComponent()
84+
{
85+
using (var session = OpenSession())
86+
{
87+
var product =
88+
(from p in session.Query<Product>() where p.Properties["Name"] == "First Product" select p).Single();
89+
90+
Assert.IsNotNull(product);
91+
Assert.AreEqual("First Product", product.Properties["Name"]);
92+
}
93+
}
94+
95+
[Test]
96+
public void Multiple_Query_Does_Not_Cache()
97+
{
98+
using (var session = OpenSession())
99+
{
100+
// Query by name
101+
var product1 = (from p in session.Query<Product>()
102+
where p.Properties["Name"] == "First Product"
103+
select p).Single();
104+
Assert.That(product1.ProductId, Is.EqualTo("1"));
105+
106+
// Query by description (this test is to verify that the dictionary
107+
// index isn't cached from the query above.
108+
var product2 = (from p in session.Query<Product>()
109+
where p.Properties["Description"] == "Second Description"
110+
select p).Single();
111+
Assert.That(product2.ProductId, Is.EqualTo("2"));
112+
}
113+
}
114+
115+
[Test]
116+
public void Different_Key_In_DynamicComponentDictionary_Returns_Different_Keys()
117+
{
118+
using (var session = OpenSession())
119+
{
120+
Expression<Func<IEnumerable>> key1 = () => (from a in session.Query<Product>() where a.Properties["Name"] == "val" select a);
121+
Expression<Func<IEnumerable>> key2 = () => (from a in session.Query<Product>() where a.Properties["Description"] == "val" select a);
122+
123+
var nhKey1 = new NhLinqExpression(key1.Body, sessions);
124+
var nhKey2 = new NhLinqExpression(key2.Body, sessions);
125+
126+
Assert.AreNotEqual(nhKey1.Key, nhKey2.Key);
127+
}
128+
}
129+
}
130+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH2664Generic">
4+
<class name="Product" table="Products">
5+
<id name="ProductId" column="ProductId" type="String">
6+
<generator class="assigned" />
7+
8+
</id>
9+
<dynamic-component name="Properties">
10+
<property name="Description" column="Description" type="String"/>
11+
<property name="Name" column="Name" type="String" />
12+
13+
</dynamic-component>
14+
15+
</class>
16+
17+
</hibernate-mapping>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH2664Generic
5+
{
6+
public class Product
7+
{
8+
public virtual string ProductId { get; set; }
9+
10+
private IDictionary<string, object> _properties;
11+
12+
public virtual IDictionary<string, object> Properties
13+
{
14+
get
15+
{
16+
if (_properties == null)
17+
_properties = new Dictionary<string, object>();
18+
19+
return _properties;
20+
}
21+
set { _properties = value; }
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)