forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicClassFixture.cs
241 lines (205 loc) · 6.42 KB
/
DynamicClassFixture.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq;
using Antlr.Runtime.Misc;
using NUnit.Framework;
using NHibernate.Criterion;
namespace NHibernate.Test.EntityModeTest.Map.Basic
{
[TestFixture]
public class DynamicClassFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new[] {"EntityModeTest.Map.Basic.ProductLine.hbm.xml"}; }
}
public delegate IDictionary SingleCarQueryDelegate(ISession session);
public delegate IList AllModelQueryDelegate(ISession session);
[Test]
public void ShouldWorkWithHQL()
{
TestLazyDynamicClass(
s => (IDictionary) s.CreateQuery("from ProductLine pl order by pl.Description").UniqueResult(),
s => s.CreateQuery("from Model m").List());
}
[Test]
public void ShouldWorkWithCriteria()
{
TestLazyDynamicClass(
s => (IDictionary) s.CreateCriteria("ProductLine").AddOrder(Order.Asc("Description")).UniqueResult(),
s => s.CreateCriteria("Model").List());
}
public void TestLazyDynamicClass(SingleCarQueryDelegate singleCarQueryHandler, AllModelQueryDelegate allModelQueryHandler)
{
ITransaction t;
IDictionary cars;
IList models;
using (ISession s = OpenSession())
{
t = s.BeginTransaction();
cars = new Hashtable();
cars["Description"] = "Cars";
IDictionary monaro = new Hashtable();
monaro["ProductLine"] = cars;
monaro["Name"] = "Monaro";
monaro["Description"] = "Holden Monaro";
IDictionary hsv = new Hashtable();
hsv["ProductLine"] = cars;
hsv["Name"] = "hsv";
hsv["Description"] = "Holden hsv";
models = new List<IDictionary> {monaro, hsv};
cars["Models"] = models;
s.Save("ProductLine", cars);
t.Commit();
}
using (ISession s = OpenSession())
{
t = s.BeginTransaction();
cars = singleCarQueryHandler(s);
models = (IList)cars["Models"];
Assert.IsFalse(NHibernateUtil.IsInitialized(models));
Assert.AreEqual(2, models.Count);
Assert.IsTrue(NHibernateUtil.IsInitialized(models));
s.Clear();
IList list = allModelQueryHandler(s);
foreach (IDictionary ht in list)
{
Assert.IsFalse(NHibernateUtil.IsInitialized(ht["ProductLine"]));
}
var model = (IDictionary)list[0];
Assert.IsTrue(((IList)((IDictionary)model["ProductLine"])["Models"]).Contains(model));
s.Clear();
t.Commit();
}
using (ISession s = OpenSession())
{
t = s.BeginTransaction();
cars = singleCarQueryHandler(s);
s.Delete(cars);
t.Commit();
}
}
[Test]
public void ShouldWorkWithHQLAndGenerics()
{
TestLazyDynamicClass(
s => s.CreateQuery("from ProductLine pl order by pl.Description").UniqueResult<IDictionary<string, object>>(),
s => s.CreateQuery("from Model m").List<IDictionary<string, object>>());
}
[Test]
public void ShouldWorkWithCriteriaAndGenerics()
{
TestLazyDynamicClass(
s => s.CreateCriteria("ProductLine").AddOrder(Order.Asc("Description")).UniqueResult<IDictionary<string, object>>(),
s => s.CreateCriteria("Model").List<IDictionary<string, object>>());
}
[Test]
public void ShouldWorkWithLinqAndGenerics()
{
TestLazyDynamicClass(
s => (IDictionary<string, object>) s.Query<dynamic>("ProductLine").OrderBy("Description").Single(),
s => s.Query<dynamic>("Model").ToList().Cast<IDictionary<string, object>>().ToList());
}
public void TestLazyDynamicClass(
Func<ISession, IDictionary<string, object>> singleCarQueryHandler,
Func<ISession, IList<IDictionary<string, object>>> allModelQueryHandler)
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var cars = new Dictionary<string, object> { ["Description"] = "Cars" };
var monaro = new Dictionary<string, object>
{
["ProductLine"] = cars,
["Name"] = "Monaro",
["Description"] = "Holden Monaro"
};
var hsv = new Dictionary<string, object>
{
["ProductLine"] = cars,
["Name"] = "hsv",
["Description"] = "Holden hsv"
};
var models = new List<IDictionary<string, object>> {monaro, hsv};
cars["Models"] = models;
s.Save("ProductLine", cars);
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var cars = singleCarQueryHandler(s);
var models = (IList<object>) cars["Models"];
Assert.That(NHibernateUtil.IsInitialized(models), Is.False);
Assert.That(models.Count, Is.EqualTo(2));
Assert.That(NHibernateUtil.IsInitialized(models), Is.True);
s.Clear();
var list = allModelQueryHandler(s);
foreach (var dic in list)
{
Assert.That(NHibernateUtil.IsInitialized(dic["ProductLine"]), Is.False);
}
var model = list[0];
Assert.That(((IList<object>) ((IDictionary<string, object>) model["ProductLine"])["Models"]).Contains(model), Is.True);
s.Clear();
t.Commit();
}
}
[Test]
public void ShouldWorkWithLinqAndDynamics()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
dynamic cars = new ExpandoObject();
cars.Description = "Cars";
dynamic monaro = new ExpandoObject();
monaro.ProductLine = cars;
monaro.Name = "Monaro";
monaro.Description = "Holden Monaro";
dynamic hsv = new ExpandoObject();
hsv.ProductLine = cars;
hsv.Name = "hsv";
hsv.Description = "Holden hsv";
var models = new List<dynamic> {monaro, hsv};
cars.Models = models;
s.Save("ProductLine", cars);
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var cars = s.Query<dynamic>("ProductLine").OrderBy("Description").Single();
var models = cars.Models;
Assert.That(NHibernateUtil.IsInitialized(models), Is.False);
Assert.That(models.Count, Is.EqualTo(2));
Assert.That(NHibernateUtil.IsInitialized(models), Is.True);
s.Clear();
var list = s.Query<dynamic>("Model").Where("ProductLine.Description = @0", "Cars").ToList();
foreach (var model in list)
{
Assert.That(NHibernateUtil.IsInitialized(model.ProductLine), Is.False);
}
var model1 = list[0];
Assert.That(model1.ProductLine.Models.Contains(model1), Is.True);
s.Clear();
t.Commit();
}
}
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from ProductLine");
t.Commit();
}
}
}
}