Skip to content

Commit 6f842fb

Browse files
authored
refactor: Update Provider to accept config over client (#4)
1 parent 8ed56a6 commit 6f842fb

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

ld_openfeature/provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, List, Optional, Union
22

3-
from ldclient import LDClient
3+
from ldclient import LDClient, Config
44
from openfeature.evaluation_context import EvaluationContext
55
from openfeature.exception import ErrorCode
66
from openfeature.flag_evaluation import FlagResolutionDetails, FlagType, Reason
@@ -13,8 +13,8 @@
1313

1414

1515
class LaunchDarklyProvider(AbstractProvider):
16-
def __init__(self, client: LDClient):
17-
self.__client = client
16+
def __init__(self, config: Config):
17+
self.__client = LDClient(config)
1818

1919
self.__context_converter = EvaluationContextConverter()
2020
self.__details_converter = ResolutionDetailsConverter()

tests/test_provider.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List, Union
2-
from unittest.mock import MagicMock
2+
from unittest.mock import patch
33

44
import pytest
55
from ldclient import Config, LDClient
@@ -25,13 +25,13 @@ def evaluation_context() -> EvaluationContext:
2525

2626

2727
@pytest.fixture
28-
def ld_client(test_data_source: TestData) -> LDClient:
29-
return LDClient(config=Config("example-key", update_processor_class=test_data_source, send_events=False))
28+
def config(test_data_source: TestData) -> Config:
29+
return Config("example-key", update_processor_class=test_data_source, send_events=False)
3030

3131

3232
@pytest.fixture
33-
def provider(ld_client) -> LaunchDarklyProvider:
34-
return LaunchDarklyProvider(ld_client)
33+
def provider(config) -> LaunchDarklyProvider:
34+
return LaunchDarklyProvider(config)
3535

3636

3737
def test_metadata_name_is_correct(provider: LaunchDarklyProvider):
@@ -56,11 +56,10 @@ def test_evaluation_results_are_converted_to_details(provider: LaunchDarklyProvi
5656
assert resolution_details.error_code is None
5757

5858

59-
def test_evaluation_error_results_are_converted_correctly(ld_client: LDClient, provider: LaunchDarklyProvider, evaluation_context: EvaluationContext):
59+
def test_evaluation_error_results_are_converted_correctly(provider: LaunchDarklyProvider, evaluation_context: EvaluationContext):
6060
detail = EvaluationDetail(True, None, {'kind': 'ERROR', 'errorKind': 'CLIENT_NOT_READY'})
61-
ld_client.variation_detail = MagicMock(return_value=detail) # type: ignore[method-assign]
62-
63-
resolution_details = provider.resolve_boolean_details("flag-key", True, evaluation_context)
61+
with patch.object(LDClient, 'variation_detail', lambda self, _key, _context, _default: detail):
62+
resolution_details = provider.resolve_boolean_details("flag-key", True, evaluation_context)
6463

6564
assert resolution_details.value is True
6665
assert resolution_details.reason == Reason.ERROR
@@ -113,14 +112,13 @@ def test_check_method_and_result_match_type(
113112
expected_value: Union[bool, str, int, float, List],
114113
method_name: str,
115114
# end of parameterized values
116-
ld_client: LDClient,
115+
test_data_source: TestData,
117116
provider: LaunchDarklyProvider,
118117
evaluation_context: EvaluationContext):
119-
detail = EvaluationDetail(return_value, 1, {'kind': 'FALLTHROUGH'})
120-
ld_client.variation_detail = MagicMock(return_value=detail) # type: ignore[method-assign]
118+
test_data_source.update(test_data_source.flag("check-method-flag").variations(return_value).variation_for_all(0))
121119

122120
method = getattr(provider, method_name)
123-
resolution_details = method("flag-key", default_value, evaluation_context)
121+
resolution_details = method("check-method-flag", default_value, evaluation_context)
124122

125123
assert resolution_details.value == expected_value
126124

0 commit comments

Comments
 (0)