...

Source file src/github.com/udacity/graphb/query_test.go

Documentation: github.com/udacity/graphb

     1  package graphb
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestQuery_checkName(t *testing.T) {
    12  	q := Query{Name: "1"}
    13  	err := q.checkName()
    14  	assert.IsType(t, InvalidNameErr{}, errors.Cause(err))
    15  	assert.Equal(t, "'1' is an invalid operation name in GraphQL. A valid name matches /[_A-Za-z][_0-9A-Za-z]*/, see: http://facebook.github.io/graphql/October2016/#sec-Names", err.Error())
    16  }
    17  
    18  func TestQuery_check(t *testing.T) {
    19  	q := Query{Name: "1", Type: TypeQuery}
    20  	err := q.check()
    21  	assert.IsType(t, InvalidNameErr{}, errors.Cause(err))
    22  	assert.Equal(t, "'1' is an invalid operation name in GraphQL. A valid name matches /[_A-Za-z][_0-9A-Za-z]*/, see: http://facebook.github.io/graphql/October2016/#sec-Names", err.Error())
    23  }
    24  
    25  func TestQuery_GetField(t *testing.T) {
    26  	q := MakeQuery(TypeQuery).SetFields(MakeField("f1"))
    27  	f := q.GetField("f1")
    28  	assert.Equal(t, "f1", f.Name)
    29  
    30  	f = q.GetField("f2")
    31  	assert.Nil(t, f)
    32  }
    33  
    34  func TestQuery_JSON(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	t.Run("Arguments can be nested structures", func(t *testing.T) {
    38  		t.Parallel()
    39  
    40  		q := NewQuery(TypeMutation).
    41  			SetFields(
    42  				NewField("createQuestion").
    43  					SetArguments(
    44  						ArgumentCustomType(
    45  							"input",
    46  							ArgumentString("title", "what"),
    47  							ArgumentString("content", "what"),
    48  							ArgumentStringSlice("tagIds"),
    49  						),
    50  					).
    51  					SetFields(
    52  						NewField("question", OfFields("id")),
    53  					),
    54  			)
    55  
    56  		c := q.stringChan()
    57  
    58  		var strs []string
    59  		for str := range c {
    60  			strs = append(strs, str)
    61  		}
    62  
    63  		assert.Equal(t, `mutation{createQuestion(input:{title:"what",content:"what",tagIds:[]}){question{id}}}`, strings.Join(strs, ""))
    64  	})
    65  
    66  }
    67  

View as plain text