-
Notifications
You must be signed in to change notification settings - Fork 7
#460, adding tests #514
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
Open
pierceroberts
wants to merge
6
commits into
cowprotocol:main
Choose a base branch
from
pierceroberts:460-add-tests
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.
Open
#460, adding tests #514
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a489e5
first test
pierceroberts e9f46cd
add test for compute_block_range
pierceroberts 8a40a51
add init test for find_block_with_timestamp
pierceroberts 75260d5
rm code dupe, add tzinfo check.
pierceroberts 294cb32
add new line at bottom.
pierceroberts 9837de2
run black
pierceroberts 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| # Python | ||
| *.pyc | ||
| venv | ||
| __pycache__ | ||
|
|
||
| .env | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ black | |
| mypy | ||
| pylint | ||
| pytest | ||
| pytest-mock | ||
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 |
|---|---|---|
|
|
@@ -76,7 +76,6 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen | |
| service_fee: Fraction, | ||
| reward_token_address: Address, | ||
| ): | ||
|
|
||
|
Author
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. this was from running |
||
| assert quote_reward_cow >= 0, "invalid quote_reward_cow" | ||
|
|
||
| self.solver = solver | ||
|
|
||
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,87 @@ | ||
| import pytest | ||
| from pytest_mock import MockerFixture | ||
| import datetime | ||
| from src.data_sync.common import ( | ||
| partition_time_range, | ||
| compute_block_range, | ||
| find_block_with_timestamp, | ||
| ) | ||
|
|
||
| from web3 import Web3 | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def times(): | ||
| start_time = datetime.datetime.now(tz=datetime.timezone.utc) | ||
| end_time = start_time + datetime.timedelta(seconds=10) | ||
| return {"start_time": start_time, "end_time": end_time} | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def mock_node(): | ||
| return Web3() | ||
|
|
||
|
|
||
| def test_partition_time_range(times): | ||
| # test that if they are equal or negative the function raises an error | ||
| with pytest.raises(AssertionError): | ||
| partition_time_range( | ||
| start_time=times["start_time"], end_time=times["start_time"] | ||
| ) | ||
|
|
||
| # test timezones don't match | ||
| with pytest.raises(AssertionError): | ||
| partition_time_range( | ||
| start_time=datetime.datetime.now(), end_time=times["end_time"] | ||
| ) | ||
|
|
||
| with pytest.raises(AssertionError): | ||
| partition_time_range( | ||
| start_time=times["start_time"], | ||
| end_time=times["end_time"] + datetime.timedelta(seconds=-10), | ||
| ) | ||
|
|
||
| # end time is not greater than a month | ||
| result = partition_time_range( | ||
| start_time=times["start_time"], end_time=times["end_time"] | ||
| ) | ||
| assert result == [(times["start_time"], times["end_time"])] | ||
|
|
||
| # end time is greater than a month | ||
| result = partition_time_range( | ||
| start_time=times["start_time"], | ||
| end_time=times["end_time"] + datetime.timedelta(days=60), | ||
| ) | ||
|
|
||
| assert len(result) == 3 | ||
|
|
||
|
|
||
| def test_compute_block_range(mocker: MockerFixture, times, mock_node): | ||
| mock_find_block = mocker.patch( | ||
| "src.data_sync.common.find_block_with_timestamp", side_effect=[200, 400] | ||
| ) | ||
| mock_get_block = mocker.patch.object( | ||
| mock_node.eth, | ||
| "get_block", | ||
| return_value={"number": 400, "timestamp": times["end_time"].timestamp()}, | ||
| ) | ||
| results = compute_block_range( | ||
| start_time=times["start_time"], end_time=times["end_time"], node=mock_node | ||
| ) | ||
|
|
||
| assert mock_find_block.call_count == 2 | ||
| mock_get_block.assert_called_once() | ||
| assert results.block_from == 200 | ||
| assert results.block_to == 399 | ||
|
|
||
|
|
||
| def test_find_block_with_timestamp(mocker: MockerFixture, mock_node, times): | ||
| end_timestamp = times["end_time"].timestamp() | ||
| mock_get_block = mocker.patch.object( | ||
| mock_node.eth, | ||
| "get_block", | ||
| return_value={"number": 400, "timestamp": end_timestamp}, | ||
| ) | ||
| results = find_block_with_timestamp(node=mock_node, time_stamp=end_timestamp) | ||
| assert results == 400 | ||
| assert mock_get_block.call_count == 3 |
Oops, something went wrong.
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.
@bh2smith I added this to one function. What do you think?
Are you cool with me adding them to the others or would you prefer I create a utility function for this check?
Thanks for the input.