- 
                Notifications
    You must be signed in to change notification settings 
- Fork 20
Add support for unit testing for user context #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
            daniel-pons
  wants to merge
  3
  commits into
  optimizely:master
from
daniel-pons:daniel-pons/support-ut-for-user-context
  
      
      
   
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      2af4a37
              
                fix(tests): Run tests on English culture Set the current thread cultu…
              
              
                daniel-pons 2b06f71
              
                feat(common): Add support to mock OptimizelyUserContext on unit tests
              
              
                daniel-pons e11d72b
              
                Merge branch 'master' into daniel-pons/support-ut-for-user-context
              
              
                daniel-pons File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2021, Optimizely | ||
| * | ||
| * 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. | ||
| */ | ||
|  | ||
| using NUnit.Framework; | ||
| using System.Globalization; | ||
| using System.Threading; | ||
|  | ||
| namespace OptimizelySDK.Tests | ||
| { | ||
| [SetUpFixture] | ||
| public class TestSetup | ||
| { | ||
| [SetUp] | ||
| public void Init() | ||
| { | ||
| /* There are some issues doing assertions on tests with floating point numbers using the .ToString() | ||
| * method, as it's culture dependent. EG: TestGetFeatureVariableValueForTypeGivenFeatureFlagIsNotEnabledForUser, | ||
| * assigning the culture to English will make this kind of tests to work on others culture based systems. */ | ||
| Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); | ||
| } | ||
|  | ||
| [TearDown] | ||
| public void Cleanup() | ||
| { | ||
| // Empty, but required: https://nunit.org/nunitv2/docs/2.6.4/setupFixture.html | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * Copyright 2020-2021, Optimizely | ||
| * | ||
| * 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. | ||
| */ | ||
|  | ||
| using OptimizelySDK.Entity; | ||
| using OptimizelySDK.OptimizelyDecisions; | ||
| using System.Collections.Generic; | ||
|  | ||
| namespace OptimizelySDK | ||
| { | ||
| public interface IOptimizelyUserContext | ||
| { | ||
| /// <summary> | ||
| /// Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag. | ||
| /// <ul> | ||
| /// <li>If the SDK finds an error, it’ll return a decision with <b>null</b> for <b>variationKey</b>. The decision will include an error message in <b>reasons</b>. | ||
| /// </ul> | ||
| /// </summary> | ||
| /// <param name="key">A flag key for which a decision will be made.</param> | ||
| /// <returns>A decision result.</returns> | ||
| OptimizelyDecision Decide(string key); | ||
|  | ||
| /// <summary> | ||
| /// Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag. | ||
| /// <ul> | ||
| /// <li>If the SDK finds an error, it’ll return a decision with <b>null</b> for <b>variationKey</b>. The decision will include an error message in <b>reasons</b>. | ||
| /// </ul> | ||
| /// </summary> | ||
| /// <param name="key">A flag key for which a decision will be made.</param> | ||
| /// <param name="options">A list of options for decision-making.</param> | ||
| /// <returns>A decision result.</returns> | ||
| OptimizelyDecision Decide(string key, OptimizelyDecideOption[] options); | ||
|  | ||
| /// <summary> | ||
| /// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys. | ||
| /// </summary> | ||
| /// <returns>A dictionary of all decision results, mapped by flag keys.</returns> | ||
| Dictionary<string, OptimizelyDecision> DecideAll(); | ||
|  | ||
| /// <summary> | ||
| /// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys. | ||
| /// </summary> | ||
| /// <param name="options">A list of options for decision-making.</param> | ||
| /// <returns>All decision results mapped by flag keys.</returns> | ||
| Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options); | ||
|  | ||
| /// <summary> | ||
| /// Returns a key-map of decision results for multiple flag keys and a user context. | ||
| /// </summary> | ||
| /// <param name="keys">list of flag keys for which a decision will be made.</param> | ||
| /// <returns>A dictionary of all decision results, mapped by flag keys.</returns> | ||
| Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys); | ||
|  | ||
| /// <summary> | ||
| /// Returns a key-map of decision results for multiple flag keys and a user context. | ||
| /// </summary> | ||
| /// <param name="keys">list of flag keys for which a decision will be made.</param> | ||
| /// <param name="options">An array of decision options.</param> | ||
| /// <returns></returns> | ||
| Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options); | ||
|  | ||
| /// <summary> | ||
| /// Returns copy of UserAttributes associated with UserContext. | ||
| /// </summary> | ||
| /// <returns>copy of UserAttributes.</returns> | ||
| UserAttributes GetAttributes(); | ||
|  | ||
| /// <summary> | ||
| /// Returns Optimizely instance associated with the UserContext. | ||
| /// </summary> | ||
| /// <returns> Optimizely instance.</returns> | ||
| Optimizely GetOptimizely(); | ||
|  | ||
| /// <summary> | ||
| /// Returns UserId associated with the UserContext | ||
| /// </summary> | ||
| /// <returns>UserId of this instance.</returns> | ||
| string GetUserId(); | ||
|  | ||
| /// <summary> | ||
| /// Set an attribute for a given key. | ||
| /// </summary> | ||
| /// <param name="key">An attribute key</param> | ||
| /// <param name="value">value An attribute value</param> | ||
| void SetAttribute(string key, object value); | ||
|  | ||
| /// <summary> | ||
| /// Track an event. | ||
| /// </summary> | ||
| /// <param name="eventName">The event name.</param> | ||
| void TrackEvent(string eventName); | ||
|  | ||
| /// <summary> | ||
| /// Track an event. | ||
| /// </summary> | ||
| /// <param name="eventName">The event name.</param> | ||
| /// <param name="eventTags">A map of event tag names to event tag values.</param> | ||
| void TrackEvent(string eventName, EventTags eventTags); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -53,7 +53,7 @@ public class OptimizelyDecision | |
| /// <summary> | ||
| /// user context for which the decision was made. | ||
| /// </summary> | ||
| public OptimizelyUserContext UserContext { get; private set; } | ||
| public IOptimizelyUserContext UserContext { get; private set; } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this one as well, please revert to  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as before | ||
|  | ||
| /// <summary> | ||
| /// an array of error/info/debug messages describing why the decision has been made. | ||
|  | @@ -65,7 +65,7 @@ public OptimizelyDecision(string variationKey, | |
| OptimizelyJSON variables, | ||
| string ruleKey, | ||
| string flagKey, | ||
| OptimizelyUserContext userContext, | ||
| IOptimizelyUserContext userContext, | ||
| string[] reasons) | ||
| { | ||
| VariationKey = variationKey; | ||
|  | @@ -84,7 +84,7 @@ public OptimizelyDecision(string variationKey, | |
| /// and error reason array | ||
| /// </summary> | ||
| public static OptimizelyDecision NewErrorDecision(string key, | ||
| OptimizelyUserContext optimizelyUserContext, | ||
| IOptimizelyUserContext optimizelyUserContext, | ||
| string error, | ||
| IErrorHandler errorHandler, | ||
| ILogger logger) | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will break existing functionality.
e.g. someone used like this.
OptimizelyUserContext ouc = obj.CreateUserContext(...) /// value can't be set when returning IOptimizelyUserContext.We should return to OptimizelyUserContext.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the idea of the PR is to get the interface, as using the explicit type when declaring is in disuse nowadays. Usually code analyzers mark this as warnings. EG:
Then, is just a matter of replacing:
var ouc = obj.CreateUserContext(...);I think that, the benefits of being able to write unit tests is by far higher for the majority of developers, than the cost of replacing the explicit type by var.
What to you thnik?.
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, if the coding standards don't allow the usage of var, is just a matter of using the interface instead:
IOptimizelyUserContext ouc = obj.CreateUserContext(...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it's not just a matter of coding standards, this would technically require a breaking change to implement in the current version.
We agree this is a worthwhile enhancement for the next major version of the SDK and will add it to our backlog for when that next release is planned.
What we can do in the interim is provide a virtual that will get the user context provided, which can then be overridden for your testing purposes.
Let us know if you think that would be a good stop gap until we can provide the interface change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, this is a breaking change, if you guys consider it useful and want to include this on the next major release, is perfect.
Then, we can wait, I prefer to close the PR and wait.
Many thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But well, you are right, making them virtual is just a nice quick win, and that will allow the mocking library, to intercept the calls and set expectations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created the PR with the virtual keyword. It's here: #280
It also includes the fix for unit tests running on non English systems.
I'll then, close this PR.
Many thanks!.