73413c59ee
build / build (push) Successful in 2m3s
- profile 新增 137(由 v090 克隆),声明知识域关联表 copied/derived/excluded 分类。 - 导出前与导入写事务内各跑漏表门禁;未声明有数据拒绝,空表警告写入 WriteSet。 - chunk_revisions/wiki_page_revisions 接入范围改写、目标身份映射、自然键与回滚顺序。
188 lines
7.1 KiB
Go
188 lines
7.1 KiB
Go
package v071
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func revisionMapper() *targetIDMapper {
|
||
mapper := newTargetIDMapper(10001, "kb-target", nil)
|
||
for table, ids := range map[string][]string{
|
||
"knowledges": {"doc-src"},
|
||
"chunks": {"chunk-src"},
|
||
"wiki_pages": {"page-src"},
|
||
"chunk_revisions": {"crev-src"},
|
||
"wiki_page_revisions": {"wrev-src"},
|
||
} {
|
||
for _, id := range ids {
|
||
mapper.registerSourceID(table, id)
|
||
}
|
||
}
|
||
return mapper
|
||
}
|
||
|
||
func TestRevisionRowsUseTheSameScopeAndIdentityMapping(t *testing.T) {
|
||
mapper := revisionMapper()
|
||
chunkRevision := map[string]json.RawMessage{
|
||
"id": json.RawMessage(`"crev-src"`),
|
||
"knowledge_id": json.RawMessage(`"doc-src"`),
|
||
"chunk_id": json.RawMessage(`"chunk-src"`),
|
||
"revision": json.RawMessage(`3`),
|
||
"content": json.RawMessage(`"edit history text"`),
|
||
"editor_id": json.RawMessage(`"user-9"`),
|
||
"knowledge_base_id": json.RawMessage(`"kb-source"`),
|
||
}
|
||
if err := mapper.remapRow("chunk_revisions", chunkRevision); err != nil {
|
||
t.Fatalf("chunk revision mapping: %v", err)
|
||
}
|
||
if string(chunkRevision["id"]) != `"`+mapper.mapID("chunk_revisions", "crev-src")+`"` {
|
||
t.Fatal("revision id must be target-mapped")
|
||
}
|
||
if string(chunkRevision["chunk_id"]) != `"`+mapper.mapID("chunks", "chunk-src")+`"` {
|
||
t.Fatal("chunk_id must map to the target chunk identity")
|
||
}
|
||
if string(chunkRevision["knowledge_id"]) != `"`+mapper.mapID("knowledges", "doc-src")+`"` {
|
||
t.Fatal("knowledge_id must map to the target knowledge identity")
|
||
}
|
||
// editor_id 是用户身份,原样保留(目标查不到显示未知编辑者)。
|
||
if string(chunkRevision["editor_id"]) != `"user-9"` {
|
||
t.Fatal("editor_id must be preserved")
|
||
}
|
||
|
||
pageRevision := map[string]json.RawMessage{
|
||
"id": json.RawMessage(`"wrev-src"`),
|
||
"page_id": json.RawMessage(`"page-src"`),
|
||
"slug": json.RawMessage(`"legacy-slug"`),
|
||
"version": json.RawMessage(`2`),
|
||
"content": json.RawMessage(`"wiki history"`),
|
||
"knowledge_base_id": json.RawMessage(`"kb-source"`),
|
||
}
|
||
if err := mapper.remapRow("wiki_page_revisions", pageRevision); err != nil {
|
||
t.Fatalf("wiki page revision mapping: %v", err)
|
||
}
|
||
if string(pageRevision["page_id"]) != `"`+mapper.mapID("wiki_pages", "page-src")+`"` {
|
||
t.Fatal("page_id must map to the target page identity")
|
||
}
|
||
if string(pageRevision["slug"]) != `"legacy-slug"` {
|
||
t.Fatal("slug is content, not identity; it must be preserved")
|
||
}
|
||
}
|
||
|
||
func TestRevisionDanglingReferencesAreRefused(t *testing.T) {
|
||
mapper := revisionMapper()
|
||
cases := []struct {
|
||
name string
|
||
row map[string]json.RawMessage
|
||
}{
|
||
{"unknown chunk", map[string]json.RawMessage{
|
||
"id": json.RawMessage(`"crev-unknown"`), "knowledge_id": json.RawMessage(`"doc-src"`),
|
||
"chunk_id": json.RawMessage(`"chunk-other"`), "revision": json.RawMessage(`1`),
|
||
}},
|
||
{"unknown page", map[string]json.RawMessage{
|
||
"id": json.RawMessage(`"wrev-unknown"`), "page_id": json.RawMessage(`"page-other"`),
|
||
}},
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
table := "chunk_revisions"
|
||
if tc.name == "unknown page" {
|
||
table = "wiki_page_revisions"
|
||
}
|
||
err := mapper.remapRow(table, tc.row)
|
||
if !errors.Is(err, errUnmappedTargetReference) || !strings.Contains(err.Error(), "unmapped") {
|
||
t.Fatalf("dangling reference must be refused, got %v", err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestRevisionUniqueIndexKeysStayUniqueAfterMapping(t *testing.T) {
|
||
mapper := revisionMapper()
|
||
// 同一 chunk 的两个 revision:chunk_id 映射后仍相同,revision 仍是 1/2,
|
||
// 因此 (chunk_id, revision) 唯一性不会被映射破坏。
|
||
rows := []map[string]json.RawMessage{
|
||
{"id": json.RawMessage(`"crev-src"`), "knowledge_id": json.RawMessage(`"doc-src"`),
|
||
"chunk_id": json.RawMessage(`"chunk-src"`), "revision": json.RawMessage(`1`)},
|
||
{"id": json.RawMessage(`"crev-src2"`), "knowledge_id": json.RawMessage(`"doc-src"`),
|
||
"chunk_id": json.RawMessage(`"chunk-src"`), "revision": json.RawMessage(`2`)},
|
||
}
|
||
mapper.registerSourceID("chunk_revisions", "crev-src2")
|
||
mapped := map[string]bool{}
|
||
for _, row := range rows {
|
||
if err := mapper.remapRow("chunk_revisions", row); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
key := string(row["chunk_id"]) + "/" + string(row["revision"])
|
||
if mapped[key] {
|
||
t.Fatalf("mapped unique key collided: %s", key)
|
||
}
|
||
mapped[key] = true
|
||
}
|
||
if len(mapped) != 2 {
|
||
t.Fatal("both revision keys must remain distinct")
|
||
}
|
||
}
|
||
|
||
func TestRevisionTablesAreDeletedBeforeParents(t *testing.T) {
|
||
position := map[string]int{}
|
||
for index, table := range jobCreatedRowDeleteOrder {
|
||
position[table] = index
|
||
}
|
||
for _, child := range []string{"wiki_page_revisions", "chunk_revisions"} {
|
||
if _, ok := position[child]; !ok {
|
||
t.Fatalf("%s missing from recovery delete order", child)
|
||
}
|
||
}
|
||
if position["chunk_revisions"] > position["chunks"] {
|
||
t.Fatal("chunk_revisions must be deleted before chunks")
|
||
}
|
||
if position["wiki_page_revisions"] > position["wiki_pages"] {
|
||
t.Fatal("wiki_page_revisions must be deleted before wiki_pages")
|
||
}
|
||
}
|
||
|
||
func TestRevisionTablesAreNotResourceBodies(t *testing.T) {
|
||
for _, table := range []string{"chunk_revisions", "wiki_page_revisions"} {
|
||
if libraryResourceBodyTable(table) {
|
||
t.Fatalf("%s must not participate in resource discovery (Q2=B)", table)
|
||
}
|
||
}
|
||
sql := libraryResourceBodyReferenceSQL([]string{"chunks", "chunk_revisions", "wiki_page_revisions"}, "TRUE")
|
||
if strings.Contains(sql, "chunk_revisions") || strings.Contains(sql, "wiki_page_revisions") {
|
||
t.Fatalf("resource reference SQL must exclude revision tables: %s", sql)
|
||
}
|
||
if !strings.Contains(sql, "FROM chunks") {
|
||
t.Fatal("resource reference SQL must still scan live chunks")
|
||
}
|
||
}
|
||
|
||
func TestStructuredReferenceValidationCoversRevisions(t *testing.T) {
|
||
snapshot := PostgresSnapshot{Tables: []TableSnapshot{
|
||
newTableSnapshot("knowledges", []json.RawMessage{json.RawMessage(`{"id":"doc-1"}`)}),
|
||
newTableSnapshot("chunks", []json.RawMessage{json.RawMessage(`{"id":"chunk-1","knowledge_id":"doc-1"}`)}),
|
||
newTableSnapshot("chunk_revisions", []json.RawMessage{
|
||
json.RawMessage(`{"id":"crev-1","knowledge_id":"doc-1","chunk_id":"chunk-1"}`),
|
||
}),
|
||
newTableSnapshot("wiki_pages", []json.RawMessage{json.RawMessage(`{"id":"page-1"}`)}),
|
||
newTableSnapshot("wiki_page_revisions", []json.RawMessage{
|
||
json.RawMessage(`{"id":"wrev-1","page_id":"page-1"}`),
|
||
}),
|
||
}}
|
||
if err := validateLibraryStructuredReferences(snapshot); err != nil {
|
||
t.Fatalf("resolved revision references must pass: %v", err)
|
||
}
|
||
broken := snapshot
|
||
broken.Tables = []TableSnapshot{
|
||
newTableSnapshot("knowledges", []json.RawMessage{json.RawMessage(`{"id":"doc-1"}`)}),
|
||
newTableSnapshot("chunks", []json.RawMessage{json.RawMessage(`{"id":"chunk-1","knowledge_id":"doc-1"}`)}),
|
||
newTableSnapshot("chunk_revisions", []json.RawMessage{
|
||
json.RawMessage(`{"id":"crev-1","knowledge_id":"doc-1","chunk_id":"chunk-missing"}`),
|
||
}),
|
||
}
|
||
if err := validateLibraryStructuredReferences(broken); err == nil || !strings.Contains(err.Error(), "chunk_id") {
|
||
t.Fatalf("missing chunk parent must be refused, got %v", err)
|
||
}
|
||
}
|