nhibernate-core /src/NHibernate.Test/Linq/ParameterisedQueries.cs

Language C# Lines 215
MD5 Hash de920c880c05845a045413a16ce40926
Repository https://github.com/okb/nhibernate-core.git View Raw File
  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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Linq;
using NHibernate.DomainModel.Northwind.Entities;
using NUnit.Framework;

namespace NHibernate.Test.Linq
{
	[TestFixture]
	public class ParameterisedQueries : ReadonlyTestCase
	{
		[Test]
		public void Identical_Expressions_Return_The_Same_Key()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london1 =
					() => from c in db.Customers where c.Address.City == "London" select c;
				Expression<Func<IEnumerable<Customer>>> london2 =
					() => from c in db.Customers where c.Address.City == "London" select c;

				var nhLondon1 = new NhLinqExpression(london1.Body, s.SessionFactory);
				var nhLondon2 = new NhLinqExpression(london2.Body, s.SessionFactory);

				Assert.AreEqual(nhLondon1.Key, nhLondon2.Key);
			}
		}

		[Test]
		public void Expressions_Differing_Only_By_Constants_Return_The_Same_Key()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london =
					() => from c in db.Customers where c.Address.City == "London" select c;

				Expression<Func<IEnumerable<Customer>>> newYork =
					() => from c in db.Customers where c.Address.City == "New York" select c;

				var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(newYork.Body, s.SessionFactory);

				Assert.AreEqual(nhLondon.Key, nhNewYork.Key);
				Assert.AreEqual(1, nhLondon.ParameterValuesByName.Count);
				Assert.AreEqual(1, nhNewYork.ParameterValuesByName.Count);
				Assert.AreEqual("London", nhLondon.ParameterValuesByName.First().Value.First);
				Assert.AreEqual("New York", nhNewYork.ParameterValuesByName.First().Value.First);
			}
		}

		[Test]
		public void Different_Where_Clauses_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london =
					() => from c in db.Customers where c.Address.City == "London" select c;
				Expression<Func<IEnumerable<Customer>>> company =
					() => from c in db.Customers where c.CompanyName == "Acme" select c;

				var nhLondon = new NhLinqExpression(london.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(company.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_Select_Properties_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<string>>> customerId =
					() => from c in db.Customers select c.CustomerId;
				Expression<Func<IEnumerable<string>>> title =
					() => from c in db.Customers select c.ContactTitle;

				var nhLondon = new NhLinqExpression(customerId.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(title.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_Select_Types_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { c.CustomerId };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select c.CustomerId;

				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_Select_Member_Initialisation_Returns_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { Id = c.CustomerId, Title = c.ContactTitle };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select new { Title = c.ContactTitle, Id = c.CustomerId };

				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_Conditionals_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { Desc = c.CustomerId == "1" ? "First" : "Not First" };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select new { Desc = c.CustomerId != "1" ? "First" : "Not First" };

				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_Unary_Operation_Returns_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers where c.CustomerId == "1" select c;
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers where !(c.CustomerId == "1") select c;

				var nhLondon = new NhLinqExpression(newCustomerId.Body, s.SessionFactory);
				var nhNewYork = new NhLinqExpression(customerId.Body, s.SessionFactory);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}

		[Test]
		public void Different_OfType_Returns_Different_Keys()
		{
			using (var session = OpenSession())
			{
				Expression<Func<IEnumerable>> ofType1 = () => (from a in session.Query<Animal>().OfType<Cat>() where a.Pregnant select a.Id);
				Expression<Func<IEnumerable>> ofType2 = () => (from a in session.Query<Animal>().OfType<Dog>() where a.Pregnant select a.Id);

				var nhOfType1 = new NhLinqExpression(ofType1.Body, session.SessionFactory);
				var nhOfType2 = new NhLinqExpression(ofType2.Body, session.SessionFactory);

				Assert.AreNotEqual(nhOfType1.Key, nhOfType2.Key);
			}
		}

		[Test]
		public void Different_Null_Returns_Different_Keys()
		{
			using (var session = OpenSession())
			{
				string nullVariable = null;
				string notNullVariable = "Hello";

				Expression<Func<IEnumerable>> null1 = () => (from a in session.Query<Animal>() where a.Description == null select a);
				Expression<Func<IEnumerable>> null2 = () => (from a in session.Query<Animal>() where a.Description == nullVariable select a);
				Expression<Func<IEnumerable>> notNull = () => (from a in session.Query<Animal>() where a.Description == notNullVariable select a);

				var nhNull1 = new NhLinqExpression(null1.Body, session.SessionFactory);
				var nhNull2 = new NhLinqExpression(null2.Body, session.SessionFactory);
				var nhNotNull = new NhLinqExpression(notNull.Body, session.SessionFactory);

				Assert.AreNotEqual(nhNull1.Key, nhNotNull.Key);
				Assert.AreNotEqual(nhNull2.Key, nhNotNull.Key);
			}
		}

		// TODO - different parameter names

		protected override IList Mappings
		{
			get { return new string[0]; }
		}
	}
}
Back to Top