github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go@v0.0.0-20240508172728-e960be013ca2/connector_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path"
     7  	"regexp"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go/command"
    12  	"github.com/hasura/ndc-sdk-go/schema"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var (
    17  	newLinesRegexp = regexp.MustCompile(`\n(\s|\t)*\n`)
    18  	tabRegexp      = regexp.MustCompile(`\t`)
    19  	spacesRegexp   = regexp.MustCompile(`\n\s+`)
    20  )
    21  
    22  func formatTextContent(input string) string {
    23  	return strings.Trim(spacesRegexp.ReplaceAllString(tabRegexp.ReplaceAllString(newLinesRegexp.ReplaceAllString(input, "\n"), "  "), "\n"), "\n")
    24  }
    25  
    26  func TestConnectorGeneration(t *testing.T) {
    27  
    28  	testCases := []struct {
    29  		Name       string
    30  		BasePath   string
    31  		ModuleName string
    32  	}{
    33  		{
    34  			Name:       "basic",
    35  			BasePath:   "testdata/basic",
    36  			ModuleName: "github.com/hasura/ndc-codegen-test",
    37  		},
    38  		{
    39  			Name:       "empty",
    40  			BasePath:   "./testdata/empty",
    41  			ModuleName: "github.com/hasura/ndc-codegen-empty-test",
    42  		},
    43  	}
    44  
    45  	rootDir, err := os.Getwd()
    46  	assert.NoError(t, err)
    47  	for _, tc := range testCases {
    48  		t.Run(tc.Name, func(t *testing.T) {
    49  			assert.NoError(t, os.Chdir(rootDir))
    50  
    51  			expectedSchemaBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/schema.json"))
    52  			assert.NoError(t, err)
    53  			connectorContentBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/connector.go.tmpl"))
    54  			assert.NoError(t, err)
    55  
    56  			srcDir := path.Join(tc.BasePath, "source")
    57  			assert.NoError(t, os.Chdir(srcDir))
    58  
    59  			assert.NoError(t, parseAndGenerateConnector(&GenerateArguments{
    60  				Path:        srcDir,
    61  				Directories: []string{"functions"},
    62  			}, tc.ModuleName))
    63  
    64  			var expectedSchema schema.SchemaResponse
    65  			assert.NoError(t, json.Unmarshal(expectedSchemaBytes, &expectedSchema))
    66  
    67  			schemaBytes, err := os.ReadFile("schema.generated.json")
    68  			assert.NoError(t, err)
    69  			var schemaOutput schema.SchemaResponse
    70  			assert.NoError(t, json.Unmarshal(schemaBytes, &schemaOutput))
    71  
    72  			assert.Equal(t, expectedSchema.Collections, schemaOutput.Collections)
    73  			assert.Equal(t, expectedSchema.Functions, schemaOutput.Functions)
    74  			assert.Equal(t, expectedSchema.Procedures, schemaOutput.Procedures)
    75  			assert.Equal(t, expectedSchema.ScalarTypes, schemaOutput.ScalarTypes)
    76  			assert.Equal(t, expectedSchema.ObjectTypes, schemaOutput.ObjectTypes)
    77  
    78  			connectorBytes, err := os.ReadFile("connector.generated.go")
    79  			assert.NoError(t, err)
    80  			assert.Equal(t, formatTextContent(string(connectorContentBytes)), formatTextContent(string(connectorBytes)))
    81  
    82  			expectedFunctionTypesBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/functions.go.tmpl"))
    83  			if err == nil {
    84  				functionTypesBytes, err := os.ReadFile("functions/types.generated.go")
    85  				assert.NoError(t, err)
    86  				assert.Equal(t, formatTextContent(string(expectedFunctionTypesBytes)), formatTextContent(string(functionTypesBytes)))
    87  			} else if !os.IsNotExist(err) {
    88  				assert.NoError(t, err)
    89  			}
    90  
    91  			// generate test cases
    92  			assert.NoError(t, command.GenTestSnapshots(&command.GenTestSnapshotArguments{
    93  				Dir:    "testdata",
    94  				Schema: "schema.generated.json",
    95  			}))
    96  		})
    97  	}
    98  }