Skip to content

Commit

Permalink
Added sorting nullable property tests (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored and michaelstaib committed Jan 27, 2020
1 parent 9dde8e1 commit 30221a5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Core/Types.Sorting.Tests/QueryableExtensionsTests.cs
Expand Up @@ -24,6 +24,7 @@ public void AddInitialSortOperation_AscOnIQueryable_ShouldAddOrderBy()
// assert
Assert.Equal(source.OrderBy(s => s.Bar), sorted);
}

[Fact]
public void AddInitialSortOperation_DescOnIQueryable_ShouldAddOrderBy()
{
Expand Down
82 changes: 69 additions & 13 deletions src/Core/Types.Sorting.Tests/QueryableSortVisitorTests.cs
Expand Up @@ -31,12 +31,9 @@ public void Sort_ComparablemMultiple_ShouldSortByStringAscThenByStringAsc()
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("baz",
new EnumValueNode(SortOperationKind.Asc)
),
new EnumValueNode(SortOperationKind.Asc)),
new ObjectFieldNode("bar",
new EnumValueNode(SortOperationKind.Asc)
)
);
new EnumValueNode(SortOperationKind.Asc)));

FooSortType sortType = CreateType(new FooSortType());

Expand Down Expand Up @@ -68,11 +65,9 @@ public void Sort_ObjectMultiple_ShouldSortByStringAscThenByStringAsc()
var value = new ObjectValueNode(new ObjectFieldNode("foo",
new ObjectValueNode(
new ObjectFieldNode("baz",
new EnumValueNode(SortOperationKind.Asc)
),
new EnumValueNode(SortOperationKind.Asc)),
new ObjectFieldNode("bar",
new EnumValueNode(SortOperationKind.Asc)
))));
new EnumValueNode(SortOperationKind.Asc)))));

FooNestedSortType sortType = CreateType(new FooNestedSortType());

Expand Down Expand Up @@ -134,8 +129,7 @@ public void Sort_ComparableAsc_ShouldSortByStringAsc()
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("bar",
new EnumValueNode(SortOperationKind.Asc)
)
new EnumValueNode(SortOperationKind.Asc))
);

FooSortType sortType = CreateType(new FooSortType());
Expand Down Expand Up @@ -165,8 +159,7 @@ public void Sort_ComparableDesc_ShouldSortByStringAsc()
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("bar",
new EnumValueNode(SortOperationKind.Desc)
)
new EnumValueNode(SortOperationKind.Desc))
);

FooSortType sortType = CreateType(new FooSortType());
Expand Down Expand Up @@ -218,6 +211,68 @@ public void Sort_NoSortSpecified_ShouldReturnUnalteredSource()
);
}

[Fact]
public void Sort_Nullable_ShouldSortNullableProperlyAsc()
{
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("nullableInt",
new EnumValueNode(SortOperationKind.Asc)));

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("b", foo.Bar),
foo => Assert.Equal("a", foo.Bar),
foo => Assert.Equal("c", foo.Bar)
);
}

[Fact]
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)
);
}

public class FooSortType
: SortInputType<Foo>
{
Expand Down Expand Up @@ -245,6 +300,7 @@ public class FooNested

public class Foo
{
public int? NullableInt { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Expand Down

0 comments on commit 30221a5

Please sign in to comment.