-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95a9e03
commit d356b2a
Showing
6 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...tChocolate/Core/test/Types.CursorPagination.Tests/CursorPagingQueryableExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Tests; | ||
using HotChocolate.Types.Pagination.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Snapshooter.Xunit; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Types.Pagination | ||
{ | ||
public class CursorPagingQueryableExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task Queryable_Query_Is_Null() | ||
{ | ||
var mock = new Mock<IResolverContext>(); | ||
|
||
async Task Fail() | ||
=> await default(IQueryable<Person>)!.ApplyCursorPaginationAsync(mock.Object); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>(Fail); | ||
} | ||
|
||
[Fact] | ||
public async Task Queryable_Context_Is_Null() | ||
{ | ||
var mock = new Mock<IQueryable<Person>>(); | ||
|
||
async Task Fail() | ||
=> await mock.Object.ApplyCursorPaginationAsync(default(IResolverContext)!); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>(Fail); | ||
} | ||
|
||
[Fact] | ||
public async Task Queryable_ApplyCursorPaginationAsync_No_Boundaries() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.ExecuteRequestAsync("{ persons { nodes { name } } }") | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Queryable_ApplyCursorPaginationAsync_First_1() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.ExecuteRequestAsync("{ persons(first: 1) { nodes { name } } }") | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Enumerable_Query_Is_Null() | ||
{ | ||
var mock = new Mock<IResolverContext>(); | ||
|
||
async Task Fail() | ||
=> await default(IEnumerable<Person>)!.ApplyCursorPaginationAsync(mock.Object); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>(Fail); | ||
} | ||
|
||
[Fact] | ||
public async Task Enumerable_Context_Is_Null() | ||
{ | ||
var mock = new Mock<IEnumerable<Person>>(); | ||
|
||
async Task Fail() | ||
=> await mock.Object.ApplyCursorPaginationAsync(default(IResolverContext)!); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>(Fail); | ||
} | ||
|
||
[Fact] | ||
public async Task Enumerable_ApplyCursorPaginationAsync_No_Boundaries() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<QueryEnumerable>() | ||
.ExecuteRequestAsync("{ persons { nodes { name } } }") | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Enumerable_ApplyCursorPaginationAsync_First_1() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<QueryEnumerable>() | ||
.ExecuteRequestAsync("{ persons(first: 1) { nodes { name } } }") | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
public class Query | ||
{ | ||
[UsePaging] | ||
public async Task<Connection<Person>> GetPersons( | ||
IResolverContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
var list = new Person[] | ||
{ | ||
new() { Name = "Foo" }, | ||
new() { Name = "Bar" }, | ||
new() { Name = "Baz" }, | ||
new() { Name = "Qux" } | ||
}; | ||
|
||
return await list.AsQueryable().ApplyCursorPaginationAsync( | ||
context, | ||
defaultPageSize: 2, | ||
totalCount: list.Length, | ||
cancellationToken: cancellationToken); | ||
} | ||
} | ||
|
||
public class QueryEnumerable | ||
{ | ||
[UsePaging] | ||
public async Task<Connection<Person>> GetPersons( | ||
IResolverContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
var list = new Person[] | ||
{ | ||
new() { Name = "Foo" }, | ||
new() { Name = "Bar" }, | ||
new() { Name = "Baz" }, | ||
new() { Name = "Qux" } | ||
}; | ||
|
||
return await list.ApplyCursorPaginationAsync( | ||
context, | ||
defaultPageSize: 2, | ||
totalCount: list.Length, | ||
cancellationToken: cancellationToken); | ||
} | ||
} | ||
|
||
public class Person | ||
{ | ||
public string Name { get; set; } | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
..._/CursorPagingQueryableExtensionsTests.Enumerable_ApplyCursorPaginationAsync_First_1.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"data": { | ||
"persons": { | ||
"nodes": [ | ||
{ | ||
"name": "Foo" | ||
} | ||
] | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...orPagingQueryableExtensionsTests.Enumerable_ApplyCursorPaginationAsync_No_Boundaries.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"data": { | ||
"persons": { | ||
"nodes": [ | ||
{ | ||
"name": "Foo" | ||
}, | ||
{ | ||
"name": "Bar" | ||
} | ||
] | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...__/CursorPagingQueryableExtensionsTests.Queryable_ApplyCursorPaginationAsync_First_1.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"data": { | ||
"persons": { | ||
"nodes": [ | ||
{ | ||
"name": "Foo" | ||
} | ||
] | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...sorPagingQueryableExtensionsTests.Queryable_ApplyCursorPaginationAsync_No_Boundaries.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"data": { | ||
"persons": { | ||
"nodes": [ | ||
{ | ||
"name": "Foo" | ||
}, | ||
{ | ||
"name": "Bar" | ||
} | ||
] | ||
} | ||
} | ||
} |