codegen: use "require.NoError(...)" when saving objects that return errors
This commit is contained in:
parent
ccd7e32cbf
commit
ad1782c73d
@ -118,6 +118,103 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate FK Check test func first, because it also detects whether there are foreign keys
|
||||||
|
shouldIncludeTestFkCheck := false
|
||||||
|
testFkChecking := &ast.FuncDecl{
|
||||||
|
Name: ast.NewIdent("Test" + tbl.GoTypeName + "FkChecking"),
|
||||||
|
Type: testFuncType,
|
||||||
|
Body: &ast.BlockStmt{
|
||||||
|
List: func() (stmts []ast.Stmt) {
|
||||||
|
isFirst := true
|
||||||
|
for _, col := range tbl.Columns {
|
||||||
|
if !col.IsForeignKey {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shouldIncludeTestFkCheck = true
|
||||||
|
|
||||||
|
// post := MakePost()
|
||||||
|
if !isFirst {
|
||||||
|
stmts = append(stmts, BlankLine())
|
||||||
|
}
|
||||||
|
|
||||||
|
stmts = append(stmts, []ast.Stmt{
|
||||||
|
// Comment header
|
||||||
|
Comment(fmt.Sprintf("Invalid %s", col.GoFieldName())),
|
||||||
|
|
||||||
|
// `Invalid
|
||||||
|
&ast.AssignStmt{
|
||||||
|
Lhs: []ast.Expr{ast.NewIdent(tbl.VarName)},
|
||||||
|
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
||||||
|
Rhs: []ast.Expr{
|
||||||
|
&ast.CallExpr{
|
||||||
|
Fun: ast.NewIdent("Make" + tbl.GoTypeName),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// `post.QuotedPostID = 94354538969386985`
|
||||||
|
&ast.AssignStmt{
|
||||||
|
Lhs: []ast.Expr{
|
||||||
|
&ast.SelectorExpr{
|
||||||
|
X: ast.NewIdent(tbl.VarName),
|
||||||
|
Sel: ast.NewIdent(col.GoFieldName()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Tok: token.ASSIGN,
|
||||||
|
Rhs: []ast.Expr{
|
||||||
|
&ast.BasicLit{
|
||||||
|
Kind: token.INT,
|
||||||
|
Value: "94354538969386985",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// `err := db.SavePost(&post)`
|
||||||
|
&ast.AssignStmt{
|
||||||
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
||||||
|
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
||||||
|
Rhs: []ast.Expr{
|
||||||
|
&ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{
|
||||||
|
X: testDB,
|
||||||
|
Sel: ast.NewIdent("Save" + tbl.GoTypeName),
|
||||||
|
},
|
||||||
|
Args: []ast.Expr{
|
||||||
|
&ast.UnaryExpr{
|
||||||
|
Op: token.AND,
|
||||||
|
X: ast.NewIdent(tbl.VarName),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// `assertForeignKeyError(t, err, "QuotedPostID", post.QuotedPostID)`
|
||||||
|
&ast.ExprStmt{
|
||||||
|
X: &ast.CallExpr{
|
||||||
|
Fun: ast.NewIdent("AssertForeignKeyError"),
|
||||||
|
Args: []ast.Expr{
|
||||||
|
ast.NewIdent("t"),
|
||||||
|
ast.NewIdent("err"),
|
||||||
|
&ast.BasicLit{
|
||||||
|
Kind: token.STRING,
|
||||||
|
Value: fmt.Sprintf("%q", col.GoFieldName()),
|
||||||
|
},
|
||||||
|
&ast.SelectorExpr{
|
||||||
|
X: ast.NewIdent(tbl.VarName),
|
||||||
|
Sel: ast.NewIdent(col.GoFieldName()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}...)
|
||||||
|
isFirst = false
|
||||||
|
}
|
||||||
|
return stmts
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
testCreateUpdateDelete := &ast.FuncDecl{
|
testCreateUpdateDelete := &ast.FuncDecl{
|
||||||
Name: ast.NewIdent("TestCreateUpdateDelete" + tbl.GoTypeName),
|
Name: ast.NewIdent("TestCreateUpdateDelete" + tbl.GoTypeName),
|
||||||
Type: testFuncType,
|
Type: testFuncType,
|
||||||
@ -156,11 +253,21 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// TestDB.SaveItem(&item)
|
// TestDB.SaveItem(&item), possibly with error check
|
||||||
&ast.ExprStmt{X: &ast.CallExpr{
|
&ast.ExprStmt{X: func() *ast.CallExpr {
|
||||||
|
mainExpr := &ast.CallExpr{
|
||||||
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent("Save" + tbl.GoTypeName)},
|
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent("Save" + tbl.GoTypeName)},
|
||||||
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
|
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
|
||||||
}},
|
}
|
||||||
|
if shouldIncludeTestFkCheck {
|
||||||
|
// Also a check for whether the Save function returns an error
|
||||||
|
return &ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{X: ast.NewIdent("require"), Sel: ast.NewIdent("NoError")},
|
||||||
|
Args: []ast.Expr{ast.NewIdent("t"), mainExpr},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mainExpr
|
||||||
|
}()},
|
||||||
|
|
||||||
// require.NotZero(t, item.ID)
|
// require.NotZero(t, item.ID)
|
||||||
&ast.ExprStmt{X: &ast.CallExpr{
|
&ast.ExprStmt{X: &ast.CallExpr{
|
||||||
@ -318,102 +425,6 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldIncludeTestFkCheck := false
|
|
||||||
testFkChecking := &ast.FuncDecl{
|
|
||||||
Name: ast.NewIdent("Test" + tbl.GoTypeName + "FkChecking"),
|
|
||||||
Type: testFuncType,
|
|
||||||
Body: &ast.BlockStmt{
|
|
||||||
List: func() (stmts []ast.Stmt) {
|
|
||||||
isFirst := true
|
|
||||||
for _, col := range tbl.Columns {
|
|
||||||
if !col.IsForeignKey {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
shouldIncludeTestFkCheck = true
|
|
||||||
|
|
||||||
// post := MakePost()
|
|
||||||
if !isFirst {
|
|
||||||
stmts = append(stmts, BlankLine())
|
|
||||||
}
|
|
||||||
|
|
||||||
stmts = append(stmts, []ast.Stmt{
|
|
||||||
// Comment header
|
|
||||||
Comment(fmt.Sprintf("Invalid %s", col.GoFieldName())),
|
|
||||||
|
|
||||||
// `Invalid BlahBlahID`
|
|
||||||
&ast.AssignStmt{
|
|
||||||
Lhs: []ast.Expr{ast.NewIdent(tbl.VarName)},
|
|
||||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
|
||||||
Rhs: []ast.Expr{
|
|
||||||
&ast.CallExpr{
|
|
||||||
Fun: ast.NewIdent("Make" + tbl.GoTypeName),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// `post.QuotedPostID = 94354538969386985`
|
|
||||||
&ast.AssignStmt{
|
|
||||||
Lhs: []ast.Expr{
|
|
||||||
&ast.SelectorExpr{
|
|
||||||
X: ast.NewIdent(tbl.VarName),
|
|
||||||
Sel: ast.NewIdent(col.GoFieldName()),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Tok: token.ASSIGN,
|
|
||||||
Rhs: []ast.Expr{
|
|
||||||
&ast.BasicLit{
|
|
||||||
Kind: token.INT,
|
|
||||||
Value: "94354538969386985",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// `err := db.SavePost(&post)`
|
|
||||||
&ast.AssignStmt{
|
|
||||||
Lhs: []ast.Expr{ast.NewIdent("err")},
|
|
||||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
|
||||||
Rhs: []ast.Expr{
|
|
||||||
&ast.CallExpr{
|
|
||||||
Fun: &ast.SelectorExpr{
|
|
||||||
X: testDB,
|
|
||||||
Sel: ast.NewIdent("Save" + tbl.GoTypeName),
|
|
||||||
},
|
|
||||||
Args: []ast.Expr{
|
|
||||||
&ast.UnaryExpr{
|
|
||||||
Op: token.AND,
|
|
||||||
X: ast.NewIdent(tbl.VarName),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// `assertForeignKeyError(t, err, "QuotedPostID", post.QuotedPostID)`
|
|
||||||
&ast.ExprStmt{
|
|
||||||
X: &ast.CallExpr{
|
|
||||||
Fun: ast.NewIdent("AssertForeignKeyError"),
|
|
||||||
Args: []ast.Expr{
|
|
||||||
ast.NewIdent("t"),
|
|
||||||
ast.NewIdent("err"),
|
|
||||||
&ast.BasicLit{
|
|
||||||
Kind: token.STRING,
|
|
||||||
Value: fmt.Sprintf("%q", col.GoFieldName()),
|
|
||||||
},
|
|
||||||
&ast.SelectorExpr{
|
|
||||||
X: ast.NewIdent(tbl.VarName),
|
|
||||||
Sel: ast.NewIdent(col.GoFieldName()),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}...)
|
|
||||||
isFirst = false
|
|
||||||
}
|
|
||||||
return stmts
|
|
||||||
}(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
testList := []ast.Decl{
|
testList := []ast.Decl{
|
||||||
makeItemFunc,
|
makeItemFunc,
|
||||||
testCreateUpdateDelete,
|
testCreateUpdateDelete,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user