-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFunctionTests.cs
More file actions
251 lines (234 loc) · 9.87 KB
/
FunctionTests.cs
File metadata and controls
251 lines (234 loc) · 9.87 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
namespace EntityFramework.Functions.Tests.UnitTests
{
using System;
using System.Data.Entity.Core.Objects;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using EntityFramework.Functions.Tests.Examples;
using EntityFramework.Functions.Tests.Library.Examples;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AdventureWorksTests
{
[TestMethod]
public void StoredProcedureWithSingleResultTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
ObjectResult<ManagerEmployee> employees = adventureWorks.uspGetManagerEmployees(2);
Assert.IsTrue(employees.Any());
}
}
[TestMethod]
public void StoreProcedureWithOutParameterTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
ObjectParameter errorLogId = new ObjectParameter("ErrorLogID", typeof(int)) { Value = 5 };
int? rows = adventureWorks.LogError(errorLogId);
Assert.AreEqual(0, errorLogId.Value);
Assert.AreEqual(typeof(int), errorLogId.ParameterType);
Assert.AreEqual(-1, rows);
}
}
[TestMethod]
public void StoreProcedureWithMultipleResultsTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
// The first type of result type: a sequence of ProductCategory objects.
ObjectResult<ProductCategory> categories = adventureWorks.uspGetCategoryAndSubCategory(1);
Assert.IsNotNull(categories.Single());
// The second type of result type: a sequence of ProductCategory objects.
ObjectResult<ProductSubcategory> subcategories = categories.GetNextResult<ProductSubcategory>();
Assert.IsTrue(subcategories.Any());
}
}
[TestMethod]
public void ComplexTypeTableValuedFunctionTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
IQueryable<ContactInformation> employees = adventureWorks.ufnGetContactInformation(1).Take(2);
Assert.IsNotNull(employees.Single());
}
}
[TestMethod]
public void EntityTypeTableValuedFunctionTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
IQueryable<Person> persons = adventureWorks.ufnGetPersons("a").Take(2);
Assert.IsTrue(persons.Any());
}
}
[TestMethod]
public void ComposedTableValuedFunctionInLinqTest()
{
using (AdventureWorks database = new AdventureWorks())
{
var employeesWithContactInformation =
from employee in database.Persons
from contactInfo in database.ufnGetContactInformation(employee.BusinessEntityID)
select new {employee.FirstName, contactInfo.JobTitle};
var employeeWithContactInformation = employeesWithContactInformation.Take(1).ToList();
Assert.AreEqual(employeeWithContactInformation.Count, 1);
}
}
public void NonComposableScalarValuedFunctionTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
decimal? cost = adventureWorks.ufnGetProductStandardCost(999, DateTime.Now);
Assert.IsNotNull(cost);
Assert.IsTrue(cost > 1);
}
}
[TestMethod]
public void NonComposableScalarValuedFunctionLinqTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
try
{
adventureWorks
.Products
.Where(product => product.ListPrice >= adventureWorks.ufnGetProductStandardCost(999, DateTime.Now))
.ToArray();
Assert.Fail();
}
catch (NotSupportedException exception)
{
Trace.WriteLine(exception);
}
}
}
[TestMethod]
public void ComposableScalarValuedFunctionLinqTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
IQueryable<Product> products = adventureWorks
.Products
.Where(product => product.ListPrice <= adventureWorks.ufnGetProductListPrice(999, DateTime.Now));
Assert.IsTrue(products.Any());
}
}
[TestMethod]
public void ComposableScalarValuedFunctionTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
try
{
adventureWorks.ufnGetProductListPrice(999, DateTime.Now);
Assert.Fail();
}
catch (NotSupportedException exception)
{
Trace.WriteLine(exception);
}
}
}
[TestMethod]
public void AggregateFunctionLinqTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
var categories = adventureWorks.ProductSubcategories
.GroupBy(subcategory => subcategory.ProductCategoryID)
.Select(category => new
{
CategoryId = category.Key,
SubcategoryNames = category.Select(subcategory => subcategory.Name).Concat()
})
.ToArray();
Assert.IsTrue(categories.Length > 0);
categories.ForEach(category =>
{
Assert.IsTrue(category.CategoryId > 0);
Assert.IsFalse(string.IsNullOrWhiteSpace(category.SubcategoryNames));
});
}
}
[TestMethod]
public void BuiltInFunctionLinqTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
var categories = adventureWorks.ProductSubcategories
.GroupBy(subcategory => subcategory.ProductCategoryID)
.Select(category => new
{
CategoryId = category.Key,
SubcategoryNames = category.Select(subcategory => subcategory.Name.Left(4)).Concat()
})
.ToArray();
Assert.IsTrue(categories.Length > 0);
categories.ForEach(category =>
{
Assert.IsTrue(category.CategoryId > 0);
Assert.IsFalse(string.IsNullOrWhiteSpace(category.SubcategoryNames));
});
}
}
[TestMethod]
public void NiladicFunctionLinqTest()
{
using (AdventureWorks adventureWorks = new AdventureWorks())
{
var firstCategory = adventureWorks.ProductSubcategories
.GroupBy(subcategory => subcategory.ProductCategoryID)
.Select(category => new
{
CategoryId = category.Key,
SubcategoryNames = category.Select(subcategory => subcategory.Name.Left(4)).Concat(),
CurrentTimestamp = NiladicFunctions.CurrentTimestamp(),
CurrentUser = NiladicFunctions.CurrentUser(),
SessionUser = NiladicFunctions.SessionUser(),
SystemUser = NiladicFunctions.SystemUser(),
User = NiladicFunctions.User()
})
.First();
Assert.IsNotNull(firstCategory);
Assert.IsNotNull(firstCategory.CurrentTimestamp);
Assert.IsTrue(DateTime.Now >= firstCategory.CurrentTimestamp);
Assert.AreEqual("dbo", firstCategory.CurrentUser, true, CultureInfo.InvariantCulture);
Assert.AreEqual("dbo", firstCategory.SessionUser, true, CultureInfo.InvariantCulture);
Assert.AreEqual($@"{Environment.UserDomainName}\{Environment.UserName}", firstCategory.SystemUser, true, CultureInfo.InvariantCulture);
Assert.AreEqual("dbo", firstCategory.User, true, CultureInfo.InvariantCulture);
}
}
[TestMethod]
public void ModelDefinedFunctionInLinqTest()
{
using (AdventureWorks database = new AdventureWorks())
{
var employees = from employee in database.Persons
where employee.Title != null
let formatted = employee.FormatName()
select new
{
formatted,
employee
};
var employeeData = employees.Take(1).ToList().FirstOrDefault();
Assert.IsNotNull(employeeData);
Assert.IsNotNull(employeeData.formatted);
Assert.AreEqual(employeeData.employee.FormatName(), employeeData.formatted);
}
}
[TestMethod]
public void ModelDefinedFunctionInLinqTest2()
{
using (AdventureWorks database = new AdventureWorks())
{
var result = (from employee in database.Persons
let formatted = "23.00".ParseDouble()
select formatted).FirstOrDefault();
Assert.AreEqual(23.00, result);
}
}
}
}