-
-
Notifications
You must be signed in to change notification settings - Fork 981
[Community] init Iceberg support #3094
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
Merged
Merged
Changes from all commits
Commits
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
159 changes: 159 additions & 0 deletions
159
additional_tests/historical_backend_tests/test_iceberg_candles.py
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,159 @@ | ||
# This file is part of OctoBot (https://github.com/Drakkar-Software/OctoBot) | ||
# Copyright (c) 2025 Drakkar-Software, All rights reserved. | ||
# | ||
# OctoBot is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# OctoBot is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public | ||
# License along with OctoBot. If not, see <https://www.gnu.org/licenses/>. | ||
import math | ||
import time | ||
import mock | ||
import pytest | ||
import asyncio | ||
import pyiceberg.table | ||
|
||
from additional_tests.historical_backend_tests import iceberg_client | ||
import octobot.community.history_backend.iceberg_historical_backend_client as iceberg_historical_backend_client | ||
import octobot.community.history_backend.util as history_backend_util | ||
|
||
import octobot_commons.enums as commons_enums | ||
import octobot_commons.constants as commons_constants | ||
|
||
|
||
# All test coroutines will be treated as marked. | ||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
EXCHANGE = "binance" | ||
SYMBOL = "BTC/USDC" | ||
SHORT_TIME_FRAME = commons_enums.TimeFrames.FIFTEEN_MINUTES | ||
|
||
|
||
async def test_fetch_candles_history_range(iceberg_client): | ||
# unknown candles | ||
min_time, max_time = await iceberg_client.fetch_candles_history_range( | ||
EXCHANGE+"plop", SYMBOL, SHORT_TIME_FRAME | ||
) | ||
assert min_time == max_time == 0 | ||
|
||
# known candles | ||
min_time, max_time = await iceberg_client.fetch_candles_history_range( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME | ||
) | ||
assert 0 < min_time < max_time < time.time() | ||
|
||
|
||
async def test_fetch_candles_history(iceberg_client): | ||
start_time = 1718785679 | ||
end_time = 1721377495 | ||
candles_count = math.floor((end_time - start_time) / ( | ||
commons_enums.TimeFramesMinutes[SHORT_TIME_FRAME] * 60 | ||
)) | ||
# requires multiple fetches | ||
assert candles_count == 2879 | ||
candles = await iceberg_client.fetch_candles_history( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME, start_time, end_time | ||
) | ||
assert sorted(candles, key=lambda c: c[0]) == candles | ||
fetched_count = candles_count + 1 | ||
assert len(candles) == fetched_count | ||
# will fail if parsed time is not UTC | ||
assert candles[0][commons_enums.PriceIndexes.IND_PRICE_TIME.value] == 1718785800 | ||
assert ( | ||
candles[0][commons_enums.PriceIndexes.IND_PRICE_TIME.value] | ||
!= candles[0][commons_enums.PriceIndexes.IND_PRICE_OPEN.value] | ||
!= candles[0][commons_enums.PriceIndexes.IND_PRICE_HIGH.value] | ||
!= candles[0][commons_enums.PriceIndexes.IND_PRICE_LOW.value] | ||
!= candles[0][commons_enums.PriceIndexes.IND_PRICE_CLOSE.value] | ||
!= candles[0][commons_enums.PriceIndexes.IND_PRICE_VOL.value] | ||
) | ||
# candles are unique | ||
assert len(set(c[0] for c in candles)) == fetched_count | ||
|
||
|
||
async def test_fetch_candles_history_asynchronousness(iceberg_client): | ||
start_time = 1718785679 | ||
end_time_1 = 1721377495 | ||
end_time_2 = 1721377495 + 2 * commons_constants.DAYS_TO_SECONDS | ||
end_time_3 = 1721377495 + 23 * commons_constants.DAYS_TO_SECONDS | ||
|
||
scan_call_times = [] | ||
_to_arrow_call_times = [] | ||
_to_arrow_return_times = [] | ||
|
||
def _get_or_create_table(*args, **kwargs): | ||
table = original_get_or_create_table(*args, **kwargs) | ||
original_scan = table.scan | ||
|
||
|
||
def _scan(*args, **kwargs): | ||
scan_call_times.append(time.time()) | ||
scan_result = original_scan(*args, **kwargs) | ||
original_to_arrow = scan_result.to_arrow | ||
|
||
def _to_arrow(*args, **kwargs): | ||
_to_arrow_call_times.append(time.time()) | ||
try: | ||
return original_to_arrow(*args, **kwargs) | ||
finally: | ||
_to_arrow_return_times.append(time.time()) | ||
|
||
scan_result.to_arrow = _to_arrow | ||
return scan_result | ||
|
||
table.scan = mock.Mock(side_effect=_scan) | ||
return table | ||
|
||
original_get_or_create_table = iceberg_client.get_or_create_table | ||
with ( | ||
mock.patch.object(iceberg_client, "get_or_create_table", mock.Mock(side_effect=_get_or_create_table)) as get_or_create_table_mock | ||
): | ||
candles_1, candles_2, candles_3 = await asyncio.gather( | ||
iceberg_client.fetch_candles_history( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME, start_time, end_time_1 | ||
), | ||
iceberg_client.fetch_candles_history( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME, start_time, end_time_2 | ||
), | ||
iceberg_client.fetch_candles_history( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME, start_time, end_time_3 | ||
), | ||
) | ||
assert get_or_create_table_mock.call_count == 3 | ||
assert len(scan_call_times) == 3 | ||
assert len(_to_arrow_call_times) == 3 | ||
assert len(_to_arrow_return_times) == 3 | ||
|
||
assert scan_call_times[0] <= scan_call_times[1] <= scan_call_times[2] | ||
assert _to_arrow_call_times[0] <= _to_arrow_call_times[1] <= _to_arrow_call_times[2] | ||
assert _to_arrow_return_times[0] < _to_arrow_return_times[1] < _to_arrow_return_times[2] | ||
|
||
# all to_arrow calls have been performed before the first to_arrow return, | ||
# which means they are running concurrently in this async context | ||
assert max(_to_arrow_call_times) < min(_to_arrow_return_times) | ||
|
||
assert len(candles_1) > 2000 | ||
assert len(candles_2) > len(candles_1) | ||
assert len(candles_3) > len(candles_2) | ||
|
||
|
||
async def test_deduplicate(iceberg_client): | ||
start_time = 1718785679 | ||
end_time = 1721377495 | ||
candles = await iceberg_client.fetch_candles_history( | ||
EXCHANGE, SYMBOL, SHORT_TIME_FRAME, start_time, end_time | ||
) | ||
duplicated = candles + candles | ||
assert len(duplicated) == len(candles) * 2 | ||
assert sorted(candles, key=lambda c: c[0]) == candles | ||
deduplicated = history_backend_util.deduplicate(duplicated, 0) | ||
# deduplicated and still sorted | ||
assert deduplicated == candles |
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
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.
👍