Skip to content

Commit

Permalink
Added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Klish committed May 6, 2020
1 parent 8f59746 commit 6a79c31
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 6 deletions.
5 changes: 5 additions & 0 deletions elide-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>elide-test-helpers</artifactId>
<version>5.0.0-pr9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-example-models</artifactId>
<version>5.0.0-pr9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.graphql;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.yahoo.elide.ElideSettings;
import com.yahoo.elide.ElideSettingsBuilder;
import com.yahoo.elide.core.filter.dialect.RSQLFilterDialect;
import com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker;
import com.yahoo.elide.graphql.parser.GraphQLProjectionInfo;
import com.yahoo.elide.request.Attribute;
import com.yahoo.elide.request.EntityProjection;
import example.models.inheritance.Character;
import example.models.inheritance.Droid;
import org.junit.jupiter.api.Test;

import java.util.TimeZone;

public class GraphQLEntityProjectionMakerTest extends GraphQLTest {
ElideSettings settings;

public GraphQLEntityProjectionMakerTest() {
RSQLFilterDialect filterDialect = new RSQLFilterDialect(dictionary);

settings = new ElideSettingsBuilder(null)
.withEntityDictionary(dictionary)
.withJoinFilterDialect(filterDialect)
.withSubqueryFilterDialect(filterDialect)
.withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"))
.build();
}

@Test
public void testInlineFragment() {
GraphQLEntityProjectionMaker maker = new GraphQLEntityProjectionMaker(settings);

String query = "{ character { edges { node { "
+ "__typename ... on Character { name } "
+ "__typename ... on Droid { primaryFunction }}}}}";

GraphQLProjectionInfo info = maker.make(query);

EntityProjection projection = info.getProjection("", "character");

Attribute nameAttribute = projection.getAttributes().stream()
.filter(attr -> attr.getName().equals("name"))
.findFirst()
.orElseThrow(() -> new IllegalStateException());

assertEquals(Character.class, nameAttribute.getParentType());

Attribute primaryFunctionAttribute = projection.getAttributes().stream()
.filter(attr -> attr.getName().equals("primaryFunction"))
.findFirst()
.orElseThrow(() -> new IllegalStateException());

assertEquals(Droid.class, primaryFunctionAttribute.getParentType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.graphql;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.yahoo.elide.core.EntityDictionary;
import example.models.inheritance.Character;
import example.models.inheritance.Droid;
import example.models.inheritance.Hero;
import org.junit.jupiter.api.Test;

import java.util.Collections;

public class GraphQLNameUtilsTest {

@Test
public void testBoundNameMapping() {
EntityDictionary dictionary = new EntityDictionary(Collections.EMPTY_MAP);

dictionary.bindEntity(Droid.class);
dictionary.bindEntity(Hero.class);
dictionary.bindEntity(Character.class);

GraphQLNameUtils nameUtils = new GraphQLNameUtils(dictionary);

assertEquals("droid", nameUtils.toBoundName("Droid"));
assertEquals("hero", nameUtils.toBoundName("Hero"));
assertEquals("character", nameUtils.toBoundName("Character"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import example.Book;
import example.Pseudonym;
import example.Publisher;
import example.models.inheritance.Character;
import example.models.inheritance.Droid;
import example.models.inheritance.Hero;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,5 +38,8 @@ public GraphQLTest() {
dictionary.bindEntity(Publisher.class);
dictionary.bindEntity(Pseudonym.class);
dictionary.bindEntity(Address.class);
dictionary.bindEntity(Hero.class);
dictionary.bindEntity(Droid.class);
dictionary.bindEntity(Character.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@

import com.yahoo.elide.core.ArgumentType;
import com.yahoo.elide.core.EntityDictionary;

import com.yahoo.elide.request.Sorting;

import example.Author;
import example.Book;
import example.Publisher;

import example.models.inheritance.Character;
import example.models.inheritance.Droid;
import example.models.inheritance.Hero;
import org.junit.jupiter.api.Test;

import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
Expand Down Expand Up @@ -57,6 +61,9 @@ public class ModelBuilderTest {
private static final String PAGE_INFO = "pageInfo";

private static final String TYPE_QUERY = "Query";
private static final String TYPE_CHARACTER = "Character";
private static final String TYPE_DROID = "Droid";
private static final String TYPE_HERO = "Hero";
private static final String TYPE_BOOK_CONNECTION = "BookConnection";
private static final String TYPE_BOOK_INPUT = "BookInput";
private static final String TYPE_BOOK = "Book";
Expand Down Expand Up @@ -92,6 +99,9 @@ public ModelBuilderTest() {
dictionary.bindEntity(Book.class);
dictionary.bindEntity(Author.class);
dictionary.bindEntity(Publisher.class);
dictionary.bindEntity(Droid.class);
dictionary.bindEntity(Hero.class);
dictionary.bindEntity(Character.class);
}

@Test
Expand Down Expand Up @@ -148,11 +158,11 @@ public void testBuild() {

GraphQLSchema schema = builder.build();

assertNotEquals(schema.getType(TYPE_AUTHOR_CONNECTION), null);
assertNotEquals(schema.getType(TYPE_BOOK_CONNECTION), null);
assertNotEquals(schema.getType(TYPE_AUTHOR_INPUT), null);
assertNotEquals(schema.getType(TYPE_BOOK_INPUT), null);
assertNotEquals(schema.getType(TYPE_QUERY), null);
assertNotEquals(null, schema.getType(TYPE_AUTHOR_CONNECTION));
assertNotEquals(null, schema.getType(TYPE_BOOK_CONNECTION));
assertNotEquals(null, schema.getType(TYPE_AUTHOR_INPUT));
assertNotEquals(null, schema.getType(TYPE_BOOK_INPUT));
assertNotEquals(null, schema.getType(TYPE_QUERY));

GraphQLObjectType bookType = getConnectedType((GraphQLObjectType) schema.getType(TYPE_BOOK_CONNECTION), null);
GraphQLObjectType authorType = getConnectedType((GraphQLObjectType) schema.getType(TYPE_AUTHOR_CONNECTION), null);
Expand Down Expand Up @@ -199,6 +209,25 @@ public void testBuild() {
assertTrue(booksInputType.getWrappedType().equals(bookInputType));
}

@Test
public void testInterfaces() {
DataFetcher fetcher = mock(DataFetcher.class);
ModelBuilder builder = new ModelBuilder(dictionary, fetcher, NO_VERSION);

GraphQLSchema schema = builder.build();
assertNotEquals(null, schema.getType(TYPE_CHARACTER));
assertNotEquals(null, schema.getType(TYPE_DROID));
assertNotEquals(null, schema.getType(TYPE_HERO));
GraphQLInterfaceType characterType = (GraphQLInterfaceType) schema.getType(TYPE_CHARACTER);
assertEquals("DeferredID", characterType.getFieldDefinition("name").getType().getName());

GraphQLObjectType heroType = (GraphQLObjectType) schema.getType(TYPE_HERO);
assertTrue(heroType.getFieldDefinition("forceSensitive").getType().equals(Scalars.GraphQLBoolean));

GraphQLObjectType droidType = (GraphQLObjectType) schema.getType(TYPE_DROID);
assertTrue(droidType.getFieldDefinition("primaryFunction").getType().equals(Scalars.GraphQLString));
}

@Test
public void checkAttributeArguments() {
Set<ArgumentType> arguments = new HashSet<>();
Expand Down

0 comments on commit 6a79c31

Please sign in to comment.