Files
go-admin/common/actions/generic_compile_test.go
T
zhangwenjian fda94c4965 refactor🎨: move app/jobs onto the generic actions
The five /sysjob routes use the generic actions. The detail route uses
ViewAs to keep answering SysJobItem, whose entryId the edit form reads
where the model marshals entry_id. GenerateM becomes ToModel, and the
Generate methods go.

Recorded before the change and replayed after it, the same 14 requests
get byte-identical responses; answering the detail with the model
instead of SysJobItem makes exactly the two detail requests differ.

With a real ToModel on both modules, a test now builds a package that
pairs the demo model with the jobs request and requires the build to
fail on that pairing, next to the same package with the pair corrected.
2026-09-27 20:46:12 +08:00

37 lines
1.1 KiB
Go

package actions_test
import (
"os/exec"
"path/filepath"
"strings"
"testing"
)
// A model paired with a request that builds a different model does not
// compile - the thing the older actions left to a type assertion at run
// time. Checked by building a package that does it, next to a control that
// is the same package with the pair corrected.
func TestGenericActionsRefuseAMismatchedPair(t *testing.T) {
root, err := filepath.Abs("../..")
if err != nil {
t.Fatal(err)
}
vet := func(pkg string) (string, error) {
cmd := exec.Command("go", "vet", "./common/actions/testdata/"+pkg)
cmd.Dir = root
out, err := cmd.CombinedOutput()
return string(out), err
}
if out, err := vet("matched"); err != nil {
t.Fatalf("the control does not build, so the mismatch below proves nothing:\n%s", out)
}
out, err := vet("mismatch")
if err == nil {
t.Fatal("a model paired with another model's request compiled")
}
if !strings.Contains(out, "does not satisfy") || !strings.Contains(out, "ToModel") {
t.Fatalf("the build failed, but not on the pairing:\n%s", out)
}
}