Compare commits
No commits in common. "dbf14e23b63bc2d60b1be3a78bcbc75726373a45" and "9a11f3986c2b3b55183c42c991b17b183161a61d" have entirely different histories.
dbf14e23b6
...
9a11f3986c
@ -200,7 +200,7 @@ func buildFKCheckLambda(tbl schema.Table) (*ast.AssignStmt, bool) {
|
|||||||
ret = append(ret, &ast.IfStmt{
|
ret = append(ret, &ast.IfStmt{
|
||||||
Init: &ast.AssignStmt{
|
Init: &ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{ast.NewIdent("err")},
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
||||||
Tok: token.DEFINE,
|
Tok: token.ASSIGN,
|
||||||
Rhs: []ast.Expr{
|
Rhs: []ast.Expr{
|
||||||
&ast.CallExpr{
|
&ast.CallExpr{
|
||||||
Fun: &ast.SelectorExpr{X: dbDB, Sel: ast.NewIdent("Get")},
|
Fun: &ast.SelectorExpr{X: dbDB, Sel: ast.NewIdent("Get")},
|
||||||
|
|||||||
@ -63,52 +63,12 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
testObj := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName))
|
testObj := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName))
|
||||||
testObj2 := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName) + "2")
|
testObj2 := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName) + "2")
|
||||||
fieldName := ast.NewIdent("Description") // TODO
|
fieldName := ast.NewIdent("Description") // TODO
|
||||||
description1 := "an item"
|
description1 := `"an item"`
|
||||||
description2 := "a big item"
|
description2 := `"a big item"`
|
||||||
testDB := ast.NewIdent("TestDB")
|
testDB := ast.NewIdent("TestDB")
|
||||||
|
|
||||||
hasCreatedAt, hasUpdatedAt := tbl.HasAutoTimestamps()
|
hasCreatedAt, hasUpdatedAt := tbl.HasAutoTimestamps()
|
||||||
|
|
||||||
makeDeepEqual := func(obj1 *ast.Ident, obj2 *ast.Ident) *ast.IfStmt {
|
|
||||||
return &ast.IfStmt{
|
|
||||||
Init: &ast.AssignStmt{
|
|
||||||
Lhs: []ast.Expr{
|
|
||||||
&ast.Ident{Name: "diff"},
|
|
||||||
},
|
|
||||||
Tok: token.DEFINE,
|
|
||||||
Rhs: []ast.Expr{
|
|
||||||
&ast.CallExpr{
|
|
||||||
Fun: &ast.SelectorExpr{
|
|
||||||
X: &ast.Ident{Name: "deep"},
|
|
||||||
Sel: &ast.Ident{Name: "Equal"},
|
|
||||||
},
|
|
||||||
Args: []ast.Expr{obj1, obj2},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Cond: &ast.BinaryExpr{
|
|
||||||
X: &ast.Ident{Name: "diff"},
|
|
||||||
Op: token.NEQ,
|
|
||||||
Y: &ast.Ident{Name: "nil"},
|
|
||||||
},
|
|
||||||
Body: &ast.BlockStmt{
|
|
||||||
List: []ast.Stmt{
|
|
||||||
&ast.ExprStmt{
|
|
||||||
X: &ast.CallExpr{
|
|
||||||
Fun: &ast.SelectorExpr{
|
|
||||||
X: &ast.Ident{Name: "t"},
|
|
||||||
Sel: &ast.Ident{Name: "Error"},
|
|
||||||
},
|
|
||||||
Args: []ast.Expr{
|
|
||||||
&ast.Ident{Name: "diff"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
testFuncType := &ast.FuncType{
|
testFuncType := &ast.FuncType{
|
||||||
Params: &ast.FieldList{
|
Params: &ast.FieldList{
|
||||||
List: []*ast.Field{{
|
List: []*ast.Field{{
|
||||||
@ -118,103 +78,6 @@ 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,
|
||||||
@ -253,21 +116,11 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// TestDB.SaveItem(&item), possibly with error check
|
// TestDB.SaveItem(&item)
|
||||||
&ast.ExprStmt{X: func() *ast.CallExpr {
|
&ast.ExprStmt{X: &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{
|
||||||
@ -298,8 +151,15 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
})},
|
})},
|
||||||
},
|
},
|
||||||
|
|
||||||
// if deep.Equal(...) {...}
|
// assert.Equal(t, item.Description, item2.Description)
|
||||||
makeDeepEqual(testObj, testObj2),
|
&ast.ExprStmt{X: &ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{X: ast.NewIdent("assert"), Sel: ast.NewIdent("Equal")},
|
||||||
|
Args: []ast.Expr{
|
||||||
|
ast.NewIdent("t"),
|
||||||
|
&ast.SelectorExpr{X: testObj, Sel: fieldName},
|
||||||
|
&ast.SelectorExpr{X: testObj2, Sel: fieldName},
|
||||||
|
},
|
||||||
|
}},
|
||||||
)
|
)
|
||||||
|
|
||||||
stmts = append(stmts,
|
stmts = append(stmts,
|
||||||
@ -310,7 +170,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{&ast.SelectorExpr{X: testObj, Sel: fieldName}},
|
Lhs: []ast.Expr{&ast.SelectorExpr{X: testObj, Sel: fieldName}},
|
||||||
Tok: token.ASSIGN,
|
Tok: token.ASSIGN,
|
||||||
Rhs: []ast.Expr{&ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("%q", description2)}},
|
Rhs: []ast.Expr{&ast.BasicLit{Kind: token.STRING, Value: description2}},
|
||||||
},
|
},
|
||||||
|
|
||||||
// TestDB.SaveItem(&item)
|
// TestDB.SaveItem(&item)
|
||||||
@ -329,8 +189,15 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
})},
|
})},
|
||||||
},
|
},
|
||||||
|
|
||||||
// if deep.Equal(...) {...}
|
// assert.Equal(t, item.Description, item2.Description)
|
||||||
makeDeepEqual(testObj, testObj2),
|
&ast.ExprStmt{X: &ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{X: ast.NewIdent("assert"), Sel: ast.NewIdent("Equal")},
|
||||||
|
Args: []ast.Expr{
|
||||||
|
ast.NewIdent("t"),
|
||||||
|
&ast.SelectorExpr{X: testObj, Sel: fieldName},
|
||||||
|
&ast.SelectorExpr{X: testObj2, Sel: fieldName},
|
||||||
|
},
|
||||||
|
}},
|
||||||
)
|
)
|
||||||
|
|
||||||
indexGets, hasIndexedGets := []ast.Stmt{
|
indexGets, hasIndexedGets := []ast.Stmt{
|
||||||
@ -425,6 +292,102 @@ 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,
|
||||||
@ -452,7 +415,6 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
||||||
Name: ast.NewIdent("."),
|
Name: ast.NewIdent("."),
|
||||||
},
|
},
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/go-test/deep"`}},
|
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/assert"`}},
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/assert"`}},
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/require"`}},
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/require"`}},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user