Skip to content

Commit

Permalink
optimizely/decision_service.py -> Removed unused profile loading
Browse files Browse the repository at this point in the history
optimizely/user_profile.py -> Fixed handling of reasons list
optimizely/user_profile.py -> Improved profile retrieval error logging
tests/test_decision_service.py -> Updated mock checks to simplify tests
tests/test_user_profile.py -> Added tests for user profile handling
tests/test_optimizely.py -> New test for variation lookup and save
  • Loading branch information
FarhanAnjum-opti committed Nov 25, 2024
1 parent 1dd1eef commit 6387ca2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 325 deletions.
1 change: 0 additions & 1 deletion optimizely/decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ def get_variation(

# Check to see if user has a decision available for the given experiment
if user_profile_tracker is not None and not ignore_user_profile:
# user_profile_tracker.load_user_profile()
variation = self.get_stored_variation(project_config, experiment, user_profile_tracker.get_user_profile())
if variation:
message = f'Returning previously activated variation ID "{variation}" of experiment ' \
Expand Down
20 changes: 3 additions & 17 deletions optimizely/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def get_user_profile(self) -> UserProfile:

def load_user_profile(self, reasons: Optional[list[str]] = [],
error_handler: Optional[BaseErrorHandler] = None) -> None:
reasons = reasons if reasons else []
if reasons is None:
reasons = []
try:
user_profile = self.user_profile_service.lookup(self.user_id) if self.user_profile_service else None
if user_profile is None:
Expand All @@ -139,27 +140,12 @@ def load_user_profile(self, reasons: Optional[list[str]] = [],
except Exception as exception:
message = str(exception)
reasons.append(message)
self.logger.exception(f'Unable to retrieve user profile for user "{self.user_id}"as lookup failed.')

if self.user_profile is None:
self.user_profile = UserProfile(self.user_id, {})
self.logger.exception(f'Unable to retrieve user profile for user "{self.user_id}" as lookup failed.')

def update_user_profile(self, experiment: Experiment, variation: Variation) -> None:
variation_id = variation.id
experiment_id = experiment.id
self.user_profile.save_variation_for_experiment(experiment_id, variation_id)
# if experiment.id in self.user_profile.experiment_bucket_map:
# decision = self.user_profile.experiment_bucket_map[experiment.id]
# if isinstance(decision, decision_service.Decision):
# decision = decision_service.Decision(
# experiment=decision.experiment,
# variation=variation,
# source=decision.source
# )
# else:
# decision = decision_service.Decision(experiment=None, variation=variation, source=None)

# self.user_profile.experiment_bucket_map[experiment.id] = decision
self.profile_updated = True

def save_user_profile(self, error_handler: Optional[BaseErrorHandler] = None) -> None:
Expand Down
Loading

0 comments on commit 6387ca2

Please sign in to comment.