Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Apr 22, 2022
1 parent 1d6ebbf commit 50e1bcb
Showing 1 changed file with 23 additions and 43 deletions.
66 changes: 23 additions & 43 deletions src/Tests/Execution/GraphQLBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using GraphQL.DI;
using GraphQL.Types;
using Moq;
Expand All @@ -8,71 +7,52 @@

namespace Execution
{
/*
public class GraphQLBuilderTests
{
private readonly Mock<IServiceRegister> _mockServiceRegister = new Mock<IServiceRegister>(MockBehavior.Strict);
private readonly Mock<IGraphQLBuilder> _mockGraphQLBuilder = new Mock<IGraphQLBuilder>(MockBehavior.Strict);
private IGraphQLBuilder _graphQLBuilder => _mockGraphQLBuilder.Object;

public GraphQLBuilderTests()
{
_mockGraphQLBuilder.Setup(x => x.Services).Returns(_mockServiceRegister.Object);
}

[Fact]
public void AddDIGraphTypes()
{
_mockGraphQLBuilder.Setup(x => x.TryRegister(typeof(DIObjectGraphType<>), typeof(DIObjectGraphType<>), ServiceLifetime.Transient)).Returns(_graphQLBuilder).Verifiable();
_mockGraphQLBuilder.Setup(x => x.TryRegister(typeof(DIObjectGraphType<,>), typeof(DIObjectGraphType<,>), ServiceLifetime.Transient)).Returns(_graphQLBuilder).Verifiable();
_mockServiceRegister.Setup(x => x.TryRegister(typeof(DIObjectGraphType<>), typeof(DIObjectGraphType<>), ServiceLifetime.Transient, RegistrationCompareMode.ServiceType)).Returns(_mockServiceRegister.Object).Verifiable();
_mockServiceRegister.Setup(x => x.TryRegister(typeof(DIObjectGraphType<,>), typeof(DIObjectGraphType<,>), ServiceLifetime.Transient, RegistrationCompareMode.ServiceType)).Returns(_mockServiceRegister.Object).Verifiable();
_graphQLBuilder.AddDIGraphTypes().ShouldBe(_graphQLBuilder);
_mockGraphQLBuilder.Verify();
}

[Fact]
public void AddDIClrTypeMappings()
{
var actual = new List<(Type clrType, Type graphType)>();
var existingMappings = new (Type clrType, Type graphType)[] {
(typeof(Class7), typeof(Graph7)),
(typeof(Class8), typeof(Graph8))
};
var mockSchema = new Mock<ISchema>(MockBehavior.Strict);
mockSchema.Setup(x => x.TypeMappings).Returns(existingMappings).Verifiable();
mockSchema.Setup(x => x.RegisterTypeMapping(It.IsAny<Type>(), It.IsAny<Type>()))
.Callback<Type, Type>((clrType, graphType) => actual.Add((clrType, graphType)));
_mockGraphQLBuilder.Setup(x => x.Register(typeof(IConfigureSchema), It.IsAny<IConfigureSchema>(), false))
.Returns<Type, IConfigureSchema, bool>((_, configure, _) => {
configure.Configure(mockSchema.Object, null);
return _mockGraphQLBuilder.Object;
})
.Verifiable();
IGraphTypeMappingProvider mapper = null;
_mockServiceRegister.Setup(x => x.Register(typeof(IGraphTypeMappingProvider), It.IsAny<IGraphTypeMappingProvider>(), false))
.Returns<Type, IGraphTypeMappingProvider, bool>((_, m, _) => {
mapper = m;
return _mockServiceRegister.Object;
});
_graphQLBuilder.AddDIClrTypeMappings();
mockSchema.Verify();
_mockGraphQLBuilder.Verify();
var expected = new List<(Type clrType, Type graphType)> {
(typeof(Class2), typeof(DIObjectGraphType<Base2, Class2>)),
(typeof(Class4), typeof(DIObjectGraphType<Base4, Class4>)),
(typeof(Class8), typeof(DIObjectGraphType<Base8, Class8>)),
};
actual.ShouldBe(actual);
}
mapper.ShouldNotBeNull();

mapper.GetGraphTypeFromClrType(typeof(Class1), false, null).ShouldBe(typeof(DIObjectGraphType<Base1, Class1>));
mapper.GetGraphTypeFromClrType(typeof(Class2), false, null).ShouldBe(typeof(DIObjectGraphType<Base2, Class2>));
mapper.GetGraphTypeFromClrType(typeof(Class3), false, null).ShouldBeNull();

mapper.GetGraphTypeFromClrType(typeof(Class1), false, typeof(Class4)).ShouldBe(typeof(DIObjectGraphType<Base1, Class1>));
mapper.GetGraphTypeFromClrType(typeof(Class2), false, typeof(Class4)).ShouldBe(typeof(DIObjectGraphType<Base2, Class2>));
mapper.GetGraphTypeFromClrType(typeof(Class3), false, typeof(Class4)).ShouldBe(typeof(Class4));
}

private class Class1 { }
private class Class2 { }
private class Class3 { }
private class Class4 { }
private class Class7 { }
private class Class8 { }
private class Base1 : DIObjectGraphBase<Class1> { } //don't register because graph1
private class Base2 : DIObjectGraphBase<Class2> { } //register because graph2 is input
private class Base3 : DIObjectGraphBase<Class3> { } //don't register because graph3
private class Base4 : DIObjectGraphBase<Class4> { } //register because no conflict
private class Base5 : DIObjectGraphBase { } //don't register because object type
private class Base6 : DIObjectGraphBase { } //don't register because object type
private class Base7 : DIObjectGraphBase<Class7> { } //don't register because graph7 was manually registered
private class Base8 : DIObjectGraphBase<Class8> { } //register because graph8 is input
private class Graph1 : ObjectGraphType<Class1> { }
private class Graph2 : InputObjectGraphType<Class2> { }
private class Graph3 : DIObjectGraphType<Base3, Class3> { }
private class Graph5 : DIObjectGraphType<Base5> { }
private class Graph7 : ObjectGraphType { }
private class Graph8 : InputObjectGraphType { }
}
*/
}

0 comments on commit 50e1bcb

Please sign in to comment.