forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantTest.cs
419 lines (360 loc) · 13 KB
/
ConstantTest.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NHibernate.Criterion;
using NHibernate.DomainModel.Northwind.Entities;
using NHibernate.Engine.Query;
using NHibernate.Linq;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.Linq
{
// Mainly adapted from tests contributed by Nicola Tuveri on NH-2500 (NH-2500.patch file)
[TestFixture]
public class ConstantTest : LinqTestCase
{
[Test]
[Ignore("Linq query not supported yet")]
public void ConstantNonCached()
{
var c1 = (from c in db.Customers
select "customer1").First();
var c2 = (from c in db.Customers
select "customer2").First();
Assert.That(c1, Is.EqualTo("customer1"));
Assert.That(c2, Is.EqualTo("customer2"));
}
[Test]
public void ConstantNonCachedInAnonymousNewExpression()
{
var c1 = (from c in db.Customers
where c.CustomerId == "ALFKI"
select new { c.CustomerId, c.ContactName, Constant = 1 }).First();
var c2 = (from c in db.Customers
where c.CustomerId == "ANATR"
select new { c.CustomerId, c.ContactName, Constant = 2 }).First();
Assert.That(c1.Constant, Is.EqualTo(1), "c1.Constant");
Assert.That(c2.Constant, Is.EqualTo(2), "c2.Constant");
Assert.That(c1.CustomerId, Is.EqualTo("ALFKI"), "c1.CustomerId");
Assert.That(c2.CustomerId, Is.EqualTo("ANATR"), "c2.CustomerId");
}
[Test]
public void ConstantNonCachedInNestedAnonymousNewExpressions()
{
var c1 = (from c in db.Customers
select new
{
c.ContactName,
Number = 1,
Customer = new { c.CustomerId, Label = "customer1" }
}).First();
var c2 = (from c in db.Customers
select new
{
c.ContactName,
Number = 2,
Customer = new { c.CustomerId, Label = "customer2" }
}).First();
Assert.That(c1.Number, Is.EqualTo(1), "c1.Number");
Assert.That(c1.Customer.Label, Is.EqualTo("customer1"), "c1.Customer.Label");
Assert.That(c2.Number, Is.EqualTo(2), "c1.Number");
Assert.That(c2.Customer.Label, Is.EqualTo("customer2"), "c2.Customer.Label");
}
[Test]
public void ConstantNonCachedInNewExpression()
{
var c1 = (from c in db.Customers
where c.CustomerId == "ALFKI"
select new KeyValuePair<string, string>(c.ContactName, "one")).First();
var c2 = (from c in db.Customers
where c.CustomerId == "ANATR"
select new KeyValuePair<string, string>(c.ContactName, "two")).First();
Assert.That(c1.Value, Is.EqualTo("one"), "c1.Value");
Assert.That(c2.Value, Is.EqualTo("two"), "c2.Value");
}
public class ShipperDto
{
public int Number { get; set; }
public string CompanyName { get; set; }
public string Name { get; set; }
}
[Test]
public void ConstantNonCachedInMemberInitExpression()
{
var s1 = (from s in db.Shippers
select new ShipperDto
{
Number = 1,
CompanyName = s.CompanyName,
Name = "shipper1"
}).ToList();
var s2 = (from s in db.Shippers
select new ShipperDto
{
Number = 2,
CompanyName = s.CompanyName,
Name = "shipper2"
}).ToList();
Assert.That(s1, Has.Count.GreaterThan(0), "s1 Count");
Assert.That(s2, Has.Count.GreaterThan(0), "s2 Count");
Assert.That(s1, Has.All.Property("Number").EqualTo(1), "s1 Numbers");
Assert.That(s1, Has.All.Property("Name").EqualTo("shipper1"), "s1 Names");
Assert.That(s2, Has.All.Property("Number").EqualTo(2), "s2 Numbers");
Assert.That(s2, Has.All.Property("Name").EqualTo("shipper2"), "s2 Names");
}
[Test]
public void ConstantNonCachedInMemberInitExpressionWithCondition()
{
var shipper1 = GetShipper(1);
var shipper2 = GetShipper(2);
Assert.That(shipper1.Number, Is.EqualTo(1));
Assert.That(shipper2.Number, Is.EqualTo(2));
}
private ShipperDto GetShipper(int id)
{
return db.Shippers.Where(o => o.ShipperId == id)
.Select(o => new ShipperDto {Number = id, CompanyName = o.CompanyName}).Single();
}
[Test]
public void ConstantInNewArrayExpression()
{
var c1 = (from c in db.Categories
select new[] { c.Name, "category1" }).ToList();
var c2 = (from c in db.Categories
select new[] { c.Name, "category2" }).ToList();
Assert.That(c1, Has.Count.GreaterThan(0), "c1 Count");
Assert.That(c2, Has.Count.GreaterThan(0), "c2 Count");
Assert.That(c1.All(c => c[1] == "category1"), Is.True, "c1 second item");
Assert.That(c2.All(c => c[1] == "category2"), Is.True, "c2 second item");
}
[Test]
public void ConstantsInNewArrayExpression()
{
var p1 = (from p in db.Products
select new Dictionary<string, int>()
{
{ p.Name, 1 },
{ "product1", p.ProductId }
}).First();
var p2 = (from p in db.Products
select new Dictionary<string, int>()
{
{ p.Name, 2 },
{ "product2", p.ProductId }
}).First();
Assert.That(p1.ElementAt(0).Value == 1 && p1.ElementAt(1).Key == "product1", Is.True, "p1");
Assert.That(p2.ElementAt(0).Value == 2 && p2.ElementAt(1).Key == "product2", Is.True, "p2");
}
public class InfoBuilder
{
private readonly int _value;
public InfoBuilder(int value)
{
_value = value;
}
public int GetItemValue(Product p)
{
return _value;
}
}
// Adapted from NH-2500 first test case by Andrey Titov (file NHTest3.zip)
[Test]
public void ObjectConstants()
{
var builder = new InfoBuilder(1);
var v1 = (from p in db.Products
select builder.GetItemValue(p)).First();
builder = new InfoBuilder(2);
var v2 = (from p in db.Products
select builder.GetItemValue(p)).First();
Assert.That(v1, Is.EqualTo(1), "v1");
Assert.That(v2, Is.EqualTo(2), "v2");
}
private int TestFunc(Product item, int closureValue)
{
return closureValue;
}
// Adapted from NH-3673
[Test]
public void ConstantsInFuncCall()
{
var closureVariable = 1;
var v1 = (from p in db.Products
select TestFunc(p, closureVariable)).First();
closureVariable = 2;
var v2 = (from p in db.Products
select TestFunc(p, closureVariable)).First();
Assert.That(v1, Is.EqualTo(1), "v1");
Assert.That(v2, Is.EqualTo(2), "v2");
}
[Test]
public void ConstantInWhereDoesNotCauseManyKeys()
{
var q1 = (from c in db.Customers
where c.CustomerId == "ALFKI"
select c);
var q2 = (from c in db.Customers
where c.CustomerId == "ANATR"
select c);
var preTransformParameters = new PreTransformationParameters(QueryMode.Select, Sfi);
var preTransformResult = NhRelinqQueryParser.PreTransform(q1.Expression, preTransformParameters);
var parameters1 = ExpressionParameterVisitor.Visit(preTransformResult);
var k1 = ExpressionKeyVisitor.Visit(preTransformResult.Expression, parameters1, Sfi);
var preTransformResult2 = NhRelinqQueryParser.PreTransform(q2.Expression, preTransformParameters);
var parameters2 = ExpressionParameterVisitor.Visit(preTransformResult2);
var k2 = ExpressionKeyVisitor.Visit(preTransformResult2.Expression, parameters2, Sfi);
Assert.That(parameters1, Has.Count.GreaterThan(0), "parameters1");
Assert.That(parameters2, Has.Count.GreaterThan(0), "parameters2");
Assert.That(k2, Is.EqualTo(k1));
}
[Test]
public void PlansAreCached()
{
var queryPlanCacheType = typeof(QueryPlanCache);
var cache = (SoftLimitMRUCache)
queryPlanCacheType
.GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(Sfi.QueryPlanCache);
cache.Clear();
(from c in db.Customers
where c.CustomerId == "ALFKI"
select new { c.CustomerId, c.ContactName }).First();
Assert.That(
cache,
Has.Count.EqualTo(1),
"First query plan should be cached.");
using (var spy = new LogSpy(queryPlanCacheType))
{
// Should hit plan cache.
(from c in db.Customers
where c.CustomerId == "ANATR"
select new { c.CustomerId, c.ContactName }).First();
Assert.That(cache, Has.Count.EqualTo(1), "Second query should not cause a plan to be cache.");
Assert.That(
spy.GetWholeLog(),
Does
.Contain("located HQL query plan in cache")
.And.Not.Contain("unable to locate HQL query plan in cache"));
}
}
[Test]
public void DmlPlansAreCached()
{
var queryPlanCacheType = typeof(QueryPlanCache);
var cache = (SoftLimitMRUCache)
queryPlanCacheType
.GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(Sfi.QueryPlanCache);
cache.Clear();
using (session.BeginTransaction())
{
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {CompanyName = "Constant1"});
db.Customers.Where(c => c.CustomerId == "ALFKI").Update(x => new Customer {CompanyName = x.CompanyName});
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {ContactName = "Constant1"});
Assert.That(
cache,
Has.Count.EqualTo(3),
"Query plans should be cached.");
using (var spy = new LogSpy(queryPlanCacheType))
{
//Queries below should hit plan cache.
using (var sqlSpy = new SqlLogSpy())
{
db.Customers.Where(c => c.CustomerId == "ANATR").Update(x => new Customer {CompanyName = x.CompanyName});
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {CompanyName = "Constant2"});
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {ContactName = "Constant2"});
var sqlEvents = sqlSpy.Appender.GetEvents();
Assert.That(
sqlEvents[0].RenderedMessage,
Does.Contain("ANATR").And.Not.Contain("UNKNOWN").And.Not.Contain("Constant1"),
"Unexpected constant parameter value");
Assert.That(
sqlEvents[1].RenderedMessage,
Does.Contain("UNKNOWN").And.Contain("Constant2").And.Contain("CompanyName").IgnoreCase
.And.Not.Contain("Constant1"),
"Unexpected constant parameter value");
Assert.That(
sqlEvents[2].RenderedMessage,
Does.Contain("UNKNOWN").And.Contain("Constant2").And.Contain("ContactName").IgnoreCase
.And.Not.Contain("Constant1"),
"Unexpected constant parameter value");
}
Assert.That(cache, Has.Count.EqualTo(3), "Additional queries should not cause a plan to be cached.");
Assert.That(
spy.GetWholeLog(),
Does
.Contain("located HQL query plan in cache")
.And.Not.Contain("unable to locate HQL query plan in cache"));
db.Customers.Where(c => c.CustomerId == "ANATR").Update(x => new Customer {ContactName = x.ContactName});
Assert.That(cache, Has.Count.EqualTo(4), "Query should be cached");
}
}
}
[Test]
public void PlansWithNonParameterizedConstantsAreNotCached()
{
var queryPlanCacheType = typeof(QueryPlanCache);
var cache = (SoftLimitMRUCache)
queryPlanCacheType
.GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(Sfi.QueryPlanCache);
cache.Clear();
(from c in db.Customers
where c.CustomerId == "ALFKI"
select new { c.CustomerId, c.ContactName, Constant = 1 }).First();
Assert.That(
cache,
Has.Count.EqualTo(0),
"Query plan should not be cached.");
}
[Test]
public void PlansWithNonParameterizedConstantsAreNotCachedForExpandedQuery()
{
var queryPlanCacheType = typeof(QueryPlanCache);
var cache = (SoftLimitMRUCache)
queryPlanCacheType
.GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(Sfi.QueryPlanCache);
cache.Clear();
var ids = new[] {"ANATR", "UNKNOWN"}.ToList();
db.Customers.Where(x => ids.Contains(x.CustomerId)).Select(
c => new {c.CustomerId, c.ContactName, Constant = 1}).First();
Assert.That(
cache,
Has.Count.EqualTo(0),
"Query plan should not be cached.");
}
//GH-2298 - Different Update queries - same query cache plan
[Test]
public void DmlPlansForExpandedQuery()
{
var queryPlanCacheType = typeof(QueryPlanCache);
var cache = (SoftLimitMRUCache)
queryPlanCacheType
.GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(Sfi.QueryPlanCache);
cache.Clear();
using (session.BeginTransaction())
{
var list = new[] {"UNKNOWN", "UNKNOWN2"}.ToList();
db.Customers.Where(x => list.Contains(x.CustomerId)).Update(
x => new Customer
{
CompanyName = "Constant1"
});
db.Customers.Where(x => list.Contains(x.CustomerId))
.Update(
x => new Customer
{
ContactName = "Constant1"
});
Assert.That(
cache.Count,
//2 original queries + 2 expanded queries are expected in cache
Is.EqualTo(0).Or.EqualTo(4),
"Query plans should either be cached separately or not cached at all.");
}
}
}
}