Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions GraphQL-Core.Tests/GraphQL-Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>GraphQL_Core.Tests</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GraphQL-Core\GraphQL-Core.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions GraphQL-Core.Tests/Initialize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using GraphQL;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace GraphQL_Core.Tests
{
[TestClass]
public class Initializer
{
public IServiceCollection services { get; set; }
public ServiceProvider provider { get; set; }
public IDependencyResolver resolver { get; set; }

[TestInitialize()]
public void Init()
{
services = new ServiceCollection();
provider = services.BuildServiceProvider();
resolver = new FuncDependencyResolver(type =>
{
var service = services.Where(svc => svc.ServiceType == type).FirstOrDefault();
if (service is null || service.ImplementationInstance is null)
{
return provider.GetService(type);
}

return service.ImplementationInstance;
});
}
}
}
18 changes: 18 additions & 0 deletions GraphQL-Core.Tests/Models/Enum/AuthorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GraphQL_Core.Tests.Models.Enum
{
public enum AuthorType
{
INTERNATIONAL,
BESTSELLING,
REGIONAL,
LOCAL,
POLITICAL,
NEWS,
BLOGGER,
PROFESSOR
}
}
22 changes: 22 additions & 0 deletions GraphQL-Core.Tests/Models/Enum/BookType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GraphQL_Core.Tests.Models.Enum
{
public enum BookType
{
HORROR,
COMEDY,
DRAMA,
ROMANCE,
ADVENTURE,
MYSTERY,
ACTION,
FANTASY,
GRAPHIC,
CHILDREN,
YOUNGADULT,
ADULT
}
}
54 changes: 54 additions & 0 deletions GraphQL-Core.Tests/Models/UserType/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using GraphQL_Core.Tests.Models.Enum;
using System;
using System.Collections.Generic;
using System.Text;

namespace GraphQL_Core.Tests.Models
{
/// <summary>
/// Contains data types supported by GraphQL-dotnet
/// </summary>
public class Author
{
public long AuthorId { get; set; }
public string Name { get; set; }
public int PhoneNumber { get; set; }
public bool IsAlive { get; set; }
public float Rating { get; set; }
public double NetWorth { get; set; }
}

public struct AuthorDescription
{
public string History { get; set; }
}

/// <summary>
/// Extends the Author class by adding all C# value types
/// <para>
/// <see cref="!:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types"></see>
/// </para>
/// </summary>
public class Author_WithValueTypes : Author
{
public Guid Identifier { get; set; }
public char Gender { get; set; }
public decimal Dewey { get; set; }
public byte BooksWritten { get; set; }
public sbyte Modifier { get; set; }
public short ChildrenCount { get; set; }
public uint Age { get; set; }
public ulong PagesWritten { get; set; }
public ushort ParentCount { get; set; }
}

public class Author_WithValueTypesAndStruct : Author_WithValueTypes
{
public AuthorDescription Info { get; set; }
}

public class Author_WithEnumTypes : Author
{
public AuthorType Type { get; set; }
}
}
68 changes: 68 additions & 0 deletions GraphQL-Core.Tests/Models/UserType/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using GraphQL_Core.Tests.Models.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphQL_Core.Tests.Models
{
/// <summary>
/// Contains data types supported by GraphQL-dotnet
/// </summary>
public class Book
{
public long BookId { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public bool IsAvailable { get; set; }
public float Rating { get; set; }
public double Cost { get; set; }
}

public struct BookDescription
{
public string Blurb { get; set; }
}

/// <summary>
/// Extends the Book class by adding all C# value types
/// <para>
/// <see cref="!:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types"></see>
/// </para>
/// </summary>
public class Book_WithValueTypes : Book
{
public Guid Identifier { get; set; }
public char Category { get; set; }
public decimal Dewey { get; set; }
public byte OwnerCount { get; set; }
public sbyte Modifier { get; set; }
public short Value { get; set; }
public uint Copies { get; set; }
public ulong Pages { get; set; }
public ushort AuthorCount { get; set; }
}

public class Book_WithValueTypesAndStruct : Book_WithValueTypes
{
public BookDescription Info { get; set; }
}

public class Book_WithEnumerables : Book
{
public List<int> OwnerIds { get; set; }
public IList<float> SiteRatings { get; set; }
public IQueryable<double> AvgSalesPerMonth { get; set; }
public IEnumerable<bool> AreFavorited { get; set; }
}

public class Book_WithAdvancedEnumerables : Book_WithEnumerables
{
public List<IEnumerable<IQueryable<int>>> Fans { get; set; }
}

public class Book_WithEnumTypes : Book
{
public BookType Type { get; set; }
}
}
22 changes: 22 additions & 0 deletions GraphQL-Core.Tests/StitchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GraphQLCore;
using GraphQL_Core.Tests.Models;

namespace GraphQL_Core.Tests
{
[TestClass]
public class StitchTests
{
public Initializer initializer { get; set; } = new Initializer();

[TestMethod]
[TestCategory("Stitch")]
public void Test_StitchUserModels_ParseField()
{
initializer.Init();
initializer.services.AddGraphQL()
.Type<Book>()
.Build();
}
}
}
142 changes: 142 additions & 0 deletions GraphQL-Core.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GraphQLCore;
using GraphQL_Core.Tests.Models;
using GraphQLCore.Types;
using GraphQLCore.Queries;
using System;
using GraphQLCore.Resolvers;
using System.Reflection;
using GraphQLCore.GraphQL;

namespace GraphQL_Core.Tests
{
[TestClass]
public class TypeTests
{
public Initializer initializer { get; set; } = new Initializer();

[TestMethod]
[TestCategory("Types")]
[DataTestMethod()]
[DataRow(typeof(Book))]
[DataRow(typeof(Book_WithValueTypes))]
[DataRow(typeof(Book_WithEnumTypes))]
[DataRow(typeof(Book_WithEnumerables))]
[DataRow(typeof(Book_WithAdvancedEnumerables))]
[DataRow(typeof(Author))]
[DataRow(typeof(Author_WithValueTypes))]
[DataRow(typeof(Author_WithEnumTypes))]
public void Test_AddType_HasDynamicallyCreatedModel(Type T)
{
var testMethod = this.GetType().GetMethod("Generic_Test_AddType_HasDynamicallyCreatedModel");
testMethod.MakeGenericMethod(T).Invoke(this, null);
}

[TestMethod]
[TestCategory("Types")]
[DataTestMethod()]
[DataRow(typeof(Book))]
[DataRow(typeof(Book_WithValueTypes))]
[DataRow(typeof(Book_WithEnumTypes))]
[DataRow(typeof(Book_WithEnumerables))]
[DataRow(typeof(Book_WithAdvancedEnumerables))]
[DataRow(typeof(Author))]
[DataRow(typeof(Author_WithValueTypes))]
[DataRow(typeof(Author_WithEnumTypes))]
public void Test_AddType_HasFields(Type T)
{
var testMethod = this.GetType().GetMethod("Generic_Test_AddType_HasFields");
testMethod.MakeGenericMethod(T).Invoke(this, null);
}

[TestMethod]
[TestCategory("Types")]
[DataTestMethod()]
[DataRow(typeof(Book))]
[DataRow(typeof(Book_WithValueTypes))]
[DataRow(typeof(Book_WithEnumTypes))]
[DataRow(typeof(Book_WithEnumerables))]
[DataRow(typeof(Book_WithAdvancedEnumerables))]
[DataRow(typeof(Author))]
[DataRow(typeof(Author_WithValueTypes))]
[DataRow(typeof(Author_WithEnumTypes))]
public void Test_AddType_IsQueryable(Type T)
{
var testMethod = this.GetType().GetMethod("Generic_Test_AddType_IsQueryable");
testMethod.MakeGenericMethod(T).Invoke(this, null);
}

public void Generic_Test_AddType_HasFields<T>()
{
initializer.Init();
initializer.services.AddGraphQL()
.Type<T>()
.Build();

var dynamicallyCreatedClass = GraphQLCoreTypeWrapperGenerator.GetDerivedGenericUserType<GenericType<T>>();

Assert.IsNotNull(dynamicallyCreatedClass);

var userModelInstance = initializer.resolver.Resolve(dynamicallyCreatedClass) as GenericType<T>;

Assert.IsNotNull(userModelInstance);

foreach (var field in typeof(T).GetProperties())
{
Assert.IsTrue(userModelInstance.HasField(field.Name));
}
}

public void Generic_Test_AddType_HasDynamicallyCreatedModel<T>()
{
initializer.Init();
initializer.services.AddGraphQL()
.Type<T>()
.Build();

var dynamicallyCreatedClass = GraphQLCoreTypeWrapperGenerator.GetDerivedGenericUserType<GenericType<T>>();

Assert.IsNotNull(dynamicallyCreatedClass);
}

public void Generic_Test_AddType_IsQueryable<T>()
{
initializer.Init();
var builder = initializer.services.AddGraphQL()
.Type<T>();

foreach (var field in typeof(T).GetProperties())
{
GraphQLQuery defaultQuery =
() => new Query() {
Expression = $"get_{field.Name}",
Resolver = (context) => Activator.CreateInstance(field.PropertyType)
};

var methodInfo = builder
.GetType()
.GetInterface("IGraphQLBuilder")
.GetMethod("Query");

methodInfo
.MakeGenericMethod(field.PropertyType)
.Invoke(
builder,
new object[] { defaultQuery });
}

builder.Build();

var dynamicallyCreatedClass = GraphQLCoreTypeWrapperGenerator.GetDerivedGenericUserType<GenericType<T>>();

Assert.IsNotNull(dynamicallyCreatedClass);

var userModelInstance = initializer.resolver.Resolve(dynamicallyCreatedClass) as GenericType<T>;

foreach (var field in typeof(T).GetProperties())
{
Assert.IsTrue(userModelInstance.HasField(field.Name));
}
}
}
}
Loading