From bb9148825e4c2b91dd42a15df972a09387a2b17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Thu, 18 Jan 2018 19:34:09 +0100 Subject: [PATCH] Add more tests for constants in LINQ queries * NH-2500 tests (#1363) * NH-3673 test (#1330) --- .../Async/Linq/ConstantTest.cs | 217 ++++++++++++++++++ src/NHibernate.Test/Linq/ConstantTest.cs | 205 +++++++++++++++++ 2 files changed, 422 insertions(+) create mode 100644 src/NHibernate.Test/Async/Linq/ConstantTest.cs create mode 100644 src/NHibernate.Test/Linq/ConstantTest.cs diff --git a/src/NHibernate.Test/Async/Linq/ConstantTest.cs b/src/NHibernate.Test/Async/Linq/ConstantTest.cs new file mode 100644 index 00000000000..b5f0f05037d --- /dev/null +++ b/src/NHibernate.Test/Async/Linq/ConstantTest.cs @@ -0,0 +1,217 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System.Collections.Generic; +using System.Linq; +using NHibernate.DomainModel.Northwind.Entities; +using NUnit.Framework; +using NHibernate.Linq; + +namespace NHibernate.Test.Linq +{ + using System.Threading.Tasks; + // Mainly adapted from tests contributed by Nicola Tuveri on NH-2500 (NH-2500.patch file) + [TestFixture] + public class ConstantTestAsync : LinqTestCase + { + [Test] + [Ignore("Linq query not supported yet")] + public async Task ConstantNonCachedAsync() + { + var c1 = await ((from c in db.Customers + select "customer1").FirstAsync()); + + var c2 = await ((from c in db.Customers + select "customer2").FirstAsync()); + + Assert.That(c1, Is.EqualTo("customer1")); + Assert.That(c2, Is.EqualTo("customer2")); + } + + [Test] + public async Task ConstantNonCachedInAnonymousNewExpressionAsync() + { + var c1 = await ((from c in db.Customers + where c.CustomerId == "ALFKI" + select new { c.CustomerId, c.ContactName, Constant = 1 }).FirstAsync()); + + var c2 = await ((from c in db.Customers + where c.CustomerId == "ANATR" + select new { c.CustomerId, c.ContactName, Constant = 2 }).FirstAsync()); + + 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 async Task ConstantNonCachedInNestedAnonymousNewExpressionsAsync() + { + var c1 = await ((from c in db.Customers + select new + { + c.ContactName, + Number = 1, + Customer = new { c.CustomerId, Label = "customer1" } + }).FirstAsync()); + + var c2 = await ((from c in db.Customers + select new + { + c.ContactName, + Number = 2, + Customer = new { c.CustomerId, Label = "customer2" } + }).FirstAsync()); + + 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 async Task ConstantNonCachedInNewExpressionAsync() + { + var c1 = await ((from c in db.Customers + where c.CustomerId == "ALFKI" + select new KeyValuePair(c.ContactName, "one")).FirstAsync()); + + var c2 = await ((from c in db.Customers + where c.CustomerId == "ANATR" + select new KeyValuePair(c.ContactName, "two")).FirstAsync()); + + 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 async Task ConstantNonCachedInMemberInitExpressionAsync() + { + var s1 = await ((from s in db.Shippers + select new ShipperDto + { + Number = 1, + CompanyName = s.CompanyName, + Name = "shipper1" + }).ToListAsync()); + + var s2 = await ((from s in db.Shippers + select new ShipperDto + { + Number = 2, + CompanyName = s.CompanyName, + Name = "shipper2" + }).ToListAsync()); + + 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 async Task ConstantInNewArrayExpressionAsync() + { + var c1 = await ((from c in db.Categories + select new [] { c.Name, "category1" }).ToListAsync()); + + var c2 = await ((from c in db.Categories + select new [] { c.Name, "category2" }).ToListAsync()); + + 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 async Task ConstantsInNewArrayExpressionAsync() + { + var p1 = await ((from p in db.Products + select new Dictionary() + { + { p.Name, 1 }, + { "product1", p.ProductId } + }).FirstAsync()); + + var p2 = await ((from p in db.Products + select new Dictionary() + { + { p.Name, 2 }, + { "product2", p.ProductId } + }).FirstAsync()); + + 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] + [Ignore("Not fixed yet")] + public async Task ObjectConstantsAsync() + { + var builder = new InfoBuilder(1); + var v1 = await ((from p in db.Products + select builder.GetItemValue(p)).FirstAsync()); + builder = new InfoBuilder(2); + var v2 = await ((from p in db.Products + select builder.GetItemValue(p)).FirstAsync()); + + 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] + [Ignore("Not fixed yet")] + public async Task ConstantsInFuncCallAsync() + { + var closureVariable = 1; + var v1 = await ((from p in db.Products + select TestFunc(p, closureVariable)).FirstAsync()); + closureVariable = 2; + var v2 = await ((from p in db.Products + select TestFunc(p, closureVariable)).FirstAsync()); + + Assert.That(v1, Is.EqualTo(1), "v1"); + Assert.That(v2, Is.EqualTo(2), "v2"); + } + } +} diff --git a/src/NHibernate.Test/Linq/ConstantTest.cs b/src/NHibernate.Test/Linq/ConstantTest.cs new file mode 100644 index 00000000000..a30118e7283 --- /dev/null +++ b/src/NHibernate.Test/Linq/ConstantTest.cs @@ -0,0 +1,205 @@ +using System.Collections.Generic; +using System.Linq; +using NHibernate.DomainModel.Northwind.Entities; +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(c.ContactName, "one")).First(); + + var c2 = (from c in db.Customers + where c.CustomerId == "ANATR" + select new KeyValuePair(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 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() + { + { p.Name, 1 }, + { "product1", p.ProductId } + }).First(); + + var p2 = (from p in db.Products + select new Dictionary() + { + { 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] + [Ignore("Not fixed yet")] + 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] + [Ignore("Not fixed yet")] + 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"); + } + } +}