From 143fc6834f5816ebc517b05f9586521dbf59c6b3 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Sun, 25 Feb 2024 00:52:20 +0700 Subject: [PATCH] improve unit tests --- codegen/connector.go | 2 +- codegen/schema.go | 3 --- codegen/templates/new/connector.go.tmpl | 3 +-- codegen/templates/new/go.mod.tmpl | 3 +-- codegen/templates/new/main.go.tmpl | 2 +- codegen/testdata/basic/schema.json | 10 ++++++++-- codegen/testdata/basic/source/comment.go.tmpl | 12 +++++------- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/codegen/connector.go b/codegen/connector.go index 1e6237b..577b9d2 100644 --- a/codegen/connector.go +++ b/codegen/connector.go @@ -50,7 +50,7 @@ func generateConnector(rawSchema *RawConnectorSchema, srcPath string, moduleName importLines := []string{} for importPath := range rawSchema.Imports { - importLines = append(importLines, importPath) + importLines = append(importLines, fmt.Sprintf(`"%s"`, importPath)) } targetPath := path.Join(srcPath, connectorOutputFile) f, err := os.Create(targetPath) diff --git a/codegen/schema.go b/codegen/schema.go index 4f723be..de0338e 100644 --- a/codegen/schema.go +++ b/codegen/schema.go @@ -484,7 +484,6 @@ func (sp *SchemaParser) parseOperationInfo(functionName string, pos token.Pos) * log.Debug().Msgf("unsupported operation kind: %s", matches) } - log.Printf("matches: %v", matches) if matchesLen > 3 && strings.TrimSpace(matches[3]) != "" { result.Name = strings.TrimSpace(matches[3]) } else { @@ -523,9 +522,7 @@ func findCommentsFromPos(fset *token.FileSet, files map[string]*ast.File, pos to continue } offset := pos - 10 - // log.Printf("file: %s, pos: %v, position: %v", fName, pos, position.Filename) for _, cg := range f.Comments { - // log.Printf("text: %s, end: %v, pos: %v - %v", cg.Text(), cg.End(), offset, pos) if len(cg.List) > 0 && (cg.Pos() <= token.Pos(offset) && cg.End() >= token.Pos(offset)) { return cg } diff --git a/codegen/templates/new/connector.go.tmpl b/codegen/templates/new/connector.go.tmpl index d2567c9..4e2562d 100644 --- a/codegen/templates/new/connector.go.tmpl +++ b/codegen/templates/new/connector.go.tmpl @@ -5,7 +5,6 @@ import ( "github.com/hasura/ndc-sdk-go/connector" "github.com/hasura/ndc-sdk-go/schema" - "github.com/swaggest/jsonschema-go" "{{.Module}}/types" ) @@ -14,7 +13,7 @@ type Connector struct{} // ParseConfiguration validates the configuration files provided by the user, returning a validated 'Configuration', // or throwing an error to prevents Connector startup. -func ParseConfiguration(configurationDir string) (*types.Configuration, error) { +func (connector *Connector) ParseConfiguration(configurationDir string) (*types.Configuration, error) { return &types.Configuration{}, nil } diff --git a/codegen/templates/new/go.mod.tmpl b/codegen/templates/new/go.mod.tmpl index 3287bf3..d0cf1c4 100644 --- a/codegen/templates/new/go.mod.tmpl +++ b/codegen/templates/new/go.mod.tmpl @@ -3,6 +3,5 @@ module {{.Module}} go 1.18 require ( - github.com/hasura/ndc-sdk-go v0.0.0-20240218161048-ce1f9dfc50bc - github.com/swaggest/jsonschema-go v0.3.64 + github.com/hasura/ndc-sdk-go v0.0.0-20240223161935-98c2e9f3b445 ) diff --git a/codegen/templates/new/main.go.tmpl b/codegen/templates/new/main.go.tmpl index 51be93a..afd932c 100644 --- a/codegen/templates/new/main.go.tmpl +++ b/codegen/templates/new/main.go.tmpl @@ -6,7 +6,7 @@ import ( ) func main() { - if err := connector.Start[types.RawConfiguration, types.Configuration, types.State]( + if err := connector.Start[types.Configuration, types.State]( &Connector{}, connector.WithMetricsPrefix("{{.Name}}"), connector.WithDefaultServiceName("{{.Name}}"), diff --git a/codegen/testdata/basic/schema.json b/codegen/testdata/basic/schema.json index 7ab65cf..e5a9241 100644 --- a/codegen/testdata/basic/schema.json +++ b/codegen/testdata/basic/schema.json @@ -56,7 +56,7 @@ "CreateArticleArgumentsAuthor": { "fields": { "created_at": { "type": { "name": "DateTime", "type": "named" } }, - "id": { "type": { "name": "String", "type": "named" } } + "id": { "type": { "name": "UUID", "type": "named" } } } }, "CreateArticleResult": { @@ -137,7 +137,13 @@ } }, "name": "create_article", - "result_type": { "name": "CreateArticleResult", "type": "named" } + "result_type": { + "type": "nullable", + "underlying_type": { + "name": "CreateArticleResult", + "type": "named" + } + } }, { "arguments": { diff --git a/codegen/testdata/basic/source/comment.go.tmpl b/codegen/testdata/basic/source/comment.go.tmpl index 5e8cf4e..510a940 100644 --- a/codegen/testdata/basic/source/comment.go.tmpl +++ b/codegen/testdata/basic/source/comment.go.tmpl @@ -29,7 +29,7 @@ func GetArticles(ctx context.Context, state *State, arguments *GetArticlesArgume type CreateArticleArguments struct { Author struct { - ID string `json:"id"` + ID uuid.UUID `json:"id"` CreatedAt time.Time `json:"created_at"` } `json:"author"` } @@ -45,11 +45,9 @@ type CreateArticleResult struct { } // @procedure create_article -func CreateArticle(ctx context.Context, state *State, arguments *CreateArticleArguments) (CreateArticleResult, error) { - return []CreateArticleResult{ - { - ID: 1, - Authors: []Author{}, - }, +func CreateArticle(ctx context.Context, state *State, arguments *CreateArticleArguments) (*CreateArticleResult, error) { + return &CreateArticleResult{ + ID: 1, + Authors: []Author{}, }, nil }