Skip to content

Commit 7a01d97

Browse files
Add more tests for constants in LINQ queries
* NH-2500 tests (#1363) * NH-3673 test (#1330)
1 parent 55ca5ee commit 7a01d97

File tree

2 files changed

+422
-0
lines changed

2 files changed

+422
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using NHibernate.DomainModel.Northwind.Entities;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.Linq
18+
{
19+
using System.Threading.Tasks;
20+
// Mainly adapted from tests contributed by Nicola Tuveri on NH-2500 (NH-2500.patch file)
21+
[TestFixture]
22+
public class ConstantTestAsync : LinqTestCase
23+
{
24+
[Test]
25+
[Ignore("Linq query not supported yet")]
26+
public async Task ConstantNonCachedAsync()
27+
{
28+
var c1 = await ((from c in db.Customers
29+
select "customer1").FirstAsync());
30+
31+
var c2 = await ((from c in db.Customers
32+
select "customer2").FirstAsync());
33+
34+
Assert.That(c1, Is.EqualTo("customer1"));
35+
Assert.That(c2, Is.EqualTo("customer2"));
36+
}
37+
38+
[Test]
39+
public async Task ConstantNonCachedInAnonymousNewExpressionAsync()
40+
{
41+
var c1 = await ((from c in db.Customers
42+
where c.CustomerId == "ALFKI"
43+
select new { c.CustomerId, c.ContactName, Constant = 1 }).FirstAsync());
44+
45+
var c2 = await ((from c in db.Customers
46+
where c.CustomerId == "ANATR"
47+
select new { c.CustomerId, c.ContactName, Constant = 2 }).FirstAsync());
48+
49+
Assert.That(c1.Constant, Is.EqualTo(1), "c1.Constant");
50+
Assert.That(c2.Constant, Is.EqualTo(2), "c2.Constant");
51+
Assert.That(c1.CustomerId, Is.EqualTo("ALFKI"), "c1.CustomerId");
52+
Assert.That(c2.CustomerId, Is.EqualTo("ANATR"), "c2.CustomerId");
53+
}
54+
55+
[Test]
56+
public async Task ConstantNonCachedInNestedAnonymousNewExpressionsAsync()
57+
{
58+
var c1 = await ((from c in db.Customers
59+
select new
60+
{
61+
c.ContactName,
62+
Number = 1,
63+
Customer = new { c.CustomerId, Label = "customer1" }
64+
}).FirstAsync());
65+
66+
var c2 = await ((from c in db.Customers
67+
select new
68+
{
69+
c.ContactName,
70+
Number = 2,
71+
Customer = new { c.CustomerId, Label = "customer2" }
72+
}).FirstAsync());
73+
74+
Assert.That(c1.Number, Is.EqualTo(1), "c1.Number");
75+
Assert.That(c1.Customer.Label, Is.EqualTo("customer1"), "c1.Customer.Label");
76+
Assert.That(c2.Number, Is.EqualTo(2), "c1.Number");
77+
Assert.That(c2.Customer.Label, Is.EqualTo("customer2"), "c2.Customer.Label");
78+
}
79+
80+
[Test]
81+
public async Task ConstantNonCachedInNewExpressionAsync()
82+
{
83+
var c1 = await ((from c in db.Customers
84+
where c.CustomerId == "ALFKI"
85+
select new KeyValuePair<string, string>(c.ContactName, "one")).FirstAsync());
86+
87+
var c2 = await ((from c in db.Customers
88+
where c.CustomerId == "ANATR"
89+
select new KeyValuePair<string, string>(c.ContactName, "two")).FirstAsync());
90+
91+
Assert.That(c1.Value, Is.EqualTo("one"), "c1.Value");
92+
Assert.That(c2.Value, Is.EqualTo("two"), "c2.Value");
93+
}
94+
95+
public class ShipperDto
96+
{
97+
public int Number { get; set; }
98+
public string CompanyName { get; set; }
99+
public string Name { get; set; }
100+
}
101+
102+
[Test]
103+
public async Task ConstantNonCachedInMemberInitExpressionAsync()
104+
{
105+
var s1 = await ((from s in db.Shippers
106+
select new ShipperDto
107+
{
108+
Number = 1,
109+
CompanyName = s.CompanyName,
110+
Name = "shipper1"
111+
}).ToListAsync());
112+
113+
var s2 = await ((from s in db.Shippers
114+
select new ShipperDto
115+
{
116+
Number = 2,
117+
CompanyName = s.CompanyName,
118+
Name = "shipper2"
119+
}).ToListAsync());
120+
121+
Assert.That(s1, Has.Count.GreaterThan(0), "s1 Count");
122+
Assert.That(s2, Has.Count.GreaterThan(0), "s2 Count");
123+
Assert.That(s1, Has.All.Property("Number").EqualTo(1), "s1 Numbers");
124+
Assert.That(s1, Has.All.Property("Name").EqualTo("shipper1"), "s1 Names");
125+
Assert.That(s2, Has.All.Property("Number").EqualTo(2), "s2 Numbers");
126+
Assert.That(s2, Has.All.Property("Name").EqualTo("shipper2"), "s2 Names");
127+
}
128+
129+
[Test]
130+
public async Task ConstantInNewArrayExpressionAsync()
131+
{
132+
var c1 = await ((from c in db.Categories
133+
select new [] { c.Name, "category1" }).ToListAsync());
134+
135+
var c2 = await ((from c in db.Categories
136+
select new [] { c.Name, "category2" }).ToListAsync());
137+
138+
Assert.That(c1, Has.Count.GreaterThan(0), "c1 Count");
139+
Assert.That(c2, Has.Count.GreaterThan(0), "c2 Count");
140+
Assert.That(c1.All(c => c[1] == "category1"), Is.True, "c1 second item");
141+
Assert.That(c2.All(c => c[1] == "category2"), Is.True, "c2 second item");
142+
}
143+
144+
[Test]
145+
public async Task ConstantsInNewArrayExpressionAsync()
146+
{
147+
var p1 = await ((from p in db.Products
148+
select new Dictionary<string, int>()
149+
{
150+
{ p.Name, 1 },
151+
{ "product1", p.ProductId }
152+
}).FirstAsync());
153+
154+
var p2 = await ((from p in db.Products
155+
select new Dictionary<string, int>()
156+
{
157+
{ p.Name, 2 },
158+
{ "product2", p.ProductId }
159+
}).FirstAsync());
160+
161+
Assert.That(p1.ElementAt(0).Value == 1 && p1.ElementAt(1).Key == "product1", Is.True, "p1");
162+
Assert.That(p2.ElementAt(0).Value == 2 && p2.ElementAt(1).Key == "product2", Is.True, "p2");
163+
}
164+
165+
public class InfoBuilder
166+
{
167+
private readonly int _value;
168+
169+
public InfoBuilder(int value)
170+
{
171+
_value = value;
172+
}
173+
174+
public int GetItemValue(Product p)
175+
{
176+
return _value;
177+
}
178+
}
179+
180+
// Adapted from NH-2500 first test case by Andrey Titov (file NHTest3.zip)
181+
[Test]
182+
[Ignore("Not fixed yet")]
183+
public async Task ObjectConstantsAsync()
184+
{
185+
var builder = new InfoBuilder(1);
186+
var v1 = await ((from p in db.Products
187+
select builder.GetItemValue(p)).FirstAsync());
188+
builder = new InfoBuilder(2);
189+
var v2 = await ((from p in db.Products
190+
select builder.GetItemValue(p)).FirstAsync());
191+
192+
Assert.That(v1, Is.EqualTo(1), "v1");
193+
Assert.That(v2, Is.EqualTo(2), "v2");
194+
}
195+
196+
private int TestFunc(Product item, int closureValue)
197+
{
198+
return closureValue;
199+
}
200+
201+
// Adapted from NH-3673
202+
[Test]
203+
[Ignore("Not fixed yet")]
204+
public async Task ConstantsInFuncCallAsync()
205+
{
206+
var closureVariable = 1;
207+
var v1 = await ((from p in db.Products
208+
select TestFunc(p, closureVariable)).FirstAsync());
209+
closureVariable = 2;
210+
var v2 = await ((from p in db.Products
211+
select TestFunc(p, closureVariable)).FirstAsync());
212+
213+
Assert.That(v1, Is.EqualTo(1), "v1");
214+
Assert.That(v2, Is.EqualTo(2), "v2");
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)