Skip to content

Commit

Permalink
feat!: EvaluationDetails.reason should be a string, Reason enum shoul…
Browse files Browse the repository at this point in the history
…d export default reasons per spec (#126)

Signed-off-by: James Carr <james.r.carr@gmail.com>
  • Loading branch information
jamescarr authored Jun 28, 2023
1 parent 959159e commit c2d225e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ init: .venv

.PHONY: test
test: .venv
ifdef TEST
$(VENV); pytest $(TEST)
else
$(VENV); pytest
endif

.PHONY: lint
lint: .venv
Expand Down
8 changes: 8 additions & 0 deletions open_feature/flag_evaluation/flag_evaluation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ class FlagEvaluationDetails(typing.Generic[T]):
reason: typing.Optional[Reason] = None
error_code: typing.Optional[ErrorCode] = None
error_message: typing.Optional[str] = None

@property
def reason(self) -> str:
return self._reason.value

@reason.setter
def reason(self, reason: Reason):
self._reason = reason
6 changes: 4 additions & 2 deletions open_feature/flag_evaluation/reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


class Reason(Enum):
CACHED = "CACHED"
DEFAULT = "DEFAULT"
DISABLED = "DISABLED"
ERROR = "ERROR"
STATIC = "STATIC"
SPLIT = "SPLIT"
TARGETING_MATCH = "TARGETING_MATCH"
DEFAULT = "DEFAULT"
UNKNOWN = "UNKNOWN"
ERROR = "ERROR"
6 changes: 3 additions & 3 deletions tests/test_open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_should_raise_exception_when_invalid_flag_type_provided(no_op_provider_c
assert flag.value
assert flag.error_message == "Unknown flag type"
assert flag.error_code == ErrorCode.GENERAL
assert flag.reason == Reason.ERROR
assert flag.reason == Reason.ERROR.value


def test_should_handle_a_generic_exception_thrown_by_a_provider(no_op_provider_client):
Expand All @@ -103,7 +103,7 @@ def test_should_handle_a_generic_exception_thrown_by_a_provider(no_op_provider_c
assert flag_details is not None
assert flag_details.value
assert isinstance(flag_details.value, bool)
assert flag_details.reason == Reason.ERROR
assert flag_details.reason == Reason.ERROR.value
assert flag_details.error_message == "Generic exception raised"


Expand All @@ -125,5 +125,5 @@ def test_should_handle_an_open_feature_exception_thrown_by_a_provider(
assert flag_details is not None
assert flag_details.value
assert isinstance(flag_details.value, bool)
assert flag_details.reason == Reason.ERROR
assert flag_details.reason == Reason.ERROR.value
assert flag_details.error_message == "error_message"
55 changes: 55 additions & 0 deletions tests/test_open_feature_flag_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from open_feature.exception.error_code import ErrorCode
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails
from open_feature.flag_evaluation.reason import Reason


def test_evaulation_details_reason_should_be_a_string():
# Given
flag_key = "my-flag"
flag_value = 100
variant = "1-hundred"
reason = Reason.DEFAULT
error_code = ErrorCode.GENERAL
error_message = "message"

# When
flag_details = FlagEvaluationDetails(
flag_key,
flag_value,
variant,
reason,
error_code,
error_message,
)

# Then
assert flag_key == flag_details.flag_key
assert flag_value == flag_details.value
assert variant == flag_details.variant
assert error_code == flag_details.error_code
assert error_message == flag_details.error_message
assert reason.value == flag_details.reason


def test_evaulation_details_reason_should_be_a_string_when_set():
# Given
flag_key = "my-flag"
flag_value = 100
variant = "1-hundred"
reason = Reason.DEFAULT
error_code = ErrorCode.GENERAL
error_message = "message"

# When
flag_details = FlagEvaluationDetails(
flag_key,
flag_value,
variant,
reason,
error_code,
error_message,
)
flag_details.reason = Reason.STATIC

# Then
assert Reason.STATIC.value == flag_details.reason

0 comments on commit c2d225e

Please sign in to comment.