Fix Reporter GetDiffsForField (#50264)

This commit is contained in:
Yuriy Tseretyan 2022-06-06 23:16:05 -04:00 committed by GitHub
parent 8876d56495
commit a88408bfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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)
})
}