-
Notifications
You must be signed in to change notification settings - Fork 24
Create mock testing page #946
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
Draft
leslie-lau
wants to merge
3
commits into
main
Choose a base branch
from
mock-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| --- | ||
| title: SDK Release Lifecycle | ||
| sidebar_position: 2 | ||
| --- | ||
|
|
||
| # SDK Release Lifecycle | ||
|
|
||
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,177 @@ | ||
| --- | ||
| title: Mock Testing with DevCycle | ||
| --- | ||
|
|
||
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
|
|
||
| Mock testing allows you to test your application's behavior with specific Feature and Variable configurations without making API calls to DevCycle. This enables faster, more reliable tests that don't depend on external services. | ||
|
|
||
| ## Mock Testing Strategy | ||
|
|
||
| The general testing strategy we'd recommend is to mock DevCycle's Variable and Variable value calls as opposed to mocking the full DevCycle client. To achieve this, you'd want to perform network level mocking to intercept the Variables or Variable by key requests to provide your own Variable response. | ||
|
|
||
| ## Server-Side SDKs: Cloud Bucketing | ||
|
|
||
| DevCycle's Server-side SDKs can operate on Cloud or Local Bucketing modes. We'd recommend using Cloud Bucketing for mock testing. Cloud Bucketing uses the [DevCycle Bucketing API](/bucketing-api/#tag/Bucketing-API) behind the scenes to fetch and retrive Variable values. You may mock the response of the `Get Variable by Key` or `Get Variables` endpoints in order to replicate the output of your Server-side SDK. | ||
|
|
||
| ### Setup | ||
|
|
||
| The SDK Key does not need to be a valid SDK key, but it needs to be in a correctly formatted starting with `dvc_server` or `server`. | ||
|
|
||
| <Tabs> | ||
| <TabItem value="python" label="Python (unittest)"> | ||
|
|
||
| ```python | ||
| def setUp(self): | ||
| self.sdk_key = "dvc_server_test_key_12345678" | ||
| self.bucketing_api_base = "https://bucketing-api.devcycle.com" | ||
| self.options = DevCycleCloudOptions( | ||
| bucketing_api_uri=self.bucketing_api_base, | ||
| request_timeout=5, | ||
| request_retries=0, | ||
| ) | ||
| self.client = DevCycleCloudClient(self.sdk_key, self.options) | ||
|
|
||
| def tearDown(self): | ||
| if self.client: | ||
| self.client.close() | ||
| responses.reset() | ||
|
|
||
| def _get_variable_url(self, key: str) -> str: | ||
| """Get the URL for a single variable request""" | ||
| return f"{self.bucketing_api_base}/v1/variables/{key}" | ||
|
|
||
| def _get_variables_url(self) -> str: | ||
| """Get the URL for all variables request""" | ||
| return f"{self.bucketing_api_base}/v1/variables" | ||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ### Variable and VariableValue Test | ||
|
|
||
| Test retrieving a Variable value through the `.variable()` or `.variableValue()` method. | ||
| <Tabs> | ||
| <TabItem value="python" label="Python (unittest)"> | ||
|
|
||
| ```python | ||
| def test_variable_value_string(self): | ||
| """Test variable_value() with a string variable""" | ||
| variable_key = "string-test-var" | ||
| expected_value = "var-on" | ||
|
|
||
| responses.add( | ||
| responses.POST, | ||
| self._get_variable_url(variable_key), | ||
| json={ | ||
| "_id": "12345", | ||
| "key": variable_key, | ||
| "type": "String", | ||
| "value": expected_value, | ||
| "eval": { | ||
| "reason": "TARGETING_MATCH", | ||
| "details": "All Users", | ||
| "target_id": "54321" | ||
| } | ||
| }, | ||
| status=200, | ||
| ) | ||
|
|
||
| user = DevCycleUser(user_id="test-user-123") | ||
| result = self.client.variable_value(user, variable_key, "default-value") | ||
|
|
||
| self.assertEqual(result, expected_value) | ||
| # Verify the request was made | ||
| self.assertEqual(len(responses.calls), 1) | ||
| self.assertEqual(responses.calls[0].request.method, "POST") | ||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ### AllVariables Test | ||
|
|
||
| Test retrieving all active Variables through the `.allVariable()` method. | ||
| <Tabs> | ||
| <TabItem value="python" label="Python (unittest)"> | ||
|
|
||
| ```python | ||
| def test_all_variables(self): | ||
| """Test retrieving all variables using all_variables()""" | ||
| responses.add( | ||
| responses.POST, | ||
| self._get_variables_url(), | ||
| json={ | ||
| "string-test-var": { | ||
| "_id": "123456", | ||
| "key": "string-test-var", | ||
| "type": "String", | ||
| "value": "var-on", | ||
| "eval": { | ||
| "reason": "TARGETING_MATCH", | ||
| "details": "All Users", | ||
| "target_id": "654321" | ||
| } | ||
| }, | ||
| "bool-test-var": { | ||
| "_id": "123456", | ||
| "key": "bool-test-var", | ||
| "type": "Boolean", | ||
| "value": True, | ||
| "eval": { | ||
| "reason": "TARGETING_MATCH", | ||
| "details": "All Users", | ||
| "target_id": "654321" | ||
| } | ||
| } | ||
| }, | ||
| status=200, | ||
| ) | ||
|
|
||
| user = DevCycleUser(user_id="test-user-123") | ||
| result = self.client.all_variables(user) | ||
|
|
||
| # Verify the response structure | ||
| self.assertIsInstance(result, dict) | ||
| self.assertIn("string-test-var", result) | ||
| self.assertIn("bool-test-var", result) | ||
|
|
||
| # Verify variable values | ||
| self.assertEqual(result["string-test-var"].value, "var-on") | ||
| self.assertEqual(result["string-test-var"].key, "string-test-var") | ||
| self.assertEqual(result["string-test-var"].type, "String") | ||
|
|
||
| self.assertTrue(result["bool-test-var"].value) | ||
| self.assertEqual(result["bool-test-var"].key, "bool-test-var") | ||
| self.assertEqual(result["bool-test-var"].type, "Boolean") | ||
|
|
||
| # Verify the request was made to the correct URL | ||
| self.assertEqual(len(responses.calls), 1) | ||
| self.assertEqual(responses.calls[0].request.method, "POST") | ||
| self.assertEqual(responses.calls[0].request.url, self._get_variables_url()) | ||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ## Client-Side SDKs: Bootstrapping | ||
|
|
||
| Client-side Web SDKs (JavaScript, React, Next.js) support bootstrapping, which allows you to pass a pre-configured DevCycle configuration during initialization. | ||
|
|
||
| ```javascript | ||
| const bootstrapConfig = { | ||
| // Your test configuration here | ||
| } | ||
|
|
||
| const devcycleClient = initializeDevCycle( | ||
| '<DEVCYCLE_CLIENT_SDK_KEY>', | ||
| user, | ||
| { bootstrapConfig } | ||
| ) | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| - Use consistent test configurations across your test suite | ||
| - Test both enabled and disabled states of Features | ||
| - Test Variables and Variations to ensure all code paths are covered | ||
| - Keep mock configurations close to your test files for easy maintenance | ||
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
We generally use
dvc_server_<token>_hashas the fake tokens.