codegen: CreateUpdateDelete test no longer creates an "Update" block if there's no updateable fields

This commit is contained in:
2026-09-19 21:36:31 -07:00
parent f98f8416e8
commit 6cf0b66dd5
4 changed files with 99 additions and 57 deletions

View File

@@ -67,26 +67,18 @@ func SampleValue(c pkgschema.Column, offset int) ast.Expr {
}
// UpdateTestFields returns the columns for which the test generator should assign its own
// sample values, in the MakeXyz() factory and in the create/update test: the same columns
// that GenerateSaveItemFunc's "update" branch writes to, minus foreign keys (which can't be
// given plausible values here), the auto-managed "created_at"/"updated_at" columns, and the
// soft-delete columns (which SoftDeleteXyz() manages).
// sample values in MakeXyz() and the create/update test. It starts with Table.GetUpdateableColumns()
// and removes fields the generated test should not assign directly: foreign keys and auto-managed
// updated_at.
func UpdateTestFields(tbl pkgschema.Table) (ret []pkgschema.Column) {
hasCreatedAt, hasUpdatedAt := tbl.HasAutoTimestamps()
hasSoftDelete := tbl.HasSoftDelete()
for _, c := range tbl.Columns {
if c.Name == "rowid" || c.IsPrimaryKey || c.IsForeignKey {
continue
}
if c.Name == "created_at" && hasCreatedAt {
_, hasUpdatedAt := tbl.HasAutoTimestamps()
for _, c := range tbl.GetUpdateableColumns() {
if c.IsForeignKey {
continue
}
if c.Name == "updated_at" && hasUpdatedAt {
continue
}
if hasSoftDelete && (c.Name == "is_deleted" || c.Name == "deleted_at") {
continue
}
ret = append(ret, c)
}
return
@@ -486,39 +478,41 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
makeDeepEqual(testObj, testObj2),
)
stmts = append(stmts,
BlankLine(),
Comment("Update"),
)
if len(tbl.GetUpdateableColumns()) > 0 { // Skip if no mutable fields
stmts = append(stmts,
BlankLine(),
Comment("Update"),
)
// item.Description = <sample value, offset 2>
// ...one assignment per updatable field
for _, c := range updateTestFields {
stmts = append(stmts, &ast.AssignStmt{
Lhs: []ast.Expr{&ast.SelectorExpr{X: testObj, Sel: ast.NewIdent(c.GoFieldName())}},
Tok: token.ASSIGN,
Rhs: []ast.Expr{SampleValue(c, 2)},
})
// item.Description = <sample value, offset 2>
// ...one assignment per updatable field
for _, c := range updateTestFields {
stmts = append(stmts, &ast.AssignStmt{
Lhs: []ast.Expr{&ast.SelectorExpr{X: testObj, Sel: ast.NewIdent(c.GoFieldName())}},
Tok: token.ASSIGN,
Rhs: []ast.Expr{SampleValue(c, 2)},
})
}
stmts = append(stmts,
// TestDB.SaveItem(&item)
&ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent("Save" + tbl.GoTypeName)},
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
}},
// item2 = must.Get(TestDB.GetItemByID(item.ID))
&ast.AssignStmt{
Lhs: []ast.Expr{testObj2},
Tok: token.ASSIGN,
Rhs: []ast.Expr{mustCall(getItemByPKCall(testObj))},
},
// if deep.Equal(...) {...}
makeDeepEqual(testObj, testObj2),
)
}
stmts = append(stmts,
// TestDB.SaveItem(&item)
&ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.SelectorExpr{X: testDB, Sel: ast.NewIdent("Save" + tbl.GoTypeName)},
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
}},
// item2 = must.Get(TestDB.GetItemByID(item.ID))
&ast.AssignStmt{
Lhs: []ast.Expr{testObj2},
Tok: token.ASSIGN,
Rhs: []ast.Expr{mustCall(getItemByPKCall(testObj))},
},
// if deep.Equal(...) {...}
makeDeepEqual(testObj, testObj2),
)
indexGets, hasIndexedGets := []ast.Stmt{
BlankLine(),
Comment("Indexed lookups"),