public void Sort_Nullable_ShouldSortNullableProperlyDesc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("nullableInt", new EnumValueNode(SortOperationKind.Desc))); FooSortType sortType = CreateType(new FooSortType()); IQueryable <Foo> a = new[] { new Foo { Bar = "b" }, new Foo { Bar = "c", NullableInt = 2 }, new Foo { Bar = "a", NullableInt = 1 } }.AsQueryable(); // act var context = new QueryableSortVisitorContext(sortType, typeof(Foo), false); QueryableSortVisitor.Default.Visit(value, context); ICollection <Foo> aFiltered = context.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Equal("c", foo.Bar), foo => Assert.Equal("a", foo.Bar), foo => Assert.Equal("b", foo.Bar) ); }
public void Sort_Inheritance_ShouldSortByStringAsc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("bar", new EnumValueNode(SortOperationKind.Desc))); SortInputType <FooInherited> sortType = CreateType(new SortInputType <FooInherited>()); IQueryable <FooInherited> a = new[] { new FooInherited { Bar = "b" }, new FooInherited { Bar = "a" }, new FooInherited { Bar = "c" } }.AsQueryable(); // act var context = new QueryableSortVisitorContext(sortType, typeof(FooInherited), false); QueryableSortVisitor.Default.Visit(value, context); ICollection <FooInherited> aFiltered = context.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Equal("c", foo.Bar), foo => Assert.Equal("b", foo.Bar), foo => Assert.Equal("a", foo.Bar) ); }
public void Sort_NoSortSpecified_ShouldReturnUnalteredSource() { // arrange var value = new ObjectValueNode(); FooSortType sortType = CreateType(new FooSortType()); IQueryable <Foo> a = new[] { new Foo { Bar = "b" }, new Foo { Bar = "a" }, new Foo { Bar = "c" } }.AsQueryable(); // act var context = new QueryableSortVisitorContext(sortType, typeof(Foo), false); QueryableSortVisitor.Default.Visit(value, context); IQueryable <Foo> aFiltered = context.Sort(a); // assert Assert.Same(a, aFiltered); Assert.Collection(aFiltered, foo => Assert.Equal("b", foo.Bar), foo => Assert.Equal("a", foo.Bar), foo => Assert.Equal("c", foo.Bar) ); }
public static SortOperationInvocation CreateSortOperation( this QueryableSortVisitorContext context, SortOperationKind kind) { if (context.InMemory) { return(context.Closure.CreateInMemorySortOperation(kind)); } return(context.Closure.CreateSortOperation(kind)); }
public static IQueryable <TSource> Sort <TSource>( this QueryableSortVisitorContext context, IQueryable <TSource> source) { if (context.SortOperations.Count == 0) { return(source); } return(source.Provider.CreateQuery <TSource>(context.Compile(source.Expression))); }
public void Sort_ComparableAsc_ShouldSortByStringNullableAsc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("bar", new ObjectValueNode( new ObjectFieldNode("baz", new EnumValueNode(SortOperationKind.Asc) ) ) )); FooSortType sortType = CreateType(new FooSortType()); IQueryable <Foo> a = new[] { new Foo { Bar = new Bar { Baz = "b" } }, new Foo { Bar = new Bar { Baz = null } }, new Foo { Bar = new Bar { Baz = "c" } } }.AsQueryable(); // act var context = new QueryableSortVisitorContext(sortType, typeof(Foo), false); QueryableSortVisitor.Default.Visit(value, context); ICollection <Foo> aFiltered = context.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Null(foo.Bar.Baz), foo => Assert.Equal("b", foo.Bar.Baz), foo => Assert.Equal("c", foo.Bar.Baz) ); }
public void Sort_ObjectMultiple_ShouldSortByStringAscThenByStringAsc() { // arrange var value = new ObjectValueNode(new ObjectFieldNode("foo", new ObjectValueNode( new ObjectFieldNode("baz", new EnumValueNode(SortOperationKind.Asc)), new ObjectFieldNode("bar", new EnumValueNode(SortOperationKind.Asc))))); FooNestedSortType sortType = CreateType(new FooNestedSortType()); IQueryable <FooNested> a = new[] { new FooNested { Foo = new Foo { Bar = "b", Baz = "b" } }, new FooNested { Foo = new Foo { Bar = "a", Baz = "b" } }, new FooNested { Foo = new Foo { Bar = "c", Baz = "a" } } }.AsQueryable(); // act var context = new QueryableSortVisitorContext(sortType, typeof(FooNested), false); QueryableSortVisitor.Default.Visit(value, context); ICollection <FooNested> aFiltered = context.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Equal("c", foo.Foo.Bar), foo => Assert.Equal("a", foo.Foo.Bar), foo => Assert.Equal("b", foo.Foo.Bar) ); }
public async Task InvokeAsync(IMiddlewareContext context) { await _next(context).ConfigureAwait(false); IValueNode sortArg = context.ArgumentLiteral <IValueNode>(_contextData.ArgumentName); if (sortArg is NullValueNode) { return; } IQueryable <T>?source = null; if (context.Result is IQueryable <T> q) { source = q; } else if (context.Result is IEnumerable <T> e) { source = e.AsQueryable(); } if (source is not null && context.Field.Arguments[_contextData.ArgumentName].Type is InputObjectType iot && iot is ISortInputType { EntityType : not null } fit) { var visitorCtx = new QueryableSortVisitorContext( context.Service <InputParser>(), iot, fit.EntityType, source is EnumerableQuery); QueryableSortVisitor.Default.Visit(sortArg, visitorCtx); source = visitorCtx.Sort(source); context.Result = source; } }
public static Expression Compile( this QueryableSortVisitorContext context, Expression source) { if (context.SortOperations.Count == 0) { return(source); } if (!OrderingMethodFinder.OrderMethodExists(source)) { source = source.CompileInitialSortOperation( context.SortOperations.Dequeue()); } while (context.SortOperations.Count != 0) { source = source.CompileSortOperation( context.SortOperations.Dequeue()); } return(source); }