Skip to content

Commit

Permalink
Added more stitching tests (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Aug 6, 2019
1 parent 24fc490 commit da4bfe5
Show file tree
Hide file tree
Showing 21 changed files with 506 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/Core/Types/Utilities/TypeConversion.Setup.cs
Expand Up @@ -17,6 +17,8 @@ public partial class TypeConversion
RegisterStringConversions(registry);
RegisterNameStringConversions(registry);

RegisterByteConversions(registry);

RegisterUInt16Conversions(registry);
RegisterUInt32Conversions(registry);
RegisterUInt64Conversions(registry);
Expand Down Expand Up @@ -120,9 +122,26 @@ public partial class TypeConversion
registry.Register<NameString, string>(from => from);
}

private static void RegisterByteConversions(
ITypeConverterRegistry registry)
{
registry.Register<byte, short>(from => SysConv.ToInt16(from));
registry.Register<byte, int>(from => SysConv.ToInt32(from));
registry.Register<byte, long>(from => SysConv.ToInt64(from));
registry.Register<byte, ushort>(from => SysConv.ToUInt16(from));
registry.Register<byte, uint>(from => SysConv.ToUInt32(from));
registry.Register<byte, ulong>(from => SysConv.ToUInt64(from));
registry.Register<byte, decimal>(from => SysConv.ToDecimal(from));
registry.Register<byte, float>(from => SysConv.ToSingle(from));
registry.Register<byte, double>(from => SysConv.ToDouble(from));
registry.Register<byte, string>(from =>
from.ToString(CultureInfo.InvariantCulture));
}

private static void RegisterUInt16Conversions(
ITypeConverterRegistry registry)
{
registry.Register<ushort, byte>(from => SysConv.ToByte(from));
registry.Register<ushort, short>(from => SysConv.ToInt16(from));
registry.Register<ushort, int>(from => SysConv.ToInt32(from));
registry.Register<ushort, long>(from => SysConv.ToInt64(from));
Expand All @@ -138,6 +157,7 @@ public partial class TypeConversion
private static void RegisterUInt32Conversions(
ITypeConverterRegistry registry)
{
registry.Register<uint, byte>(from => SysConv.ToByte(from));
registry.Register<uint, short>(from => SysConv.ToInt16(from));
registry.Register<uint, int>(from => SysConv.ToInt32(from));
registry.Register<uint, long>(from => SysConv.ToInt64(from));
Expand All @@ -153,6 +173,7 @@ public partial class TypeConversion
private static void RegisterUInt64Conversions(
ITypeConverterRegistry registry)
{
registry.Register<ulong, byte>(from => SysConv.ToByte(from));
registry.Register<ulong, short>(from => SysConv.ToInt16(from));
registry.Register<ulong, int>(from => SysConv.ToInt32(from));
registry.Register<ulong, long>(from => SysConv.ToInt64(from));
Expand All @@ -168,6 +189,7 @@ public partial class TypeConversion
private static void RegisterInt16Conversions(
ITypeConverterRegistry registry)
{
registry.Register<short, byte>(from => SysConv.ToByte(from));
registry.Register<short, int>(from => SysConv.ToInt32(from));
registry.Register<short, long>(from => SysConv.ToInt64(from));
registry.Register<short, ushort>(from => SysConv.ToUInt16(from));
Expand All @@ -183,6 +205,7 @@ public partial class TypeConversion
private static void RegisterInt32Conversions(
ITypeConverterRegistry registry)
{
registry.Register<int, byte>(from => SysConv.ToByte(from));
registry.Register<int, short>(from => SysConv.ToInt16(from));
registry.Register<int, long>(from => SysConv.ToInt64(from));
registry.Register<int, ushort>(from => SysConv.ToUInt16(from));
Expand All @@ -198,6 +221,7 @@ public partial class TypeConversion
private static void RegisterInt64Conversions(
ITypeConverterRegistry registry)
{
registry.Register<long, byte>(from => SysConv.ToByte(from));
registry.Register<long, short>(from => SysConv.ToInt16(from));
registry.Register<long, int>(from => SysConv.ToInt32(from));
registry.Register<long, ushort>(from => SysConv.ToUInt16(from));
Expand All @@ -213,6 +237,7 @@ public partial class TypeConversion
private static void RegisterSingleConversions(
ITypeConverterRegistry registry)
{
registry.Register<float, byte>(from => SysConv.ToByte(from));
registry.Register<float, short>(from => SysConv.ToInt16(from));
registry.Register<float, int>(from => SysConv.ToInt32(from));
registry.Register<float, long>(from => SysConv.ToInt64(from));
Expand All @@ -228,6 +253,7 @@ public partial class TypeConversion
private static void RegisterDoubleConversions(
ITypeConverterRegistry registry)
{
registry.Register<double, byte>(from => SysConv.ToByte(from));
registry.Register<double, short>(from => SysConv.ToInt16(from));
registry.Register<double, int>(from => SysConv.ToInt32(from));
registry.Register<double, long>(from => SysConv.ToInt64(from));
Expand Down
156 changes: 156 additions & 0 deletions src/Stitching/Stitching.Tests/Middleware/ScalarTests.cs
@@ -0,0 +1,156 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Snapshooter.Xunit;
using HotChocolate.Execution;
using HotChocolate.AspNetCore.Tests.Utilities;
using Snapshooter;
using HotChocolate.Types;
using HotChocolate.Language;
using System.Collections.Generic;
using Microsoft.AspNetCore.TestHost;
using HotChocolate.Stitching.Schemas.Contracts;
using HotChocolate.Stitching.Schemas.Customers;
using HotChocolate.AspNetCore;
using HotChocolate.Stitching.Schemas.SpecialCases;
using Moq;

namespace HotChocolate.Stitching
{
public class ScalarTests
: StitchingTestBase
{
public ScalarTests(TestServerFactory testServerFactory)
: base(testServerFactory)
{
}

[InlineData("date_field")]
[InlineData("date_time_field")]
[InlineData("string_field")]
[InlineData("id_field")]
[InlineData("byte_field")]
[InlineData("int_field")]
[InlineData("long_field")]
[InlineData("float_field")]
[InlineData("decimal_field")]
[Theory]
public async Task Scalar_Serializes_And_Deserializes_Correctly(
string fieldName)
{
// arrange
IHttpClientFactory clientFactory = CreateRemoteSchemas();

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(clientFactory);
serviceCollection.AddStitchedSchema(builder => builder
.AddSchemaFromHttp("contract")
.AddSchemaFromHttp("customer"));

IServiceProvider services =
serviceCollection.BuildServiceProvider();

IQueryExecutor executor = services
.GetRequiredService<IQueryExecutor>();
IExecutionResult result = null;

// act
using (IServiceScope scope = services.CreateScope())
{
IReadOnlyQueryRequest request =
QueryRequestBuilder.New()
.SetQuery($@"
query a($contractId: ID!) {{
contract(contractId: $contractId) {{
... on LifeInsuranceContract {{
{fieldName}
}}
}}
}}")
.SetVariableValue(
"contractId",
"TGlmZUluc3VyYW5jZUNvbnRyYWN0LXgx")
.SetServices(scope.ServiceProvider)
.Create();

result = await executor.ExecuteAsync(request);
}

// assert
result.MatchSnapshot(new SnapshotNameExtension(fieldName));
}

[Fact]
public async Task Custom_Scalar_Types()
{
// arrange
IHttpClientFactory clientFactory = CreateRemoteSchemas();

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(clientFactory);
serviceCollection.AddStitchedSchema(builder => builder
.AddSchemaFromHttp("special")
.AddSchemaConfiguration(c =>
c.RegisterType<MyCustomScalarType>()));

IServiceProvider services =
serviceCollection.BuildServiceProvider();

IQueryExecutor executor = services
.GetRequiredService<IQueryExecutor>();
IExecutionResult result = null;

// act
using (IServiceScope scope = services.CreateScope())
{
IReadOnlyQueryRequest request =
QueryRequestBuilder.New()
.SetQuery("{ custom_scalar(bar: \"custom_string\") }")
.SetServices(scope.ServiceProvider)
.Create();

result = await executor.ExecuteAsync(request);
}

// assert
result.MatchSnapshot();
}

protected override IHttpClientFactory CreateRemoteSchemas(
Dictionary<string, HttpClient> connections)
{
TestServer server_contracts = TestServerFactory.Create(
ContractSchemaFactory.ConfigureServices,
app => app.UseGraphQL());

TestServer server_customers = TestServerFactory.Create(
CustomerSchemaFactory.ConfigureServices,
app => app.UseGraphQL());

TestServer server_special = TestServerFactory.Create(
SpecialCasesSchemaFactory.ConfigureServices,
app => app.UseGraphQL());

connections["contract"] = server_contracts.CreateClient();
connections["customer"] = server_customers.CreateClient();
connections["special"] = server_special.CreateClient();

var httpClientFactory = new Mock<IHttpClientFactory>();
httpClientFactory.Setup(t => t.CreateClient(It.IsAny<string>()))
.Returns(new Func<string, HttpClient>(n =>
{
if (connections.ContainsKey(n))
{
return connections[n];
}
throw new Exception();
}));

return httpClientFactory.Object;
}

}
}
Expand Up @@ -52,6 +52,7 @@ protected IHttpClientFactory CreateRemoteSchemas()
throw new Exception();
}));

return httpClientFactory.Object;
}
}
Expand Down
Expand Up @@ -47,9 +47,14 @@ public async Task ListVariableIsCorrectlyPassed()
using (IServiceScope scope = services.CreateScope())
{
IReadOnlyQueryRequest request = QueryRequestBuilder.New()
.SetQuery("query foo ($ids: [ID!]!) { customers(ids: $ids) { id } }")
.SetQuery("query foo ($ids: [ID!]!) " +
"{ customers(ids: $ids) { id } }")
.SetServices(scope.ServiceProvider)
.SetVariableValue("ids", new List<object> { "Q3VzdG9tZXIteDE=", "Q3VzdG9tZXIteDE=" })
.SetVariableValue("ids", new List<object>
{
"Q3VzdG9tZXIteDE=",
"Q3VzdG9tZXIteDE="
})
.Create();

result = await executor.ExecuteAsync(request);
Expand Down
@@ -0,0 +1,8 @@
{
"Data": {
"custom_scalar": "custom_string"
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"byte_field": 123
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"date_field": "2018-05-17"
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"date_time_field": "2018-05-17T08:59:00.000Z"
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"decimal_field": 123.123
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"float_field": 123.123
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"id_field": "abc_123"
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"int_field": 123
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"long_field": 123
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
@@ -0,0 +1,10 @@
{
"Data": {
"contract": {
"string_field": "abc"
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}

0 comments on commit da4bfe5

Please sign in to comment.