From ccd7e32cbf0400c0640de1256c427a97ff0582dd Mon Sep 17 00:00:00 2001 From: ~wispem-wantex Date: Sat, 14 Feb 2026 18:33:41 -0800 Subject: [PATCH] codegen: test file now uses deep.Equal instead of comparing one field --- .../modelgenerate/generate_testfile.go | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/pkg/codegen/modelgenerate/generate_testfile.go b/pkg/codegen/modelgenerate/generate_testfile.go index d866b3e..ed7d740 100644 --- a/pkg/codegen/modelgenerate/generate_testfile.go +++ b/pkg/codegen/modelgenerate/generate_testfile.go @@ -69,6 +69,46 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam 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{ Params: &ast.FieldList{ List: []*ast.Field{{ @@ -151,15 +191,8 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam })}, }, - // assert.Equal(t, item.Description, item2.Description) - &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}, - }, - }}, + // if deep.Equal(...) {...} + makeDeepEqual(testObj, testObj2), ) stmts = append(stmts, @@ -189,15 +222,8 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam })}, }, - // assert.Equal(t, item.Description, item2.Description) - &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}, - }, - }}, + // if deep.Equal(...) {...} + makeDeepEqual(testObj, testObj2), ) indexGets, hasIndexedGets := []ast.Stmt{ @@ -415,6 +441,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)}, 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/require"`}}, },