-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cf4a40
commit 437a156
Showing
9 changed files
with
111 additions
and
73 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
pytest==7.2.0 | ||
pytest-cov==4.0.0 | ||
pytest-postgresql==4.1.1 | ||
pytest-responses==0.5.1 |
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
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 |
---|---|---|
@@ -1,50 +1,33 @@ | ||
from typing import Generator | ||
import pandas as pd | ||
from datetime import datetime, timezone | ||
from worker import Worker | ||
from config import Config | ||
from sources import Binance | ||
|
||
|
||
class OfflineBinance(Binance): | ||
def __init__(self, data: pd.DataFrame, api_key: str = None, api_secret: str = None): | ||
super().__init__(api_key, api_secret) | ||
self._data = data | ||
self._data_generator = self._make_data_generator() | ||
self._next_data = next(self._data_generator, None) | ||
self._has_next = True | ||
|
||
def _make_data_generator(self) -> Generator[pd.DataFrame, None, None]: | ||
chunk_size = 100 | ||
pos = 0 | ||
while pos < len(self._data): | ||
yield self._data.iloc[pos:pos+chunk_size] | ||
pos = pos + chunk_size | ||
|
||
def get_klines(self, symbol: str, startTime: str, endTime: str = 'NOW', interval: str = '1m') -> pd.DataFrame: | ||
data = self._next_data | ||
self._next_data = next(self._data_generator, None) | ||
if self._next_data is None: | ||
self._has_next = False | ||
return data | ||
|
||
|
||
def test_worker(config: Config): | ||
df = pd.read_pickle('tests/test_dataframe.pkl') | ||
|
||
config.timescaledb.createDatabase(config.database) | ||
def test_worker(config: Config, responses, binance_ping_response, binance_get_klines_response): | ||
config.timescaledb.createDatabase(config.database, True) | ||
|
||
# Test with subset | ||
config.start_time = "25.12.2021" | ||
config.binance = OfflineBinance(data=df[df['time'].dt.day == 25]) | ||
config.start_time = "25.12.2022" | ||
config.end_time = "26.12.2022" | ||
worker = Worker(config) | ||
while config.binance._has_next: | ||
worker.run() | ||
worker.run() | ||
|
||
assert len(responses.calls) == 4 | ||
assert config.timescaledb.getFirstTimestamp( | ||
config.database, config.symbols[0]) == datetime(2022, 12, 25, tzinfo=timezone.utc) | ||
assert config.timescaledb.getLastTimestamp( | ||
config.database, config.symbols[0]) == datetime(2022, 12, 26, tzinfo=timezone.utc) | ||
|
||
# Use earlier start time and newer data | ||
config.start_time = "24.12.2021" | ||
config.binance = OfflineBinance(data=df) | ||
config.start_time = "24.12.2022" | ||
config.end_time = "NOW" | ||
worker = Worker(config) | ||
while config.binance._has_next: | ||
worker.run() | ||
worker.run() | ||
|
||
assert len(responses.calls) == 10 | ||
assert config.timescaledb.getFirstTimestamp( | ||
config.database, config.symbols[0]) == datetime(2022, 12, 24, tzinfo=timezone.utc) | ||
assert config.timescaledb.getLastTimestamp( | ||
config.database, config.symbols[0]) == datetime(2022, 12, 26, 17, 38, tzinfo=timezone.utc) | ||
|
||
config.timescaledb.dropDatabase(config.database) |