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 SortConvention_Should_Fail_When_FieldHandlerIsNotRegistered() { // arrange var provider = new QueryableSortProvider( descriptor => { descriptor.AddOperationHandler <QueryableAscendingSortOperationHandler>(); }); var convention = new SortConvention( descriptor => { descriptor.Operation(DefaultSortOperations.Ascending).Name("asc"); descriptor.BindRuntimeType <string, TestEnumType>(); descriptor.Provider(provider); }); var type = new FooSortType(); //act SchemaException?error = Assert.Throws <SchemaException>(() => CreateSchemaWith(type, convention)); Assert.Single(error.Errors); error.Errors[0].Message.MatchSnapshot(); }
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 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 filter = new QueryableSortVisitor( sortType, typeof(Foo)); value.Accept(filter); IQueryable <Foo> aFiltered = filter.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 void Sort_ComparableDesc_ShouldSortByStringAsc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("bar", new EnumValueNode(SortOperationKind.Desc)) ); 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( new(new DefaultTypeConverter()), 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("b", foo.Bar), foo => Assert.Equal("a", foo.Bar) ); }
public void Sort_ComparableDesc_ShouldSortByStringAsc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("bar", new EnumValueNode(SortOperationKind.Desc)) ); FooSortType sortType = CreateType(new FooSortType()); IQueryable <Foo> a = new[] { new Foo { Bar = "b" }, new Foo { Bar = "a" }, new Foo { Bar = "c" } }.AsQueryable(); // act var filter = new QueryableSortVisitor( sortType, typeof(Foo)); value.Accept(filter); ICollection <Foo> aFiltered = filter.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 SortConvention_Should_Work_With_ProviderExtensionsType() { // arrange var provider = new QueryableSortProvider( descriptor => { descriptor.AddFieldHandler <QueryableDefaultSortFieldHandler>(); }); var convention = new SortConvention( descriptor => { descriptor.BindRuntimeType <string, TestEnumType>(); descriptor.Provider(provider); }); var extension1 = new SortConventionExtension( descriptor => { descriptor.Operation(DefaultSortOperations.Ascending).Name("ASC"); descriptor.AddProviderExtension <MockSortProviderExtensionConvention>(); }); IValueNode value = Utf8GraphQLParser.Syntax.ParseValueLiteral("{ bar: ASC}"); var type = new FooSortType(); //act CreateSchemaWith(type, convention, extension1); var executor = new ExecutorBuilder(type); Func <Foo[], Foo[]> func = executor.Build <Foo>(value); // assert Foo[]? a = new[] { new Foo { Bar = "a" }, new Foo { Bar = "b" }, new Foo { Bar = "c" } }; Assert.Collection( func(a), x => Assert.Equal("a", x.Bar), x => Assert.Equal("b", x.Bar), x => Assert.Equal("c", x.Bar)); Foo[]? b = new[] { new Foo { Bar = "c" }, new Foo { Bar = "b" }, new Foo { Bar = "a" } }; Assert.Collection( func(b), x => Assert.Equal("a", x.Bar), x => Assert.Equal("b", x.Bar), x => Assert.Equal("c", x.Bar)); }
public void Ctor_SourceTypeNull_ShouldThrowArgumentNullException() { // arrange FooSortType sortType = CreateType(new FooSortType()); // act Func <QueryableSortVisitorContext> createVisitor = () => new QueryableSortVisitorContext( sortType, null, false); // assert Assert.Throws <ArgumentNullException>(createVisitor); }
public void Sort_ComparableAsc_ShouldSortByStringWithNullableObjectInRootAsc() { // 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 = "c" } }, new Foo { Bar = new Bar { Baz = "b" } }, null }.AsQueryable(); // act var filter = new QueryableSortVisitorInMemory( sortType, typeof(Foo)); value.Accept(filter); ICollection <Foo> aFiltered = filter.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Null(foo), foo => Assert.Equal("b", foo.Bar.Baz), foo => Assert.Equal("c", foo.Bar.Baz) ); }
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 filter = new QueryableSortVisitor( sortType, typeof(Foo)); value.Accept(filter); IQueryable <Foo> aFiltered = filter.Sort(a); // assert Assert.Collection(aFiltered, foo => Assert.Equal("c", foo.Bar), foo => Assert.Equal("a", foo.Bar), foo => Assert.Equal("b", foo.Bar) ); }