Skip to content

Commit

Permalink
use gotest.tools/v3/assert
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Sep 28, 2024
1 parent c31575e commit eb56757
Show file tree
Hide file tree
Showing 23 changed files with 344 additions and 499 deletions.
56 changes: 28 additions & 28 deletions cmd/hasura-ndc-go/command/internal/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/hasura/ndc-sdk-go/schema"
"github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
)

var (
Expand Down Expand Up @@ -63,17 +63,17 @@ func TestConnectorGeneration(t *testing.T) {
}

rootDir, err := os.Getwd()
assert.NoError(t, err)
assert.NilError(t, err)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.NoError(t, os.Chdir(rootDir))
assert.NilError(t, os.Chdir(rootDir))
expectedSchemaBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/schema.json"))
assert.NoError(t, err)
assert.NilError(t, err)
connectorContentBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/connector.go.tmpl"))
assert.NoError(t, err)
assert.NilError(t, err)

srcDir := path.Join(tc.BasePath, "source")
assert.NoError(t, os.Chdir(srcDir))
assert.NilError(t, os.Chdir(srcDir))
err = ParseAndGenerateConnector(ConnectorGenerationArguments{
ConnectorDir: tc.ConnectorDir,
Directories: tc.Directories,
Expand All @@ -88,34 +88,34 @@ func TestConnectorGeneration(t *testing.T) {
}

var expectedSchema schema.SchemaResponse
assert.NoError(t, json.Unmarshal(expectedSchemaBytes, &expectedSchema))
assert.NilError(t, json.Unmarshal(expectedSchemaBytes, &expectedSchema))

schemaBytes, err := os.ReadFile(path.Join(tc.ConnectorDir, "schema.generated.json"))
assert.NoError(t, err)
assert.NilError(t, err)
var schemaOutput schema.SchemaResponse
assert.NoError(t, json.Unmarshal(schemaBytes, &schemaOutput))
assert.NilError(t, json.Unmarshal(schemaBytes, &schemaOutput))

assert.Equal(t, expectedSchema.Collections, schemaOutput.Collections)
assert.Equal(t, expectedSchema.Functions, schemaOutput.Functions)
assert.Equal(t, expectedSchema.Procedures, schemaOutput.Procedures)
assert.Equal(t, expectedSchema.ScalarTypes, schemaOutput.ScalarTypes)
assert.Equal(t, expectedSchema.ObjectTypes, schemaOutput.ObjectTypes)
assert.DeepEqual(t, expectedSchema.Collections, schemaOutput.Collections)
assert.DeepEqual(t, expectedSchema.Functions, schemaOutput.Functions)
assert.DeepEqual(t, expectedSchema.Procedures, schemaOutput.Procedures)
assert.DeepEqual(t, expectedSchema.ScalarTypes, schemaOutput.ScalarTypes)
assert.DeepEqual(t, expectedSchema.ObjectTypes, schemaOutput.ObjectTypes)

connectorBytes, err := os.ReadFile(path.Join(tc.ConnectorDir, "connector.generated.go"))
assert.NoError(t, err)
assert.NilError(t, err)
assert.Equal(t, formatTextContent(string(connectorContentBytes)), formatTextContent(string(connectorBytes)))

// go to the base test directory
assert.NoError(t, os.Chdir(".."))
assert.NilError(t, os.Chdir(".."))

for _, td := range tc.Directories {
expectedFunctionTypesBytes, err := os.ReadFile(path.Join("expected", "functions.go.tmpl"))
if err == nil {
functionTypesBytes, err := os.ReadFile(path.Join("source", td, "types.generated.go"))
assert.NoError(t, err)
assert.NilError(t, err)
assert.Equal(t, formatTextContent(string(expectedFunctionTypesBytes)), formatTextContent(string(functionTypesBytes)))
} else if !os.IsNotExist(err) {
assert.NoError(t, err)
assert.NilError(t, err)
}
}
})
Expand All @@ -124,25 +124,25 @@ func TestConnectorGeneration(t *testing.T) {
// go template
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.NoError(t, os.Chdir(rootDir))
assert.NilError(t, os.Chdir(rootDir))

expectedSchemaBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/schema.go.tmpl"))
if err != nil {
if os.IsNotExist(err) {
return
}
assert.NoError(t, err)
assert.NilError(t, err)
}
connectorContentBytes, err := os.ReadFile(path.Join(tc.BasePath, "expected/connector-go.go.tmpl"))
if err != nil {
if os.IsNotExist(err) {
return
}
assert.NoError(t, err)
assert.NilError(t, err)
}

srcDir := path.Join(tc.BasePath, "source")
assert.NoError(t, os.Chdir(srcDir))
assert.NilError(t, os.Chdir(srcDir))
err = ParseAndGenerateConnector(ConnectorGenerationArguments{
ConnectorDir: tc.ConnectorDir,
Directories: tc.Directories,
Expand All @@ -153,27 +153,27 @@ func TestConnectorGeneration(t *testing.T) {
assert.ErrorContains(t, err, tc.errorMsg)
return
}
assert.NoError(t, err)
assert.NilError(t, err)

schemaBytes, err := os.ReadFile(path.Join(tc.ConnectorDir, "schema.generated.go"))
assert.NoError(t, err)
assert.NilError(t, err)
assert.Equal(t, formatTextContent(string(expectedSchemaBytes)), formatTextContent(string(schemaBytes)))

connectorBytes, err := os.ReadFile(path.Join(tc.ConnectorDir, "connector.generated.go"))
assert.NoError(t, err)
assert.NilError(t, err)
assert.Equal(t, formatTextContent(string(connectorContentBytes)), formatTextContent(string(connectorBytes)))

// go to the base test directory
assert.NoError(t, os.Chdir(".."))
assert.NilError(t, os.Chdir(".."))

for _, td := range tc.Directories {
expectedFunctionTypesBytes, err := os.ReadFile(path.Join("expected", "functions.go.tmpl"))
if err == nil {
functionTypesBytes, err := os.ReadFile(path.Join("source", td, "types.generated.go"))
assert.NoError(t, err)
assert.NilError(t, err)
assert.Equal(t, formatTextContent(string(expectedFunctionTypesBytes)), formatTextContent(string(functionTypesBytes)))
} else if !os.IsNotExist(err) {
assert.NoError(t, err)
assert.NilError(t, err)
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/hasura-ndc-go/command/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package command
import (
"testing"

"github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
)

func TestGenerateNewProject(t *testing.T) {
tempDir := t.TempDir()
assert.NoError(t, GenerateNewProject(&NewArguments{
assert.NilError(t, GenerateNewProject(&NewArguments{
Name: "test",
Module: "hasura.dev/connector",
Output: tempDir,
Expand Down
4 changes: 2 additions & 2 deletions cmd/hasura-ndc-go/command/snapshots_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go/command"
"github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go/command/internal"
"github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
)

//go:embed testdata/snapshots/schema.json
Expand All @@ -23,7 +23,7 @@ func TestGenTestSnapshots(t *testing.T) {
server := httptest.NewServer(mux)
defer server.Close()

assert.NoError(t, command.GenTestSnapshots(&command.GenTestSnapshotArguments{
assert.NilError(t, command.GenTestSnapshots(&command.GenTestSnapshotArguments{
Endpoint: server.URL,
Dir: tmpDir,
Depth: 10,
Expand Down
8 changes: 3 additions & 5 deletions cmd/hasura-ndc-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ require (
github.com/alecthomas/kong v1.2.1
github.com/fatih/structtag v1.2.0
github.com/google/uuid v1.6.0
github.com/hasura/ndc-sdk-go v1.3.1-0.20240927065958-e616e7135a51
github.com/hasura/ndc-sdk-go v1.4.0
github.com/iancoleman/strcase v0.3.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.20.0
golang.org/x/tools v0.24.0
gotest.tools/v3 v3.5.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions cmd/hasura-ndc-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hasura/ndc-sdk-go v1.3.1-0.20240927065958-e616e7135a51 h1:DlUBBLlK9fvL4xE0Nvdgag5b1AQOBNI/m8ctszAivJQ=
github.com/hasura/ndc-sdk-go v1.3.1-0.20240927065958-e616e7135a51/go.mod h1:1DV7DoWoCrBdsyg/eSkdRxuvQJRySmyzdpcvFUmDoGo=
github.com/hasura/ndc-sdk-go v1.4.0 h1:RdkQm0efQfhDOxp/Gb1sLyOPh/q3XM7n1oOVKoG/rms=
github.com/hasura/ndc-sdk-go v1.4.0/go.mod h1:1DV7DoWoCrBdsyg/eSkdRxuvQJRySmyzdpcvFUmDoGo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
Expand Down Expand Up @@ -49,7 +49,7 @@ golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
4 changes: 2 additions & 2 deletions connector/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
"time"

"github.com/hasura/ndc-sdk-go/internal"
"github.com/google/uuid"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -291,7 +291,7 @@ func (rt *router) Build() *http.ServeMux {
func getRequestID(r *http.Request) string {
requestID := r.Header.Get("x-request-id")
if requestID == "" {
requestID = internal.GenRandomString(16)
requestID = uuid.NewString()
}
return requestID
}
Expand Down
8 changes: 2 additions & 6 deletions connector/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"testing"
"time"

"github.com/hasura/ndc-sdk-go/internal"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
"gotest.tools/v3/assert"
)

type mockConfiguration struct {
Expand Down Expand Up @@ -207,11 +207,7 @@ func assertHTTPResponse[B any](t *testing.T, res *http.Response, statusCode int,
t.FailNow()
}

if !internal.DeepEqual(body, expectedBody) {
expectedBytes, _ := json.Marshal(expectedBody)
t.Errorf("\nexpect: %+v\ngot: %+v", string(expectedBytes), string(bodyBytes))
t.FailNow()
}
assert.DeepEqual(t, body, expectedBody)
}

func TestNewServer(t *testing.T) {
Expand Down
Loading

0 comments on commit eb56757

Please sign in to comment.