fake 版本库不实现 Store 的 1..100 上限,正是它让"限 out of range"一路绿到线上。 这组用例跑真实 PostgreSQL:真实 Run → contentTrackedTarget → 真实 libraryStageRecorder.WriteSetPlanned → 真实 store,断言台账在第一笔写入之前可读、 原备份证据字段与 writeset_digest 原子共存,并把记录推到 250 条更新版本之后 (窗口里已看不到它)仍能按身份取回。 store 级契约用例覆盖:命中、真缺失 found=false、别的作业=冲突、同身份两行=拒绝挑选、 跨 scope/跨 kind 不命中、不完整身份拒绝。变异成"列最近 N 条再筛"时四条全红。 隔离 schema 逻辑收敛到 pgtest 一份,postgres 侧改为委托。
This commit is contained in:
@@ -1,44 +1,14 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"knowledge-governance/internal/platform/postgres/pgtest"
|
||||
)
|
||||
|
||||
// 隔离 schema 的实现只留一份(pgtest):postgres 与 cmd/worker 的集成用例
|
||||
// 都要连真库,复制一份到两边,迟早只改一边。
|
||||
func isolatedIntegrationDatabaseURL(t *testing.T, databaseURL string) string {
|
||||
t.Helper()
|
||||
if databaseURL == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(databaseURL)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
t.Fatalf("TEST_DATABASE_URL must be a PostgreSQL URL: %v", err)
|
||||
}
|
||||
digest := sha256.Sum256([]byte(t.Name()))
|
||||
schema := "kgtest_" + hex.EncodeToString(digest[:8])
|
||||
ctx := context.Background()
|
||||
admin, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("connect integration database: %v", err)
|
||||
}
|
||||
defer admin.Close()
|
||||
if _, err := admin.Exec(ctx, "CREATE SCHEMA "+schema); err != nil {
|
||||
t.Fatalf("create isolated integration schema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
cleanup, err := pgxpool.New(context.Background(), databaseURL)
|
||||
if err == nil {
|
||||
_, _ = cleanup.Exec(context.Background(), "DROP SCHEMA IF EXISTS "+schema+" CASCADE")
|
||||
cleanup.Close()
|
||||
}
|
||||
})
|
||||
query := parsed.Query()
|
||||
query.Set("search_path", schema)
|
||||
parsed.RawQuery = query.Encode()
|
||||
return parsed.String()
|
||||
return pgtest.IsolatedDatabaseURL(t, databaseURL)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/platform/postgres/pgtest"
|
||||
)
|
||||
|
||||
// 版本台账的身份查询必须按 (scope, kind, 作业, 快照) 精确命中:
|
||||
// 缺 → found=false(调用方失败),冲突 → 错误(调用方拒绝),
|
||||
// 而不是"读最近 N 条再筛"——历史超过窗口就会把存在读成不存在。
|
||||
// 这里跑真实 PostgreSQL,因为契约的一半是 Store 的分页上限与 SQL 条件本身。
|
||||
|
||||
func openVersionStore(t *testing.T) (*Store, *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
databaseURL := pgtest.IsolatedDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
|
||||
if databaseURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
return NewStore(pool), pool
|
||||
}
|
||||
|
||||
func recordPrebackup(t *testing.T, ctx context.Context, store *Store, scope, jobID, snapshot string, created time.Time) {
|
||||
t.Helper()
|
||||
if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{
|
||||
ID: governance.LibraryVersionID(governance.LibraryVersionProductionPrebackup, scope, snapshot),
|
||||
Scope: scope, Kind: governance.LibraryVersionProductionPrebackup, SnapshotID: snapshot,
|
||||
SourceJobID: jobID, CreatedBy: "worker",
|
||||
Evidence: map[string]any{"backup_snapshot": snapshot, "backup_bytes": 4096, "job": jobID},
|
||||
CreatedAt: created,
|
||||
}); err != nil {
|
||||
t.Fatalf("record prebackup %s: %v", snapshot, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVersionByRefSeparatesMissingFromConflict(t *testing.T) {
|
||||
store, _ := openVersionStore(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
recordPrebackup(t, ctx, store, "internal", "job-a", "snap-1", now)
|
||||
|
||||
version, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionPrebackup, "job-a", "snap-1")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("本作业的记录必须命中:found=%v err=%v", found, err)
|
||||
}
|
||||
if version.Evidence["backup_bytes"] != float64(4096) {
|
||||
t.Fatalf("命中的记录必须带着原有证据:%v", version.Evidence)
|
||||
}
|
||||
|
||||
// 同一个快照的台账归别的作业所有:这是冲突,必须报错,不能当成"没有记录"。
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionPrebackup, "job-b", "snap-1"); err == nil || found {
|
||||
t.Fatalf("作业不一致时必须报冲突:found=%v err=%v", found, err)
|
||||
} else if !strings.Contains(err.Error(), "属于作业 job-a") {
|
||||
t.Fatalf("冲突错误要指明归属,实际:%v", err)
|
||||
}
|
||||
|
||||
// 真的没有这条记录:found=false 且无错误(调用方据此失败,可重试造记录)。
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionPrebackup, "job-a", "snap-missing"); err != nil || found {
|
||||
t.Fatalf("缺失必须是 found=false 且无错误:found=%v err=%v", found, err)
|
||||
}
|
||||
// 目标范围隔离:另一个 scope 的同名快照不算命中。
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, "external", governance.LibraryVersionProductionPrebackup, "job-a", "snap-1"); err != nil || found {
|
||||
t.Fatalf("跨 scope 不许命中:found=%v err=%v", found, err)
|
||||
}
|
||||
// 同快照但别的类型(例如生效版本)也不算命中。
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionEffective, "job-a", "snap-1"); err != nil || found {
|
||||
t.Fatalf("跨 kind 不许命中:found=%v err=%v", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVersionByRefRejectsAmbiguousIdentity(t *testing.T) {
|
||||
store, pool := openVersionStore(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
recordPrebackup(t, ctx, store, "internal", "job-a", "snap-dup", now)
|
||||
// 台账被写坏(同 scope+kind+snapshot 两行、不同 id):身份不再唯一。
|
||||
if _, err := pool.Exec(ctx, `
|
||||
INSERT INTO library_versions (id, scope, kind, snapshot_id, source_job_id, evidence, created_by, created_at)
|
||||
VALUES ('libver-dup-fixture','internal','production_prebackup','snap-dup','job-a','{}'::jsonb,'worker',$1)`, now); err != nil {
|
||||
t.Fatalf("insert duplicate row: %v", err)
|
||||
}
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionPrebackup, "job-a", "snap-dup"); err == nil || found {
|
||||
t.Fatalf("身份不唯一时必须拒绝:found=%v err=%v", found, err)
|
||||
} else if !strings.Contains(err.Error(), "拒绝挑选") {
|
||||
t.Fatalf("错误要说清是重复身份,实际:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVersionByRefFindsRowOutsideHistoryWindow(t *testing.T) {
|
||||
store, _ := openVersionStore(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
// 目标记录最老:任何"最近 N 条"(真实上限 100)都看不到它。
|
||||
recordPrebackup(t, ctx, store, "internal", "job-old", "snap-old", now.Add(-48*time.Hour))
|
||||
for i := 0; i < 150; i++ {
|
||||
recordPrebackup(t, ctx, store, "internal", fmt.Sprintf("job-new-%03d", i), fmt.Sprintf("snap-new-%03d", i),
|
||||
now.Add(-time.Duration(i)*time.Minute))
|
||||
}
|
||||
window, err := store.LibraryVersions(ctx, "internal", 100)
|
||||
if err != nil {
|
||||
t.Fatalf("list versions: %v", err)
|
||||
}
|
||||
for _, item := range window {
|
||||
if item.SnapshotID == "snap-old" {
|
||||
t.Fatal("窗口里仍能看到目标记录:本用例证明不了窗口之外仍可精确取回(夹具失效)")
|
||||
}
|
||||
}
|
||||
version, found, err := store.LibraryVersionByRef(ctx, "internal", governance.LibraryVersionProductionPrebackup, "job-old", "snap-old")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("窗口之外也必须精确命中:found=%v err=%v", found, err)
|
||||
}
|
||||
if version.SourceJobID != "job-old" || version.SnapshotID != "snap-old" {
|
||||
t.Fatalf("命中的不是目标记录:%+v", version)
|
||||
}
|
||||
}
|
||||
|
||||
// 空身份不许被当成"查所有":少一个字段就等于换了一种查询,必须直接拒绝。
|
||||
func TestLibraryVersionByRefRejectsIncompleteRef(t *testing.T) {
|
||||
store, _ := openVersionStore(t)
|
||||
ctx := context.Background()
|
||||
cases := []struct{ scope, job, snapshot string }{
|
||||
{"", "job-a", "snap-1"},
|
||||
{"internal", "", "snap-1"},
|
||||
{"internal", "job-a", ""},
|
||||
}
|
||||
for _, item := range cases {
|
||||
if _, found, err := store.LibraryVersionByRef(ctx, item.scope,
|
||||
governance.LibraryVersionProductionPrebackup, item.job, item.snapshot); err == nil || found {
|
||||
t.Fatalf("不完整身份必须拒绝:%+v found=%v err=%v", item, found, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Package pgtest 给真实 PostgreSQL 集成用例提供"一个用例一个 schema"的隔离入口。
|
||||
//
|
||||
// 为什么要单独成包:postgres 与 cmd/worker 的集成用例都要连真库,隔离逻辑只该有一份;
|
||||
// 复制一份到另一个包里,迟早只改一边(AGENTS.md 硬约束 1:同一含义的代码只准有一份)。
|
||||
package pgtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// IsolatedDatabaseURL 为当前用例建一个独立 schema 并返回指向它的连接串;
|
||||
// 用例结束自动 DROP。基地址为空时返回空串(调用方据此 Skip)。
|
||||
func IsolatedDatabaseURL(t *testing.T, databaseURL string) string {
|
||||
t.Helper()
|
||||
if databaseURL == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(databaseURL)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
t.Fatalf("TEST_DATABASE_URL must be a PostgreSQL URL: %v", err)
|
||||
}
|
||||
digest := sha256.Sum256([]byte(t.Name()))
|
||||
schema := "kgtest_" + hex.EncodeToString(digest[:8])
|
||||
ctx := context.Background()
|
||||
admin, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("connect integration database: %v", err)
|
||||
}
|
||||
defer admin.Close()
|
||||
if _, err := admin.Exec(ctx, "CREATE SCHEMA "+schema); err != nil {
|
||||
t.Fatalf("create isolated integration schema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
cleanup, err := pgxpool.New(context.Background(), databaseURL)
|
||||
if err == nil {
|
||||
_, _ = cleanup.Exec(context.Background(), "DROP SCHEMA IF EXISTS "+schema+" CASCADE")
|
||||
cleanup.Close()
|
||||
}
|
||||
})
|
||||
query := parsed.Query()
|
||||
query.Set("search_path", schema)
|
||||
parsed.RawQuery = query.Encode()
|
||||
return parsed.String()
|
||||
}
|
||||
Reference in New Issue
Block a user