-
Notifications
You must be signed in to change notification settings - Fork 10
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
TDL-18879: Clear offset when max_skip
error is encountered and return 0 if skip
is greater than 250k in the current state.
#29
Open
hpatel41
wants to merge
7
commits into
master
Choose a base branch
from
TDL-18879-update-default-window-size-for-activities-stream-to-5-days
base: master
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7848ba0
updated default window size to 5 days for activities stream
2add6aa
addressed review comments
e6ef2be
updated the code to clear offset of max_skip error is encountered
451c435
resolved review comments
a33f38b
added unittests
5ce87fb
updated error message
4a99c74
updated error message
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 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 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 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 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,119 @@ | ||
from unittest import mock | ||
from tap_closeio.schemas import IDS | ||
from tap_closeio.context import Context | ||
from tap_closeio.streams import paginated_sync, create_request | ||
import unittest | ||
|
||
# mock Page class and set next_skip and records in the page | ||
class MockPage: | ||
def __init__(self, records, next_skip): | ||
self.records = records | ||
self.next_skip = next_skip | ||
|
||
# mock paginate function and yield MockPage | ||
def mock_paginate(*args, **kwargs): | ||
pages = [ | ||
MockPage([{"id": 1, "date_created": "2022-01-02"}, {"id": 1, "date_created": "2022-01-03"}], 2), | ||
MockPage([{"id": 1, "date_created": "2022-01-04"}, {"id": 1, "date_created": "2022-01-04"}], 4) | ||
] | ||
# yield 1st page, as a result after 1st page, the skip will be 2 | ||
yield pages[0] | ||
# if there is param in the request to raise error, then raise max_skip error | ||
if args[2].params.get("error"): | ||
raise Exception("The skip you set is larger than the maximum skip for this resource (max_skip = 250000).") | ||
# yield 2nd page | ||
yield pages[1] | ||
|
||
# mock format_dts function and return 3rd argument, ie. record | ||
def mock_format_dts(*args, **kwargs): | ||
return args[2] | ||
|
||
class TestExistingStateOffset(unittest.TestCase): | ||
""" | ||
Test cases to verify we are returning 0 if skip > 250K else return the skip in the existing state file | ||
""" | ||
def test_existing_state_offset_greater_than_250K(self): | ||
""" | ||
Test case to verify we are returning 0 as skip is greater than 250K in existing state | ||
""" | ||
# mock config | ||
config = { | ||
"start_date": "2022-01-01", | ||
"api_key": "test_API_key" | ||
} | ||
# mock state with skip > 250K | ||
state = { | ||
"currently_syncing": "activities", | ||
"bookmarks": { | ||
"activities": { | ||
"date_created": "2022-04-01T00:00:00", | ||
"offset": {"skip": 259000} | ||
} | ||
} | ||
} | ||
# create Context with config and state | ||
context = Context(config, state) | ||
# function call | ||
offset = context.get_offset(["activities", "skip"]) | ||
# verify we got 0 as we had skip > 250K | ||
self.assertEqual(offset, 0) | ||
|
||
def test_existing_state_offset_lesser_than_250K(self): | ||
""" | ||
Test case to verify we are returning existing skip as it is lesser than 250K in existing state | ||
""" | ||
# mock config | ||
config = { | ||
"start_date": "2022-01-01", | ||
"api_key": "test_API_key" | ||
} | ||
# mock state with skip < 250K | ||
state = { | ||
"currently_syncing": "activities", | ||
"bookmarks": { | ||
"activities": { | ||
"date_created": "2022-04-01T00:00:00", | ||
"offset": {"skip": 1000} | ||
} | ||
} | ||
} | ||
# create Context with config and state | ||
context = Context(config, state) | ||
# function call | ||
offset = context.get_offset(["activities", "skip"]) | ||
# verify we got existing skip from the state as skip < 250K | ||
self.assertEqual(offset, 1000) | ||
|
||
@mock.patch("tap_closeio.streams.write_records") | ||
@mock.patch("tap_closeio.streams.LOGGER.warning") | ||
@mock.patch("tap_closeio.streams.paginate", side_effect = mock_paginate) | ||
@mock.patch("tap_closeio.streams.format_dts", side_effect = mock_format_dts) | ||
class TestOffsetClear(unittest.TestCase): | ||
def test_clear_state_for_max_skip_error(self, mocked_format_dts, mocked_paginate, mocked_logger_warning, mocked_write_records): | ||
""" | ||
Test case to verify we clear the 'skip' when we encounter 'max_skip' error for 'activity' stream | ||
""" | ||
# mock config | ||
config = { | ||
"start_date": "2022-01-01", | ||
"api_key": "test_API_key" | ||
} | ||
# mock state | ||
state = {} | ||
# mock param to raise 'max_skip' error | ||
params = {"error": "true"} | ||
# create Context with config and state | ||
context = Context(config, state) | ||
# create request for activity stream with params | ||
request = create_request(IDS.ACTIVITIES, params) | ||
|
||
# verify we raise Exception during function call | ||
with self.assertRaises(Exception) as e: | ||
paginated_sync(IDS.ACTIVITIES, context, request, "2022-01-01") | ||
|
||
# verify the error message | ||
self.assertEqual(str(e.exception), "The skip you set is larger than the maximum skip for this resource (max_skip = 250000). So, clearing skip offset, please reduce the date window size and try again.") | ||
# verify we did not get 'skip' in the state file | ||
self.assertIsNone(state.get("bookmarks").get("activities").get("offset").get("skip")) | ||
# verify we log 'reduce date window' message | ||
mocked_logger_warning.assert_called_with("Hit max_skip error so clearing skip offset, please reduce the date window size and try again.") |
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.
It should be an error and must be raised with this message.
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.
Added