Example #1
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");
        }
        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()
                    )
                );
        }
Example #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");
        }
        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 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());
        }
Example #11
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);
        }
Example #12
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");
        }
Example #13
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");
        }
        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 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 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 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 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 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");
        }
        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");
        }