Skip to content

Commit

Permalink
fix response modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Jan 24, 2024
1 parent 62dd93b commit c7e9aaf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
15 changes: 15 additions & 0 deletions delta/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ func ratio(asymmetric bool, added int, deleted int, modifiedDelta float64, all i

return (float64(added+deleted) + modifiedDelta) / float64(all)
}

func modifiedLeafDelta(asymmetric bool, modified float64) float64 {
if asymmetric {
return modified / 2
}

return modified
}

func boolToFloat64(b bool) float64 {
if b {
return 1.0
}
return 0.0
}
36 changes: 34 additions & 2 deletions delta/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func TestEndpointAdded(t *testing.T) {
}

require.Equal(t, 0.5, delta.Get(false, d))
require.Equal(t, 0.0, delta.Get(true, d))
}

func TestEndpointDeletedAsym(t *testing.T) {
func TestEndpointDeleted(t *testing.T) {
d := &diff.Diff{
EndpointsDiff: &diff.EndpointsDiff{
Deleted: diff.Endpoints{
Expand All @@ -54,6 +55,7 @@ func TestEndpointDeletedAsym(t *testing.T) {
},
}

require.Equal(t, 0.5, delta.Get(false, d))
require.Equal(t, 0.5, delta.Get(true, d))
}

Expand All @@ -76,6 +78,7 @@ func TestEndpointAddedAndDeleted(t *testing.T) {
}

require.Equal(t, 1.0, delta.Get(false, d))
require.Equal(t, 0.5, delta.Get(true, d))
}

func TestParameters(t *testing.T) {
Expand All @@ -96,10 +99,11 @@ func TestParameters(t *testing.T) {
},
}

require.Equal(t, 0.5, delta.Get(false, d))
require.Equal(t, 0.5, delta.Get(true, d))
}

func TestResponses(t *testing.T) {
func TestResponses_AddedAndDeleted(t *testing.T) {
d := &diff.Diff{
EndpointsDiff: &diff.EndpointsDiff{
Modified: diff.ModifiedEndpoints{
Expand All @@ -117,6 +121,33 @@ func TestResponses(t *testing.T) {
}

require.Equal(t, 0.5, delta.Get(false, d))
require.Equal(t, 0.25, delta.Get(true, d))
}

func TestResponses_Modified(t *testing.T) {
d := &diff.Diff{
EndpointsDiff: &diff.EndpointsDiff{
Modified: diff.ModifiedEndpoints{
diff.Endpoint{
Method: "GET",
Path: "/test",
}: &diff.MethodDiff{
ResponsesDiff: &diff.ResponsesDiff{
Modified: diff.ModifiedResponses{
"200": &diff.ResponseDiff{
ContentDiff: &diff.ContentDiff{
MediaTypeAdded: utils.StringList{"json"},
},
},
},
},
},
},
},
}

require.Equal(t, 0.25, delta.Get(false, d))
require.Equal(t, 0.125, delta.Get(true, d))
}

func TestSchema(t *testing.T) {
Expand Down Expand Up @@ -147,6 +178,7 @@ func TestSchema(t *testing.T) {
}

require.Equal(t, 0.25, delta.Get(false, d))
require.Equal(t, 0.125, delta.Get(true, d))
}

func TestSymmetric(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion delta/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func getResponsesDelta(asymmetric bool, d *diff.ResponsesDiff) *WeightedDelta {
all := added + deleted + modified + unchanged

// TODO: drill down into modified
modifiedDelta := coefficient * float64(modified)
modifiedDelta := coefficient * modifiedLeafDelta(asymmetric, float64(modified))

return NewWeightedDelta(ratio(asymmetric, added, deleted, modifiedDelta, all), all)
}
15 changes: 0 additions & 15 deletions delta/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,3 @@ func getSchemaDelta(asymmetric bool, d *diff.SchemaDiff) float64 {

return typeDelta
}

func modifiedLeafDelta(asymmetric bool, modified float64) float64 {
if asymmetric {
return modified / 2
}

return modified
}

func boolToFloat64(b bool) float64 {
if b {
return 1.0
}
return 0.0
}

0 comments on commit c7e9aaf

Please sign in to comment.