|
| 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 | +} |
0 commit comments