Skip to content

Commit 0de6797

Browse files
author
Brent McSharry
committed
Working testing framework
1 parent 3d4dd58 commit 0de6797

File tree

9 files changed

+280
-92
lines changed

9 files changed

+280
-92
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace Mvc.JQuery.DataTables.Tests
5+
{
6+
public class SomeModel
7+
{
8+
[Key]
9+
public int Id { get; set; }
10+
public string DisplayName { get; set; }
11+
public int Category { get; set; }
12+
public double Scale { get; set; }
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
namespace Mvc.JQuery.DataTables.Tests
3+
{
4+
public class SomeView
5+
{
6+
public int Id { get; set; }
7+
public string Name { get; set; }
8+
public int Category { get; set; }
9+
public double Scale { get; set; }
10+
}
11+
}

Mvc.Jquery.DataTables.Tests/EF/EntityFramework.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

Mvc.Jquery.DataTables.Tests/EF/SomeModel.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Mvc.Jquery.DataTables.Tests/EF/SomeView.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Data.Entity;
2+
using System.Data.Entity.Infrastructure;
3+
using System.IO;
4+
using Mvc.JQuery.Datatables;
5+
using NUnit.Framework;
6+
using System;
7+
using System.Linq;
8+
using System.Collections.Generic;
9+
10+
namespace Mvc.JQuery.DataTables.Tests
11+
{
12+
public abstract class EntityFramework : Linq
13+
{
14+
public class SomeContext : DbContext
15+
{
16+
public DbSet<SomeModel> Models { get; set; }
17+
}
18+
19+
protected readonly SomeContext DataContext;
20+
21+
public EntityFramework(SomeContext dataContext)
22+
{
23+
DataContext = dataContext;
24+
if (DataContext.Models.Any())
25+
{
26+
DataContext.Database.ExecuteSqlCommand("DELETE FROM SomeModels");
27+
}
28+
foreach (var sm in SomeModelQueryable)
29+
{
30+
DataContext.Models.Add(sm);
31+
}
32+
DataContext.SaveChanges();
33+
34+
SomeModelQueryable = DataContext.Models;
35+
}
36+
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Data.Entity;
2+
using System.Data.Entity.Infrastructure;
3+
using System.IO;
4+
using Mvc.JQuery.Datatables;
5+
using NUnit.Framework;
6+
using System;
7+
using System.Linq;
8+
using System.Collections.Generic;
9+
10+
namespace Mvc.JQuery.DataTables.Tests
11+
{
12+
public class EntityFrameworkCe : EntityFramework, IDisposable
13+
{
14+
15+
protected const string DbFile = "Test.sdf";
16+
protected const string Password = "1234567890";
17+
18+
public EntityFrameworkCe() : base(CreateDb())
19+
{
20+
}
21+
22+
private static SomeContext CreateDb()
23+
{
24+
var context = new SomeContext();
25+
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "",
26+
string.Format("Data Source=\"{0}\";Password={1}", DbFile, Password));
27+
28+
context.Database.Initialize(true);
29+
return context;
30+
}
31+
32+
public void Dispose()
33+
{
34+
DataContext.Dispose();
35+
if (File.Exists(DbFile))
36+
{
37+
File.Delete(DbFile);
38+
}
39+
}
40+
}
41+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using Mvc.JQuery.Datatables;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
10+
namespace Mvc.JQuery.DataTables.Tests
11+
{
12+
[TestFixture]
13+
public class Linq
14+
{
15+
protected const int SomeModelPropertyCount = 4;
16+
protected const int SomeViewPropertyCount = 4;
17+
private const int TotalRecords = 100;
18+
private const int DisplayLength = 5;
19+
20+
protected IQueryable<SomeModel> SomeModelQueryable { get; set; }
21+
private Func<DataTablesParam, DataTablesData> _executeParams;
22+
23+
public Linq()
24+
{
25+
var dataSet = new List<SomeModel>(TotalRecords);
26+
var startDate = new DateTime(2013, 2, 1);
27+
for (var i = 1; i < TotalRecords; i++)
28+
{
29+
dataSet.Add(new SomeModel()
30+
{
31+
Id = i,
32+
DisplayName = "Name " + i,
33+
Category = i % 4,
34+
Scale = Math.Abs(50 - i)
35+
});
36+
}
37+
SomeModelQueryable = dataSet.AsQueryable();
38+
}
39+
40+
private Exception ExecuteParamsFail = null;
41+
[TestFixtureSetUp]
42+
public void SetExecuteParamsMethod()
43+
{
44+
var dataTablesParam = EmptyParam();
45+
dataTablesParam.sSearch = "Name 10";
46+
try
47+
{
48+
ExecuteParamsReturningViewModel(dataTablesParam);
49+
_executeParams = ExecuteParamsReturningViewModel;
50+
}
51+
catch (Exception e1)
52+
{
53+
try
54+
{
55+
ExecuteParamsReturningModel(dataTablesParam);
56+
_executeParams = ExecuteParamsReturningModel;
57+
ExecuteParamsFail = e1;
58+
}
59+
catch (Exception e2)
60+
{
61+
_executeParams = null;
62+
ExecuteParamsFail = e2;
63+
}
64+
}
65+
}
66+
[Test(Description = "DataTablesResult.Create working?")]
67+
public void CanCreateDataTablesResult()
68+
{
69+
Assert.That(ExecuteParamsFail == null,
70+
string.Format("failed {0}{1}{2}",
71+
(_executeParams == null)?"all create atttempts - other tests to fail on ignore":"transform argument mapping to view model. Will use (model=>model) for all tests",
72+
Environment.NewLine,
73+
ExecuteParamsFail
74+
));
75+
}
76+
[Test(Description = "Simple Ordering")]
77+
public void SimpleOrdering()
78+
{
79+
if (_executeParams == null) { Assert.Ignore("Unable to create new DataTableResult"); }
80+
//arrange
81+
var dataTablesParam = EmptyParam();
82+
dataTablesParam.sSortDir[0] = "asc";
83+
dataTablesParam.iSortingCols = 1;
84+
Assert.AreEqual(_executeParams(dataTablesParam).RecordIds(), Enumerable.Range(1, DisplayLength).ToArray());
85+
}
86+
87+
[Test(Description = "Single Record Text Search")]
88+
public void SingleRecordSearch()
89+
{
90+
if (_executeParams == null) { Assert.Ignore("Unable to create new DataTableResult"); }
91+
//arrange
92+
var dataTablesParam = EmptyParam();
93+
//dataTablesParam.sSortDir[0] = "asc";
94+
dataTablesParam.sSearch = "Name 10";
95+
96+
Assert.AreEqual(_executeParams(dataTablesParam).RecordIds(), new int[] { 10 });
97+
}
98+
[Test(Description = "Combination of Sort, Filter & Paginate")]
99+
public void SortFilterPage()
100+
{
101+
if (_executeParams == null) { Assert.Ignore("Unable to create new DataTableResult"); }
102+
var dataTablesParam = EmptyParam();
103+
dataTablesParam.iSortingCols = 1;
104+
dataTablesParam.iSortCol[0] = 2;
105+
dataTablesParam.sSearchColumns[3] = "25~35";
106+
dataTablesParam.iDisplayStart = 6;
107+
var result = _executeParams(dataTablesParam).RecordIds();
108+
Assert.AreEqual(_executeParams(dataTablesParam).RecordIds(), new int[] { 17,21,25,77,81 });
109+
}
110+
private DataTablesData ExecuteParamsReturningModel(DataTablesParam dataTablesParam)
111+
{
112+
113+
var result = DataTablesResult.Create(SomeModelQueryable, //DataContext.Models,
114+
dataTablesParam,
115+
model => model);
116+
return (DataTablesData)result.Data;
117+
}
118+
private DataTablesData ExecuteParamsReturningViewModel(DataTablesParam dataTablesParam)
119+
{
120+
var result = DataTablesResult.Create(SomeModelQueryable,
121+
dataTablesParam,
122+
model => new SomeView
123+
{
124+
Name = model.DisplayName,
125+
Category = model.Category,
126+
Scale = model.Scale,
127+
Id = model.Id
128+
});
129+
return (DataTablesData)result.Data;
130+
}
131+
132+
protected static DataTablesParam EmptyParam(int columns = SomeModelPropertyCount)
133+
{
134+
return new DataTablesParam
135+
{
136+
bEscapeRegex = false,
137+
bEscapeRegexColumns = Populate<bool>(false, columns),
138+
bSearchable = Populate<bool>(true, columns),
139+
bSortable = Populate<bool>(true, columns),
140+
iColumns = columns,
141+
iDisplayLength = DisplayLength,
142+
iSortingCols=1,
143+
iSortCol = Populate<int>(0, columns),
144+
sEcho = 1,
145+
sSearchColumns = Populate<string>("", columns),
146+
sSortDir = Populate<string>(null, columns),
147+
sSearch = ""
148+
};
149+
}
150+
protected static List<Tlist> Populate<Tlist>(Tlist value, int capacity = SomeModelPropertyCount)
151+
{
152+
var returnVal = new Tlist[capacity];
153+
if (!EqualityComparer<Tlist>.Default.Equals(value, default(Tlist)))
154+
{
155+
for (var i = 0; i < capacity; ++i)
156+
{
157+
returnVal[i] = value;
158+
}
159+
}
160+
return new List<Tlist>(returnVal);
161+
}
162+
163+
}
164+
public static class LinqTestStaticMethods
165+
{
166+
internal static int[] RecordIds(this DataTablesData data)
167+
{
168+
return Array.ConvertAll<object, int>(data.aaData, d => int.Parse((string)((IEnumerable<object>)d).First()));
169+
}
170+
}
171+
}

Mvc.Jquery.DataTables.Tests/Mvc.JQuery.DataTables.Tests.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55-
<Compile Include="EF\EntityFramework.cs" />
55+
<Compile Include="DummyPocos\SomeModel.cs" />
56+
<Compile Include="DummyPocos\SomeView.cs" />
57+
<Compile Include="Fixtures\EntityFramework.cs" />
58+
<Compile Include="Fixtures\EntityFrameworkCe.cs" />
59+
<Compile Include="Fixtures\Linq.cs" />
5660
<Compile Include="Properties\AssemblyInfo.cs" />
57-
<Compile Include="EF\SomeModel.cs" />
58-
<Compile Include="EF\SomeView.cs" />
5961
</ItemGroup>
6062
<ItemGroup>
6163
<None Include="App.config" />

0 commit comments

Comments
 (0)