codegen: improve the "TestFkChecking" generated function
This commit is contained in:
parent
8c29d455ff
commit
9a11f3986c
@ -297,25 +297,35 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
||||
Name: ast.NewIdent("Test" + tbl.GoTypeName + "FkChecking"),
|
||||
Type: testFuncType,
|
||||
Body: &ast.BlockStmt{
|
||||
List: func() []ast.Stmt {
|
||||
List: func() (stmts []ast.Stmt) {
|
||||
isFirst := true
|
||||
for _, col := range tbl.Columns {
|
||||
if !col.IsForeignKey {
|
||||
continue
|
||||
}
|
||||
shouldIncludeTestFkCheck = true
|
||||
|
||||
// post := MakePost()
|
||||
stmts := []ast.Stmt{
|
||||
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: token.DEFINE,
|
||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
||||
Rhs: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: ast.NewIdent("Make" + tbl.GoTypeName),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
shouldDefineErr := true
|
||||
for _, col := range tbl.Columns {
|
||||
if col.IsForeignKey {
|
||||
shouldIncludeTestFkCheck = true
|
||||
stmts = append(stmts, []ast.Stmt{
|
||||
// post.QuotedPostID = 94354538969386985
|
||||
|
||||
// `post.QuotedPostID = 94354538969386985`
|
||||
&ast.AssignStmt{
|
||||
Lhs: []ast.Expr{
|
||||
&ast.SelectorExpr{
|
||||
@ -332,10 +342,10 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
||||
},
|
||||
},
|
||||
|
||||
// err := db.SavePost(&post)
|
||||
// `err := db.SavePost(&post)`
|
||||
&ast.AssignStmt{
|
||||
Lhs: []ast.Expr{ast.NewIdent("err")},
|
||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[shouldDefineErr],
|
||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[isFirst],
|
||||
Rhs: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
@ -352,7 +362,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
||||
},
|
||||
},
|
||||
|
||||
// assertForeignKeyError(t, err, "QuotedPostID", post.QuotedPostID)
|
||||
// `assertForeignKeyError(t, err, "QuotedPostID", post.QuotedPostID)`
|
||||
&ast.ExprStmt{
|
||||
X: &ast.CallExpr{
|
||||
Fun: ast.NewIdent("AssertForeignKeyError"),
|
||||
@ -371,8 +381,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
||||
},
|
||||
},
|
||||
}...)
|
||||
shouldDefineErr = false
|
||||
}
|
||||
isFirst = false
|
||||
}
|
||||
return stmts
|
||||
}(),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user