forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuplizerDynamicEntity.cs
120 lines (107 loc) · 3.44 KB
/
TuplizerDynamicEntity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using NHibernate.Cfg;
using NUnit.Framework;
namespace NHibernate.Test.DynamicEntity.Tuplizer
{
[TestFixture]
[Obsolete("Require dynamic proxies")]
public class TuplizerDynamicEntity : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new string[] {"DynamicEntity.Tuplizer.Customer.hbm.xml"}; }
}
protected override void Configure(Configuration configuration)
{
configuration.SetInterceptor(new EntityNameInterceptor());
}
[Test]
public void It()
{
var company = ProxyHelper.NewCompanyProxy();
var customer = ProxyHelper.NewCustomerProxy();
var address = ProxyHelper.NewAddressProxy();
var son = ProxyHelper.NewPersonProxy();
var wife = ProxyHelper.NewPersonProxy();
// Test saving these dyna-proxies
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
company.Name = "acme";
session.Save(company);
customer.Name = "Steve";
customer.Company = company;
address.Street = "somewhere over the rainbow";
address.City = "lawerence, kansas";
address.PostalCode = "toto";
customer.Address = address;
customer.Family = new HashSet<Person>();
son.Name = "son";
customer.Family.Add(son);
wife.Name = "wife";
customer.Family.Add(wife);
session.Save(customer);
tran.Commit();
session.Close();
}
Assert.IsNotNull(company.Id, "company id not assigned");
Assert.IsNotNull(customer.Id, "customer id not assigned");
Assert.IsNotNull(address.Id, "address id not assigned");
Assert.IsNotNull(son.Id, "son:Person id not assigned");
Assert.IsNotNull(wife.Id, "wife:Person id not assigned");
// Test loading these dyna-proxies, along with flush processing
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
customer = session.Load<Customer>(customer.Id);
Assert.IsFalse(NHibernateUtil.IsInitialized(customer), "should-be-proxy was initialized");
customer.Name = "other";
session.Flush();
Assert.IsFalse(NHibernateUtil.IsInitialized(customer.Company), "should-be-proxy was initialized");
session.Refresh(customer);
Assert.AreEqual("other", customer.Name, "name not updated");
Assert.AreEqual("acme", customer.Company.Name, "company association not correct");
tran.Commit();
session.Close();
}
// Test detached entity re-attachment with these dyna-proxies
customer.Name = "Steve";
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Update(customer);
session.Flush();
session.Refresh(customer);
Assert.AreEqual("Steve", customer.Name, "name not updated");
tran.Commit();
session.Close();
}
// Test querying
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
int count = session.CreateQuery("from Customer").List().Count;
Assert.AreEqual(1, count, "querying dynamic entity");
session.Clear();
count = session.CreateQuery("from Person").List().Count;
Assert.AreEqual(3, count, "querying dynamic entity");
tran.Commit();
session.Close();
}
// test deleteing
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Delete(company);
session.Delete(customer);
tran.Commit();
session.Close();
}
}
}
}