From eb5675708c9b10b4f36800d69308e25749ba1a6b Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Sat, 28 Sep 2024 23:49:52 +0700 Subject: [PATCH] use gotest.tools/v3/assert --- .../command/internal/connector_test.go | 56 +-- cmd/hasura-ndc-go/command/new_test.go | 4 +- .../command/snapshots_gen_test.go | 4 +- cmd/hasura-ndc-go/go.mod | 8 +- cmd/hasura-ndc-go/go.sum | 8 +- connector/http.go | 4 +- connector/server_test.go | 8 +- example/codegen/connector_test.go | 45 +-- example/codegen/go.mod | 6 +- example/codegen/go.sum | 11 +- example/reference/connector_test.go | 8 +- go.mod | 2 + go.sum | 2 + internal/utils.go | 105 ------ schema/mutation_test.go | 25 +- schema/query_test.go | 12 +- schema/scalar_test.go | 94 ++--- schema/schema_test.go | 19 +- schema/type_test.go | 12 +- utils/connector.go | 20 +- utils/connector_test.go | 50 +-- utils/decode_test.go | 326 +++++++++--------- utils/helper_test.go | 14 +- 23 files changed, 344 insertions(+), 499 deletions(-) delete mode 100644 internal/utils.go diff --git a/cmd/hasura-ndc-go/command/internal/connector_test.go b/cmd/hasura-ndc-go/command/internal/connector_test.go index 8d750ab..9b8f3ac 100644 --- a/cmd/hasura-ndc-go/command/internal/connector_test.go +++ b/cmd/hasura-ndc-go/command/internal/connector_test.go @@ -9,7 +9,7 @@ import ( "testing" "github.com/hasura/ndc-sdk-go/schema" - "github.com/stretchr/testify/assert" + "gotest.tools/v3/assert" ) var ( @@ -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, @@ -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) } } }) @@ -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, @@ -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) } } }) diff --git a/cmd/hasura-ndc-go/command/new_test.go b/cmd/hasura-ndc-go/command/new_test.go index dd0d22d..1a66a34 100644 --- a/cmd/hasura-ndc-go/command/new_test.go +++ b/cmd/hasura-ndc-go/command/new_test.go @@ -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, diff --git a/cmd/hasura-ndc-go/command/snapshots_gen_test.go b/cmd/hasura-ndc-go/command/snapshots_gen_test.go index bdbe26e..0bb867a 100644 --- a/cmd/hasura-ndc-go/command/snapshots_gen_test.go +++ b/cmd/hasura-ndc-go/command/snapshots_gen_test.go @@ -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 @@ -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, diff --git a/cmd/hasura-ndc-go/go.mod b/cmd/hasura-ndc-go/go.mod index ba5867f..3e4d672 100644 --- a/cmd/hasura-ndc-go/go.mod +++ b/cmd/hasura-ndc-go/go.mod @@ -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 ) diff --git a/cmd/hasura-ndc-go/go.sum b/cmd/hasura-ndc-go/go.sum index 4b60044..5c34cd2 100644 --- a/cmd/hasura-ndc-go/go.sum +++ b/cmd/hasura-ndc-go/go.sum @@ -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= @@ -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= diff --git a/connector/http.go b/connector/http.go index fe0e6fe..5358f9e 100644 --- a/connector/http.go +++ b/connector/http.go @@ -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" @@ -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 } diff --git a/connector/server_test.go b/connector/server_test.go index 873cb92..4ea5b18 100644 --- a/connector/server_test.go +++ b/connector/server_test.go @@ -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 { @@ -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) { diff --git a/example/codegen/connector_test.go b/example/codegen/connector_test.go index ae800c3..c874ba3 100644 --- a/example/codegen/connector_test.go +++ b/example/codegen/connector_test.go @@ -7,16 +7,19 @@ import ( "io" "log" "net/http" + "reflect" + "strings" "testing" "time" + "github.com/google/go-cmp/cmp" "github.com/google/uuid" "github.com/hasura/ndc-codegen-example/types" "github.com/hasura/ndc-codegen-example/types/arguments" "github.com/hasura/ndc-sdk-go/connector" "github.com/hasura/ndc-sdk-go/scalar" "github.com/hasura/ndc-sdk-go/utils" - "github.com/stretchr/testify/assert" + "gotest.tools/v3/assert" ) func createTestServer(t *testing.T) *connector.Server[types.Configuration, types.State] { @@ -25,16 +28,16 @@ func createTestServer(t *testing.T) *connector.Server[types.Configuration, types InlineConfig: true, }, connector.WithoutRecovery()) - assert.NoError(t, err) + assert.NilError(t, err) return server } func TestQueryGetTypes(t *testing.T) { commentText := types.CommentText{} - assert.NoError(t, commentText.FromValue("a comment")) + assert.NilError(t, commentText.FromValue("a comment")) commentTextPtr := types.CommentText{} - assert.NoError(t, commentTextPtr.FromValue("a comment pointer")) + assert.NilError(t, commentTextPtr.FromValue("a comment pointer")) testCases := []struct { name string @@ -1514,12 +1517,12 @@ func TestQueryGetTypes(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { resp, err := http.DefaultClient.Post(fmt.Sprintf("%s/query", testServer.URL), "application/json", bytes.NewReader([]byte(tc.body))) - assert.NoError(t, err, "failed to request query") + assert.NilError(t, err, "failed to request query") assert.Equal(t, tc.status, resp.StatusCode) respBody, err := io.ReadAll(resp.Body) if tc.errorMsg != "" { - assert.NoError(t, err) - assert.Contains(t, string(respBody), tc.errorMsg) + assert.NilError(t, err) + assert.Check(t, strings.Contains(string(respBody), tc.errorMsg)) } else if resp.StatusCode != http.StatusOK { t.Errorf("expected successful response, got error: %s", string(respBody)) } else { @@ -1529,10 +1532,10 @@ func TestQueryGetTypes(t *testing.T) { Value arguments.GetTypesArguments `json:"__value"` } `json:"rows,omitempty" mapstructure:"rows,omitempty"` } - assert.NoError(t, json.Unmarshal(respBody, &results), "failed to decode response") + assert.NilError(t, json.Unmarshal(respBody, &results), "failed to decode response") assert.Equal(t, 1, len(results)) assert.Equal(t, 1, len(results[0].Rows)) - assert.Equal(t, tc.response, results[0].Rows[0].Value) + assert.DeepEqual(t, tc.response, results[0].Rows[0].Value, cmp.Exporter(func(t reflect.Type) bool { return true })) } }) } @@ -1693,27 +1696,27 @@ func TestQueries(t *testing.T) { t.Run(tc.name, func(t *testing.T) { resp, err := http.DefaultClient.Post(fmt.Sprintf("%s/query", testServer.URL), "application/json", bytes.NewReader([]byte(tc.body))) - assert.NoError(t, err, "failed to request query") + assert.NilError(t, err, "failed to request query") assert.Equal(t, tc.status, resp.StatusCode) respBody, err := io.ReadAll(resp.Body) if tc.errorMsg != "" { - assert.NoError(t, err) - assert.Contains(t, string(respBody), tc.errorMsg) + assert.NilError(t, err) + assert.Check(t, strings.Contains(string(respBody), tc.errorMsg)) } else if resp.StatusCode != http.StatusOK { t.Errorf("expected successful response, got error: %s", string(respBody)) } else { log.Print("response: ", string(respBody)) var expected any - assert.NoError(t, json.Unmarshal([]byte(tc.response), &expected)) + assert.NilError(t, json.Unmarshal([]byte(tc.response), &expected)) var results []struct { Rows []struct { Value any `json:"__value"` } `json:"rows,omitempty" mapstructure:"rows,omitempty"` } - assert.NoError(t, json.Unmarshal(respBody, &results), "failed to decode response") + assert.NilError(t, json.Unmarshal(respBody, &results), "failed to decode response") assert.Equal(t, 1, len(results)) assert.Equal(t, 1, len(results[0].Rows)) - assert.Equal(t, expected, results[0].Rows[0].Value) + assert.DeepEqual(t, expected, results[0].Rows[0].Value) } }) } @@ -1899,26 +1902,26 @@ func TestProcedures(t *testing.T) { t.Run(tc.name, func(t *testing.T) { resp, err := http.DefaultClient.Post(fmt.Sprintf("%s/mutation", testServer.URL), "application/json", bytes.NewReader([]byte(tc.body))) - assert.NoError(t, err, "failed to request mutation") + assert.NilError(t, err, "failed to request mutation") assert.Equal(t, tc.status, resp.StatusCode) respBody, err := io.ReadAll(resp.Body) if tc.errorMsg != "" { - assert.NoError(t, err) - assert.Contains(t, string(respBody), tc.errorMsg) + assert.NilError(t, err) + assert.Check(t, strings.Contains(string(respBody), tc.errorMsg)) } else if resp.StatusCode != http.StatusOK { t.Errorf("expected successful response, got error: %s", string(respBody)) } else { log.Print("response: ", string(respBody)) var expected any - assert.NoError(t, json.Unmarshal([]byte(tc.response), &expected)) + assert.NilError(t, json.Unmarshal([]byte(tc.response), &expected)) var results struct { OperationResults []struct { Result any `json:"result"` } `json:"operation_results"` } - assert.NoError(t, json.Unmarshal(respBody, &results), "failed to decode response") + assert.NilError(t, json.Unmarshal(respBody, &results), "failed to decode response") assert.Equal(t, 1, len(results.OperationResults)) - assert.Equal(t, expected, results.OperationResults[0].Result) + assert.DeepEqual(t, expected, results.OperationResults[0].Result) } }) } diff --git a/example/codegen/go.mod b/example/codegen/go.mod index 7ca89b3..aeeddf8 100644 --- a/example/codegen/go.mod +++ b/example/codegen/go.mod @@ -3,11 +3,12 @@ module github.com/hasura/ndc-codegen-example go 1.21 require ( + github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/hasura/ndc-sdk-go v1.2.0 - github.com/stretchr/testify v1.9.0 go.opentelemetry.io/otel v1.28.0 go.opentelemetry.io/otel/trace v1.28.0 + gotest.tools/v3 v3.5.1 ) require ( @@ -15,14 +16,12 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-viper/mapstructure/v2 v2.2.1 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect github.com/klauspost/compress v1.17.10 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.20.4 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.59.1 // indirect @@ -45,7 +44,6 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 // indirect google.golang.org/grpc v1.65.0 // indirect google.golang.org/protobuf v1.34.2 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) replace github.com/hasura/ndc-sdk-go => ../../ diff --git a/example/codegen/go.sum b/example/codegen/go.sum index 15231d2..9cc98b7 100644 --- a/example/codegen/go.sum +++ b/example/codegen/go.sum @@ -29,10 +29,6 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0= github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= @@ -47,8 +43,6 @@ github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJ github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= -github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= -github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= go.opentelemetry.io/contrib/propagators/b3 v1.28.0 h1:XR6CFQrQ/ttAYmTBX2loUEFGdk1h17pxYI8828dk/1Y= @@ -93,8 +87,7 @@ google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 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= diff --git a/example/reference/connector_test.go b/example/reference/connector_test.go index 1030122..824b5c2 100644 --- a/example/reference/connector_test.go +++ b/example/reference/connector_test.go @@ -10,8 +10,8 @@ import ( "testing" "github.com/hasura/ndc-sdk-go/connector" - "github.com/hasura/ndc-sdk-go/internal" "github.com/hasura/ndc-sdk-go/schema" + "gotest.tools/v3/assert" ) const test_SpecVersion = "v0.1.6" @@ -74,11 +74,7 @@ func assertHTTPResponse[B any](t *testing.T, res *http.Response, statusCode int, t.FailNow() } - if !internal.DeepEqual(expectedBody, body) { - expectedBytes, _ := json.Marshal(expectedBody) - t.Errorf("\nexpect: %+v\ngot: %+v", string(expectedBytes), string(bodyBytes)) - t.FailNow() - } + assert.DeepEqual(t, expectedBody, body) } func TestGeneralMethods(t *testing.T) { diff --git a/go.mod b/go.mod index f62769a..8ba608c 100755 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alecthomas/kong v1.2.1 github.com/go-logr/logr v1.4.2 github.com/go-viper/mapstructure/v2 v2.2.1 + github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/prometheus/client_golang v1.20.4 go.opentelemetry.io/contrib/propagators/b3 v1.28.0 @@ -20,6 +21,7 @@ require ( go.opentelemetry.io/otel/sdk v1.28.0 go.opentelemetry.io/otel/sdk/metric v1.28.0 go.opentelemetry.io/otel/trace v1.28.0 + gotest.tools/v3 v3.5.1 ) require ( diff --git a/go.sum b/go.sum index 4e8b908..9cc98b7 100644 --- a/go.sum +++ b/go.sum @@ -89,3 +89,5 @@ google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6h google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= 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= diff --git a/internal/utils.go b/internal/utils.go deleted file mode 100644 index efca919..0000000 --- a/internal/utils.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "encoding/json" - "log" - "math/rand" - "reflect" - "strings" - "time" -) - -const ( - letterIdxBits = 6 // 6 bits to represent a letter index - letterIdxMask = 1<= 0; { - if remain == 0 { - cache, remain = src.Int63(), letterIdxMax - } - if idx := int(cache & letterIdxMask); idx < len(allowedChars) { - sb.WriteByte(allowedChars[idx]) - i-- - } - cache >>= letterIdxBits - remain-- - } - - return sb.String() -} - -// DeepEqual checks if both values are recursively equal -// used for testing purpose only -func DeepEqual(v1, v2 any) bool { - if reflect.DeepEqual(v1, v2) { - return true - } - - bytesA, _ := json.Marshal(v1) - bytesB, _ := json.Marshal(v2) - if string(bytesA) == string(bytesB) { - return true - } - - switch reflect.ValueOf(v1).Kind() { - case reflect.Slice, reflect.Array: - var values1 []map[string]any - var values2 []map[string]any - if err := json.Unmarshal(bytesA, &values1); err == nil { - if err2 := json.Unmarshal(bytesB, &values2); err2 != nil { - return false - } - if len(values1) != len(values2) { - return false - } - - for i, value1 := range values1 { - if !DeepEqual(value1, values2[i]) { - j1, _ := json.Marshal(value1) - j2, _ := json.Marshal(values2[i]) - log.Printf("deep equality is failed at index: %d\n value 1: %s\n value 2: %s\n", i, string(j1), string(j2)) - return false - } - } - return true - } - case reflect.Struct, reflect.Map: - var map1 map[string]any - var map2 map[string]any - if err := json.Unmarshal(bytesA, &map1); err == nil { - if err2 := json.Unmarshal(bytesB, &map2); err2 != nil { - return false - } - if len(map1) != len(map2) { - return false - } - for k, v1 := range map1 { - v2, ok := map2[k] - if !ok || !DeepEqual(v1, v2) { - j1, _ := json.Marshal(v1) - j2, _ := json.Marshal(v2) - log.Printf("deep equality is failed at key: %s\n expected : %s\n got : %s\n", k, string(j1), string(j2)) - return false - } - } - return true - } - } - - var x1 any - var x2 any - _ = json.Unmarshal(bytesA, &x1) - _ = json.Unmarshal(bytesB, &x2) - return reflect.DeepEqual(x1, x2) -} diff --git a/schema/mutation_test.go b/schema/mutation_test.go index 0edb4cd..a52bf8a 100644 --- a/schema/mutation_test.go +++ b/schema/mutation_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/hasura/ndc-sdk-go/internal" + "gotest.tools/v3/assert" ) func TestUnmarshalProcedureInfo(t *testing.T) { @@ -52,15 +52,15 @@ func TestUnmarshalProcedureInfo(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var procedure ProcedureInfo - assertNoError(t, json.Unmarshal([]byte(tc.raw), &procedure)) - assertDeepEqual(t, tc.expected, procedure) + assert.NilError(t, json.Unmarshal([]byte(tc.raw), &procedure)) + assert.DeepEqual(t, tc.expected, procedure) var jsonMap map[string]json.RawMessage - assertNoError(t, json.Unmarshal([]byte(tc.raw), &jsonMap)) + assert.NilError(t, json.Unmarshal([]byte(tc.raw), &jsonMap)) var result ProcedureInfo - assertNoError(t, result.UnmarshalJSONMap(jsonMap)) - assertDeepEqual(t, procedure, result) + assert.NilError(t, result.UnmarshalJSONMap(jsonMap)) + assert.DeepEqual(t, procedure, result) }) } } @@ -75,16 +75,3 @@ func assertError(t *testing.T, err error, msg string) { t.FailNow() } } - -func assertNoError(t *testing.T, err error, msgs ...string) { - if err != nil { - t.Errorf("%s expected no error, got: %s", strings.Join(msgs, " "), err) - t.FailNow() - } -} - -func assertDeepEqual(t *testing.T, expected any, reality any, msgs ...string) { - if !internal.DeepEqual(expected, reality) { - t.Fatalf("%s not equal\nexpected: %+v \ngot : %+v", strings.Join(msgs, " "), expected, reality) - } -} diff --git a/schema/query_test.go b/schema/query_test.go index 3a28b77..efe0421 100644 --- a/schema/query_test.go +++ b/schema/query_test.go @@ -3,6 +3,8 @@ package schema import ( "encoding/json" "testing" + + "gotest.tools/v3/assert" ) func TestUnmarshalFunctionInfo(t *testing.T) { @@ -49,15 +51,15 @@ func TestUnmarshalFunctionInfo(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var procedure FunctionInfo - assertNoError(t, json.Unmarshal([]byte(tc.raw), &procedure)) - assertDeepEqual(t, tc.expected, procedure) + assert.NilError(t, json.Unmarshal([]byte(tc.raw), &procedure)) + assert.DeepEqual(t, tc.expected, procedure) var jsonMap map[string]json.RawMessage - assertNoError(t, json.Unmarshal([]byte(tc.raw), &jsonMap)) + assert.NilError(t, json.Unmarshal([]byte(tc.raw), &jsonMap)) var result FunctionInfo - assertNoError(t, result.UnmarshalJSONMap(jsonMap)) - assertDeepEqual(t, procedure, result) + assert.NilError(t, result.UnmarshalJSONMap(jsonMap)) + assert.DeepEqual(t, procedure, result) }) } } diff --git a/schema/scalar_test.go b/schema/scalar_test.go index 893aa2c..f17dde7 100644 --- a/schema/scalar_test.go +++ b/schema/scalar_test.go @@ -3,6 +3,8 @@ package schema import ( "encoding/json" "testing" + + "gotest.tools/v3/assert" ) func TestTypeRepresentation(t *testing.T) { @@ -14,8 +16,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected integer, got boolean") anyType, ok := rawType.Interface().(*TypeRepresentationBoolean) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("string", func(t *testing.T) { @@ -26,8 +28,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected integer, got string") anyType, ok := rawType.Interface().(*TypeRepresentationString) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("integer", func(t *testing.T) { @@ -38,8 +40,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got integer") anyType, ok := rawType.Interface().(*TypeRepresentationInteger) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("number", func(t *testing.T) { @@ -50,8 +52,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got number") anyType, ok := rawType.Interface().(*TypeRepresentationNumber) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("enum", func(t *testing.T) { @@ -68,18 +70,18 @@ func TestTypeRepresentation(t *testing.T) { var enumType TypeRepresentation assertError(t, json.Unmarshal(rawEmptyBytes, &enumType), "TypeRepresentation requires at least 1 item in one_of field for enum type") - assertNoError(t, json.Unmarshal(rawBytes, &enumType)) + assert.NilError(t, json.Unmarshal(rawBytes, &enumType)) typeRep := NewTypeRepresentationEnum([]string{"foo"}) rawType := typeRep.Encode() - assertDeepEqual(t, enumType, rawType) + assert.DeepEqual(t, enumType, rawType) _, err := rawType.AsString() assertError(t, err, "invalid TypeRepresentation type; expected string, got enum") anyType, ok := rawType.Interface().(*TypeRepresentationEnum) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) invalidTypeRep := NewTypeRepresentationEnum([]string{}) invalidRawType := invalidTypeRep.Encode() @@ -88,7 +90,7 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got enum") _, ok = invalidRawType.Interface().(*TypeRepresentationEnum) - assertDeepEqual(t, true, ok) + assert.DeepEqual(t, true, ok) _, err = (TypeRepresentation{ "type": TypeRepresentationTypeEnum, @@ -104,8 +106,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got int8") anyType, ok := rawType.Interface().(*TypeRepresentationInt8) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("int16", func(t *testing.T) { @@ -116,8 +118,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got int16") anyType, ok := rawType.Interface().(*TypeRepresentationInt16) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("int32", func(t *testing.T) { @@ -128,8 +130,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got int32") anyType, ok := rawType.Interface().(*TypeRepresentationInt32) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("int64", func(t *testing.T) { @@ -140,8 +142,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got int64") anyType, ok := rawType.Interface().(*TypeRepresentationInt64) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("float32", func(t *testing.T) { typeRep := NewTypeRepresentationFloat32() @@ -151,8 +153,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got float32") anyType, ok := rawType.Interface().(*TypeRepresentationFloat32) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("float64", func(t *testing.T) { @@ -163,8 +165,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got float64") anyType, ok := rawType.Interface().(*TypeRepresentationFloat64) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("big integer", func(t *testing.T) { @@ -175,8 +177,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got biginteger") anyType, ok := rawType.Interface().(*TypeRepresentationBigInteger) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("big decimal", func(t *testing.T) { @@ -187,8 +189,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got bigdecimal") anyType, ok := rawType.Interface().(*TypeRepresentationBigDecimal) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("uuid", func(t *testing.T) { @@ -199,8 +201,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got uuid") anyType, ok := rawType.Interface().(*TypeRepresentationUUID) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("date", func(t *testing.T) { @@ -211,8 +213,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got date") anyType, ok := rawType.Interface().(*TypeRepresentationDate) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("timestamp", func(t *testing.T) { @@ -223,8 +225,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got timestamp") anyType, ok := rawType.Interface().(*TypeRepresentationTimestamp) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("timestamptz", func(t *testing.T) { @@ -235,8 +237,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got timestamptz") anyType, ok := rawType.Interface().(*TypeRepresentationTimestampTZ) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("geography", func(t *testing.T) { @@ -247,8 +249,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got geography") anyType, ok := rawType.Interface().(*TypeRepresentationGeography) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("geometry", func(t *testing.T) { @@ -259,8 +261,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got geometry") anyType, ok := rawType.Interface().(*TypeRepresentationGeometry) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("bytes", func(t *testing.T) { @@ -271,8 +273,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got bytes") anyType, ok := rawType.Interface().(*TypeRepresentationBytes) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("json", func(t *testing.T) { @@ -283,8 +285,8 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "invalid TypeRepresentation type; expected string, got json") anyType, ok := rawType.Interface().(*TypeRepresentationJSON) - assertDeepEqual(t, true, ok) - assertDeepEqual(t, anyType, typeRep) + assert.DeepEqual(t, true, ok) + assert.DeepEqual(t, anyType, typeRep) }) t.Run("invalid", func(t *testing.T) { @@ -294,7 +296,7 @@ func TestTypeRepresentation(t *testing.T) { assertError(t, err, "type field is required") _, ok := rawType.Interface().(*TypeRepresentationEnum) - assertDeepEqual(t, false, ok) + assert.DeepEqual(t, false, ok) assertError(t, json.Unmarshal([]byte(`{"type": "enum"}`), &rawType), "required") }) diff --git a/schema/schema_test.go b/schema/schema_test.go index c6f7f37..547cb19 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -4,7 +4,7 @@ import ( "encoding/json" "testing" - "github.com/hasura/ndc-sdk-go/internal" + "gotest.tools/v3/assert" ) func TestQueryRequest(t *testing.T) { @@ -571,20 +571,9 @@ func TestQueryRequest(t *testing.T) { t.FailNow() } - if !internal.DeepEqual(tc.expected.Collection, q.Collection) { - t.Errorf("unexpected collection equality; expected: %+v, got: %+v", tc.expected.Collection, q.Collection) - t.FailNow() - } - - if !internal.DeepEqual(tc.expected.Query, q.Query) { - t.Errorf("unexpected Query equality;\n expected: %+v,\n got: %+v\n", tc.expected.Query, q.Query) - t.FailNow() - } - - if !internal.DeepEqual(tc.expected.CollectionRelationships, q.CollectionRelationships) { - t.Errorf("unexpected CollectionRelationships equality;\n expected: %+v,\n got: %+v\n", tc.expected.CollectionRelationships, q.CollectionRelationships) - t.FailNow() - } + assert.DeepEqual(t, tc.expected.Collection, q.Collection) + assert.DeepEqual(t, tc.expected.Query, q.Query) + assert.DeepEqual(t, tc.expected.CollectionRelationships, q.CollectionRelationships) }) } } diff --git a/schema/type_test.go b/schema/type_test.go index 5fd6f4d..89c1dfe 100644 --- a/schema/type_test.go +++ b/schema/type_test.go @@ -1,6 +1,10 @@ package schema -import "testing" +import ( + "testing" + + "gotest.tools/v3/assert" +) func TestTypeStringer(t *testing.T) { testCases := []struct { @@ -35,7 +39,7 @@ func TestTypeStringer(t *testing.T) { for _, tc := range testCases { t.Run(tc.Expected, func(t *testing.T) { - assertDeepEqual(t, tc.Expected, tc.Input.String()) + assert.DeepEqual(t, tc.Expected, tc.Input.String()) }) } } @@ -73,7 +77,7 @@ func TestGetUnderlyingNamedType(t *testing.T) { for _, tc := range testCases { t.Run(tc.Name, func(t *testing.T) { - assertDeepEqual(t, tc.Expected, GetUnderlyingNamedType(tc.Input)) + assert.DeepEqual(t, tc.Expected, GetUnderlyingNamedType(tc.Input)) }) } } @@ -111,7 +115,7 @@ func TestUnwrapNullableType(t *testing.T) { for _, tc := range testCases { t.Run(tc.Name, func(t *testing.T) { - assertDeepEqual(t, tc.Expected, UnwrapNullableType(tc.Input)) + assert.DeepEqual(t, tc.Expected, UnwrapNullableType(tc.Input)) }) } } diff --git a/utils/connector.go b/utils/connector.go index 4e1c231..de49b41 100644 --- a/utils/connector.go +++ b/utils/connector.go @@ -284,21 +284,21 @@ func MergeSchemas(schemas ...*schema.SchemaResponse) (*schema.SchemaResponse, [] } for _, col := range s.Collections { if _, ok := collectionMap[col.Name]; ok { - errs = append(errs, fmt.Errorf("collection `%s` exists", col.Name)) + errs = append(errs, fmt.Errorf("collection %s exists", col.Name)) } collectionMap[col.Name] = col } for _, fn := range s.Functions { if _, ok := functionMap[fn.Name]; ok { - errs = append(errs, fmt.Errorf("function `%s` exists", fn.Name)) + errs = append(errs, fmt.Errorf("function %s exists", fn.Name)) } functionMap[fn.Name] = fn } for _, fn := range s.Procedures { if _, ok := procedureMap[fn.Name]; ok { - errs = append(errs, fmt.Errorf("procedure `%s` exists", fn.Name)) + errs = append(errs, fmt.Errorf("procedure %s exists", fn.Name)) } procedureMap[fn.Name] = fn } @@ -307,18 +307,18 @@ func MergeSchemas(schemas ...*schema.SchemaResponse) (*schema.SchemaResponse, [] result.Functions = GetSortedValuesByKey(functionMap) result.Procedures = GetSortedValuesByKey(procedureMap) - for k, sl := range s.ScalarTypes { - if _, ok := result.ScalarTypes[k]; ok { - errs = append(errs, fmt.Errorf("scalar type %s exists", k)) - } - result.ScalarTypes[k] = sl - } for k, obj := range s.ObjectTypes { if _, ok := result.ObjectTypes[k]; ok { - errs = append(errs, fmt.Errorf("object type %s exists", k)) + errs = append(errs, fmt.Errorf("object %s exists", k)) } result.ObjectTypes[k] = obj } + for k, sl := range s.ScalarTypes { + if _, ok := result.ScalarTypes[k]; ok { + errs = append(errs, fmt.Errorf("scalar %s exists", k)) + } + result.ScalarTypes[k] = sl + } } return &result, errs } diff --git a/utils/connector_test.go b/utils/connector_test.go index fa308de..6643405 100644 --- a/utils/connector_test.go +++ b/utils/connector_test.go @@ -2,11 +2,13 @@ package utils import ( "errors" + "reflect" "testing" "time" - "github.com/hasura/ndc-sdk-go/internal" + "github.com/google/go-cmp/cmp" "github.com/hasura/ndc-sdk-go/schema" + "gotest.tools/v3/assert" ) func TestEvalNestedFields(t *testing.T) { @@ -81,8 +83,8 @@ func TestEvalNestedFields(t *testing.T) { }).Encode(), Expected: map[string]any{ "id": "1", - "articles": []map[string]any{ - { + "articles": []any{ + map[string]any{ "id": 1, "created_at": time.Date(2020, 01, 01, 00, 0, 0, 0, time.UTC), }, @@ -118,8 +120,8 @@ func TestEvalNestedFields(t *testing.T) { Expected: map[string]any{ "id": "1", "Name": "John", - "articles": []map[string]any{ - { + "articles": []any{ + map[string]any{ "name": "Article 1", }, }, @@ -134,10 +136,7 @@ func TestEvalNestedFields(t *testing.T) { t.Errorf("failed to evaluate nested column fields: %s", err) t.FailNow() } - if !internal.DeepEqual(tc.Expected, result) { - t.Errorf("evaluated result does not equal\nexpected: %+v, got: %+v", tc.Expected, result) - t.FailNow() - } + assert.DeepEqual(t, tc.Expected, result) }) } } @@ -362,16 +361,6 @@ func TestMergeSchemas(t *testing.T) { }, }, }, - "GetTypesArgumentsObjectPtr": schema.ObjectType{ - Fields: schema.ObjectTypeFields{ - "Lat": schema.ObjectField{ - Type: schema.NewNamedType("Int32").Encode(), - }, - "Long": schema.ObjectField{ - Type: schema.NewNamedType("Int32").Encode(), - }, - }, - }, "HelloResult": schema.ObjectType{ Fields: schema.ObjectTypeFields{ "error": schema.ObjectField{ @@ -663,12 +652,11 @@ func TestMergeSchemas(t *testing.T) { }, }, Errors: []error{ - errors.New("collection type Foo exists"), - errors.New("function type getBool exists"), - errors.New("procedure type create_article exists"), - errors.New("object type HelloResult exists"), - errors.New("object type GetTypesArgumentsObjectPtr exists"), - errors.New("scalar type BigInt exists"), + errors.New("collection Foo exists"), + errors.New("function getBool exists"), + errors.New("procedure create_article exists"), + errors.New("object HelloResult exists"), + errors.New("scalar BigInt exists"), }, }, } @@ -676,12 +664,12 @@ func TestMergeSchemas(t *testing.T) { for _, tc := range testCases { t.Run(tc.Name, func(t *testing.T) { result, errs := MergeSchemas(tc.Inputs...) - assertDeepEqual(t, tc.Errors, errs) - assertDeepEqual(t, tc.Expected.Collections, result.Collections) - assertDeepEqual(t, tc.Expected.Functions, result.Functions) - assertDeepEqual(t, tc.Expected.ObjectTypes, result.ObjectTypes) - assertDeepEqual(t, tc.Expected.Procedures, result.Procedures) - assertDeepEqual(t, tc.Expected.ScalarTypes, result.ScalarTypes) + assert.DeepEqual(t, tc.Errors, errs, cmp.Exporter(func(t reflect.Type) bool { return true })) + assert.DeepEqual(t, tc.Expected.Collections, result.Collections) + assert.DeepEqual(t, tc.Expected.Functions, result.Functions) + assert.DeepEqual(t, tc.Expected.ObjectTypes, result.ObjectTypes) + assert.DeepEqual(t, tc.Expected.Procedures, result.Procedures) + assert.DeepEqual(t, tc.Expected.ScalarTypes, result.ScalarTypes) }) } } diff --git a/utils/decode_test.go b/utils/decode_test.go index f349d1a..1865740 100644 --- a/utils/decode_test.go +++ b/utils/decode_test.go @@ -8,15 +8,9 @@ import ( "time" "github.com/google/uuid" - "github.com/hasura/ndc-sdk-go/internal" + "gotest.tools/v3/assert" ) -func assertNoError(t *testing.T, err error) { - if err != nil { - t.Fatalf("expected no error, got: %s", err) - } -} - func assertError(t *testing.T, err error, message string) { if err == nil { t.Fatal("expected error, got nil") @@ -31,23 +25,17 @@ func assertEqual(t *testing.T, expected any, reality any) { } } -func assertDeepEqual(t *testing.T, expected any, reality any) { - if !internal.DeepEqual(expected, reality) { - t.Fatalf("not equal, expected: %+v got: %+v", expected, reality) - } -} - func TestDecodeBool(t *testing.T) { value, err := DecodeBoolean(true) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, value) ptr, err := DecodeNullableBoolean(true) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, *ptr) ptr2, err := DecodeNullableBoolean(ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, *ptr, *ptr2) _, err = DecodeBoolean(nil) @@ -59,16 +47,16 @@ func TestDecodeBool(t *testing.T) { func TestDecodeBooleanSlice(t *testing.T) { value, err := DecodeBooleanSlice([]bool{true, false}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true, false}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true, false}, value) value, err = DecodeBooleanSlice([]any{false, true}) - assertNoError(t, err) - assertDeepEqual(t, []bool{false, true}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{false, true}, value) value, err = DecodeBooleanSlice(&[]any{true}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true}, value) _, err = DecodeBooleanSlice(nil) assertError(t, err, "boolean slice must not be null") @@ -88,35 +76,35 @@ func TestDecodeBooleanSlice(t *testing.T) { func TestDecodeNullableBooleanSlice(t *testing.T) { value, err := DecodeNullableBooleanSlice([]bool{true, false}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true, false}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true, false}, *value) value, err = DecodeNullableBooleanSlice([]*bool{ToPtr(true), ToPtr(false)}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true, false}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true, false}, *value) value, err = DecodeNullableBooleanSlice(&[]*bool{ToPtr(true), ToPtr(false)}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true, false}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true, false}, *value) value, err = DecodeNullableBooleanSlice([]any{false, true}) - assertNoError(t, err) - assertDeepEqual(t, []bool{false, true}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{false, true}, *value) value, err = DecodeNullableBooleanSlice(&[]any{true}) - assertNoError(t, err) - assertDeepEqual(t, []bool{true}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true}, *value) _, err = DecodeNullableBooleanSlice([]any{nil}) assertError(t, err, "boolean element at 0 must not be null") value, err = DecodeNullableBooleanSlice(nil) - assertNoError(t, err) - assertDeepEqual(t, []bool{}, value) + assert.NilError(t, err) + assert.Check(t, value == nil) value, err = DecodeNullableBooleanSlice((*string)(nil)) - assertNoError(t, err) - assertDeepEqual(t, []bool{}, value) + assert.NilError(t, err) + assert.Check(t, value == nil) _, err = DecodeNullableBooleanSlice([]any{"true"}) assertError(t, err, "failed to decode boolean element at 0: failed to convert Boolean, got: interface") @@ -131,15 +119,15 @@ func TestDecodeNullableBooleanSlice(t *testing.T) { func TestDecodeString(t *testing.T) { value, err := DecodeString("success") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "success", value) ptr, err := DecodeNullableString("pointer") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "pointer", *ptr) ptr2, err := DecodeNullableString(ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, *ptr, *ptr2) _, err = DecodeString(nil) @@ -151,24 +139,24 @@ func TestDecodeString(t *testing.T) { func TestDecodeStringSlice(t *testing.T) { value, err := DecodeStringSlice([]string{"foo", "bar"}) - assertNoError(t, err) - assertDeepEqual(t, []string{"foo", "bar"}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"foo", "bar"}, value) value, err = DecodeStringSlice([]*string{ToPtr("foo"), ToPtr("bar")}) - assertNoError(t, err) - assertDeepEqual(t, []string{"foo", "bar"}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"foo", "bar"}, value) value, err = DecodeStringSlice(&[]*string{ToPtr("foo")}) - assertNoError(t, err) - assertDeepEqual(t, []string{"foo"}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"foo"}, value) value, err = DecodeStringSlice([]any{"bar", "foo"}) - assertNoError(t, err) - assertDeepEqual(t, []string{"bar", "foo"}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"bar", "foo"}, value) value, err = DecodeStringSlice(&[]any{"foo"}) - assertNoError(t, err) - assertDeepEqual(t, []string{"foo"}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"foo"}, value) _, err = DecodeStringSlice(nil) assertError(t, err, "string slice must not be null") @@ -188,23 +176,23 @@ func TestDecodeStringSlice(t *testing.T) { func TestDecodeNullableStringSlice(t *testing.T) { _, err := DecodeNullableStringSlice([]string{"foo", "bar"}) - assertNoError(t, err) + assert.NilError(t, err) _, err = DecodeNullableStringSlice([]*string{ToPtr("foo"), ToPtr("bar")}) - assertNoError(t, err) + assert.NilError(t, err) _, err = DecodeNullableStringSlice(&[]*string{ToPtr("foo")}) - assertNoError(t, err) + assert.NilError(t, err) _, err = DecodeNullableStringSlice([]any{"bar", "foo"}) - assertNoError(t, err) + assert.NilError(t, err) _, err = DecodeNullableStringSlice(&[]any{"foo"}) - assertNoError(t, err) + assert.NilError(t, err) value, err := DecodeNullableStringSlice(nil) - assertNoError(t, err) - assertDeepEqual(t, []string{}, value) + assert.NilError(t, err) + assert.Check(t, value == nil) _, err = DecodeNullableStringSlice("failure") assertError(t, err, "expected a string slice, got: string") @@ -220,34 +208,34 @@ func TestDecodeDateTime(t *testing.T) { now := time.Now() t.Run("decode_value", func(t *testing.T) { value, err := DecodeDateTime(now) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now, value) }) t.Run("unix_int", func(t *testing.T) { iNow := now.UnixMilli() value, err := DecodeDateTime(iNow) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now.UnixMilli(), value.UnixMilli()) value, err = DecodeDateTime(&iNow) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now.UnixMilli(), value.UnixMilli()) var nilI64 *int64 = nil ptr, err := DecodeNullableDateTime(nilI64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, IsNil(ptr)) }) t.Run("from_string", func(t *testing.T) { nowStr := now.Format(time.RFC3339) value, err := DecodeDateTime(nowStr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now.Unix(), value.Unix()) value, err = DecodeDateTime(&nowStr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now.Unix(), value.Unix()) invalidStr := "test" @@ -257,11 +245,11 @@ func TestDecodeDateTime(t *testing.T) { t.Run("decode_pointer", func(t *testing.T) { ptr, err := DecodeNullableDateTime(&now) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, now, *ptr) ptr2, err := DecodeNullableDateTime(ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, *ptr, *ptr2) }) @@ -280,29 +268,29 @@ func TestDecodeDuration(t *testing.T) { duration := 10 * time.Second t.Run("decode_value", func(t *testing.T) { value, err := DecodeDuration(duration) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, value) }) t.Run("unix_int", func(t *testing.T) { iDuration := int64(duration) value, err := DecodeDuration(iDuration) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, value) value, err = DecodeDuration(&iDuration) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, value) }) t.Run("from_string", func(t *testing.T) { durationStr := "10s" value, err := DecodeDuration(durationStr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, value) value, err = DecodeDuration(&durationStr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, value) invalidStr := "test" @@ -314,17 +302,17 @@ func TestDecodeDuration(t *testing.T) { var nilStr *string nilValue, err := DecodeNullableDuration(nilStr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, IsNil(nilValue)) }) t.Run("decode_pointer", func(t *testing.T) { ptr, err := DecodeNullableDuration(&duration) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, duration, *ptr) ptr2, err := DecodeNullableDuration(ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, *ptr, *ptr2) }) @@ -345,15 +333,15 @@ func TestDecodeInt(t *testing.T) { t.Run(fmt.Sprintf("decode_%s", reflect.TypeOf(expected).String()), func(t *testing.T) { value, err := DecodeInt[int64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprint(value)) ptr, err := DecodeNullableInt[int64](&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprint(*ptr)) ptr2, err := DecodeNullableInt[int64](ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(*ptr), fmt.Sprint(*ptr2)) }) } @@ -361,57 +349,57 @@ func TestDecodeInt(t *testing.T) { t.Run("decode_pointers", func(t *testing.T) { vInt := int(1) ptr, err := DecodeNullableInt[int64](&vInt) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt), *ptr) ptrInt := &vInt ptr, err = DecodeNullableInt[int64](&ptrInt) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt), *ptr) vInt8 := int8(1) ptr, err = DecodeNullableInt[int64](&vInt8) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt8), *ptr) vInt16 := int16(1) ptr, err = DecodeNullableInt[int64](&vInt16) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt16), *ptr) vInt32 := int32(1) ptr, err = DecodeNullableInt[int64](&vInt32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt32), *ptr) vInt64 := int64(1) ptr, err = DecodeNullableInt[int64](&vInt64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vInt64), *ptr) vUint := uint(1) ptr, err = DecodeNullableInt[int64](&vUint) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vUint), *ptr) vUint8 := uint8(1) ptr, err = DecodeNullableInt[int64](&vUint8) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vUint8), *ptr) vUint16 := uint16(1) ptr, err = DecodeNullableInt[int64](&vUint16) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vUint16), *ptr) vUint32 := uint32(1) ptr, err = DecodeNullableInt[int64](&vUint32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vUint32), *ptr) vUint64 := uint64(1) ptr, err = DecodeNullableInt[int64](&vUint64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, int64(vUint64), *ptr) var vAny any = float64(1.1) @@ -456,13 +444,13 @@ func TestDecodeIntSlice(t *testing.T) { {&[]*string{ToPtr("11")}, []int{11}}, {&[]*float32{ToPtr[float32](12)}, []int{12}}, {&[]*float64{ToPtr[float64](13)}, []int{13}}, } { value, err := DecodeIntSlice[int](tc.input) - assertNoError(t, err) - assertDeepEqual(t, tc.expected, value) + assert.NilError(t, err) + assert.DeepEqual(t, tc.expected, value) } value, err := DecodeIntSlice[int](&[]any{"1", 2}) - assertNoError(t, err) - assertDeepEqual(t, []int{1, 2}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []int{1, 2}, value) _, err = DecodeIntSlice[int](nil) assertError(t, err, "the int slice must not be null") @@ -502,17 +490,17 @@ func TestDecodeNullableIntSlice(t *testing.T) { {&[]*string{ToPtr("11")}, []int{11}}, {&[]*float32{ToPtr[float32](12)}, []int{12}}, {&[]*float64{ToPtr[float64](13)}, []int{13}}, } { value, err := DecodeNullableIntSlice[int](tc.input) - assertNoError(t, err) - assertDeepEqual(t, tc.expected, value) + assert.NilError(t, err) + assert.DeepEqual(t, tc.expected, *value) } value, err := DecodeNullableIntSlice[int](&[]any{"1", 2}) - assertNoError(t, err) - assertDeepEqual(t, []int{1, 2}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []int{1, 2}, *value) value, err = DecodeNullableIntSlice[int](nil) - assertNoError(t, err) - assertDeepEqual(t, []int{}, value) + assert.NilError(t, err) + assert.Check(t, value == nil) _, err = DecodeNullableIntSlice[int]([]any{nil}) assertError(t, err, "number element at 0 must not be null") @@ -530,19 +518,19 @@ func TestDecodeUint(t *testing.T) { t.Run(fmt.Sprintf("decode_%s", reflect.TypeOf(expected).String()), func(t *testing.T) { value, err := DecodeUint[uint64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprint(value)) value, err = DecodeUint[uint64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprint(value)) ptr, err := DecodeNullableUint[uint64](&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprint(*ptr)) ptr2, err := DecodeNullableUint[uint64](ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(*ptr), fmt.Sprint(*ptr2)) }) } @@ -550,12 +538,12 @@ func TestDecodeUint(t *testing.T) { t.Run("decode_pointers", func(t *testing.T) { vUint := uint(1) ptr, err := DecodeNullableUint[uint64](&vUint) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, uint64(vUint), *ptr) ptrUint := &vUint ptr, err = DecodeNullableUint[uint64](&ptrUint) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, uint64(vUint), *ptr) var vAny any = float64(1.1) @@ -580,18 +568,18 @@ func TestDecodeUintSlice(t *testing.T) { &[]int{1}, &[]int8{2}, &[]int16{3}, &[]int32{4}, &[]int64{5}, &[]uint{6}, &[]uint8{7}, &[]uint16{8}, &[]uint32{9}, &[]uint64{10}, &[]string{"11"}, &[]float32{12}, &[]float64{13}, } { value, err := DecodeUintSlice[uint64](expected) - assertNoError(t, err) + assert.NilError(t, err) rexpected := reflect.ValueOf(expected) if rexpected.Kind() == reflect.Pointer { - assertDeepEqual(t, fmt.Sprint(rexpected.Elem().Interface()), fmt.Sprint(value)) + assert.DeepEqual(t, fmt.Sprint(rexpected.Elem().Interface()), fmt.Sprint(value)) } else { - assertDeepEqual(t, fmt.Sprint(expected), fmt.Sprint(value)) + assert.DeepEqual(t, fmt.Sprint(expected), fmt.Sprint(value)) } } value, err := DecodeUintSlice[uint64](&[]any{"1", 2}) - assertNoError(t, err) - assertDeepEqual(t, []int{1, 2}, value) + assert.NilError(t, err) + assert.DeepEqual(t, []uint64{1, 2}, value) _, err = DecodeUintSlice[uint64](nil) assertError(t, err, "the uint slice must not be null") @@ -614,15 +602,15 @@ func TestDecodeFloat(t *testing.T) { t.Run(fmt.Sprintf("decode_%s", reflect.TypeOf(expected).String()), func(t *testing.T) { value, err := DecodeFloat[float64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprintf("%.0f", value)) ptr, err := DecodeNullableFloat[float64](&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(expected), fmt.Sprintf("%.0f", *ptr)) ptr2, err := DecodeNullableFloat[float64](ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprintf("%.1f", *ptr), fmt.Sprintf("%.1f", *ptr2)) }) } @@ -631,15 +619,15 @@ func TestDecodeFloat(t *testing.T) { t.Run(fmt.Sprintf("decode_%s", reflect.TypeOf(expected).String()), func(t *testing.T) { value, err := DecodeFloat[float64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprintf("%.1f", expected), fmt.Sprintf("%.1f", value)) ptr, err := DecodeNullableFloat[float64](&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprintf("%.1f", expected), fmt.Sprintf("%.1f", *ptr)) ptr2, err := DecodeNullableFloat[float64](ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprintf("%.1f", *ptr), fmt.Sprintf("%.1f", *ptr2)) }) } @@ -648,15 +636,15 @@ func TestDecodeFloat(t *testing.T) { expected := "0" value, err := DecodeFloat[float64](expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "0", fmt.Sprintf("%.0f", value)) ptr, err := DecodeNullableFloat[float64](&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, expected, fmt.Sprintf("%.0f", *ptr)) ptr2, err := DecodeNullableFloat[float64](ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprintf("%.1f", *ptr), fmt.Sprintf("%.1f", *ptr2)) }) @@ -664,77 +652,77 @@ func TestDecodeFloat(t *testing.T) { vInt := int(1) ptr, err := DecodeNullableFloat[float64](&vInt) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt), *ptr) ptrInt := &vInt ptr, err = DecodeNullableFloat[float64](&ptrInt) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt), *ptr) vInt8 := int8(1) ptr, err = DecodeNullableFloat[float64](&vInt8) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt8), *ptr) vInt16 := int16(1) ptr, err = DecodeNullableFloat[float64](&vInt16) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt16), *ptr) vInt32 := int32(1) ptr, err = DecodeNullableFloat[float64](&vInt32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt32), *ptr) vInt64 := int64(1) ptr, err = DecodeNullableFloat[float64](&vInt64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vInt64), *ptr) vUint := uint(1) ptr, err = DecodeNullableFloat[float64](&vUint) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint), *ptr) ptrUint := &vUint ptr, err = DecodeNullableFloat[float64](&ptrUint) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint), *ptr) vUint8 := uint8(1) ptr, err = DecodeNullableFloat[float64](&vUint8) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint8), *ptr) vUint16 := uint16(1) ptr, err = DecodeNullableFloat[float64](&vUint16) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint16), *ptr) vUint32 := uint32(1) ptr, err = DecodeNullableFloat[float64](&vUint32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint32), *ptr) vUint64 := uint64(1) ptr, err = DecodeNullableFloat[float64](&vUint64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vUint64), *ptr) vFloat32 := float32(1) ptr, err = DecodeNullableFloat[float64](&vFloat32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vFloat32), *ptr) ptrFloat32 := &vFloat32 ptr, err = DecodeNullableFloat[float64](&ptrFloat32) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vFloat32), *ptr) vFloat64 := float64(2.2) ptr, err = DecodeNullableFloat[float64](&vFloat64) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, float64(vFloat64), *ptr) var vAny any = "test" @@ -761,18 +749,18 @@ func TestDecodeUUID(t *testing.T) { t.Run("decode_value", func(t *testing.T) { expected := uuid.NewString() value, err := DecodeUUID(expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, expected, value.String()) }) t.Run("decode_pointer", func(t *testing.T) { expected := uuid.NewString() ptr, err := DecodeNullableUUID(&expected) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, expected, (*ptr).String()) ptr2, err := DecodeNullableUUID(ptr) - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, ptr.String(), ptr2.String()) }) @@ -829,120 +817,120 @@ func TestDecodeNestedInterface(t *testing.T) { } b, err := GetBoolean(fixture, "bool") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, b) s, err := GetString(fixture, "string") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "test", s) i, err := GetInt[int](fixture, "int") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, i) f, err := GetFloat[float32](fixture, "float") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(1.1), fmt.Sprint(f)) b, err = GetBoolean(fixture, "boolPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, b) s, err = GetString(fixture, "stringPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "test", s) i, err = GetInt[int](fixture, "intPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, i) f, err = GetFloat[float32](fixture, "floatPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(1.1), fmt.Sprint(f)) arrItemAny := fixture["array"].([]any)[0] arrItem := arrItemAny.(map[string]any) b, err = GetBoolean(arrItem, "bool") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, b) s, err = GetString(arrItem, "string") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "test", s) i, err = GetInt[int](arrItem, "int") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, i) f, err = GetFloat[float32](arrItem, "float") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(1.1), fmt.Sprint(f)) b, err = GetBoolean(arrItem, "boolPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, b) s, err = GetString(arrItem, "stringPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, "test", s) i, err = GetInt[int](arrItem, "intPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, i) f, err = GetFloat[float32](arrItem, "floatPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(1.1), fmt.Sprint(f)) j, err := GetArbitraryJSON(arrItem, "int") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, j) jp, err := GetNullableArbitraryJSON(arrItem, "int") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, 1, *jp) jp, err = GetNullableArbitraryJSON(arrItem, "floatPtr") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, fmt.Sprint(1.1), fmt.Sprint(*jp)) jp, err = GetNullableArbitraryJSON(arrItem, "not_exist") - assertNoError(t, err) + assert.NilError(t, err) assertEqual(t, true, jp == nil) bs, err := GetBooleanSlice(fixture, "boolSlicePtr") - assertNoError(t, err) - assertDeepEqual(t, []bool{true, false}, bs) + assert.NilError(t, err) + assert.DeepEqual(t, []bool{true, false}, bs) _, err = GetNullableBooleanSlice(fixture, "boolSlicePtr") - assertNoError(t, err) + assert.NilError(t, err) ss, err := GetStringSlice(fixture, "stringSlicePtr") - assertNoError(t, err) - assertDeepEqual(t, []string{"foo", "bar"}, ss) + assert.NilError(t, err) + assert.DeepEqual(t, []string{"foo", "bar"}, ss) _, err = GetNullableStringSlice(fixture, "stringSlicePtr") - assertNoError(t, err) + assert.NilError(t, err) is, err := GetIntSlice[int64](fixture, "intSlicePtr") - assertNoError(t, err) - assertDeepEqual(t, []int64{1, 2, 3}, is) + assert.NilError(t, err) + assert.DeepEqual(t, []int64{1, 2, 3}, is) _, err = GetNullableIntSlice[int64](fixture, "intSlicePtr") - assertNoError(t, err) + assert.NilError(t, err) uis, err := GetUintSlice[uint64](fixture, "uintSlicePtr") - assertNoError(t, err) - assertDeepEqual(t, []uint64{1, 2, 3}, uis) + assert.NilError(t, err) + assert.DeepEqual(t, []uint64{1, 2, 3}, uis) _, err = GetNullableUintSlice[uint64](fixture, "uintSlicePtr") - assertNoError(t, err) + assert.NilError(t, err) fis, err := GetFloatSlice[float32](fixture, "floatSlicePtr") - assertNoError(t, err) - assertDeepEqual(t, []float32{1.1, 2.2, 3.3}, fis) + assert.NilError(t, err) + assert.DeepEqual(t, []float32{1.1, 2.2, 3.3}, fis) _, err = GetNullableFloatSlice[float32](fixture, "floatSlicePtr") - assertNoError(t, err) + assert.NilError(t, err) } diff --git a/utils/helper_test.go b/utils/helper_test.go index e41c4a3..6770dbc 100644 --- a/utils/helper_test.go +++ b/utils/helper_test.go @@ -3,17 +3,19 @@ package utils import ( "log/slog" "testing" + + "gotest.tools/v3/assert" ) func TestToPtrs(t *testing.T) { _, err := PointersToValues([]*string{ToPtr(""), nil}) assertError(t, err, "element at 1 must not be nil") input, err := PointersToValues(ToPtrs([]string{"a", "b", "c"})) - assertNoError(t, err) + assert.NilError(t, err) expected, err := PointersToValues([]*string{ToPtr("a"), ToPtr("b"), ToPtr("c")}) - assertNoError(t, err) + assert.NilError(t, err) - assertDeepEqual(t, expected, input) + assert.DeepEqual(t, expected, input) } func TestIsDebug(t *testing.T) { @@ -31,7 +33,7 @@ func TestGetSortedKeys(t *testing.T) { } expected := []string{"a", "b", "c", "d"} - assertDeepEqual(t, expected, GetSortedKeys(input)) + assert.DeepEqual(t, expected, GetSortedKeys(input)) } func TestMergeMap(t *testing.T) { @@ -43,8 +45,8 @@ func TestMergeMap(t *testing.T) { "a": 1, "b": 2, } - assertDeepEqual(t, mapB, MergeMap(nil, mapB)) - assertDeepEqual(t, map[string]any{ + assert.DeepEqual(t, mapB, MergeMap(nil, mapB)) + assert.DeepEqual(t, map[string]any{ "a": 1, "b": 2, "c": 3,