Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fusion] Add tests for various error cases #7006

Merged
merged 9 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Fusion.Tests" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Tests.Shared" />
<InternalsVisibleTo Include="HotChocolate.Fusion.CommandLine" />
<InternalsVisibleTo Include="BananaCakePop.Services.Fusion" />
<InternalsVisibleTo Include="CookieCrumble" />
Expand Down
359 changes: 359 additions & 0 deletions src/HotChocolate/Fusion/test/Core.Tests/AutomaticMockingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
using HotChocolate.Execution;
using HotChocolate.Fusion.Shared;
using Microsoft.Extensions.DependencyInjection;
using static HotChocolate.Fusion.TestHelper;

namespace HotChocolate.Fusion;

public class AutomaticMockingTests
{
[Fact]
public async Task Object()
{
var request = """
query {
obj {
id
str
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
obj: Object
}

type Object {
id: ID!
str: String!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Singular_ById()
{
var request = """
query {
productById(id: "5") {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productById(id: ID!): Product
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Singular_ById_Error()
{
var request = """
query {
productById(id: "5") {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productById(id: ID!): Product @error
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Singular_ById_Null()
{
var request = """
query {
productById(id: "5") {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productById(id: ID!): Product @null
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Plural_ById()
{
var request = """
query {
productsById(ids: ["5", "6"]) {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productsById(ids: [ID!]!): [Product!]!
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Plural_ById_Error()
{
var request = """
query {
productsById(ids: ["5", "6"]) {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productsById(ids: [ID!]!): [Product!] @error
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Plural_ById_Error_At_Index()
{
var request = """
query {
productsById(ids: ["5", "6"]) {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productsById(ids: [ID!]!): [Product] @error(atIndex: 1)
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Plural_ById_Null()
{
var request = """
query {
productsById(ids: ["5", "6"]) {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productsById(ids: [ID!]!): [Product!] @null
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task Plural_ById_Null_At_Index()
{
var request = """
query {
productsById(ids: ["5", "6"]) {
id
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
productsById(ids: [ID!]!): [Product] @null(atIndex: 1)
}

type Product {
id: ID!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task ListOfScalars()
{
var request = """
query {
scalars
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
scalars: [String!]!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task ListOfObjects()
{
var request = """
query {
objs {
id
str
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
objs: [Object!]!
}

type Object {
id: ID!
str: String!
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task ListOfObjects_Property_Error_At_Index()
{
var request = """
query {
objs {
str
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
objs: [Object!]!
}

type Object {
str: String @error(atIndex: 1)
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

[Fact]
public async Task ListOfObjects_Property_Null_At_Index()
{
var request = """
query {
objs {
str
}
}
""";

var result = await ExecuteRequestAgainstSchemaAsync(
"""
type Query {
objs: [Object!]!
}

type Object {
str: String @null(atIndex: 1)
}
""",
request);

MatchMarkdownSnapshot(request, result);
}

private static async Task<IExecutionResult> ExecuteRequestAgainstSchemaAsync(
string schemaText,
string request)
{
var executor = await new ServiceCollection()
.AddGraphQL()
.AddDocumentFromString(schemaText)
.AddResolverMocking()
.AddTestDirectives()
.BuildRequestExecutorAsync();

return await executor.ExecuteAsync(request);
}
}
Loading
Loading