forked from ChilliCream/graphql-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
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
deddc2b
commit d0b6041
Showing
5 changed files
with
162 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
17 changes: 17 additions & 0 deletions
17
src/HotChocolate/Core/test/Utilities/HotChocolate.Tests.Utilities.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>HotChocolate.Types.Tests</AssemblyName> | ||
<RootNamespace>HotChocolate</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Core\HotChocolate.Core.csproj" /> | ||
<ProjectReference Include="..\StarWars\HotChocolate.StarWars.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
24 changes: 24 additions & 0 deletions
24
src/HotChocolate/Core/test/Utilities/SnapshotExtensions.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,24 @@ | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using Snapshooter.Xunit; | ||
|
||
namespace HotChocolate.Tests | ||
{ | ||
public static class SnapshotExtensions | ||
{ | ||
public static IExecutionResult MatchSnapshot( | ||
this IExecutionResult result) | ||
{ | ||
result.ToJson(true).MatchSnapshot(); | ||
return result; | ||
} | ||
|
||
public static async Task<IExecutionResult> MatchSnapshotAsync( | ||
this Task<IExecutionResult> task) | ||
{ | ||
IExecutionResult result = await task; | ||
result.ToJson(true).MatchSnapshot(); | ||
return result; | ||
} | ||
} | ||
} |
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,12 @@ | ||
using System; | ||
using HotChocolate.Execution; | ||
|
||
namespace HotChocolate.Tests | ||
{ | ||
public class TestConfiguration | ||
{ | ||
public ISchema? Schema { get; set; } | ||
public IQueryExecutor? Executor { get; set; } | ||
public IServiceProvider? Service { get; set; } | ||
} | ||
} |
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,94 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.Execution; | ||
using HotChocolate.StarWars; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Tests | ||
{ | ||
public static class TestHelper | ||
{ | ||
public static Task<IExecutionResult> ExpectValid(string query) | ||
{ | ||
return ExpectValid(new TestConfiguration(), query); | ||
} | ||
|
||
public static Task<IExecutionResult> ExpectValid(ISchema schema, string query) | ||
{ | ||
return ExpectValid(new TestConfiguration { Schema = schema }, query); | ||
} | ||
|
||
public static async Task<IExecutionResult> ExpectValid( | ||
TestConfiguration? configuration, | ||
string query) | ||
{ | ||
// arrange | ||
configuration = new TestConfiguration(); | ||
configuration.Schema ??= SchemaBuilder.New().AddStarWarsTypes().Create(); | ||
configuration.Executor ??= configuration.Schema.MakeExecutable(); | ||
configuration.Service ??= | ||
new ServiceCollection().AddStarWarsRepositories().BuildServiceProvider(); | ||
|
||
IReadOnlyQueryRequest request = | ||
QueryRequestBuilder.New() | ||
.SetQuery(query) | ||
.SetServices(configuration.Service) | ||
.Create(); | ||
|
||
// act | ||
IExecutionResult result = await configuration.Executor.ExecuteAsync(request, default); | ||
|
||
// assert | ||
Assert.Null(result.Errors); | ||
return result; | ||
} | ||
|
||
public static Task ExpectError( | ||
string query, | ||
params Action<IError>[] elementInspectors) | ||
{ | ||
return ExpectError(new TestConfiguration(), query, elementInspectors); | ||
} | ||
|
||
public static Task ExpectError( | ||
ISchema schema, | ||
string query, | ||
params Action<IError>[] elementInspectors) | ||
{ | ||
return ExpectError(new TestConfiguration { Schema = schema }, query, elementInspectors); | ||
} | ||
|
||
public static async Task ExpectError( | ||
TestConfiguration? configuration, | ||
string query, | ||
params Action<IError>[] elementInspectors) | ||
{ | ||
// arrange | ||
configuration = new TestConfiguration(); | ||
configuration.Schema ??= SchemaBuilder.New().AddStarWarsTypes().Create(); | ||
configuration.Executor ??= configuration.Schema.MakeExecutable(); | ||
configuration.Service ??= | ||
new ServiceCollection().AddStarWarsRepositories().BuildServiceProvider(); | ||
|
||
IReadOnlyQueryRequest request = | ||
QueryRequestBuilder.New() | ||
.SetQuery(query) | ||
.SetServices(configuration.Service) | ||
.Create(); | ||
|
||
// act | ||
IExecutionResult result = await configuration.Executor.ExecuteAsync(request, default); | ||
|
||
// assert | ||
Assert.NotNull(result.Errors); | ||
|
||
if (elementInspectors.Length > 0) | ||
{ | ||
Assert.Collection(result.Errors, elementInspectors); | ||
} | ||
|
||
result.MatchSnapshot(); | ||
} | ||
} | ||
} |