Skip to content

Commit

Permalink
add mandatory flags property in bulk response
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Jun 25, 2024
1 parent 83bdbb5 commit ffa1afc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/pkg/service/ofrep/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type EvaluationSuccess struct {
}

type BulkEvaluationResponse struct {
Flags []interface{} `json:"flags,omitempty"`
Flags []interface{} `json:"flags"`
}

type EvaluationError struct {
Expand Down
60 changes: 42 additions & 18 deletions core/pkg/service/ofrep/models_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ofrep

import (
"encoding/json"
"errors"
"reflect"
"testing"
Expand Down Expand Up @@ -45,31 +46,54 @@ func TestSuccessResult(t *testing.T) {
}
}

func TestBulkEvaluationResult(t *testing.T) {
// given
values := []evaluator.AnyValue{
func TestBulkEvaluationResponse(t *testing.T) {
tests := []struct {
name string
input []evaluator.AnyValue
marshalledOutput string
}{
{
Value: false,
Variant: "false",
Reason: model.StaticReason,
FlagKey: "key",
Metadata: map[string]interface{}{
"key": "value",
},
name: "empty input",
input: nil,
marshalledOutput: "{\"flags\":[]}",
},
{
Value: false,
Variant: "false",
Reason: model.ErrorReason,
FlagKey: "errorFlag",
Error: errors.New(model.FlagNotFoundErrorCode),
name: "valid values",
input: []evaluator.AnyValue{
{
Value: false,
Variant: "false",
Reason: model.StaticReason,
FlagKey: "key",
Metadata: map[string]interface{}{
"key": "value",
},
},
{
Value: false,
Variant: "false",
Reason: model.ErrorReason,
FlagKey: "errorFlag",
Error: errors.New(model.FlagNotFoundErrorCode),
},
},
marshalledOutput: "{\"flags\":[{\"value\":false,\"key\":\"key\",\"reason\":\"STATIC\",\"variant\":\"false\",\"metadata\":{\"key\":\"value\"}},{\"key\":\"errorFlag\",\"errorCode\":\"FLAG_NOT_FOUND\",\"errorDetails\":\"flag `errorFlag` does not exist\"}]}",
},
}

bulkResponse := BulkEvaluationResponseFrom(values)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
response := BulkEvaluationResponseFrom(test.input)

marshal, err := json.Marshal(response)
if err != nil {
t.Errorf("error marshalling the response: %v", err)
}

if len(bulkResponse.Flags) != 2 {
t.Errorf("expected bulk response to contain 2 results, but got %d", len(bulkResponse.Flags))
if test.marshalledOutput != string(marshal) {
t.Errorf("expected %s, got %s", test.marshalledOutput, string(marshal))
}
})
}
}

Expand Down

0 comments on commit ffa1afc

Please sign in to comment.