github.com/dgraph-io/dgraph@v1.2.8/graphql/test/test.go (about)

     1  /*
     2   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package test
    18  
    19  import (
    20  	"encoding/json"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	"github.com/dgraph-io/dgraph/graphql/schema"
    25  	"github.com/stretchr/testify/require"
    26  	"github.com/vektah/gqlparser/ast"
    27  	"github.com/vektah/gqlparser/parser"
    28  	"github.com/vektah/gqlparser/validator"
    29  )
    30  
    31  // Various helpers used in GQL testing
    32  
    33  // LoadSchema parses and validates the given schema string and requires
    34  // no errors.
    35  func LoadSchema(t *testing.T, gqlSchema string) schema.Schema {
    36  
    37  	doc, gqlErr := parser.ParseSchemas(validator.Prelude, &ast.Source{Input: gqlSchema})
    38  	require.Nil(t, gqlErr)
    39  	// ^^ We can't use NoError here because gqlErr is of type *gqlerror.Error,
    40  	// so passing into something that just expects an error, will always be a
    41  	// non-nil interface.
    42  
    43  	gql, gqlErr := validator.ValidateSchemaDocument(doc)
    44  	require.Nil(t, gqlErr)
    45  
    46  	return schema.AsSchema(gql)
    47  }
    48  
    49  // LoadSchemaFromFile reads a graphql schema file as would be the initial schema
    50  // definition.  It runs all validation, generates the completed schema and
    51  // returns that.
    52  func LoadSchemaFromFile(t *testing.T, gqlFile string) schema.Schema {
    53  	gql, err := ioutil.ReadFile(gqlFile)
    54  	require.NoError(t, err, "Unable to read schema file")
    55  
    56  	return LoadSchemaFromString(t, string(gql))
    57  }
    58  
    59  func LoadSchemaFromString(t *testing.T, sch string) schema.Schema {
    60  	handler, err := schema.NewHandler(string(sch))
    61  	require.NoError(t, err, "input schema contained errors")
    62  
    63  	return LoadSchema(t, handler.GQLSchema())
    64  }
    65  
    66  // GetMutation gets a single schema.Mutation from a schema.Operation.
    67  // It will fail if op is not a mutation or there's more than one mutation in
    68  // op.
    69  func GetMutation(t *testing.T, op schema.Operation) schema.Mutation {
    70  	require.NotNil(t, op)
    71  
    72  	mutations := op.Mutations()
    73  	require.Len(t, mutations, 1)
    74  
    75  	return mutations[0]
    76  }
    77  
    78  // GetQuery gets a single schema.Mutation from a schema.Operation.
    79  // It will fail if op is not a mutation or there's more than one mutation in
    80  // op.
    81  func GetQuery(t *testing.T, op schema.Operation) schema.Query {
    82  	require.NotNil(t, op)
    83  
    84  	queries := op.Queries()
    85  	require.Len(t, queries, 1)
    86  
    87  	return queries[0]
    88  }
    89  
    90  // RequireJSONEq converts to JSON and tests JSON equality.
    91  // It's easier to understand the diff, when a test fails, with json than
    92  // require.Equal on for example GraphQL error lists.
    93  func RequireJSONEq(t *testing.T, expected, got interface{}) {
    94  	jsonExpected, err := json.Marshal(expected)
    95  	require.NoError(t, err)
    96  
    97  	jsonGot, err := json.Marshal(got)
    98  	require.NoError(t, err)
    99  
   100  	require.JSONEq(t, string(jsonExpected), string(jsonGot))
   101  }
   102  
   103  // RequireJSONEqStr converts to JSON and tests JSON equality.
   104  // It's easier to understand the diff, when a test fails, with json than
   105  // require.Equal on for example GraphQL error lists.
   106  func RequireJSONEqStr(t *testing.T, expected string, got interface{}) {
   107  	jsonGot, err := json.Marshal(got)
   108  	require.NoError(t, err)
   109  
   110  	require.JSONEq(t, expected, string(jsonGot))
   111  }