152 lines
5.1 KiB
Go
152 lines
5.1 KiB
Go
// Package assert 四层断言 + 每步六字段报告。
|
||||
|
|
package assert
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"strings"
|
|||
|
|
|
|||
|
|
"bj_power_test/internal/client"
|
|||
|
|
"bj_power_test/internal/db"
|
|||
|
|
"bj_power_test/internal/fixture"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Layer 断言分层。
|
|||
|
|
type Layer string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
L1 Layer = "L1 接口响应"
|
|||
|
|
L2 Layer = "L2 数据库落库"
|
|||
|
|
L3 Layer = "L3 状态流转"
|
|||
|
|
L4 Layer = "L4 交付物"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Result 每步报告:步骤ID + 案例.md行号 + 期望 + 实际 + 通过/失败 + 失败原因。
|
|||
|
|
type Result struct {
|
|||
|
|
StepID string `json:"stepId"`
|
|||
|
|
CaseLine string `json:"caseLine"`
|
|||
|
|
Layer Layer `json:"layer"`
|
|||
|
|
Expect string `json:"expect"`
|
|||
|
|
Actual string `json:"actual"`
|
|||
|
|
Pass bool `json:"pass"`
|
|||
|
|
FailReason string `json:"failReason"`
|
|||
|
|
Status string `json:"status"` // 通过 / 失败 / 阻断
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Ctx 用例执行上下文。
|
|||
|
|
type Ctx struct {
|
|||
|
|
C context.Context
|
|||
|
|
Run *fixture.Run
|
|||
|
|
Client *client.Set
|
|||
|
|
DB *db.DB
|
|||
|
|
Results []Result
|
|||
|
|
Blocked bool
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func NewCtx(c context.Context, run *fixture.Run, cl *client.Set, d *db.DB) *Ctx {
|
|||
|
|
return &Ctx{C: c, Run: run, Client: cl, DB: d}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (c *Ctx) add(r Result) {
|
|||
|
|
if r.Status == "" {
|
|||
|
|
if r.Pass {
|
|||
|
|
r.Status = "通过"
|
|||
|
|
} else {
|
|||
|
|
r.Status = "失败"
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
c.Results = append(c.Results, r)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Block 标记阻断:服务/库不可用时不编造数据,明确写「阻断」。
|
|||
|
|
func (c *Ctx) Block(stepID, caseLine, expect, reason string) {
|
|||
|
|
c.Blocked = true
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L1, Expect: expect,
|
|||
|
|
Actual: "未执行(前置条件不满足)", FailReason: reason, Status: "阻断"})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// L1 接口响应:HTTP 状态码、业务码、提示文案(三者都判)。
|
|||
|
|
func (c *Ctx) L1(stepID, caseLine, expect string, resp *client.Response, callErr error, wantHTTP, wantCode int, wantMsg string) {
|
|||
|
|
if callErr != nil {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L1, Expect: expect,
|
|||
|
|
Actual: "调用异常:" + callErr.Error(), FailReason: "HTTP 请求未送达(服务未启动或网络不通)", Status: "阻断"})
|
|||
|
|
c.Blocked = true
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
var reasons []string
|
|||
|
|
if resp.HTTPStatus != wantHTTP {
|
|||
|
|
reasons = append(reasons, fmt.Sprintf("HTTP 状态码期望 %d 实际 %d", wantHTTP, resp.HTTPStatus))
|
|||
|
|
}
|
|||
|
|
if resp.Code != wantCode {
|
|||
|
|
reasons = append(reasons, fmt.Sprintf("业务码期望 %d 实际 %d", wantCode, resp.Code))
|
|||
|
|
}
|
|||
|
|
if wantMsg != "" && !strings.Contains(resp.Message+resp.Raw, wantMsg) {
|
|||
|
|
reasons = append(reasons, fmt.Sprintf("提示文案未包含 %q", wantMsg))
|
|||
|
|
}
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L1, Expect: expect,
|
|||
|
|
Actual: resp.Brief(), Pass: len(reasons) == 0, FailReason: strings.Join(reasons, ";")})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// L2 数据库落库:直连库执行 SQL,比对期望值。
|
|||
|
|
func (c *Ctx) L2(stepID, caseLine, expect string, target db.Target, query string, args []any, want string) {
|
|||
|
|
if !c.DB.Ready(target) {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L2, Expect: expect,
|
|||
|
|
Actual: "库未连接", FailReason: string(target) + " 库不可用", Status: "阻断"})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
got, err := c.DB.Scalar(c.C, target, query, args...)
|
|||
|
|
actual := fmt.Sprintf("%s = %q", query, got)
|
|||
|
|
if err != nil {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L2, Expect: expect,
|
|||
|
|
Actual: "SQL 执行失败:" + err.Error(), FailReason: err.Error()})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
pass := got == want
|
|||
|
|
reason := ""
|
|||
|
|
if !pass {
|
|||
|
|
reason = fmt.Sprintf("期望 %q,实际 %q", want, got)
|
|||
|
|
}
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L2, Expect: expect, Actual: actual,
|
|||
|
|
Pass: pass, FailReason: reason})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// L3 状态流转:查单据当前状态是否与状态机期望一致。
|
|||
|
|
func (c *Ctx) L3(stepID, caseLine, expect string, target db.Target, query string, args []any, allow []string) {
|
|||
|
|
if !c.DB.Ready(target) {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L3, Expect: expect,
|
|||
|
|
Actual: "库未连接", FailReason: string(target) + " 库不可用", Status: "阻断"})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
got, err := c.DB.Scalar(c.C, target, query, args...)
|
|||
|
|
if err != nil {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L3, Expect: expect,
|
|||
|
|
Actual: "SQL 执行失败:" + err.Error(), FailReason: err.Error()})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
pass := false
|
|||
|
|
for _, a := range allow {
|
|||
|
|
if got == a {
|
|||
|
|
pass = true
|
|||
|
|
break
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
reason := ""
|
|||
|
|
if !pass {
|
|||
|
|
reason = fmt.Sprintf("状态期望属于 %v,实际 %q", allow, got)
|
|||
|
|
}
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L3, Expect: expect,
|
|||
|
|
Actual: fmt.Sprintf("当前状态 = %q", got), Pass: pass, FailReason: reason})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// L4 交付物:接口返回或聚合查询是否产出交付物数据。
|
|||
|
|
func (c *Ctx) L4(stepID, caseLine, expect, actual string, pass bool, reason string) {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L4, Expect: expect,
|
|||
|
|
Actual: actual, Pass: pass, FailReason: reason})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Note 记录一条不带断言的事实(例如前一步真实单号),便于报告串起流水。
|
|||
|
|
func (c *Ctx) Note(stepID, caseLine, fact string) {
|
|||
|
|
c.add(Result{StepID: stepID, CaseLine: caseLine, Layer: L1, Expect: "记录本步真实产出供下一步使用",
|
|||
|
|
Actual: fact, Pass: true, Status: "通过"})
|
|||
|
|
}
|