Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mandatory flags property in bulk response #1339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading