Skip to content

Commit

Permalink
Adds Tests and GraphQL Types (#1)
Browse files Browse the repository at this point in the history
* added extended types-date,time

* added test cases
  • Loading branch information
ani-sha authored Jul 20, 2020
1 parent 4aadd81 commit 48ab119
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 44 deletions.
5 changes: 5 additions & 0 deletions engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions engine/src/main/java/io/graphqlcrud/GraphQLSchemaBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});

Expand All @@ -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();
Expand Down Expand Up @@ -120,7 +120,7 @@ private void addQueryOperationsForEntity(Entity entity, Builder queryTypeBuilder
}
}

protected List<GraphQLFieldDefinition.Builder> buildTypeFeilds(Entity entity, Schema schema) {
protected List<GraphQLFieldDefinition.Builder> buildTypeFields(Entity entity, Schema schema) {
ArrayList<GraphQLFieldDefinition.Builder> fields = new ArrayList<GraphQLFieldDefinition.Builder>();

// build fields for every attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
package io.graphqlcrud;

import java.util.Arrays;
import java.util.List;

Expand Down
27 changes: 14 additions & 13 deletions engine/src/main/java/io/graphqlcrud/types/JdbcTypeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.graphqlcrud.types;

import graphql.Scalars;
import graphql.scalars.ExtendedScalars;
import graphql.schema.GraphQLOutputType;

import java.sql.Types;
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 19 additions & 14 deletions engine/src/test/java/io/graphqlcrud/DatabaseSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,33 +28,37 @@
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;

@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<Entity> 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<Attribute> 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<Relation> accountRelations = entities.get(3).getRelations();
Assertions.assertEquals(Cardinality.ONE_TO_MANY, accountRelations.get(0).getCardinality());
Assertions.assertEquals("accounts",accountRelations.get(0).getName());
}
}
}
27 changes: 16 additions & 11 deletions engine/src/test/java/io/graphqlcrud/GraphQLSchemaBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());


}
}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.5.2.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
<failsafe-plugin.version>3.0.0-M3</failsafe-plugin.version>
<version.com.graphql-java>15.0</version.com.graphql-java>
<version.org.postgresql>42.2.14</version.org.postgresql>
</properties>
Expand Down Expand Up @@ -115,7 +116,7 @@
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<version>${failsafe-plugin.version}</version>
<executions>
<execution>
<goals>
Expand Down

0 comments on commit 48ab119

Please sign in to comment.