|
| 1 | +using Mvc.JQuery.Datatables; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.Specialized; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Web.Mvc; |
| 9 | +using Mvc.JQuery.Datatables.Tests.TestingUtilities; |
| 10 | + |
| 11 | +namespace Mvc.JQuery.Datatables.Tests |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class ModelBinding |
| 15 | + { |
| 16 | + [Test] |
| 17 | + public void TestDataTablesIModelBinder() |
| 18 | + { |
| 19 | + Mvc.JQuery.Datatables.Example.RegisterDatatablesModelBinder.Start(); |
| 20 | + IModelBinder dataTablesBinder = ModelBinders.Binders.GetBinder(typeof(DataTablesParam), false); |
| 21 | + Assert.That(dataTablesBinder, Is.Not.Null, "DataTablesParam model binder not found in ModelBindersDictionary"); |
| 22 | + |
| 23 | + var formCollection = new NameValueCollection |
| 24 | + { |
| 25 | + { "sEcho", "1" }, |
| 26 | + { "iColumns", "9" }, |
| 27 | + { "sColumns", "" }, |
| 28 | + { "iDisplayStart", "0" }, |
| 29 | + { "iDisplayLength", "10" }, |
| 30 | + { "mDataProp_0", "0" }, |
| 31 | + { "mDataProp_1", "1" }, |
| 32 | + { "mDataProp_2", "2" }, |
| 33 | + { "mDataProp_3", "3" }, |
| 34 | + { "mDataProp_4", "4" }, |
| 35 | + { "mDataProp_5", "5" }, |
| 36 | + { "mDataProp_6", "6" }, |
| 37 | + { "mDataProp_7", "7" }, |
| 38 | + { "mDataProp_8", "8" }, |
| 39 | + { "sSearch", "" }, |
| 40 | + { "bRegex", "false" }, |
| 41 | + { "sSearch_0", "" }, |
| 42 | + { "sSearch_1", "" }, |
| 43 | + { "sSearch_2", "" }, |
| 44 | + { "sSearch_3", "" }, |
| 45 | + { "sSearch_4", "" }, |
| 46 | + { "sSearch_5", "" }, |
| 47 | + { "sSearch_6", "" }, |
| 48 | + { "sSearch_7", "" }, |
| 49 | + { "sSearch_8", "" }, |
| 50 | + { "bRegex_0", "false" }, |
| 51 | + { "bRegex_1", "false" }, |
| 52 | + { "bRegex_2", "false" }, |
| 53 | + { "bRegex_3", "false" }, |
| 54 | + { "bRegex_4", "false" }, |
| 55 | + { "bRegex_5", "false" }, |
| 56 | + { "bRegex_6", "false" }, |
| 57 | + { "bRegex_7", "false" }, |
| 58 | + { "bRegex_8", "false" }, |
| 59 | + { "bSearchable_0", "true" }, |
| 60 | + { "bSearchable_1", "true" }, |
| 61 | + { "bSearchable_2", "false" }, |
| 62 | + { "bSearchable_3", "true" }, |
| 63 | + { "bSearchable_4", "true" }, |
| 64 | + { "bSearchable_5", "true" }, |
| 65 | + { "bSearchable_6", "true" }, |
| 66 | + { "bSearchable_7", "true" }, |
| 67 | + { "bSearchable_8", "true" }, |
| 68 | + { "iSortCol_0", "0" }, |
| 69 | + { "sSortDir_0", "asc" }, |
| 70 | + { "iSortingCols", "1" }, |
| 71 | + { "bSortable_0", "true" }, |
| 72 | + { "bSortable_1", "true" }, |
| 73 | + { "bSortable_2", "true" }, |
| 74 | + { "bSortable_3", "false" }, |
| 75 | + { "bSortable_4", "true" }, |
| 76 | + { "bSortable_5", "true" }, |
| 77 | + { "bSortable_6", "true" }, |
| 78 | + { "bSortable_7", "true" }, |
| 79 | + { "bSortable_8", "true" } |
| 80 | + }; |
| 81 | + |
| 82 | + var res = SetupAndBind<DataTablesParam>(formCollection, dataTablesBinder); |
| 83 | + Assert.That(res.iColumns, Is.EqualTo(9), "iColumns"); |
| 84 | + Assert.That(res.bEscapeRegex, Is.EqualTo(false),"bEscapeRegex"); |
| 85 | + Assert.That(res.bEscapeRegexColumns, Is.EqualTo(Enumerable.Repeat(false, 9)), "bEscapeRegexColumns"); |
| 86 | + Assert.That(res.bSearchable, Is.EqualTo(Enumerable.Repeat(true, 9).ReplaceAtIndex(false, 2)), "bSearchable"); |
| 87 | + Assert.That(res.bSortable, Is.EqualTo(Enumerable.Repeat(true, 9).ReplaceAtIndex(false, 3)), "bSortable"); |
| 88 | + Assert.That(res.iDisplayLength, Is.EqualTo(10), "iDisplayLength"); |
| 89 | + Assert.That(res.iDisplayStart, Is.EqualTo(0), "iDisplayStart"); |
| 90 | + Assert.That(res.iSortCol, Is.EqualTo(Enumerable.Repeat(0, 9)), "iSortCol"); |
| 91 | + Assert.That(res.iSortingCols, Is.EqualTo(1), "iSortingCols"); |
| 92 | + Assert.That(res.sEcho, Is.EqualTo(1), "sEcho"); |
| 93 | + Assert.That(res.sSearch,Is.EqualTo(""),"sSearch"); |
| 94 | + Assert.That(res.sSearchColumns, Is.EqualTo(Enumerable.Repeat("", 9)), "sSearchColumns"); |
| 95 | + Assert.That(res.sSortDir, Is.EqualTo(Enumerable.Repeat<string>(null, 9).ReplaceAtIndex("asc", 0)), "sSortDir"); |
| 96 | + Assert.That(res.bEscapeRegex, Is.EqualTo(false), "bEscapeRegex"); |
| 97 | + } |
| 98 | + |
| 99 | + //http://www.jamie-dixon.co.uk/unit-testing/unit-testing-your-custom-model-binder/ |
| 100 | + static TModel SetupAndBind<TModel>(NameValueCollection nameValueCollection, IModelBinder modelBinder) |
| 101 | + where TModel : class |
| 102 | + { |
| 103 | + var valueProvider = new NameValueCollectionValueProvider(nameValueCollection, null); |
| 104 | + var modelMetaData = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)); |
| 105 | + var controllerContext = new ControllerContext(); |
| 106 | + var bindingContext = new ModelBindingContext |
| 107 | + { |
| 108 | + ModelName = string.Empty, |
| 109 | + ValueProvider = valueProvider, |
| 110 | + ModelMetadata = modelMetaData, |
| 111 | + }; |
| 112 | + |
| 113 | + return (TModel)modelBinder.BindModel(controllerContext, bindingContext); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments