PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms 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
  1using System;
  2using System.Collections;
  3using System.Collections.Generic;
  4using System.Linq;
  5using System.Linq.Expressions;
  6using NHibernate.Linq;
  7using NHibernate.DomainModel.Northwind.Entities;
  8using NUnit.Framework;
  9
 10namespace NHibernate.Test.Linq
 11{
 12	[TestFixture]
 13	public class ParameterisedQueries : ReadonlyTestCase
 14	{
 15		[Test]
 16		public void Identical_Expressions_Return_The_Same_Key()
 17		{
 18			using (var s = OpenSession())
 19			{
 20				var db = new Northwind(s);
 21
 22				Expression<Func<IEnumerable<Customer>>> london1 =
 23					() => from c in db.Customers where c.Address.City == "London" select c;
 24				Expression<Func<IEnumerable<Customer>>> london2 =
 25					() => from c in db.Customers where c.Address.City == "London" select c;
 26
 27				var nhLondon1 = new NhLinqExpression(london1.Body, s.SessionFactory);
 28				var nhLondon2 = new NhLinqExpression(london2.Body, s.SessionFactory);
 29
 30				Assert.AreEqual(nhLondon1.Key, nhLondon2.Key);
 31			}
 32		}
 33
 34		[Test]
 35		public void Expressions_Differing_Only_By_Constants_Return_The_Same_Key()
 36		{
 37			using (var s = OpenSession())
 38			{
 39				var db = new Northwind(s);
 40
 41				Expression<Func<IEnumerable<Customer>>> london =
 42					() => from c in db.Customers where c.Address.City == "London" select c;
 43
 44				Expression<Func<IEnumerable<Customer>>> newYork =
 45					() => from c in db.Customers where c.Address.City == "New York" select c;
 46
 47				var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
 48				var nhNewYork = new NhLinqExpression(newYork.Body, s.SessionFactory);
 49
 50				Assert.AreEqual(nhLondon.Key, nhNewYork.Key);
 51				Assert.AreEqual(1, nhLondon.ParameterValuesByName.Count);
 52				Assert.AreEqual(1, nhNewYork.ParameterValuesByName.Count);
 53				Assert.AreEqual("London", nhLondon.ParameterValuesByName.First().Value.First);
 54				Assert.AreEqual("New York", nhNewYork.ParameterValuesByName.First().Value.First);
 55			}
 56		}
 57
 58		[Test]
 59		public void Different_Where_Clauses_Return_Different_Keys()
 60		{
 61			using (var s = OpenSession())
 62			{
 63				var db = new Northwind(s);
 64
 65				Expression<Func<IEnumerable<Customer>>> london =
 66					() => from c in db.Customers where c.Address.City == "London" select c;
 67				Expression<Func<IEnumerable<Customer>>> company =
 68					() => from c in db.Customers where c.CompanyName == "Acme" select c;
 69
 70				var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
 71				var nhNewYork = new NhLinqExpression(company.Body, s.SessionFactory);
 72
 73				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
 74			}
 75		}
 76
 77		[Test]
 78		public void Different_Select_Properties_Return_Different_Keys()
 79		{
 80			using (var s = OpenSession())
 81			{
 82				var db = new Northwind(s);
 83
 84				Expression<Func<IEnumerable<string>>> customerId =
 85					() => from c in db.Customers select c.CustomerId;
 86				Expression<Func<IEnumerable<string>>> title =
 87					() => from c in db.Customers select c.ContactTitle;
 88
 89				var nhLondon = new NhLinqExpression(customerId.Body, s.SessionFactory);
 90				var nhNewYork = new NhLinqExpression(title.Body, s.SessionFactory);
 91
 92				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
 93			}
 94		}
 95
 96		[Test]
 97		public void Different_Select_Types_Return_Different_Keys()
 98		{
 99			using (var s = OpenSession())
100			{
101				var db = new Northwind(s);
102
103				Expression<Func<IEnumerable>> newCustomerId =
104					() => from c in db.Customers select new { c.CustomerId };
105				Expression<Func<IEnumerable>> customerId =
106					() => from c in db.Customers select c.CustomerId;
107
108				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
109				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
110
111				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
112			}
113		}
114
115		[Test]
116		public void Different_Select_Member_Initialisation_Returns_Different_Keys()
117		{
118			using (var s = OpenSession())
119			{
120				var db = new Northwind(s);
121
122				Expression<Func<IEnumerable>> newCustomerId =
123					() => from c in db.Customers select new { Id = c.CustomerId, Title = c.ContactTitle };
124				Expression<Func<IEnumerable>> customerId =
125					() => from c in db.Customers select new { Title = c.ContactTitle, Id = c.CustomerId };
126
127				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
128				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
129
130				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
131			}
132		}
133
134		[Test]
135		public void Different_Conditionals_Return_Different_Keys()
136		{
137			using (var s = OpenSession())
138			{
139				var db = new Northwind(s);
140
141				Expression<Func<IEnumerable>> newCustomerId =
142					() => from c in db.Customers select new { Desc = c.CustomerId == "1" ? "First" : "Not First" };
143				Expression<Func<IEnumerable>> customerId =
144					() => from c in db.Customers select new { Desc = c.CustomerId != "1" ? "First" : "Not First" };
145
146				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
147				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
148
149				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
150			}
151		}
152
153		[Test]
154		public void Different_Unary_Operation_Returns_Different_Keys()
155		{
156			using (var s = OpenSession())
157			{
158				var db = new Northwind(s);
159
160				Expression<Func<IEnumerable>> newCustomerId =
161					() => from c in db.Customers where c.CustomerId == "1" select c;
162				Expression<Func<IEnumerable>> customerId =
163					() => from c in db.Customers where !(c.CustomerId == "1") select c;
164
165				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
166				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);
167
168				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
169			}
170		}
171
172		[Test]
173		public void Different_OfType_Returns_Different_Keys()
174		{
175			using (var session = OpenSession())
176			{
177				Expression<Func<IEnumerable>> ofType1 = () => (from a in session.Query<Animal>().OfType<Cat>() where a.Pregnant select a.Id);
178				Expression<Func<IEnumerable>> ofType2 = () => (from a in session.Query<Animal>().OfType<Dog>() where a.Pregnant select a.Id);
179
180				var nhOfType1 = new NhLinqExpression(ofType1.Body, session.SessionFactory);
181				var nhOfType2 = new NhLinqExpression(ofType2.Body, session.SessionFactory);
182
183				Assert.AreNotEqual(nhOfType1.Key, nhOfType2.Key);
184			}
185		}
186
187		[Test]
188		public void Different_Null_Returns_Different_Keys()
189		{
190			using (var session = OpenSession())
191			{
192				string nullVariable = null;
193				string notNullVariable = "Hello";
194
195				Expression<Func<IEnumerable>> null1 = () => (from a in session.Query<Animal>() where a.Description == null select a);
196				Expression<Func<IEnumerable>> null2 = () => (from a in session.Query<Animal>() where a.Description == nullVariable select a);
197				Expression<Func<IEnumerable>> notNull = () => (from a in session.Query<Animal>() where a.Description == notNullVariable select a);
198
199				var nhNull1 = new NhLinqExpression(null1.Body, session.SessionFactory);
200				var nhNull2 = new NhLinqExpression(null2.Body, session.SessionFactory);
201				var nhNotNull = new NhLinqExpression(notNull.Body, session.SessionFactory);
202
203				Assert.AreNotEqual(nhNull1.Key, nhNotNull.Key);
204				Assert.AreNotEqual(nhNull2.Key, nhNotNull.Key);
205			}
206		}
207
208		// TODO - different parameter names
209
210		protected override IList Mappings
211		{
212			get { return new string[0]; }
213		}
214	}
215}