Add semantic paths and safe diagnostics
This commit is contained in:
58
metadata_test.go
Normal file
58
metadata_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package configkit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMetadataQueriesReturnDetachedRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
path := Keys("service", "workers")
|
||||
metadata := Metadata{
|
||||
fields: map[string]FieldInfo{
|
||||
path.String(): {
|
||||
Explicit: true,
|
||||
Origin: Origin{
|
||||
Layer: LayerBase,
|
||||
Locations: []Location{{SourceID: "source-1", Line: 2, Column: 3}},
|
||||
},
|
||||
},
|
||||
},
|
||||
history: map[string][]Change{
|
||||
path.String(): {{
|
||||
Path: path,
|
||||
Origin: Origin{Layer: LayerResolution, Locations: []Location{{SourceID: "source-1"}}},
|
||||
Previous: []Origin{{Layer: LayerDefault, Locations: []Location{{SourceID: "default"}}}},
|
||||
Reasons: []string{"bounded worker count"},
|
||||
Dependencies: []Path{Keys("limits")},
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
info, ok := metadata.Lookup(path)
|
||||
if !ok || !info.Explicit || !metadata.IsExplicit(path) {
|
||||
t.Fatalf("Lookup() = %#v, %v", info, ok)
|
||||
}
|
||||
info.Origin.Locations[0].SourceID = "mutated"
|
||||
again, _ := metadata.Lookup(path)
|
||||
if again.Origin.Locations[0].SourceID != "source-1" {
|
||||
t.Fatal("Lookup returned aliased locations")
|
||||
}
|
||||
|
||||
history := metadata.History(path)
|
||||
history[0].Reasons[0] = "mutated"
|
||||
history[0].Previous[0].Locations[0].SourceID = "mutated"
|
||||
history[0].Dependencies[0].segments[0].key = "mutated"
|
||||
againHistory := metadata.History(path)
|
||||
if againHistory[0].Reasons[0] != "bounded worker count" ||
|
||||
againHistory[0].Previous[0].Locations[0].SourceID != "default" ||
|
||||
againHistory[0].Dependencies[0].String() != "/limits" {
|
||||
t.Fatal("History returned aliased nested records")
|
||||
}
|
||||
|
||||
missing := Keys("missing")
|
||||
if _, ok := metadata.Lookup(missing); ok || metadata.IsExplicit(missing) || metadata.History(missing) != nil {
|
||||
t.Fatal("unknown path produced metadata")
|
||||
}
|
||||
if _, ok := metadata.Lookup(Path{}.Index(-1)); ok {
|
||||
t.Fatal("invalid path produced metadata")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user