github.com/dgraph-io/dgraph@v1.2.8/graphql/resolve/query_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 resolve 18 19 import ( 20 "io/ioutil" 21 "testing" 22 23 "github.com/dgraph-io/dgraph/graphql/dgraph" 24 "github.com/dgraph-io/dgraph/graphql/schema" 25 "github.com/dgraph-io/dgraph/graphql/test" 26 "github.com/stretchr/testify/require" 27 _ "github.com/vektah/gqlparser/validator/rules" // make gql validator init() all rules 28 "gopkg.in/yaml.v2" 29 ) 30 31 // Tests showing that the query rewriter produces the expected Dgraph queries 32 33 type QueryRewritingCase struct { 34 Name string 35 GQLQuery string 36 Variables map[string]interface{} 37 DGQuery string 38 } 39 40 func TestQueryRewriting(t *testing.T) { 41 b, err := ioutil.ReadFile("query_test.yaml") 42 require.NoError(t, err, "Unable to read test file") 43 44 var tests []QueryRewritingCase 45 err = yaml.Unmarshal(b, &tests) 46 require.NoError(t, err, "Unable to unmarshal tests to yaml.") 47 48 gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql") 49 50 testRewriter := NewQueryRewriter() 51 52 for _, tcase := range tests { 53 t.Run(tcase.Name, func(t *testing.T) { 54 55 op, err := gqlSchema.Operation( 56 &schema.Request{ 57 Query: tcase.GQLQuery, 58 Variables: tcase.Variables, 59 }) 60 require.NoError(t, err) 61 gqlQuery := test.GetQuery(t, op) 62 63 dgQuery, err := testRewriter.Rewrite(gqlQuery) 64 require.Nil(t, err) 65 require.Equal(t, tcase.DGQuery, dgraph.AsString(dgQuery)) 66 }) 67 } 68 }