Skip to content

Commit 7b987a0

Browse files
committed
Add input converter tests
1 parent f7a09b4 commit 7b987a0

File tree

4 files changed

+88
-33
lines changed

4 files changed

+88
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1-
using GraphQL;
2-
using Newtonsoft.Json;
31
using System;
42
using System.Collections.Generic;
3+
using GraphQL;
4+
using Newtonsoft.Json;
55

6-
namespace Our.Umbraco.GraphQL.Web
6+
namespace Our.Umbraco.GraphQL.Json.Converters
77
{
8-
internal class InputConverter : JsonConverter
8+
internal class InputsConverter : JsonConverter
99
{
10-
public override bool CanConvert(Type objectType)
11-
{
12-
return objectType == typeof(Inputs);
13-
}
10+
public override bool CanConvert(Type objectType) => objectType == typeof(Inputs);
11+
12+
public override bool CanWrite => false;
1413

1514
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
1615
JsonSerializer serializer)
1716
{
18-
19-
if (reader.TokenType == JsonToken.Null)
20-
return null;
21-
2217
var dict = serializer.Deserialize<Dictionary<string, object>>(reader);
23-
2418
return new Inputs(dict ?? new Dictionary<string, object>());
2519
}
26-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
27-
{
20+
21+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) =>
2822
throw new NotSupportedException();
29-
}
3023
}
3124
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Our.Umbraco.GraphQL.Tests")]
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
using GraphQL;
2-
using GraphQL.Conversion;
3-
using GraphQL.Http;
4-
using GraphQL.Utilities;
5-
using LightInject;
6-
using Microsoft.Owin;
7-
using Microsoft.Owin.Cors;
82
using Newtonsoft.Json;
9-
using Owin;
10-
using StackExchange.Profiling;
11-
using System;
12-
using System.Collections.Generic;
13-
using System.IO;
14-
using System.Linq;
15-
using System.Threading.Tasks;
16-
using System.Web.Cors;
17-
using Umbraco.Web;
3+
using Our.Umbraco.GraphQL.Json.Converters;
184

195
namespace Our.Umbraco.GraphQL.Web
206
{
@@ -24,7 +10,7 @@ public class GraphQLQuery
2410
public string OperationName { get; set; }
2511
public string Query { get; set; }
2612

27-
[JsonConverter(typeof(InputConverter))]
13+
[JsonConverter(typeof(InputsConverter))]
2814
public Inputs Variables { get; set; }
2915
}
30-
}
16+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.IO;
3+
using FluentAssertions;
4+
using GraphQL;
5+
using Newtonsoft.Json;
6+
using Our.Umbraco.GraphQL.Json.Converters;
7+
using Xunit;
8+
9+
namespace Our.Umbraco.GraphQL.Tests.Json
10+
{
11+
public class InputConverterTests
12+
{
13+
[Fact]
14+
public void CanConvert_WithInputsType_ReturnsTrue()
15+
{
16+
var converter = new InputsConverter();
17+
18+
converter.CanConvert(typeof(Inputs))
19+
.Should().BeTrue();
20+
}
21+
22+
[Fact]
23+
public void CanConvert_WithNonInputsType_ReturnsFalse()
24+
{
25+
var converter = new InputsConverter();
26+
27+
converter.CanConvert(typeof(string))
28+
.Should().BeFalse();
29+
}
30+
31+
[Fact]
32+
public void CanWrite_ReturnsFalse()
33+
{
34+
var converter = new InputsConverter();
35+
36+
converter.CanWrite.Should().BeFalse();
37+
}
38+
39+
[Fact]
40+
public void WriteJson_ThrowsNotSupportedException()
41+
{
42+
var converter = new InputsConverter();
43+
44+
Action action = () => converter.WriteJson(null, null, null);
45+
action.Should().Throw<NotSupportedException>();
46+
}
47+
48+
[Fact]
49+
public void ReadJson_WithData_ReturnsInputs()
50+
{
51+
var converter = new InputsConverter();
52+
53+
var data = converter.ReadJson(new JsonTextReader(new StringReader("{\"name\": \"test\"}")), typeof(Inputs),
54+
null, new JsonSerializer());
55+
56+
data.Should().BeOfType<Inputs>()
57+
.Which.Should().ContainKey("name")
58+
.WhichValue.Should().Be("test");
59+
}
60+
61+
[Fact]
62+
public void ReadJson_WithNoData_ReturnsEmptyInputs()
63+
{
64+
var converter = new InputsConverter();
65+
66+
var data = converter.ReadJson(new JsonTextReader(new StringReader("null")), typeof(Inputs),
67+
null, new JsonSerializer());
68+
69+
data.Should().BeOfType<Inputs>()
70+
.Which.Should().BeEmpty();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)