コード例 #1
0
        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()
                    )
                );
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        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");
        }
コード例 #4
0
        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");
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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");
        }
コード例 #7
0
        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()
                    )
                );
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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());
        }
コード例 #11
0
        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());
        }
コード例 #12
0
        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());
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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");
        }
コード例 #15
0
        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");
        }
コード例 #16
0
        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());
        }
コード例 #17
0
        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");
        }
コード例 #18
0
        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");
        }
コード例 #19
0
ファイル: WebGrid.cs プロジェクト: jesshaw/ASP.NET-Mvc-3
        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;
        }
コード例 #20
0
        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");
        }
コード例 #21
0
        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");
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        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");
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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");
        }