mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
Fix Reporter GetDiffsForField (#50264)
This commit is contained in:
parent
8876d56495
commit
a88408bfd1
@ -14,7 +14,13 @@ type DiffReport []Diff
|
||||
func (r DiffReport) GetDiffsForField(path string) DiffReport {
|
||||
var result []Diff
|
||||
for _, diff := range r {
|
||||
if strings.HasPrefix(path, diff.Path) {
|
||||
if strings.HasPrefix(diff.Path, path) {
|
||||
if diff.Path != path {
|
||||
char := []rune(diff.Path)[len(path)]
|
||||
if char != '.' && char != '[' { // if the following symbol is not a delimiter or bracket then that's not our path
|
||||
continue
|
||||
}
|
||||
}
|
||||
result = append(result, diff)
|
||||
}
|
||||
}
|
||||
|
@ -130,3 +130,68 @@ func TestIsAddedDeleted_Collections(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDiffsForField(t *testing.T) {
|
||||
t.Run("should not include fields that starts has prefix", func(t *testing.T) {
|
||||
diff := DiffReport{
|
||||
Diff{
|
||||
Path: "Property",
|
||||
},
|
||||
Diff{
|
||||
Path: "PropertyData",
|
||||
},
|
||||
}
|
||||
|
||||
result := diff.GetDiffsForField("Property")
|
||||
require.Len(t, result, 1)
|
||||
require.Equal(t, "Property", result[0].Path)
|
||||
})
|
||||
|
||||
t.Run("should return all changes by parent path", func(t *testing.T) {
|
||||
diff := DiffReport{
|
||||
Diff{
|
||||
Path: "Property.Data.Value",
|
||||
},
|
||||
Diff{
|
||||
Path: "Property.Array[0].Value",
|
||||
},
|
||||
}
|
||||
|
||||
result := diff.GetDiffsForField("Property")
|
||||
require.Len(t, result, 2)
|
||||
})
|
||||
|
||||
t.Run("should return all elements of array", func(t *testing.T) {
|
||||
diff := DiffReport{
|
||||
Diff{
|
||||
Path: "Property[0].Data.Test",
|
||||
},
|
||||
Diff{
|
||||
Path: "Property",
|
||||
},
|
||||
Diff{
|
||||
Path: "Property[1]",
|
||||
},
|
||||
}
|
||||
|
||||
result := diff.GetDiffsForField("Property")
|
||||
require.Len(t, result, 3)
|
||||
})
|
||||
|
||||
t.Run("should find nothing if parent path does not exist", func(t *testing.T) {
|
||||
diff := DiffReport{
|
||||
Diff{
|
||||
Path: "Property[0].Data.Test",
|
||||
},
|
||||
Diff{
|
||||
Path: "Property",
|
||||
},
|
||||
Diff{
|
||||
Path: "Property[1]",
|
||||
},
|
||||
}
|
||||
|
||||
result := diff.GetDiffsForField("Proper")
|
||||
require.Empty(t, result)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user