@@ -12,18 +12,16 @@ namespace Mvc.JQuery.DataTables.Tests
1212 [ TestFixture ]
1313 public class Linq
1414 {
15- protected const int SomeModelPropertyCount = 4 ;
16- protected const int SomeViewPropertyCount = 4 ;
15+ internal const int SomeModelPropertyCount = 4 ;
16+ internal const int SomeViewPropertyCount = 4 ;
1717 private const int TotalRecords = 100 ;
1818 private const int DisplayLength = 5 ;
1919
2020 protected IQueryable < SomeModel > SomeModelQueryable { get ; set ; }
21- private Func < DataTablesParam , DataTablesData > _executeParams ;
2221
2322 public Linq ( )
2423 {
2524 var dataSet = new List < SomeModel > ( TotalRecords ) ;
26- var startDate = new DateTime ( 2013 , 2 , 1 ) ;
2725 for ( var i = 1 ; i < TotalRecords ; i ++ )
2826 {
2927 dataSet . Add ( new SomeModel ( )
@@ -37,93 +35,56 @@ public Linq()
3735 SomeModelQueryable = dataSet . AsQueryable ( ) ;
3836 }
3937
40- private Exception ExecuteParamsFail = null ;
41- [ TestFixtureSetUp ]
42- public void SetExecuteParamsMethod ( )
38+ [ Test ( Description = "Simple Ordering" ) ]
39+ public void SimpleOrder ( )
4340 {
4441 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- ) ) ;
42+ dataTablesParam . sSortDir [ 0 ] = "asc" ;
43+ dataTablesParam . iSortingCols = 1 ;
44+ var result = DataTablesResult . Create ( SomeModelQueryable , //DataContext.Models,
45+ dataTablesParam ,
46+ model => model ) ;
47+ var data = ( DataTablesData ) result . Data ;
48+ Assert . AreEqual ( data . RecordIds ( ) , Enumerable . Range ( 1 , DisplayLength ) . ToArray ( ) ) ;
7549 }
76- [ Test ( Description = "Simple Ordering" ) ]
77- public void SimpleOrdering ( )
50+ [ Test ( Description = "Simple Ordering with transform to view model " ) ]
51+ public void SimpleOrderAndTransform ( )
7852 {
79- if ( _executeParams == null ) { Assert . Ignore ( "Unable to create new DataTableResult" ) ; }
80- //arrange
8153 var dataTablesParam = EmptyParam ( ) ;
8254 dataTablesParam . sSortDir [ 0 ] = "asc" ;
8355 dataTablesParam . iSortingCols = 1 ;
84- Assert . AreEqual ( _executeParams ( dataTablesParam ) . RecordIds ( ) , Enumerable . Range ( 1 , DisplayLength ) . ToArray ( ) ) ;
56+ Assert . AreEqual ( ExecuteParams ( dataTablesParam ) . RecordIds ( ) , Enumerable . Range ( 1 , DisplayLength ) . ToArray ( ) ) ;
8557 }
8658
8759 [ Test ( Description = "Single Record Text Search" ) ]
8860 public void SingleRecordSearch ( )
8961 {
90- if ( _executeParams == null ) { Assert . Ignore ( "Unable to create new DataTableResult" ) ; }
91- //arrange
9262 var dataTablesParam = EmptyParam ( ) ;
9363 //dataTablesParam.sSortDir[0] = "asc";
9464 dataTablesParam . sSearch = "Name 10" ;
9565
96- Assert . AreEqual ( _executeParams ( dataTablesParam ) . RecordIds ( ) , new int [ ] { 10 } ) ;
66+ Assert . AreEqual ( ExecuteParams ( dataTablesParam ) . RecordIds ( ) , new int [ ] { 10 } ) ;
9767 }
9868 [ Test ( Description = "Combination of Sort, Filter & Paginate" ) ]
9969 public void SortFilterPage ( )
10070 {
101- if ( _executeParams == null ) { Assert . Ignore ( "Unable to create new DataTableResult" ) ; }
10271 var dataTablesParam = EmptyParam ( ) ;
10372 dataTablesParam . iSortingCols = 1 ;
10473 dataTablesParam . iSortCol [ 0 ] = 2 ;
10574 dataTablesParam . sSearchColumns [ 3 ] = "25~35" ;
10675 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 ;
76+ var result = ExecuteParams ( dataTablesParam ) . RecordIds ( ) ;
77+ Assert . AreEqual ( ExecuteParams ( dataTablesParam ) . RecordIds ( ) , new int [ ] { 17 , 21 , 25 , 77 , 81 } ) ;
11778 }
118- private DataTablesData ExecuteParamsReturningViewModel ( DataTablesParam dataTablesParam )
79+ private DataTablesData ExecuteParams ( DataTablesParam dataTablesParam )
11980 {
12081 var result = DataTablesResult . Create ( SomeModelQueryable ,
12182 dataTablesParam ,
12283 model => new SomeView
12384 {
12485 Name = model . DisplayName ,
125- Category = model . Category ,
126- Scale = model . Scale ,
86+ Cat = model . Category ,
87+ ViewScale = model . Scale ,
12788 Id = model . Id
12889 } ) ;
12990 return ( DataTablesData ) result . Data ;
@@ -134,20 +95,27 @@ protected static DataTablesParam EmptyParam(int columns = SomeModelPropertyCount
13495 return new DataTablesParam
13596 {
13697 bEscapeRegex = false ,
137- bEscapeRegexColumns = Populate < bool > ( false , columns ) ,
138- bSearchable = Populate < bool > ( true , columns ) ,
139- bSortable = Populate < bool > ( true , columns ) ,
98+ bEscapeRegexColumns = LinqTestStaticMethods . Populate < bool > ( false , columns ) ,
99+ bSearchable = LinqTestStaticMethods . Populate < bool > ( true , columns ) ,
100+ bSortable = LinqTestStaticMethods . Populate < bool > ( true , columns ) ,
140101 iColumns = columns ,
141102 iDisplayLength = DisplayLength ,
142103 iSortingCols = 1 ,
143- iSortCol = Populate < int > ( 0 , columns ) ,
104+ iSortCol = LinqTestStaticMethods . Populate < int > ( 0 , columns ) ,
144105 sEcho = 1 ,
145- sSearchColumns = Populate < string > ( "" , columns ) ,
146- sSortDir = Populate < string > ( null , columns ) ,
106+ sSearchColumns = LinqTestStaticMethods . Populate < string > ( "" , columns ) ,
107+ sSortDir = LinqTestStaticMethods . Populate < string > ( null , columns ) ,
147108 sSearch = ""
148109 } ;
149110 }
150- protected static List < Tlist > Populate < Tlist > ( Tlist value , int capacity = SomeModelPropertyCount )
111+ }
112+ public static class LinqTestStaticMethods
113+ {
114+ public static int [ ] RecordIds ( this DataTablesData data )
115+ {
116+ return Array . ConvertAll < object , int > ( data . aaData , d => int . Parse ( ( string ) ( ( IEnumerable < object > ) d ) . First ( ) ) ) ;
117+ }
118+ public static List < Tlist > Populate < Tlist > ( Tlist value , int capacity = Linq . SomeModelPropertyCount )
151119 {
152120 var returnVal = new Tlist [ capacity ] ;
153121 if ( ! EqualityComparer < Tlist > . Default . Equals ( value , default ( Tlist ) ) )
@@ -159,13 +127,5 @@ protected static List<Tlist> Populate<Tlist>(Tlist value, int capacity = SomeMod
159127 }
160128 return new List < Tlist > ( returnVal ) ;
161129 }
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- }
170130 }
171131}
0 commit comments