|
|
|
|
@@ -8,6 +8,7 @@ import (
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
|
|
|
|
|
|
pkgschema "git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GenerateModelTestAST produces an AST for a starter test file for a given model.
|
|
|
|
|
@@ -15,9 +16,11 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
packageName := "db"
|
|
|
|
|
testpackageName := packageName + "_test"
|
|
|
|
|
|
|
|
|
|
makeHelperName := ast.NewIdent("Make" + tbl.GoTypeName)
|
|
|
|
|
|
|
|
|
|
// func MakeItem() Item { return Item{} }
|
|
|
|
|
makeItemFunc := &ast.FuncDecl{
|
|
|
|
|
Name: ast.NewIdent("Make" + tbl.GoTypeName),
|
|
|
|
|
Name: makeHelperName,
|
|
|
|
|
Type: &ast.FuncType{
|
|
|
|
|
Params: &ast.FieldList{},
|
|
|
|
|
Results: &ast.FieldList{
|
|
|
|
|
@@ -57,8 +60,8 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testObj := ast.NewIdent("item")
|
|
|
|
|
testObj2 := ast.NewIdent("item2")
|
|
|
|
|
testObj := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName))
|
|
|
|
|
testObj2 := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName) + "2")
|
|
|
|
|
fieldName := ast.NewIdent("Description") // TODO
|
|
|
|
|
description1 := `"an item"`
|
|
|
|
|
description2 := `"a big item"`
|
|
|
|
|
@@ -94,13 +97,13 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
&ast.AssignStmt{
|
|
|
|
|
Lhs: []ast.Expr{testObj},
|
|
|
|
|
Tok: token.DEFINE,
|
|
|
|
|
Rhs: []ast.Expr{&ast.CallExpr{Fun: ast.NewIdent("MakeItem"), Args: nil}},
|
|
|
|
|
Rhs: []ast.Expr{&ast.CallExpr{Fun: makeHelperName, Args: nil}},
|
|
|
|
|
},
|
|
|
|
|
// item.Description = "an item"
|
|
|
|
|
&ast.AssignStmt{
|
|
|
|
|
Lhs: []ast.Expr{
|
|
|
|
|
&ast.SelectorExpr{
|
|
|
|
|
X: ast.NewIdent("item"),
|
|
|
|
|
X: testObj,
|
|
|
|
|
Sel: ast.NewIdent("Description"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
@@ -307,11 +310,11 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
shouldDefineErr := true
|
|
|
|
|
for _, col := range tbl.Columns {
|
|
|
|
|
if col.IsForeignKey {
|
|
|
|
|
shouldIncludeTestFkCheck = true
|
|
|
|
|
stmts = append(stmts, []ast.Stmt{
|
|
|
|
|
|
|
|
|
|
// post.QuotedPostID = 94354538969386985
|
|
|
|
|
&ast.AssignStmt{
|
|
|
|
|
Lhs: []ast.Expr{
|
|
|
|
|
@@ -332,7 +335,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
// err := db.SavePost(&post)
|
|
|
|
|
&ast.AssignStmt{
|
|
|
|
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
|
|
|
|
Tok: token.DEFINE,
|
|
|
|
|
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[shouldDefineErr],
|
|
|
|
|
Rhs: []ast.Expr{
|
|
|
|
|
&ast.CallExpr{
|
|
|
|
|
Fun: &ast.SelectorExpr{
|
|
|
|
|
@@ -368,6 +371,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}...)
|
|
|
|
|
shouldDefineErr = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return stmts
|
|
|
|
|
|