Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusjp committed Oct 2, 2019
1 parent 79867e9 commit 762a8c1
Show file tree
Hide file tree
Showing 21 changed files with 441 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/Our.Umbraco.GraphQL/Adapters/GraphTypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ private QueryArgument CreateArgument(ParameterInfo parameterInfo)
IGraphType inputType;
if (unwrappedType == typeof(OrderBy))
{
var returnType = parameterInfo.Member.GetReturnType();
var returnType = parameterInfo.Member.GetReturnType().Unwrap();

if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Connection<>))
returnType = returnType.GenericTypeArguments[0].GetTypeInfo();

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

inputType = new OrderByGraphType((IComplexGraphType) graphType);
_visitor.Visit((EnumerationGraphType) inputType);

if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ContentVariationGraphType : EnumerationGraphType<ContentVariation>
public ContentVariationGraphType()
{
Name = TypeName;
Description = "Indicates how values can vary.";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PublishedItemTypeGraphType : EnumerationGraphType<PublishedItemType
public PublishedItemTypeGraphType()
{
Name = TypeName;
Description = "The type of published element.";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class UrlModeGraphType : EnumerationGraphType<UrlMode>
public UrlModeGraphType()
{
Name = TypeName;
Description = "Specifies the type of urls that the url provider should produce, Auto is the default.";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override void Visit(ISchema schema)
}
}

private void CreateContentField<T>(ObjectGraphType<T> query, IComplexGraphType graphType,
private void CreateContentField<T>(ComplexGraphType<T> query, IComplexGraphType graphType,
IPublishedContentType publishedContentType,
Func<IPublishedContentCache, string, IEnumerable<IPublishedContent>> fetch)
{
Expand All @@ -114,15 +114,18 @@ private void CreateContentField<T>(ObjectGraphType<T> query, IComplexGraphType g
.Bidirectional()
.Resolve(ctx =>
{
var items = fetch(_publishedSnapshotAccessor.PublishedSnapshot.Content, ctx.GetArgument<string>("culture")).ToList();
var items = fetch(_publishedSnapshotAccessor.PublishedSnapshot.Content,
ctx.GetArgument<string>("culture")).ToList();

return items.OrderBy(ctx.GetArgument<IEnumerable<OrderBy>>("orderBy"))
.ToConnection(x => x.Key, ctx.First, ctx.After, ctx.Last, ctx.Before, items.Count);
.ToConnection(x => x.Key, ctx.First, ctx.After, ctx.Last, ctx.Before, items.Count);
});

var connectionField = query.GetField(publishedContentType.Alias);
connectionField.ResolvedType = new ConnectionGraphType(graphType);
connectionField.Arguments.Add(new QueryArgument(new ListGraphType(new NonNullGraphType(new OrderByGraphType(graphType)))) { Name = "orderBy" });
connectionField.Arguments.Add(
new QueryArgument(new ListGraphType(new NonNullGraphType(new OrderByGraphType(graphType))))
{Name = "orderBy"});
}
}
}
1 change: 1 addition & 0 deletions src/Our.Umbraco.GraphQL/Adapters/Types/IdGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public override object ParseValue(object value)
var parsed = base.ParseValue(value);
return parsed == null ? (Id?)null : new Id(parsed.ToString());
}

public override object ParseLiteral(IValue value)
{
var parsed = base.ParseLiteral(value);
Expand Down
2 changes: 1 addition & 1 deletion src/Our.Umbraco.GraphQL/Adapters/Types/UdiGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class UdiGraphType : ScalarGraphType
{
public UdiGraphType()
{
Name = "Udi";
Name = "UDI";
Description = "Represents an entity identifier.";
}

Expand Down
1 change: 1 addition & 0 deletions src/Our.Umbraco.GraphQL/Compose/GraphQLComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Newtonsoft.Json;
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Visitors;
using GraphQL.DataLoader;
using Our.Umbraco.GraphQL.FieldMiddleware;
using Our.Umbraco.GraphQL.Middleware;

namespace Our.Umbraco.GraphQL.Compose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
using System.Web;
using GraphQL.Instrumentation;
using GraphQL.Types;
using Our.Umbraco.GraphQL.Middleware;
using Umbraco.Web;

namespace Our.Umbraco.GraphQL.Middleware
namespace Our.Umbraco.GraphQL.FieldMiddleware
{
internal class EnsureHttpContextFieldMiddleware : IFieldMiddleware
{
Expand Down
38 changes: 38 additions & 0 deletions test/Our.Umbraco.GraphQL.Tests/Adapters/GraphTypeAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NSubstitute;
using Our.Umbraco.GraphQL.Adapters;
using Our.Umbraco.GraphQL.Adapters.Resolvers;
using Our.Umbraco.GraphQL.Adapters.Types;
using Our.Umbraco.GraphQL.Adapters.Types.Relay;
using Our.Umbraco.GraphQL.Adapters.Types.Resolution;
using Our.Umbraco.GraphQL.Adapters.Visitors;
Expand Down Expand Up @@ -981,6 +982,36 @@ public void Adapt_MethodCancellationToken_ArgumentIsNotAdded()
.Which.Arguments.Should().NotContain(x => x.Name == "cancellationToken");
}

[Fact]
public void Adapt_MethodWithOrderByArgument_ArgumentIsAdded()
{
var adapter = CreateSUT();

var graphType = adapter.Adapt<ClassWithName>();

graphType.Should().BeAssignableTo<IObjectGraphType>()
.Which.Fields.Should().Contain(x => x.Name == nameof(ClassWithName.HasOrderByArgument))
.Which.Arguments.Should().Contain(x => x.Name == "orderBy")
.Which.ResolvedType.Should().BeAssignableTo<ListGraphType>()
.Which.ResolvedType.Should().BeAssignableTo<NonNullGraphType>()
.Which.ResolvedType.Should().BeAssignableTo<OrderByGraphType>();
}

[Fact]
public void Adapt_MethodReturnTypeIsConnectionHasOrderByArgument_ArgumentIsAdded()
{
var adapter = CreateSUT();

var graphType = adapter.Adapt<ClassWithName>();

graphType.Should().BeAssignableTo<IObjectGraphType>()
.Which.Fields.Should().Contain(x => x.Name == nameof(ClassWithName.ConnectionWithOrderByArgument))
.Which.Arguments.Should().Contain(x => x.Name == "orderBy")
.Which.ResolvedType.Should().BeAssignableTo<ListGraphType>()
.Which.ResolvedType.Should().BeAssignableTo<NonNullGraphType>()
.Which.ResolvedType.Should().BeAssignableTo<OrderByGraphType>();
}

private abstract class AbstractClassWithoutDescription
{
public string PublicField;
Expand Down Expand Up @@ -1121,6 +1152,12 @@ private class ClassWithName

public Task<IEnumerable<ClassWithDescription>> TaskEnumerableDescriptions() =>
Task.FromResult(Enumerable.Empty<ClassWithDescription>());

public IEnumerable<ClassWithDescription> HasOrderByArgument(IEnumerable<OrderBy> orderBy = null) =>
Enumerable.Empty<ClassWithDescription>();

public Connection<ClassWithDescription> ConnectionWithOrderByArgument(IEnumerable<OrderBy> orderBy = null) =>
null;
}

[Name("MyEnum")]
Expand Down Expand Up @@ -1154,6 +1191,7 @@ private class ClassWithDescription

[Attributes.Description("Property description.")]
public string PropertyWithDescription { get; set; }

[Attributes.Description("Method description.")]
public string MethodWithDescription() => null;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
using Umbraco.Core.Models;
using Xunit;

namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
{
public class ContentVariationTests
{
[Fact]
public void Ctor_SetsName()
{
var sut = new ContentVariationGraphType();

sut.Name.Should().Be("ContentVariation");
}
[Fact]
public void Ctor_SetsDescription()
{
var sut = new ContentVariationGraphType();

sut.Description.Should().Be("Indicates how values can vary.");
}

[Theory]
[InlineData("NOTHING", ContentVariation.Nothing)]
[InlineData("CULTURE", ContentVariation.Culture)]
[InlineData("SEGMENT", ContentVariation.Segment)]
[InlineData("CULTURE_AND_SEGMENT", ContentVariation.CultureAndSegment)]
public void Ctor_AddsFields(string field, ContentVariation value)
{
var sut = new ContentVariationGraphType();

sut.Values.Should().Contain(x => x.Name == field)
.Which.Value.Should().Be(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using GraphQL.Types;
using GraphQL.Types.Relay;
Expand All @@ -8,17 +10,19 @@
using Our.Umbraco.GraphQL.Types;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Xunit;
using IdGraphType = Our.Umbraco.GraphQL.Adapters.Types.IdGraphType;

namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
{
public class PublishedContentGraphTypeTests
{
private PublishedContentGraphType CreateSUT(IContentTypeComposition contentType = null)
private PublishedContentGraphType CreateSUT(IContentTypeComposition contentType = null,
IPublishedContentType publishedContentType = null)
{
return new PublishedContentGraphType(contentType ?? Substitute.For<IContentTypeComposition>(),
Substitute.For<IPublishedContentType>(), new TypeRegistry());
publishedContentType ?? Substitute.For<IPublishedContentType>(), new TypeRegistry());
}

[Theory]
Expand Down Expand Up @@ -131,4 +135,20 @@ public void IdFieldResolver_WhenCalled_ReturnsId()
.Should().Be(new Id(content?.Key.ToString()));
}
}

internal static class PropertyTypeExtensions
{
/// <summary>
/// Set the property type alias without requiring `Current`.
/// </summary>
/// <param name="propertyType"></param>
/// <param name="alias"></param>
/// <returns></returns>
public static PropertyType SetAlias(this PropertyType propertyType, string alias)
{
typeof(PropertyType).GetField("_alias", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(propertyType, alias);
return propertyType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
using Umbraco.Core.Models.PublishedContent;
using Xunit;

namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
{
public class PublishedItemTypeGraphTypeTests
{
[Fact]
public void Ctor_SetsName()
{
var sut = new PublishedItemTypeGraphType();

sut.Name.Should().Be("PublishedItemType");
}
[Fact]
public void Ctor_SetsDescription()
{
var sut = new PublishedItemTypeGraphType();

sut.Description.Should().Be("The type of published element.");
}

[Theory]
[InlineData("CONTENT", PublishedItemType.Content)]
[InlineData("ELEMENT", PublishedItemType.Element)]
[InlineData("MEDIA", PublishedItemType.Media)]
[InlineData("MEMBER", PublishedItemType.Member)]
[InlineData("UNKNOWN", PublishedItemType.Unknown)]
public void Ctor_AddsFields(string field, PublishedItemType value)
{
var sut = new PublishedItemTypeGraphType();

sut.Values.Should().Contain(x => x.Name == field)
.Which.Value.Should().Be(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
using Umbraco.Core.Models.PublishedContent;
using Xunit;

namespace Our.Umbraco.GraphQL.Tests.Adapters.PublishedContent.Types
{
public class UrlModeGraphTypeTests
{
[Fact]
public void Ctor_SetsName()
{
var sut = new UrlModeGraphType();

sut.Name.Should().Be("UrlMode");
}
[Fact]
public void Ctor_SetsDescription()
{
var sut = new UrlModeGraphType();

sut.Description.Should()
.Be("Specifies the type of urls that the url provider should produce, Auto is the default.");
}

[Theory]
[InlineData("ABSOLUTE", UrlMode.Absolute)]
[InlineData("AUTO", UrlMode.Auto)]
[InlineData("DEFAULT", UrlMode.Default)]
[InlineData("RELATIVE", UrlMode.Relative)]
public void Ctor_AddsFields(string field, UrlMode value)
{
var sut = new UrlModeGraphType();

sut.Values.Should().Contain(x => x.Name == field)
.Which.Value.Should().Be(value);
}
}
}
Loading

0 comments on commit 762a8c1

Please sign in to comment.