public void WebGridDataSourceReturnsUnsortedListIfSortCannotBeInferred() { // Arrange var values = GetValues(); var dataSource = new WebGridDataSource( new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canPage: false, canSort: true ); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Does-not-exist" }, 0); // Assert Assert.True( Enumerable.SequenceEqual <object>( values.ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer() ) ); }
public void WebGridDataSourceSortsUsingSpecifiedSort() { // Arrange var defaultSort = new SortInfo { SortColumn = "FirstName", SortDirection = SortDirection.Ascending }; IEnumerable <dynamic> values = new[] { new Person { LastName = "Z" }, new Person { LastName = "X" }, new Person { LastName = "Y" } }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: false) { DefaultSort = defaultSort }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "LastName" }, 0); // Assert Assert.Equal(rows.ElementAt(0).Value.LastName, "X"); Assert.Equal(rows.ElementAt(1).Value.LastName, "Y"); Assert.Equal(rows.ElementAt(2).Value.LastName, "Z"); }
public void WebGridDataSourceSortsDictionaryBasedDynamicType() { // Arrange var value1 = new DynamicDictionary(); value1["col"] = "val1"; var value2 = new DynamicDictionary(); value2["col"] = "val2"; var value3 = new DynamicDictionary(); value3["col"] = "val3"; IEnumerable <dynamic> values = new[] { value1, value2, value3 }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(TestDynamicType), canSort: true, canPage: false); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "col", SortDirection = SortDirection.Descending }, 0); // Assert Assert.Equal(rows.ElementAt(0).Value.col, "val3"); Assert.Equal(rows.ElementAt(1).Value.col, "val2"); Assert.Equal(rows.ElementAt(2).Value.col, "val1"); }
public void WebGridDataSourceReturnsPagedSortedResultsIfRowsPerPageAndSortAreSpecified() { // Arrange IEnumerable <dynamic> values = GetValues(); var dataSource = new WebGridDataSource( new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: true ) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows( new SortInfo { SortColumn = "LastName", SortDirection = SortDirection.Descending }, 0 ); // Assert Assert.Equal(2, rows.Count); Assert.Equal(rows.ElementAt(0).Value.LastName, "E2"); Assert.Equal(rows.ElementAt(1).Value.LastName, "D2"); }
public void WebGridDataSourceReturnsOriginalDataSourceIfValuesCannotBeSorted() { // Arrange IEnumerable <dynamic> values = new object[] { new TestDynamicType("col", "val1"), new TestDynamicType("col", "val2"), new TestDynamicType("col", DBNull.Value) }; var dataSource = new WebGridDataSource( new WebGrid(GetContext()), values: values, elementType: typeof(TestDynamicType), canSort: true, canPage: false ); // Act var rows = dataSource.GetRows( new SortInfo { SortColumn = "col", SortDirection = SortDirection.Descending }, 0 ); // Assert Assert.Equal(rows.ElementAt(0).Value.col, "val1"); Assert.Equal(rows.ElementAt(1).Value.col, "val2"); Assert.Equal(rows.ElementAt(2).Value.col, DBNull.Value); }
public void WebGridDataSourceWithNestedPropertySortsCorrectly() { // Arrange var element1 = new { Foo = new { Bar = "val2" } }; var element2 = new { Foo = new { Bar = "val1" } }; var element3 = new { Foo = new { Bar = "val3" } }; IEnumerable <dynamic> values = new[] { element1, element2, element3 }; var dataSource = new WebGridDataSource( new WebGrid(GetContext()), values: values, elementType: element1.GetType(), canSort: true, canPage: false ); // Act var rows = dataSource.GetRows( new SortInfo { SortColumn = "Foo.Bar", SortDirection = SortDirection.Descending }, 0 ); // Assert Assert.Equal(rows.ElementAt(0).Value.Foo.Bar, "val3"); Assert.Equal(rows.ElementAt(1).Value.Foo.Bar, "val2"); Assert.Equal(rows.ElementAt(2).Value.Foo.Bar, "val1"); }
public void WebGridDataSourceUsesDefaultSortWhenCurrentSortCannotBeInferred() { // Arrange var values = GetValues(); var defaultSort = new SortInfo { SortColumn = "FirstName" }; var dataSource = new WebGridDataSource( new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canSort: true, canPage: false ) { DefaultSort = defaultSort }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Does-not-exist" }, 0); // Assert Assert.True( Enumerable.SequenceEqual <object>( values.OrderBy(p => p.FirstName).ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer() ) ); }
public void WebGridDataSourceReturnsNumberOfItemsAsTotalRowCount() { // Arrange var rows = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canPage: false, canSort: false); // Act and Assert Assert.AreEqual(rows.Count(), dataSource.TotalRowCount); }
public void WebGridDataSourceReturnsNumberOfItemsAsTotalRowCount() { // Arrange var rows = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canPage: false, canSort: false); // Act and Assert Assert.Equal(rows.Count(), dataSource.TotalRowCount); }
public void WebGridDataSourceReturnsUnsortedListIfSortCannotBeInferred() { // Arrange var values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canPage: false, canSort: true); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Does-not-exist" }, 0); // Assert CollectionAssert.AreEqual(values.ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer()); }
public void WebGridDataSourceUsesDefaultSortWhenCurrentSortCannotBeInferred() { // Arrange var values = GetValues(); var defaultSort = new SortInfo { SortColumn = "FirstName" }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canSort: true, canPage: false) { DefaultSort = defaultSort }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Does-not-exist" }, 0); // Assert CollectionAssert.AreEqual(values.OrderBy(p => p.FirstName).ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer()); }
public void WebGridDataSourceReturnsUnsortedListIfSortColumnIsEmpty() { // Arrange var values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canPage: false, canSort: true); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = String.Empty }, 0); // Assert CollectionAssert.AreEqual(values.ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer()); }
public void WebGridDataSourceDoesNotThrowIfValuesAreNull() { // Arrange IEnumerable <dynamic> values = new object[] { String.Empty, null, DBNull.Value, null }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(object), canSort: true, canPage: true) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows(new SortInfo(), 0); // Assert Assert.Equal(rows.Count, 2); Assert.Equal(rows.ElementAt(0).Value, String.Empty); Assert.Null(rows.ElementAt(1).Value); }
public void WebGridDataSourceReturnsPagedResultsIfRowsPerPageIsSpecified() { // Arrange IEnumerable <dynamic> values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: false, canPage: true) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows(new SortInfo(), 0); // Assert Assert.Equal(rows.Count, 2); Assert.Equal(rows.ElementAt(0).Value.LastName, "B2"); Assert.Equal(rows.ElementAt(1).Value.LastName, "A2"); }
public void WebGridDataSourceReturnsFewerThanRowsPerPageIfNumberOfItemsIsInsufficient() { // Arrange IEnumerable <dynamic> values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: true) { RowsPerPage = 3 }; // Act var rows = dataSource.GetRows(new SortInfo(), 1); // Assert Assert.Equal(rows.Count, 2); Assert.Equal(rows.ElementAt(0).Value.LastName, "C2"); Assert.Equal(rows.ElementAt(1).Value.LastName, "E2"); }
public void WebGridDataSourceReturnsUnsortedListIfDefaultSortCannotBeInferred() { // Arrange var values = GetValues(); var defaultSort = new SortInfo { SortColumn = "cannot-be-inferred" }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: GetValues(), elementType: typeof(Person), canSort: true, canPage: false) { DefaultSort = defaultSort }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Does-not-exist" }, 0); // Assert CollectionAssert.AreEqual(values.ToList(), rows.Select(r => r.Value).ToList(), new PersonComparer()); }
public void WebGridDataSourceReturnsPagedSortedResultsIfRowsPerPageAndSortAreSpecified() { // Arrange IEnumerable<dynamic> values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: true) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "LastName", SortDirection = SortDirection.Descending }, 0); // Assert Assert.AreEqual(rows.Count, 2); Assert.AreEqual(rows.ElementAt(0).Value.LastName, "E2"); Assert.AreEqual(rows.ElementAt(1).Value.LastName, "D2"); }
public void WebGridDataSourceWithNestedPropertySortsCorrectly() { // Arrange var element1 = new { Foo = new { Bar = "val2" } }; var element2 = new { Foo = new { Bar = "val1" } }; var element3 = new { Foo = new { Bar = "val3" } }; IEnumerable<dynamic> values = new[] { element1, element2, element3 }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: element1.GetType(), canSort: true, canPage: false); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "Foo.Bar", SortDirection = SortDirection.Descending }, 0); // Assert Assert.AreEqual(rows.ElementAt(0).Value.Foo.Bar, "val3"); Assert.AreEqual(rows.ElementAt(1).Value.Foo.Bar, "val2"); Assert.AreEqual(rows.ElementAt(2).Value.Foo.Bar, "val1"); }
public WebGrid Bind(IEnumerable<dynamic> source, IEnumerable<string> columnNames = null, bool autoSortAndPage = true, int rowCount = -1) { if (_dataSourceBound) { throw new InvalidOperationException(HelpersResources.WebGrid_DataSourceBound); } if (source == null) { throw new ArgumentNullException("source"); } if (!autoSortAndPage && _canPage && rowCount == -1) { throw new ArgumentException(HelpersResources.WebGrid_RowCountNotSpecified, "rowCount"); } _elementType = GetElementType(source); if (_columnNames == null) { _columnNames = columnNames ?? GetDefaultColumnNames(source, elementType: _elementType); } if (!autoSortAndPage) { _dataSource = new PreComputedGridDataSource(grid: this, values: source, totalRows: rowCount); } else { WebGridDataSource dataSource = new WebGridDataSource(grid: this, values: source, elementType: _elementType, canPage: _canPage, canSort: _canSort); dataSource.DefaultSort = new SortInfo { SortColumn = _defaultSort, SortDirection = SortDirection.Ascending }; dataSource.RowsPerPage = _rowsPerPage; _dataSource = dataSource; } _dataSourceBound = true; ValidatePreDataBoundValues(); return this; }
public void WebGridDataSourceSortsDictionaryBasedDynamicType() { // Arrange var value1 = new DynamicDictionary(); value1["col"] = "val1"; var value2 = new DynamicDictionary(); value2["col"] = "val2"; var value3 = new DynamicDictionary(); value3["col"] = "val3"; IEnumerable<dynamic> values = new[] { value1, value2, value3 }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(TestDynamicType), canSort: true, canPage: false); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "col", SortDirection = SortDirection.Descending }, 0); // Assert Assert.AreEqual(rows.ElementAt(0).Value.col, "val3"); Assert.AreEqual(rows.ElementAt(1).Value.col, "val2"); Assert.AreEqual(rows.ElementAt(2).Value.col, "val1"); }
public void WebGridDataSourceSortsUsingSpecifiedSort() { // Arrange var defaultSort = new SortInfo { SortColumn = "FirstName", SortDirection = SortDirection.Ascending }; IEnumerable<dynamic> values = new[] { new Person { LastName = "Z" }, new Person { LastName = "X" }, new Person { LastName = "Y" } }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: false) { DefaultSort = defaultSort }; // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "LastName" }, 0); // Assert Assert.AreEqual(rows.ElementAt(0).Value.LastName, "X"); Assert.AreEqual(rows.ElementAt(1).Value.LastName, "Y"); Assert.AreEqual(rows.ElementAt(2).Value.LastName, "Z"); }
public void WebGridDataSourceReturnsOriginalDataSourceIfValuesCannotBeSorted() { // Arrange IEnumerable<dynamic> values = new object[] { new TestDynamicType("col", "val1"), new TestDynamicType("col", "val2"), new TestDynamicType("col", DBNull.Value) }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(TestDynamicType), canSort: true, canPage: false); // Act var rows = dataSource.GetRows(new SortInfo { SortColumn = "col", SortDirection = SortDirection.Descending }, 0); // Assert Assert.AreEqual(rows.ElementAt(0).Value.col, "val1"); Assert.AreEqual(rows.ElementAt(1).Value.col, "val2"); Assert.AreEqual(rows.ElementAt(2).Value.col, DBNull.Value); }
public void WebGridDataSourceReturnsPagedResultsIfRowsPerPageIsSpecified() { // Arrange IEnumerable<dynamic> values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: false, canPage: true) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows(new SortInfo(), 0); // Assert Assert.AreEqual(rows.Count, 2); Assert.AreEqual(rows.ElementAt(0).Value.LastName, "B2"); Assert.AreEqual(rows.ElementAt(1).Value.LastName, "A2"); }
public void WebGridDataSourceDoesNotThrowIfValuesAreNull() { // Arrange IEnumerable<dynamic> values = new object[] { String.Empty, null, DBNull.Value, null }; var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(object), canSort: true, canPage: true) { RowsPerPage = 2 }; // Act var rows = dataSource.GetRows(new SortInfo(), 0); // Assert Assert.AreEqual(rows.Count, 2); Assert.AreEqual(rows.ElementAt(0).Value, String.Empty); Assert.AreEqual(rows.ElementAt(1).Value, null); }
public void WebGridDataSourceReturnsFewerThanRowsPerPageIfNumberOfItemsIsInsufficient() { // Arrange IEnumerable<dynamic> values = GetValues(); var dataSource = new WebGridDataSource(new WebGrid(GetContext()), values: values, elementType: typeof(Person), canSort: true, canPage: true) { RowsPerPage = 3 }; // Act var rows = dataSource.GetRows(new SortInfo(), 1); // Assert Assert.AreEqual(rows.Count, 2); Assert.AreEqual(rows.ElementAt(0).Value.LastName, "C2"); Assert.AreEqual(rows.ElementAt(1).Value.LastName, "E2"); }