Validate metadata queries against semantic path shapes
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package configkit
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMetadataQueriesReturnDetachedRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
path := Keys("service", "workers")
|
||||
metadata := Metadata{
|
||||
effectivePaths: map[string]Path{path.String(): path},
|
||||
historicalPaths: map[string][]Path{path.String(): {path}},
|
||||
fields: map[string]FieldInfo{
|
||||
path.String(): {
|
||||
Explicit: true,
|
||||
@@ -56,3 +61,45 @@ func TestMetadataQueriesReturnDetachedRecords(t *testing.T) {
|
||||
t.Fatal("invalid path produced metadata")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueriesRespectActualHistoricalAndEffectiveContainers(t *testing.T) {
|
||||
result, err := Load[map[string]any](Bytes("base", []byte("items: [1]\nm: {'0': 2}\nx: 3\nretired: {child: 4}\n")), Options[map[string]any]{Overrides: []Override{{Path: Keys("retired"), Value: "removed", Flag: "--retired"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []Path{Keys("items", "0"), Keys("m").Index(0), Keys("x", "missing"), Keys("items").Index(8)} {
|
||||
if _, ok := result.Metadata().Lookup(path); ok {
|
||||
t.Fatalf("invalid lookup %s", path)
|
||||
}
|
||||
if result.Metadata().History(path) != nil {
|
||||
t.Fatalf("invalid history %s", path)
|
||||
}
|
||||
if _, ok := result.Explain(path); ok {
|
||||
t.Fatalf("invalid explanation %s", path)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err := result.WriteExplanation(&out, path); err == nil || out.Len() != 0 {
|
||||
t.Fatalf("invalid path wrote output: %s", path)
|
||||
}
|
||||
}
|
||||
for _, pointer := range []string{"/items/0", "/m/0"} {
|
||||
path, _ := ParsePath(pointer)
|
||||
if _, ok := result.Metadata().Lookup(path); !ok {
|
||||
t.Fatal("parsed path lost lookup")
|
||||
}
|
||||
if _, ok := result.Explain(path); !ok {
|
||||
t.Fatal("parsed path lost explanation")
|
||||
}
|
||||
}
|
||||
retired := Keys("retired", "child")
|
||||
if _, ok := result.Metadata().Lookup(retired); ok {
|
||||
t.Fatal("removed path remains effective")
|
||||
}
|
||||
if len(result.Metadata().History(retired)) == 0 {
|
||||
t.Fatal("removed history missing")
|
||||
}
|
||||
x, ok := result.Explain(retired)
|
||||
if !ok || x.Effective.Present || len(x.History) == 0 {
|
||||
t.Fatal("removed explanation missing")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user