github.com/Desuuuu/genqlient@v0.5.3/generate/parse_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"path/filepath"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/vektah/gqlparser/v2/ast"
     9  )
    10  
    11  var (
    12  	parseDataDir   = "testdata/parsing"
    13  	parseErrorsDir = "testdata/parsing-errors"
    14  )
    15  
    16  func sortQueries(queryDoc *ast.QueryDocument) {
    17  	sort.Slice(queryDoc.Operations, func(i, j int) bool {
    18  		return queryDoc.Operations[i].Name < queryDoc.Operations[j].Name
    19  	})
    20  	sort.Slice(queryDoc.Fragments, func(i, j int) bool {
    21  		return queryDoc.Fragments[i].Name < queryDoc.Fragments[j].Name
    22  	})
    23  }
    24  
    25  func getTestQueries(t *testing.T, ext string) *ast.QueryDocument {
    26  	graphqlQueries, err := getQueries(
    27  		parseDataDir, []string{filepath.Join(parseDataDir, "*."+ext)})
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	// The different file-types may have the operations/fragments in a
    33  	// different order.
    34  	sortQueries(graphqlQueries)
    35  
    36  	return graphqlQueries
    37  }
    38  
    39  // TestParse tests that query-extraction from different language source files
    40  // produces equivalent results.  We do not test the results it produces (that's
    41  // covered by TestGenerate), just that they are equivalent in different
    42  // languages (since TestGenerate only uses .graphql as input).
    43  // TODO: redo this as more standard snapshot tests?
    44  func TestParse(t *testing.T) {
    45  	extensions := []string{"go"}
    46  
    47  	graphqlQueries := getTestQueries(t, "graphql")
    48  
    49  	// check it's at least non-empty
    50  	if len(graphqlQueries.Operations) == 0 || len(graphqlQueries.Fragments) == 0 {
    51  		t.Fatalf("Didn't find any queries in *.graphql files")
    52  	}
    53  
    54  	sortQueries(graphqlQueries)
    55  
    56  	for _, ext := range extensions {
    57  		t.Run(ext, func(t *testing.T) {
    58  			queries := getTestQueries(t, ext)
    59  
    60  			got, want := ast.Dump(graphqlQueries), ast.Dump(queries)
    61  			if got != want {
    62  				// TODO: nice diffing
    63  				t.Errorf("got:\n%v\nwant:\n%v\n", got, want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  // TestParseErrors tests that query-extraction from different language source files
    70  // produces appropriate errors if your query is invalid.
    71  func TestParseErrors(t *testing.T) {
    72  	extensions := []string{"graphql", "go"}
    73  
    74  	for _, ext := range extensions {
    75  		t.Run(ext, func(t *testing.T) {
    76  			g, err := getQueries(
    77  				parseErrorsDir,
    78  				[]string{filepath.Join(parseErrorsDir, "*."+ext)})
    79  			if err == nil {
    80  				t.Errorf("expected error from getQueries(*.%v)", ext)
    81  				t.Logf("%#v", g)
    82  			}
    83  		})
    84  	}
    85  }