Skip to content
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

Add support for unit testing for user context #271

Conversation

daniel-pons
Copy link
Contributor

Summary

The following versions of Optimizely C# SDK would use the methods defined on the class OptimizelyUserContext to perform some processes, E.G: Decide, TrackEvent, DecideAll, etc...

Them, it is required on the vast majority of unit testing frameworks for mocking this object to have an interface for it.

This PR creates the interface IOptimizelyUserContext for doing so.

Also fixes a problem with development environments with a culture different from english.

…re to english, as for systems on others cultures, some tests were failing.
@msohailhussain
Copy link
Contributor

Thanks Daniel. Can you please sign this CLA and update this branch, so we can review & merge this PR?
https://docs.google.com/forms/d/e/1FAIpQLSf9cbouWptIpMgukAKZZOIAhafvjFCV8hS00XJLWQnWDFtwtA/viewform

Copy link
Contributor

@msohailhussain msohailhussain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please address comments.

@daniel-pons
Copy link
Contributor Author

Thanks Daniel. Can you please sign this CLA and update this branch, so we can review & merge this PR?
https://docs.google.com/forms/d/e/1FAIpQLSf9cbouWptIpMgukAKZZOIAhafvjFCV8hS00XJLWQnWDFtwtA/viewform

Thanks Daniel. Can you please sign this CLA and update this branch, so we can review & merge this PR?
https://docs.google.com/forms/d/e/1FAIpQLSf9cbouWptIpMgukAKZZOIAhafvjFCV8hS00XJLWQnWDFtwtA/viewform

Hi, I already did before creating PR. probably I'm around your records

Supports mocking OptimizelyUserContext on frameworks as almost all of them require an interface.
@daniel-pons daniel-pons force-pushed the daniel-pons/support-ut-for-user-context branch from 97b7f5c to 2b06f71 Compare August 11, 2021 07:19
@daniel-pons
Copy link
Contributor Author

daniel-pons commented Aug 11, 2021

please address comments.

I think I fixed what was requested. However I'm getting some kind of error when running the integration tests.
The command "mkdir $HOME/travisci-tools && pushd $HOME/travisci-tools && git init && git pull https://$CI_USER_TOKEN@github.com/optimizely/travisci-tools.git && popd" failed and exited with 1 during .

Many thanks.

@daniel-pons daniel-pons requested review from msohailhussain and removed request for a team August 11, 2021 08:50
@msohailhussain
Copy link
Contributor

me kind of error when running the integration tests.

Thanks I will take care of it CI stuff.

@@ -41,7 +41,7 @@ public interface IOptimizely
/// <param name="userId">The user ID to be used for bucketing.</param>
/// <param name="userAttributes">The user's attributes</param>
/// <returns>OptimizelyUserContext | An OptimizelyUserContext associated with this OptimizelyClient.</returns>
OptimizelyUserContext CreateUserContext(string userId, UserAttributes userAttributes = null);
IOptimizelyUserContext CreateUserContext(string userId, UserAttributes userAttributes = null);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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(...)

Copy link
Contributor

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(...)

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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!.

@@ -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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one as well, please revert to OptimizelyUserContext

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as before

@msohailhussain
Copy link
Contributor

@daniel-pons I tested your changes but that code will break existing code, can you please revert those. I have pointed out as well.
In CreateUserContext, should return OptimizelyUserContext and UserContext in OptimizelyDecisions should be OptimizelyUserContext not an interface.

Copy link
Contributor

@msohailhussain msohailhussain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert mentioned above.

@daniel-pons
Copy link
Contributor Author

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!.

@daniel-pons daniel-pons closed this Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants