Skip to content

Commit 762a8c1

Browse files
committed
Add more tests
1 parent 79867e9 commit 762a8c1

21 files changed

+441
-23
lines changed

src/Our.Umbraco.GraphQL/Adapters/GraphTypeAdapter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ private QueryArgument CreateArgument(ParameterInfo parameterInfo)
214214
IGraphType inputType;
215215
if (unwrappedType == typeof(OrderBy))
216216
{
217-
var returnType = parameterInfo.Member.GetReturnType();
217+
var returnType = parameterInfo.Member.GetReturnType().Unwrap();
218+
218219
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Connection<>))
219220
returnType = returnType.GenericTypeArguments[0].GetTypeInfo();
220221

@@ -225,6 +226,7 @@ private QueryArgument CreateArgument(ParameterInfo parameterInfo)
225226
: Activator.CreateInstance(foundType);
226227

227228
inputType = new OrderByGraphType((IComplexGraphType) graphType);
229+
_visitor.Visit((EnumerationGraphType) inputType);
228230

229231
if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
230232
{

src/Our.Umbraco.GraphQL/Adapters/PublishedContent/Types/ContentVariationGraphType.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ContentVariationGraphType : EnumerationGraphType<ContentVariation>
1212
public ContentVariationGraphType()
1313
{
1414
Name = TypeName;
15+
Description = "Indicates how values can vary.";
1516
}
1617
}
1718
}

src/Our.Umbraco.GraphQL/Adapters/PublishedContent/Types/PublishedItemTypeGraphType.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class PublishedItemTypeGraphType : EnumerationGraphType<PublishedItemType
1212
public PublishedItemTypeGraphType()
1313
{
1414
Name = TypeName;
15+
Description = "The type of published element.";
1516
}
1617
}
1718
}

src/Our.Umbraco.GraphQL/Adapters/PublishedContent/Types/UrlModeGraphType.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class UrlModeGraphType : EnumerationGraphType<UrlMode>
1212
public UrlModeGraphType()
1313
{
1414
Name = TypeName;
15+
Description = "Specifies the type of urls that the url provider should produce, Auto is the default.";
1516
}
1617
}
1718
}

src/Our.Umbraco.GraphQL/Adapters/PublishedContent/Visitors/PublishedContentVisitor.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public override void Visit(ISchema schema)
105105
}
106106
}
107107

108-
private void CreateContentField<T>(ObjectGraphType<T> query, IComplexGraphType graphType,
108+
private void CreateContentField<T>(ComplexGraphType<T> query, IComplexGraphType graphType,
109109
IPublishedContentType publishedContentType,
110110
Func<IPublishedContentCache, string, IEnumerable<IPublishedContent>> fetch)
111111
{
@@ -114,15 +114,18 @@ private void CreateContentField<T>(ObjectGraphType<T> query, IComplexGraphType g
114114
.Bidirectional()
115115
.Resolve(ctx =>
116116
{
117-
var items = fetch(_publishedSnapshotAccessor.PublishedSnapshot.Content, ctx.GetArgument<string>("culture")).ToList();
117+
var items = fetch(_publishedSnapshotAccessor.PublishedSnapshot.Content,
118+
ctx.GetArgument<string>("culture")).ToList();
118119

119120
return items.OrderBy(ctx.GetArgument<IEnumerable<OrderBy>>("orderBy"))
120-
.ToConnection(x => x.Key, ctx.First, ctx.After, ctx.Last, ctx.Before, items.Count);
121+
.ToConnection(x => x.Key, ctx.First, ctx.After, ctx.Last, ctx.Before, items.Count);
121122
});
122123

123124
var connectionField = query.GetField(publishedContentType.Alias);
124125
connectionField.ResolvedType = new ConnectionGraphType(graphType);
125-
connectionField.Arguments.Add(new QueryArgument(new ListGraphType(new NonNullGraphType(new OrderByGraphType(graphType)))) { Name = "orderBy" });
126+
connectionField.Arguments.Add(
127+
new QueryArgument(new ListGraphType(new NonNullGraphType(new OrderByGraphType(graphType))))
128+
{Name = "orderBy"});
126129
}
127130
}
128131
}

src/Our.Umbraco.GraphQL/Adapters/Types/IdGraphType.cs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public override object ParseValue(object value)
1010
var parsed = base.ParseValue(value);
1111
return parsed == null ? (Id?)null : new Id(parsed.ToString());
1212
}
13+
1314
public override object ParseLiteral(IValue value)
1415
{
1516
var parsed = base.ParseLiteral(value);

src/Our.Umbraco.GraphQL/Adapters/Types/UdiGraphType.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class UdiGraphType : ScalarGraphType
88
{
99
public UdiGraphType()
1010
{
11-
Name = "Udi";
11+
Name = "UDI";
1212
Description = "Represents an entity identifier.";
1313
}
1414

src/Our.Umbraco.GraphQL/Compose/GraphQLComposer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Newtonsoft.Json;
1313
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Visitors;
1414
using GraphQL.DataLoader;
15+
using Our.Umbraco.GraphQL.FieldMiddleware;
1516
using Our.Umbraco.GraphQL.Middleware;
1617

1718
namespace Our.Umbraco.GraphQL.Compose

src/Our.Umbraco.GraphQL/Middleware/EnsureHttpContextFieldMiddleware.cs renamed to src/Our.Umbraco.GraphQL/FieldMiddleware/EnsureHttpContextFieldMiddleware.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
using System.Web;
33
using GraphQL.Instrumentation;
44
using GraphQL.Types;
5+
using Our.Umbraco.GraphQL.Middleware;
56
using Umbraco.Web;
67

7-
namespace Our.Umbraco.GraphQL.Middleware
8+
namespace Our.Umbraco.GraphQL.FieldMiddleware
89
{
910
internal class EnsureHttpContextFieldMiddleware : IFieldMiddleware
1011
{

test/Our.Umbraco.GraphQL.Tests/Adapters/GraphTypeAdapterTests.cs

+38
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using NSubstitute;
1111
using Our.Umbraco.GraphQL.Adapters;
1212
using Our.Umbraco.GraphQL.Adapters.Resolvers;
13+
using Our.Umbraco.GraphQL.Adapters.Types;
1314
using Our.Umbraco.GraphQL.Adapters.Types.Relay;
1415
using Our.Umbraco.GraphQL.Adapters.Types.Resolution;
1516
using Our.Umbraco.GraphQL.Adapters.Visitors;
@@ -981,6 +982,36 @@ public void Adapt_MethodCancellationToken_ArgumentIsNotAdded()
981982
.Which.Arguments.Should().NotContain(x => x.Name == "cancellationToken");
982983
}
983984

985+
[Fact]
986+
public void Adapt_MethodWithOrderByArgument_ArgumentIsAdded()
987+
{
988+
var adapter = CreateSUT();
989+
990+
var graphType = adapter.Adapt<ClassWithName>();
991+
992+
graphType.Should().BeAssignableTo<IObjectGraphType>()
993+
.Which.Fields.Should().Contain(x => x.Name == nameof(ClassWithName.HasOrderByArgument))
994+
.Which.Arguments.Should().Contain(x => x.Name == "orderBy")
995+
.Which.ResolvedType.Should().BeAssignableTo<ListGraphType>()
996+
.Which.ResolvedType.Should().BeAssignableTo<NonNullGraphType>()
997+
.Which.ResolvedType.Should().BeAssignableTo<OrderByGraphType>();
998+
}
999+
1000+
[Fact]
1001+
public void Adapt_MethodReturnTypeIsConnectionHasOrderByArgument_ArgumentIsAdded()
1002+
{
1003+
var adapter = CreateSUT();
1004+
1005+
var graphType = adapter.Adapt<ClassWithName>();
1006+
1007+
graphType.Should().BeAssignableTo<IObjectGraphType>()
1008+
.Which.Fields.Should().Contain(x => x.Name == nameof(ClassWithName.ConnectionWithOrderByArgument))
1009+
.Which.Arguments.Should().Contain(x => x.Name == "orderBy")
1010+
.Which.ResolvedType.Should().BeAssignableTo<ListGraphType>()
1011+
.Which.ResolvedType.Should().BeAssignableTo<NonNullGraphType>()
1012+
.Which.ResolvedType.Should().BeAssignableTo<OrderByGraphType>();
1013+
}
1014+
9841015
private abstract class AbstractClassWithoutDescription
9851016
{
9861017
public string PublicField;
@@ -1121,6 +1152,12 @@ private class ClassWithName
11211152

11221153
public Task<IEnumerable<ClassWithDescription>> TaskEnumerableDescriptions() =>
11231154
Task.FromResult(Enumerable.Empty<ClassWithDescription>());
1155+
1156+
public IEnumerable<ClassWithDescription> HasOrderByArgument(IEnumerable<OrderBy> orderBy = null) =>
1157+
Enumerable.Empty<ClassWithDescription>();
1158+
1159+
public Connection<ClassWithDescription> ConnectionWithOrderByArgument(IEnumerable<OrderBy> orderBy = null) =>
1160+
null;
11241161
}
11251162

11261163
[Name("MyEnum")]
@@ -1154,6 +1191,7 @@ private class ClassWithDescription
11541191

11551192
[Attributes.Description("Property description.")]
11561193
public string PropertyWithDescription { get; set; }
1194+
11571195
[Attributes.Description("Method description.")]
11581196
public string MethodWithDescription() => null;
11591197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using FluentAssertions;
2+
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
3+
using Umbraco.Core.Models;
4+
using Xunit;
5+
6+
namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
7+
{
8+
public class ContentVariationTests
9+
{
10+
[Fact]
11+
public void Ctor_SetsName()
12+
{
13+
var sut = new ContentVariationGraphType();
14+
15+
sut.Name.Should().Be("ContentVariation");
16+
}
17+
[Fact]
18+
public void Ctor_SetsDescription()
19+
{
20+
var sut = new ContentVariationGraphType();
21+
22+
sut.Description.Should().Be("Indicates how values can vary.");
23+
}
24+
25+
[Theory]
26+
[InlineData("NOTHING", ContentVariation.Nothing)]
27+
[InlineData("CULTURE", ContentVariation.Culture)]
28+
[InlineData("SEGMENT", ContentVariation.Segment)]
29+
[InlineData("CULTURE_AND_SEGMENT", ContentVariation.CultureAndSegment)]
30+
public void Ctor_AddsFields(string field, ContentVariation value)
31+
{
32+
var sut = new ContentVariationGraphType();
33+
34+
sut.Values.Should().Contain(x => x.Name == field)
35+
.Which.Value.Should().Be(value);
36+
}
37+
}
38+
}

test/Our.Umbraco.GraphQL.Tests/Adapters/PublishedContent/Types/PublishedContentGraphTypeTests.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Linq;
3+
using System.Reflection;
24
using FluentAssertions;
35
using GraphQL.Types;
46
using GraphQL.Types.Relay;
@@ -8,17 +10,19 @@
810
using Our.Umbraco.GraphQL.Types;
911
using Umbraco.Core.Models;
1012
using Umbraco.Core.Models.PublishedContent;
13+
using Umbraco.Core.PropertyEditors;
1114
using Xunit;
1215
using IdGraphType = Our.Umbraco.GraphQL.Adapters.Types.IdGraphType;
1316

1417
namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
1518
{
1619
public class PublishedContentGraphTypeTests
1720
{
18-
private PublishedContentGraphType CreateSUT(IContentTypeComposition contentType = null)
21+
private PublishedContentGraphType CreateSUT(IContentTypeComposition contentType = null,
22+
IPublishedContentType publishedContentType = null)
1923
{
2024
return new PublishedContentGraphType(contentType ?? Substitute.For<IContentTypeComposition>(),
21-
Substitute.For<IPublishedContentType>(), new TypeRegistry());
25+
publishedContentType ?? Substitute.For<IPublishedContentType>(), new TypeRegistry());
2226
}
2327

2428
[Theory]
@@ -131,4 +135,20 @@ public void IdFieldResolver_WhenCalled_ReturnsId()
131135
.Should().Be(new Id(content?.Key.ToString()));
132136
}
133137
}
138+
139+
internal static class PropertyTypeExtensions
140+
{
141+
/// <summary>
142+
/// Set the property type alias without requiring `Current`.
143+
/// </summary>
144+
/// <param name="propertyType"></param>
145+
/// <param name="alias"></param>
146+
/// <returns></returns>
147+
public static PropertyType SetAlias(this PropertyType propertyType, string alias)
148+
{
149+
typeof(PropertyType).GetField("_alias", BindingFlags.Instance | BindingFlags.NonPublic)
150+
.SetValue(propertyType, alias);
151+
return propertyType;
152+
}
153+
}
134154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using FluentAssertions;
2+
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
3+
using Umbraco.Core.Models.PublishedContent;
4+
using Xunit;
5+
6+
namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
7+
{
8+
public class PublishedItemTypeGraphTypeTests
9+
{
10+
[Fact]
11+
public void Ctor_SetsName()
12+
{
13+
var sut = new PublishedItemTypeGraphType();
14+
15+
sut.Name.Should().Be("PublishedItemType");
16+
}
17+
[Fact]
18+
public void Ctor_SetsDescription()
19+
{
20+
var sut = new PublishedItemTypeGraphType();
21+
22+
sut.Description.Should().Be("The type of published element.");
23+
}
24+
25+
[Theory]
26+
[InlineData("CONTENT", PublishedItemType.Content)]
27+
[InlineData("ELEMENT", PublishedItemType.Element)]
28+
[InlineData("MEDIA", PublishedItemType.Media)]
29+
[InlineData("MEMBER", PublishedItemType.Member)]
30+
[InlineData("UNKNOWN", PublishedItemType.Unknown)]
31+
public void Ctor_AddsFields(string field, PublishedItemType value)
32+
{
33+
var sut = new PublishedItemTypeGraphType();
34+
35+
sut.Values.Should().Contain(x => x.Name == field)
36+
.Which.Value.Should().Be(value);
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using FluentAssertions;
2+
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
3+
using Umbraco.Core.Models.PublishedContent;
4+
using Xunit;
5+
6+
namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
7+
{
8+
public class UrlModeGraphTypeTests
9+
{
10+
[Fact]
11+
public void Ctor_SetsName()
12+
{
13+
var sut = new UrlModeGraphType();
14+
15+
sut.Name.Should().Be("UrlMode");
16+
}
17+
[Fact]
18+
public void Ctor_SetsDescription()
19+
{
20+
var sut = new UrlModeGraphType();
21+
22+
sut.Description.Should()
23+
.Be("Specifies the type of urls that the url provider should produce, Auto is the default.");
24+
}
25+
26+
[Theory]
27+
[InlineData("ABSOLUTE", UrlMode.Absolute)]
28+
[InlineData("AUTO", UrlMode.Auto)]
29+
[InlineData("DEFAULT", UrlMode.Default)]
30+
[InlineData("RELATIVE", UrlMode.Relative)]
31+
public void Ctor_AddsFields(string field, UrlMode value)
32+
{
33+
var sut = new UrlModeGraphType();
34+
35+
sut.Values.Should().Contain(x => x.Name == field)
36+
.Which.Value.Should().Be(value);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)