From 48ab119e03b9cf7c8a84facd73a13006ac413e19 Mon Sep 17 00:00:00 2001 From: Anisha Mohanty Date: Mon, 20 Jul 2020 19:47:27 +0530 Subject: [PATCH] Adds Tests and GraphQL Types (#1) * added extended types-date,time * added test cases --- engine/pom.xml | 5 +++ .../io/graphqlcrud/GraphQLSchemaBuilder.java | 8 ++--- .../SQLBasedDataFetcherFactory.java | 1 - .../io/graphqlcrud/types/JdbcTypeMap.java | 27 +++++++-------- .../io/graphqlcrud/DatabaseSchemaTest.java | 33 +++++++++++-------- .../graphqlcrud/GraphQLSchemaBuilderTest.java | 27 ++++++++------- pom.xml | 3 +- 7 files changed, 60 insertions(+), 44 deletions(-) diff --git a/engine/pom.xml b/engine/pom.xml index 4ada94e..d978720 100644 --- a/engine/pom.xml +++ b/engine/pom.xml @@ -28,6 +28,11 @@ quarkus-junit5 test + + com.graphql-java + graphql-java-extended-scalars + 1.0 + io.quarkus quarkus-flyway diff --git a/engine/src/main/java/io/graphqlcrud/GraphQLSchemaBuilder.java b/engine/src/main/java/io/graphqlcrud/GraphQLSchemaBuilder.java index c11bc06..aa63c70 100644 --- a/engine/src/main/java/io/graphqlcrud/GraphQLSchemaBuilder.java +++ b/engine/src/main/java/io/graphqlcrud/GraphQLSchemaBuilder.java @@ -61,7 +61,7 @@ public GraphQLSchema buildSchema(Schema schema) { typeBuilder.name(entity.getName()); // Add fields in a Type - buildTypeFeilds(entity, schema).stream().forEach(fieldBuilder -> { + buildTypeFields(entity, schema).stream().forEach(fieldBuilder -> { typeBuilder.field(fieldBuilder.build()); }); @@ -84,12 +84,12 @@ public GraphQLSchema buildSchema(Schema schema) { // add Code Registry GraphQLCodeRegistry.Builder codeBuilder = GraphQLCodeRegistry.newCodeRegistry(); - + SQLBasedDataFetcherFactory defaultDff = new SQLBasedDataFetcherFactory(); codeBuilder.defaultDataFetcher(defaultDff); codeBuilder.dataFetcher(FieldCoordinates.coordinates("accounts", "ACCOUNT_ID"), defaultDff); codeBuilder.dataFetcher(FieldCoordinates.coordinates("accounts", "SSN"), defaultDff); - + builder.codeRegistry(codeBuilder.build()); return builder.build(); @@ -120,7 +120,7 @@ private void addQueryOperationsForEntity(Entity entity, Builder queryTypeBuilder } } - protected List buildTypeFeilds(Entity entity, Schema schema) { + protected List buildTypeFields(Entity entity, Schema schema) { ArrayList fields = new ArrayList(); // build fields for every attribute diff --git a/engine/src/main/java/io/graphqlcrud/SQLBasedDataFetcherFactory.java b/engine/src/main/java/io/graphqlcrud/SQLBasedDataFetcherFactory.java index cba5432..5c79075 100644 --- a/engine/src/main/java/io/graphqlcrud/SQLBasedDataFetcherFactory.java +++ b/engine/src/main/java/io/graphqlcrud/SQLBasedDataFetcherFactory.java @@ -14,7 +14,6 @@ * limitations under the License. */ package io.graphqlcrud; - import java.util.Arrays; import java.util.List; diff --git a/engine/src/main/java/io/graphqlcrud/types/JdbcTypeMap.java b/engine/src/main/java/io/graphqlcrud/types/JdbcTypeMap.java index 7aaa0aa..0edf8ec 100644 --- a/engine/src/main/java/io/graphqlcrud/types/JdbcTypeMap.java +++ b/engine/src/main/java/io/graphqlcrud/types/JdbcTypeMap.java @@ -16,6 +16,7 @@ package io.graphqlcrud.types; import graphql.Scalars; +import graphql.scalars.ExtendedScalars; import graphql.schema.GraphQLOutputType; import java.sql.Types; @@ -43,22 +44,22 @@ public GraphQLOutputType getAsGraphQLTypeString(int dataType) { case Types.DECIMAL: typeString = Scalars.GraphQLFloat; break; -// case Types.DATE: -// case Types.TIMESTAMP: -// case Types.TIME: -// typeString = "Date"; -// break; + case Types.DATE: + case Types.TIMESTAMP: + case Types.TIME: + typeString = ExtendedScalars.DateTime; + break; case Types.BIT: typeString = Scalars.GraphQLBoolean; break; -// case Types.OTHER: -// typeString = "Object"; -// break; -// case Types.BINARY: -// case Types.VARBINARY: -// case Types.LONGVARBINARY: -// typeString = "Object"; -// break; + case Types.OTHER: + typeString = ExtendedScalars.Object; + break; + case Types.BINARY: + case Types.VARBINARY: + case Types.LONGVARBINARY: + typeString = ExtendedScalars.Object; + break; default: typeString = Scalars.GraphQLString; break; diff --git a/engine/src/test/java/io/graphqlcrud/DatabaseSchemaTest.java b/engine/src/test/java/io/graphqlcrud/DatabaseSchemaTest.java index 5fbb74c..88c84fc 100644 --- a/engine/src/test/java/io/graphqlcrud/DatabaseSchemaTest.java +++ b/engine/src/test/java/io/graphqlcrud/DatabaseSchemaTest.java @@ -15,10 +15,11 @@ */ package io.graphqlcrud; -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; +import io.graphqlcrud.model.*; +import org.junit.jupiter.api.Assertions; import java.sql.Connection; +import java.sql.Types; import java.util.Collections; import java.util.List; @@ -27,8 +28,6 @@ import org.junit.jupiter.api.Test; import io.agroal.api.AgroalDataSource; -import io.graphqlcrud.model.Entity; -import io.graphqlcrud.model.Schema; import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.h2.H2DatabaseTestResource; import io.quarkus.test.junit.QuarkusTest; @@ -36,24 +35,30 @@ @QuarkusTestResource(H2DatabaseTestResource.class) @QuarkusTest public class DatabaseSchemaTest { - + @Inject private AgroalDataSource datasource; - + @Test public void testSchemaPrint() throws Exception { try (Connection connection = datasource.getConnection()){ - assertNotNull(connection); + Assertions.assertNotNull(connection); Schema s = DatabaseSchemaBuilder.getSchema(connection, "PUBLIC"); - assertNotNull(s); + Assertions.assertNotNull(s); List entities = s.getEntities(); Collections.sort(entities); - assertEquals(4, entities.size()); - - // TODO: Write more assertions about what kind of columns and types - // and then relations this entity has - assertEquals("PRODUCT", entities.get(0).getName()); - + Assertions.assertEquals(4, entities.size()); + + Assertions.assertEquals("CUSTOMER", entities.get(2).getName()); + List customerAttributes = entities.get(2).getAttributes(); + Assertions.assertEquals(9, customerAttributes.size()); + Assertions.assertEquals("SSN", customerAttributes.get(5).getName()); + Assertions.assertEquals(Types.CHAR, customerAttributes.get(5).getType()); + + Assertions.assertEquals("ACCOUNT",entities.get(3).getName()); + List accountRelations = entities.get(3).getRelations(); + Assertions.assertEquals(Cardinality.ONE_TO_MANY, accountRelations.get(0).getCardinality()); + Assertions.assertEquals("accounts",accountRelations.get(0).getName()); } } } diff --git a/engine/src/test/java/io/graphqlcrud/GraphQLSchemaBuilderTest.java b/engine/src/test/java/io/graphqlcrud/GraphQLSchemaBuilderTest.java index 3380664..0166d98 100644 --- a/engine/src/test/java/io/graphqlcrud/GraphQLSchemaBuilderTest.java +++ b/engine/src/test/java/io/graphqlcrud/GraphQLSchemaBuilderTest.java @@ -15,13 +15,12 @@ */ package io.graphqlcrud; -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.sql.Connection; import javax.inject.Inject; +import graphql.schema.GraphQLObjectType; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import graphql.schema.GraphQLSchema; @@ -34,20 +33,26 @@ @QuarkusTestResource(H2DatabaseTestResource.class) @QuarkusTest public class GraphQLSchemaBuilderTest { - + @Inject private AgroalDataSource datasource; - + @Test public void testSchemaPrint() throws Exception { try (Connection connection = datasource.getConnection()){ - assertNotNull(connection); + Assertions.assertNotNull(connection); Schema s = DatabaseSchemaBuilder.getSchema(connection, "PUBLIC"); - assertNotNull(s); - - // TODO: write more tests validating the schema generated - GraphQLSchema gqls = GraphQLSchemaBuilder.getSchema(s); - assertEquals("QueryType", gqls.getQueryType().getName()); + Assertions.assertNotNull(s); + + GraphQLSchema schema = GraphQLSchemaBuilder.getSchema(s); + GraphQLObjectType objectType = schema.getQueryType(); + Assertions.assertNotNull(objectType); + Assertions.assertEquals("QueryType", objectType.getName()); + Assertions.assertEquals("account",objectType.getFieldDefinition("account").getName()); + Assertions.assertEquals("ACCOUNT_ID",objectType.getFieldDefinition("account").getArguments().get(0).getName()); +// Assertions.assertEquals(GraphQLList.list( GraphQLTypeReference.typeRef("CUSTOMER")),objectType.getFieldDefinition("customers").getType()); + + } } } diff --git a/pom.xml b/pom.xml index 9e83907..a417e57 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ io.quarkus 1.5.2.Final 2.22.1 + 3.0.0-M3 15.0 42.2.14 @@ -115,7 +116,7 @@ maven-failsafe-plugin - ${surefire-plugin.version} + ${failsafe-plugin.version}