Skip to content

Commit

Permalink
chore: add tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yquansah committed Jul 3, 2023
1 parent 703fddf commit 38604ac
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions internal/server/evaluation/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,87 @@ func TestEvaluator_RulesOutOfOrder(t *testing.T) {
assert.Equal(t, flipt.EvaluationReason_ERROR_EVALUATION_REASON, resp.Reason)
}

func TestEvaluator_ErrorParsingNumber(t *testing.T) {
var (
store = &evaluationStoreMock{}
logger = zaptest.NewLogger(t)
s = NewEvaluator(logger, store)
)

store.On("GetEvaluationRules", mock.Anything, mock.Anything, "foo").Return(
[]*storage.EvaluationRule{
{
ID: "1",
FlagKey: "foo",
SegmentKey: "bar",
SegmentMatchType: flipt.MatchType_ALL_MATCH_TYPE,
Rank: 1,
Constraints: []storage.EvaluationConstraint{
{
ID: "2",
Type: flipt.ComparisonType_NUMBER_COMPARISON_TYPE,
Property: "bar",
Operator: flipt.OpEQ,
Value: "boz",
},
},
},
}, nil)

resp, err := s.Evaluate(context.TODO(), enabledFlag, &flipt.EvaluationRequest{
EntityId: "1",
FlagKey: "foo",
Context: map[string]string{
"bar": "baz",
},
})

assert.Error(t, err)
assert.EqualError(t, err, "parsing number from \"baz\"")
assert.False(t, resp.Match)
assert.Equal(t, flipt.EvaluationReason_ERROR_EVALUATION_REASON, resp.Reason)
}
func TestEvaluator_ErrorParsingDateTime(t *testing.T) {
var (
store = &evaluationStoreMock{}
logger = zaptest.NewLogger(t)
s = NewEvaluator(logger, store)
)

store.On("GetEvaluationRules", mock.Anything, mock.Anything, "foo").Return(
[]*storage.EvaluationRule{
{
ID: "1",
FlagKey: "foo",
SegmentKey: "bar",
SegmentMatchType: flipt.MatchType_ALL_MATCH_TYPE,
Rank: 1,
Constraints: []storage.EvaluationConstraint{
{
ID: "2",
Type: flipt.ComparisonType_DATETIME_COMPARISON_TYPE,
Property: "bar",
Operator: flipt.OpEQ,
Value: "boz",
},
},
},
}, nil)

resp, err := s.Evaluate(context.TODO(), enabledFlag, &flipt.EvaluationRequest{
EntityId: "1",
FlagKey: "foo",
Context: map[string]string{
"bar": "baz",
},
})

assert.Error(t, err)
assert.EqualError(t, err, "parsing datetime from \"baz\"")
assert.False(t, resp.Match)
assert.Equal(t, flipt.EvaluationReason_ERROR_EVALUATION_REASON, resp.Reason)
}

func TestEvaluator_ErrorGettingDistributions(t *testing.T) {
var (
store = &evaluationStoreMock{}
Expand Down Expand Up @@ -1023,6 +1104,69 @@ func TestEvaluator_MatchAll_NoVariants_NoDistributions(t *testing.T) {
}
}

func TestEvaluator_DistributionNotMatched(t *testing.T) {
var (
store = &evaluationStoreMock{}
logger = zaptest.NewLogger(t)
s = NewEvaluator(logger, store)
)

store.On("GetEvaluationRules", mock.Anything, mock.Anything, "foo").Return(
[]*storage.EvaluationRule{
{
ID: "1",
FlagKey: "foo",
SegmentKey: "bar",
SegmentMatchType: flipt.MatchType_ALL_MATCH_TYPE,
Rank: 0,
Constraints: []storage.EvaluationConstraint{
// constraint: bar (string) == baz
{
ID: "2",
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "bar",
Operator: flipt.OpEQ,
Value: "baz",
},
// constraint: admin (bool) == true
{
ID: "3",
Type: flipt.ComparisonType_BOOLEAN_COMPARISON_TYPE,
Property: "admin",
Operator: flipt.OpTrue,
},
},
},
}, nil)

store.On("GetEvaluationDistributions", mock.Anything, "1").Return(
[]*storage.EvaluationDistribution{
{
ID: "4",
RuleID: "1",
VariantID: "5",
Rollout: 10,
VariantKey: "boz",
VariantAttachment: `{"key":"value"}`,
},
}, nil)

resp, err := s.Evaluate(context.TODO(), enabledFlag, &flipt.EvaluationRequest{
FlagKey: "foo",
EntityId: "123",
Context: map[string]string{
"bar": "baz",
"admin": "true",
},
})

assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "foo", resp.FlagKey)

assert.False(t, resp.Match, "distribution not matched")
}

func TestEvaluator_MatchAll_SingleVariantDistribution(t *testing.T) {
var (
store = &evaluationStoreMock{}
Expand Down

0 comments on commit 38604ac

Please sign in to comment.