Index collection policy retirement
This commit is contained in:
@@ -2,7 +2,10 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
)
|
||||
|
||||
func TestSourceReplacementRetiresIdentityForEveryValueKind(t *testing.T) {
|
||||
@@ -109,3 +112,132 @@ func TestReplacementRetirementPreservesApplicationConstraintsAndRetainedKeys(t *
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssemblyPolicyRetirementUsesSemanticPathSegments(t *testing.T) {
|
||||
t.Parallel()
|
||||
var policies collectionPolicyScope
|
||||
slashPath := Keys("branch/name", "items")
|
||||
separateKeysPath := Keys("branch", "name", "items")
|
||||
numericKeyPath := Keys("items", "0", "named")
|
||||
sequenceIndexPath := Keys("items").Index(0).Key("indexed")
|
||||
policies.set(slashPath, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
policies.set(separateKeysPath, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
policies.set(numericKeyPath, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
policies.set(sequenceIndexPath, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
|
||||
assembly := newAssemblyAccumulator(&composedDocument{collectionPolicies: policies})
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("branch/name"))
|
||||
assertRetirement := func(path Path, identityPresent bool) {
|
||||
t.Helper()
|
||||
policy, found := assembly.current.collectionPolicies.at(path)
|
||||
if !found || policy.identityPresent != identityPresent {
|
||||
t.Fatalf("policy at %s = %#v (found %t), want identity present %t", path, policy, found, identityPresent)
|
||||
}
|
||||
}
|
||||
assertRetirement(slashPath, false)
|
||||
assertRetirement(separateKeysPath, true)
|
||||
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("items", "0"))
|
||||
assertRetirement(numericKeyPath, false)
|
||||
assertRetirement(sequenceIndexPath, true)
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("items").Index(0))
|
||||
assertRetirement(sequenceIndexPath, false)
|
||||
}
|
||||
|
||||
func TestAssemblyPolicyRetirementPrunesAndReindexesAroundModeOnlyEntries(t *testing.T) {
|
||||
t.Parallel()
|
||||
identityPath := Keys("catalog", "items")
|
||||
modePath := Keys("catalog", "settings")
|
||||
var policies collectionPolicyScope
|
||||
policies.set(identityPath, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
policies.set(modePath, collectionPolicy{explicitMode: sequenceModeAppend, explicitModePresent: true})
|
||||
assembly := newAssemblyAccumulator(&composedDocument{collectionPolicies: policies})
|
||||
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("catalog"))
|
||||
retired, found := assembly.current.collectionPolicies.at(identityPath)
|
||||
if !found || retired.identityPresent || !assembly.collectionPolicyRetirement.empty() {
|
||||
t.Fatalf("first retirement left identity or stale index: %#v, index %#v", retired, assembly.collectionPolicyRetirement)
|
||||
}
|
||||
mode, found := assembly.current.collectionPolicies.at(modePath)
|
||||
if !found || mode.explicitMode != sequenceModeAppend || !mode.explicitModePresent {
|
||||
t.Fatalf("mode-only policy changed during retirement: %#v, found %t", mode, found)
|
||||
}
|
||||
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("catalog"))
|
||||
newPath := Keys("catalog", "items", "nested")
|
||||
assembly.setCollectionPolicy(newPath, collectionPolicy{identityFields: []string{"name"}, identityPresent: true})
|
||||
if assembly.collectionPolicyRetirement.empty() {
|
||||
t.Fatal("new identity-bearing policy was not reindexed")
|
||||
}
|
||||
assembly.retireCollectionPoliciesAtOrBelow(Keys("catalog"))
|
||||
newPolicy, found := assembly.current.collectionPolicies.at(newPath)
|
||||
if !found || newPolicy.identityPresent || !assembly.collectionPolicyRetirement.empty() {
|
||||
t.Fatalf("repeated ancestor retirement left new identity or stale index: %#v, index %#v", newPolicy, assembly.collectionPolicyRetirement)
|
||||
}
|
||||
mode, found = assembly.current.collectionPolicies.at(modePath)
|
||||
if !found || mode.explicitMode != sequenceModeAppend || !mode.explicitModePresent {
|
||||
t.Fatalf("repeated retirement changed retained mode: %#v, found %t", mode, found)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisjointScalarReplacementsRetainIndexedCollectionPolicies(t *testing.T) {
|
||||
t.Parallel()
|
||||
const policyCount = 128
|
||||
const replacementCount = 32
|
||||
|
||||
var policies collectionPolicyScope
|
||||
collectionEntries := make([]document.Entry, 0, policyCount)
|
||||
for index := 0; index < policyCount; index++ {
|
||||
key := fmt.Sprintf("collection-%03d", index)
|
||||
path := Keys("collections", key)
|
||||
policies.set(path, collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
|
||||
collectionEntries = append(collectionEntries, document.Entry{
|
||||
Key: key, Value: &document.Node{Kind: document.Sequence},
|
||||
})
|
||||
}
|
||||
|
||||
baseScalars := make([]document.Entry, 0, replacementCount)
|
||||
incomingScalars := make([]document.Entry, 0, replacementCount)
|
||||
for index := 0; index < replacementCount; index++ {
|
||||
key := fmt.Sprintf("scalar-%03d", index)
|
||||
baseScalars = append(baseScalars, document.Entry{
|
||||
Key: key, Value: &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
|
||||
Key: "previous", Value: &document.Node{Kind: document.Boolean, Text: "true"},
|
||||
}}},
|
||||
})
|
||||
incomingScalars = append(incomingScalars, document.Entry{
|
||||
Key: key, Value: &document.Node{Kind: document.String, Text: "replaced"},
|
||||
})
|
||||
}
|
||||
base := &composedDocument{
|
||||
root: &document.Node{Kind: document.Mapping, Mapping: []document.Entry{
|
||||
{Key: "collections", Value: &document.Node{Kind: document.Mapping, Mapping: collectionEntries}},
|
||||
{Key: "scalars", Value: &document.Node{Kind: document.Mapping, Mapping: baseScalars}},
|
||||
}},
|
||||
collectionPolicies: policies,
|
||||
origins: make(map[string]Origin),
|
||||
}
|
||||
incoming := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
|
||||
Key: "scalars", Value: &document.Node{Kind: document.Mapping, Mapping: incomingScalars},
|
||||
}}}
|
||||
assembly := newAssemblyAccumulator(base)
|
||||
if _, diagnostics := assembly.applyOverlayWithCollectionProjection(
|
||||
incoming, nil, collectionPolicyScope{}, nil, LayerOverlay, sequencePolicy{}, nil,
|
||||
); len(diagnostics) != 0 {
|
||||
t.Fatalf("apply disjoint scalar replacements: %#v", diagnostics)
|
||||
}
|
||||
for index := 0; index < policyCount; index++ {
|
||||
path := Keys("collections", fmt.Sprintf("collection-%03d", index))
|
||||
policy, found := assembly.current.collectionPolicies.at(path)
|
||||
if !found || !policy.identityPresent || len(policy.identityFields) != 1 || policy.identityFields[0] != "id" {
|
||||
t.Fatalf("disjoint replacement changed policy %s: %#v, found %t", path, policy, found)
|
||||
}
|
||||
}
|
||||
for index := 0; index < replacementCount; index++ {
|
||||
path := Keys("scalars", fmt.Sprintf("scalar-%03d", index))
|
||||
value, found := documentAtPath(assembly.current.root, path)
|
||||
if !found || value.Kind != document.String || value.Text != "replaced" {
|
||||
t.Fatalf("scalar replacement at %s = %#v, found %t", path, value, found)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user