-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathLinq.cs
118 lines (105 loc) · 4.41 KB
/
Linq.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Mvc.JQuery.DataTables;
using Mvc.JQuery.DataTables.Tests.DummyPocos;
using NUnit.Framework;
namespace Mvc.JQuery.DataTables.Tests.Fixtures
{
[TestFixture]
public class Linq
{
private const int TotalRecords = 100;
protected IQueryable<SomeModel> SomeModelQueryable { get; set; }
public Linq()
{
DateTime startDate = new DateTime(2000, 1, 1);
var dataSet = new List<SomeModel>(TotalRecords);
for (var i = 1; i < TotalRecords; i++)
{
dataSet.Add(new SomeModel()
{
Id = i,
DisplayName = "Name " + i,
Category = i % 4,
Scale = Math.Abs(50 - i),
When = startDate.AddDays(i)
});
}
SomeModelQueryable = dataSet.AsQueryable();
}
[Test, TestCaseSource(typeof(MyFactoryClass), "TestCases")]
public virtual int[] ExecuteParams(DataTablesParam dataTablesParam)
{
var result = new DataTablesResult<SomeModel>(SomeModelQueryable, dataTablesParam);
var data = result.Data;
return data.aaData.Select(row => ((SomeModel)row).Id).ToArray();
}
//[Test, TestCaseSource(typeof(MyFactoryClass), "TestCases")]
public virtual int[] ExecuteParamsAndTransform(DataTablesParam dataTablesParam)
{
var result = DataTablesResult.Create(SomeModelQueryable,
dataTablesParam,
m => new {
FriendlyWhen = m.When.ToShortDateString(),
});
var data = result.Data;
return data.aaData.Select(d=>Convert.ToInt32(((IList)d)[0])).ToArray();
}
}
public static class MyFactoryClass
{
static int PropertyCount(Type T)
{
return T.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.Count(pi=>pi.CanRead);
}
static int DefaultTestCasesLength = 5;
public static IEnumerable TestCases
{
get
{
int propertyCount = PropertyCount(typeof(DataTablesParam));
var dataTablesParam = GetEmptyParam(propertyCount);
dataTablesParam.sSortDir[0] = "asc";
dataTablesParam.iSortingCols = 1;
yield return new TestCaseData(dataTablesParam)
.Returns(Enumerable.Range(1, DefaultTestCasesLength).ToArray())
.SetName("SimpleOrder")
.SetDescription("Simple Ordering");
dataTablesParam = GetEmptyParam(propertyCount);
dataTablesParam.sSearch = "Name 10";
yield return new TestCaseData(dataTablesParam)
.Returns(new int[] { 10 })
.SetName("SingleRecordSearch")
.SetDescription("Single Record Text Search");
dataTablesParam = GetEmptyParam(propertyCount);
dataTablesParam.iSortingCols = 1;
dataTablesParam.iSortCol[0] = 2;
dataTablesParam.sSearchValues[3] = "25~35";
dataTablesParam.iDisplayStart = 6;
yield return new TestCaseData(dataTablesParam)
.Returns(new int[] { 17, 21, 25, 77, 81 })
.SetName("SortFilterPage")
.SetDescription("Combination of Sort, Filter & Paginate");
}
}
static DataTablesParam GetEmptyParam(int columns)
{
var returnVar = new DataTablesParam(columns);
returnVar.iDisplayLength = DefaultTestCasesLength;
returnVar.iSortingCols = 1;
returnVar.sEcho = 1;
returnVar.sSearch = "";
returnVar.bEscapeRegexColumns.AddRange(Enumerable.Repeat(false, columns));
returnVar.bSearchable.AddRange(Enumerable.Repeat(true, columns));
returnVar.bSortable.AddRange(Enumerable.Repeat(true, columns));
returnVar.iSortCol.AddRange(Enumerable.Repeat(0, columns));
returnVar.sSearchValues.AddRange(Enumerable.Repeat("", columns));
returnVar.sSortDir.AddRange(Enumerable.Repeat<string>(null, columns));
return returnVar;
}
}
}