mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 09:18:55 +02:00
GraphQL - custom scalar support (#1012)
* x * CustomScalars * more tests * . * add or set * ... * x
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
#if GRAPHQL
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CSScripting;
|
||||
using FluentAssertions;
|
||||
using GraphQLParser.Exceptions;
|
||||
using WireMock.Exceptions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Models;
|
||||
using Xunit;
|
||||
@@ -99,6 +102,62 @@ public class GraphQLMatcherTests
|
||||
matcher.GetPatterns().Should().Contain(TestSchema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_UsingCustomType_Mutation_IsMatch()
|
||||
{
|
||||
// Arrange
|
||||
const string testSchema = @"
|
||||
scalar DateTime
|
||||
scalar MyCustomScalar
|
||||
|
||||
type Message {
|
||||
id: ID!
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createMessage(x: MyCustomScalar, dt: DateTime): Message
|
||||
}";
|
||||
|
||||
var input = @"{
|
||||
""query"": ""mutation CreateMessage($x: MyCustomScalar!, $dt: DateTime!) { createMessage(x: $x, dt: $dt) { id } }"",
|
||||
""variables"": { ""x"": 100, ""dt"": ""2007-12-03T10:15:30Z"" }
|
||||
}";
|
||||
|
||||
var customScalars = new Dictionary<string, Type> { { "MyCustomScalar", typeof(int) } };
|
||||
|
||||
// Act
|
||||
var matcher = new GraphQLMatcher(testSchema, customScalars);
|
||||
var result = matcher.IsMatch(input);
|
||||
|
||||
// Assert
|
||||
result.Score.Should().Be(MatchScores.Perfect);
|
||||
|
||||
matcher.GetPatterns().Should().Contain(testSchema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_UsingCustomType_But_NoDefinedCustomScalars_Mutation_IsNoMatch()
|
||||
{
|
||||
// Arrange
|
||||
const string testSchema = @"
|
||||
scalar DateTime
|
||||
scalar MyCustomScalar
|
||||
|
||||
type Message {
|
||||
id: ID!
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createMessage(x: MyCustomScalar, dt: DateTime): Message
|
||||
}";
|
||||
|
||||
// Act
|
||||
Action action = () => _ = new GraphQLMatcher(testSchema);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<WireMockException>().WithMessage("The GraphQL Scalar type 'MyCustomScalar' is not defined in the CustomScalars dictionary.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GraphQLMatcher_For_ValidSchema_And_IncorrectQuery_IsMismatch()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#if GRAPHQL
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers.Models;
|
||||
|
||||
public class WireMockCustomScalarGraphTypeTests
|
||||
{
|
||||
private class MyIntScalarGraphType : WireMockCustomScalarGraphType<int> { }
|
||||
|
||||
private class MyStringScalarGraphType : WireMockCustomScalarGraphType<string> { }
|
||||
|
||||
[Fact]
|
||||
public void ParseValue_ShouldReturnNull_When_ValueIsNull()
|
||||
{
|
||||
// Arrange
|
||||
var intGraphType = new MyIntScalarGraphType();
|
||||
|
||||
// Act
|
||||
var result = intGraphType.ParseValue(null);
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseValue_ShouldReturnValue_When_ValueIsOfCorrectType()
|
||||
{
|
||||
// Arrange
|
||||
var intGraphType = new MyIntScalarGraphType();
|
||||
|
||||
// Act
|
||||
var result = intGraphType.ParseValue(5);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(5);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("someString")]
|
||||
[InlineData("100")]
|
||||
public void ParseValue_ShouldThrowInvalidCastException_When_ValueIsStringAndTargetIsNotString(string stringValue)
|
||||
{
|
||||
// Arrange
|
||||
var intGraphType = new MyIntScalarGraphType();
|
||||
|
||||
// Act
|
||||
Action act = () => intGraphType.ParseValue(stringValue);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<InvalidCastException>()
|
||||
.WithMessage("Unable to convert value '*' of type 'System.String' to type 'System.Int32'.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseValue_ShouldConvertValue_WhenTypeIsConvertible()
|
||||
{
|
||||
// Arrange
|
||||
var intGraphType = new MyIntScalarGraphType();
|
||||
|
||||
// Act
|
||||
var result = intGraphType.ParseValue(5L);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseValue_ShouldThrowException_When_ValueIsMaxLongAndTargetIsInt()
|
||||
{
|
||||
// Arrange
|
||||
var intGraphType = new MyIntScalarGraphType();
|
||||
|
||||
// Act
|
||||
Action act = () => intGraphType.ParseValue(long.MaxValue);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<OverflowException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseValue_ShouldReturnStringValue_When_TypeIsString()
|
||||
{
|
||||
// Arrange
|
||||
var stringGraphType = new MyStringScalarGraphType();
|
||||
|
||||
// Act
|
||||
var result = stringGraphType.ParseValue("someString");
|
||||
|
||||
// Assert
|
||||
result.Should().Be("someString");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user