Skip to content
Closed
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
32 changes: 31 additions & 1 deletion pkg/decision/evaluator/condition.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019, Optimizely, Inc. and contributors *
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -28,7 +28,9 @@ const (
exactMatchType = "exact"
existsMatchType = "exists"
ltMatchType = "lt"
leMatchType = "le"
gtMatchType = "gt"
geMatchType = "ge"
substringMatchType = "substring"
)

Expand Down Expand Up @@ -66,14 +68,42 @@ func (c CustomAttributeConditionEvaluator) Evaluate(condition entities.Condition
matcher = matchers.LtMatcher{
Condition: condition,
}
case leMatchType:
matcher = matchers.LeMatcher{
Condition: condition,
}
case gtMatchType:
matcher = matchers.GtMatcher{
Condition: condition,
}
case geMatchType:
matcher = matchers.GeMatcher{
Condition: condition,
}
case substringMatchType:
matcher = matchers.SubstringMatcher{
Condition: condition,
}
case matchers.SemverEqMatchType:
matcher = matchers.SemVerEqMatcher{
Condition: condition,
}
case matchers.SemverLtMatchType:
matcher = matchers.SemVerLtMatcher{
Condition: condition,
}
case matchers.SemverLeMatchType:
matcher = matchers.SemVerLeMatcher{
Condition: condition,
}
case matchers.SemverGtMatchType:
matcher = matchers.SemVerGtMatcher{
Condition: condition,
}
case matchers.SemverGeMatchType:
matcher = matchers.SemVerGeMatcher{
Condition: condition,
}
default:
return false, fmt.Errorf(`invalid Condition matcher "%s"`, condition.Match)
}
Expand Down
186 changes: 149 additions & 37 deletions pkg/decision/evaluator/condition_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019, Optimizely, Inc. and contributors *
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -17,14 +17,24 @@
package evaluator

import (
"testing"

"github.com/optimizely/go-sdk/pkg/entities"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestCustomAttributeConditionEvaluator(t *testing.T) {
conditionEvaluator := CustomAttributeConditionEvaluator{}
type ConditionTestSuite struct {
suite.Suite
user entities.UserContext
conditionEvaluator CustomAttributeConditionEvaluator
}

func (s *ConditionTestSuite) SetupTest() {
s.conditionEvaluator = CustomAttributeConditionEvaluator{}
s.user = entities.UserContext{
Attributes: map[string]interface{}{},
}
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluator() {
condition := entities.Condition{
Match: "exact",
Value: "foo",
Expand All @@ -33,51 +43,153 @@ func TestCustomAttributeConditionEvaluator(t *testing.T) {
}

// Test condition passes
user := entities.UserContext{
Attributes: map[string]interface{}{
"string_foo": "foo",
},
}
s.user.Attributes["string_foo"] = "foo"

condTreeParams := entities.NewTreeParameters(&user, map[string]entities.Audience{})
result, _ := conditionEvaluator.Evaluate(condition, condTreeParams)
assert.Equal(t, result, true)
condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)

// Test condition fails
user = entities.UserContext{
Attributes: map[string]interface{}{
"string_foo": "not_foo",
},
}
result, _ = conditionEvaluator.Evaluate(condition, condTreeParams)
assert.Equal(t, result, false)
s.user.Attributes["string_foo"] = "not_foo"
result, _ = s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, false)
}

func TestCustomAttributeConditionEvaluatorWithoutMatchType(t *testing.T) {
conditionEvaluator := CustomAttributeConditionEvaluator{}
func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorWithoutMatchType() {
condition := entities.Condition{
Value: "foo",
Name: "string_foo",
Type: "custom_attribute",
}

// Test condition passes
user := entities.UserContext{
Attributes: map[string]interface{}{
"string_foo": "foo",
},
}
s.user.Attributes["string_foo"] = "foo"

condTreeParams := entities.NewTreeParameters(&user, map[string]entities.Audience{})
result, _ := conditionEvaluator.Evaluate(condition, condTreeParams)
assert.Equal(t, result, true)
condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)

// Test condition fails
user = entities.UserContext{
Attributes: map[string]interface{}{
"string_foo": "not_foo",
},
s.user.Attributes["string_foo"] = "not_foo"
result, _ = s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, false)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticSame() {
// Test if same when all target is only major.minor
condition := entities.Condition{
Value: "2.0.0",
Match: "semver_eq",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "2.0"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticSameFull() {
// Test when target is full semantic version major.minor.patch
condition := entities.Condition{
Value: "3.0.0",
Match: "semver_eq",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "3.0.0"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticLess() {
// Test compare less when target is only major.minor
condition := entities.Condition{
Value: "2.1.6",
Match: "semver_lt",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "2.2"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticFullLess() {
// Test compare less when target is full major.minor.patch
condition := entities.Condition{
Value: "2.1.6",
Match: "semver_lt",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "2.1.9"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticMore() {
// Test compare greater when target is only major.minor
condition := entities.Condition{
Value: "2.3.6",
Match: "semver_gt",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "2.2"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticFullMore() {
// Test compare greater when target is major.minor.patch
condition := entities.Condition{
Value: "2.1.9",
Match: "semver_gt",
Name: "version",
Type: "custom_attribute",
}

// Test condition passes
s.user.Attributes["version"] = "2.1.6"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}

func (s *ConditionTestSuite) TestCustomAttributeConditionEvaluatorSemanticFullEqual() {
// Test compare equal when target is major.minor.patch-beta
condition := entities.Condition{
Value: "2.1.9-beta",
Match: "semver_eq",
Name: "version",
Type: "custom_attribute",
}
result, _ = conditionEvaluator.Evaluate(condition, condTreeParams)
assert.Equal(t, result, false)

// Test condition passes
s.user.Attributes["version"] = "2.1.9-beta"

condTreeParams := entities.NewTreeParameters(&s.user, map[string]entities.Audience{})
result, _ := s.conditionEvaluator.Evaluate(condition, condTreeParams)
s.Equal(result, true)
}
47 changes: 47 additions & 0 deletions pkg/decision/evaluator/matchers/ge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/****************************************************************************
* Copyright 2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package matchers //
package matchers

import (
"github.com/optimizely/go-sdk/pkg/entities"
)

// GeMatcher matches against the "ge" match type
type GeMatcher struct {
Condition entities.Condition
}

// Match returns true if the user's attribute is greater than or equal to the condition's string value
func (m GeMatcher) Match(user entities.UserContext) (bool, error) {

var result bool
var err error

ltMatcher := LtMatcher(m)
if result, err = ltMatcher.Match(user); err == nil && result {
return true, nil
}

exactMatcher := ExactMatcher(m)

if result, err = exactMatcher.Match(user); err == nil && result {
return true, nil
}

return false, err
}
Loading