PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NHibernate.Test/Linq/ParameterisedQueries.cs

https://github.com/okb/nhibernate-core
C# | 215 lines | 172 code | 42 blank | 1 comment | 13 complexity | de920c880c05845a045413a16ce40926 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using NHibernate.Linq;
  7. using NHibernate.DomainModel.Northwind.Entities;
  8. using NUnit.Framework;
  9. namespace NHibernate.Test.Linq
  10. {
  11. [TestFixture]
  12. public class ParameterisedQueries : ReadonlyTestCase
  13. {
  14. [Test]
  15. public void Identical_Expressions_Return_The_Same_Key()
  16. {
  17. using (var s = OpenSession())
  18. {
  19. var db = new Northwind(s);
  20. Expression<Func<IEnumerable<Customer>>> london1 =
  21. () => from c in db.Customers where c.Address.City == "London" select c;
  22. Expression<Func<IEnumerable<Customer>>> london2 =
  23. () => from c in db.Customers where c.Address.City == "London" select c;
  24. var nhLondon1 = new NhLinqExpression(london1.Body, s.SessionFactory);
  25. var nhLondon2 = new NhLinqExpression(london2.Body, s.SessionFactory);
  26. Assert.AreEqual(nhLondon1.Key, nhLondon2.Key);
  27. }
  28. }
  29. [Test]
  30. public void Expressions_Differing_Only_By_Constants_Return_The_Same_Key()
  31. {
  32. using (var s = OpenSession())
  33. {
  34. var db = new Northwind(s);
  35. Expression<Func<IEnumerable<Customer>>> london =
  36. () => from c in db.Customers where c.Address.City == "London" select c;
  37. Expression<Func<IEnumerable<Customer>>> newYork =
  38. () => from c in db.Customers where c.Address.City == "New York" select c;
  39. var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
  40. var nhNewYork = new NhLinqExpression(newYork.Body, s.SessionFactory);
  41. Assert.AreEqual(nhLondon.Key, nhNewYork.Key);
  42. Assert.AreEqual(1, nhLondon.ParameterValuesByName.Count);
  43. Assert.AreEqual(1, nhNewYork.ParameterValuesByName.Count);
  44. Assert.AreEqual("London", nhLondon.ParameterValuesByName.First().Value.First);
  45. Assert.AreEqual("New York", nhNewYork.ParameterValuesByName.First().Value.First);
  46. }
  47. }
  48. [Test]
  49. public void Different_Where_Clauses_Return_Different_Keys()
  50. {
  51. using (var s = OpenSession())
  52. {
  53. var db = new Northwind(s);
  54. Expression<Func<IEnumerable<Customer>>> london =
  55. () => from c in db.Customers where c.Address.City == "London" select c;
  56. Expression<Func<IEnumerable<Customer>>> company =
  57. () => from c in db.Customers where c.CompanyName == "Acme" select c;
  58. var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
  59. var nhNewYork = new NhLinqExpression(company.Body, s.SessionFactory);
  60. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  61. }
  62. }
  63. [Test]
  64. public void Different_Select_Properties_Return_Different_Keys()
  65. {
  66. using (var s = OpenSession())
  67. {
  68. var db = new Northwind(s);
  69. Expression<Func<IEnumerable<string>>> customerId =
  70. () => from c in db.Customers select c.CustomerId;
  71. Expression<Func<IEnumerable<string>>> title =
  72. () => from c in db.Customers select c.ContactTitle;
  73. var nhLondon = new NhLinqExpression(customerId.Body, s.SessionFactory);
  74. var nhNewYork = new NhLinqExpression(title.Body, s.SessionFactory);
  75. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  76. }
  77. }
  78. [Test]
  79. public void Different_Select_Types_Return_Different_Keys()
  80. {
  81. using (var s = OpenSession())
  82. {
  83. var db = new Northwind(s);
  84. Expression<Func<IEnumerable>> newCustomerId =
  85. () => from c in db.Customers select new { c.CustomerId };
  86. Expression<Func<IEnumerable>> customerId =
  87. () => from c in db.Customers select c.CustomerId;
  88. var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
  89. var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
  90. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  91. }
  92. }
  93. [Test]
  94. public void Different_Select_Member_Initialisation_Returns_Different_Keys()
  95. {
  96. using (var s = OpenSession())
  97. {
  98. var db = new Northwind(s);
  99. Expression<Func<IEnumerable>> newCustomerId =
  100. () => from c in db.Customers select new { Id = c.CustomerId, Title = c.ContactTitle };
  101. Expression<Func<IEnumerable>> customerId =
  102. () => from c in db.Customers select new { Title = c.ContactTitle, Id = c.CustomerId };
  103. var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
  104. var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
  105. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  106. }
  107. }
  108. [Test]
  109. public void Different_Conditionals_Return_Different_Keys()
  110. {
  111. using (var s = OpenSession())
  112. {
  113. var db = new Northwind(s);
  114. Expression<Func<IEnumerable>> newCustomerId =
  115. () => from c in db.Customers select new { Desc = c.CustomerId == "1" ? "First" : "Not First" };
  116. Expression<Func<IEnumerable>> customerId =
  117. () => from c in db.Customers select new { Desc = c.CustomerId != "1" ? "First" : "Not First" };
  118. var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
  119. var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
  120. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  121. }
  122. }
  123. [Test]
  124. public void Different_Unary_Operation_Returns_Different_Keys()
  125. {
  126. using (var s = OpenSession())
  127. {
  128. var db = new Northwind(s);
  129. Expression<Func<IEnumerable>> newCustomerId =
  130. () => from c in db.Customers where c.CustomerId == "1" select c;
  131. Expression<Func<IEnumerable>> customerId =
  132. () => from c in db.Customers where !(c.CustomerId == "1") select c;
  133. var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
  134. var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
  135. Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
  136. }
  137. }
  138. [Test]
  139. public void Different_OfType_Returns_Different_Keys()
  140. {
  141. using (var session = OpenSession())
  142. {
  143. Expression<Func<IEnumerable>> ofType1 = () => (from a in session.Query<Animal>().OfType<Cat>() where a.Pregnant select a.Id);
  144. Expression<Func<IEnumerable>> ofType2 = () => (from a in session.Query<Animal>().OfType<Dog>() where a.Pregnant select a.Id);
  145. var nhOfType1 = new NhLinqExpression(ofType1.Body, session.SessionFactory);
  146. var nhOfType2 = new NhLinqExpression(ofType2.Body, session.SessionFactory);
  147. Assert.AreNotEqual(nhOfType1.Key, nhOfType2.Key);
  148. }
  149. }
  150. [Test]
  151. public void Different_Null_Returns_Different_Keys()
  152. {
  153. using (var session = OpenSession())
  154. {
  155. string nullVariable = null;
  156. string notNullVariable = "Hello";
  157. Expression<Func<IEnumerable>> null1 = () => (from a in session.Query<Animal>() where a.Description == null select a);
  158. Expression<Func<IEnumerable>> null2 = () => (from a in session.Query<Animal>() where a.Description == nullVariable select a);
  159. Expression<Func<IEnumerable>> notNull = () => (from a in session.Query<Animal>() where a.Description == notNullVariable select a);
  160. var nhNull1 = new NhLinqExpression(null1.Body, session.SessionFactory);
  161. var nhNull2 = new NhLinqExpression(null2.Body, session.SessionFactory);
  162. var nhNotNull = new NhLinqExpression(notNull.Body, session.SessionFactory);
  163. Assert.AreNotEqual(nhNull1.Key, nhNotNull.Key);
  164. Assert.AreNotEqual(nhNull2.Key, nhNotNull.Key);
  165. }
  166. }
  167. // TODO - different parameter names
  168. protected override IList Mappings
  169. {
  170. get { return new string[0]; }
  171. }
  172. }
  173. }