-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Data Client #1147
Open
s2t2
wants to merge
9
commits into
sammchardy:master
Choose a base branch
from
s2t2:df
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
Data Client #1147
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8c59aa9
Optional pandas dependency
s2t2 e95d639
Optional dependency
s2t2 1de6d0f
Refactor mock klines response
s2t2 eb915fc
Inherit methods from client
s2t2 4e63679
README example
s2t2 bf6c866
README
s2t2 820becd
Refactor mock data
s2t2 bacc32a
Alias methods for readability
s2t2 4ecba53
Lock pandas version
s2t2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pandas import DataFrame | ||
|
||
from binance.client import Client | ||
from binance.enums import KLINES_RESPONSE_COLUMNS | ||
|
||
class DataClient(Client): | ||
|
||
def get_historical_klines(self, **args): | ||
klines = super().get_historical_klines(**args) | ||
return DataFrame(klines, columns=KLINES_RESPONSE_COLUMNS) |
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,102 @@ | ||
|
||
import pytest | ||
import requests_mock | ||
|
||
from binance.client import Client | ||
from binance.data_client import DataClient | ||
from binance.enums import KLINE_INTERVAL_1MINUTE | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def client(): | ||
"""Returns a client to use for testing purposes""" | ||
return Client("api_key", "api_secret") | ||
|
||
@pytest.fixture(scope="module") | ||
def data_client(): | ||
"""Returns a data client to use for testing purposes""" | ||
return DataClient("api_key", "api_secret") | ||
|
||
|
||
# | ||
# KLINES RESPONSE DATA | ||
# | ||
|
||
|
||
klines_row_1 = [ | ||
1500004800000, | ||
"0.00005000", | ||
"0.00005300", | ||
"0.00001000", | ||
"0.00004790", | ||
"663152.00000000", | ||
1500004859999, | ||
"30.55108144", | ||
43, | ||
"559224.00000000", | ||
"25.65468144", | ||
"83431971.04346950", | ||
] | ||
|
||
klines_row_2 = [ | ||
1519892340000, | ||
"0.00099400", | ||
"0.00099810", | ||
"0.00099400", | ||
"0.00099810", | ||
"4806.04000000", | ||
1519892399999, | ||
"4.78553253", | ||
154, | ||
"1785.14000000", | ||
"1.77837524", | ||
"0", | ||
] | ||
|
||
url_1 = "https://api.binance.com/api/v3/klines?interval=1m&limit=1&startTime=0&symbol=BNBBTC" | ||
url_2 = "https://api.binance.com/api/v3/klines?interval=1m&limit=1000&startTime=1519862400000&symbol=BNBBTC" | ||
url_3 = "https://api.binance.com/api/v3/klines?interval=1m&limit=1000&startTime=1519892400000&symbol=BNBBTC" | ||
|
||
response_1 = [klines_row_1] | ||
response_2 = [klines_row_2 for _ in range(0,500)] | ||
response_3 = [] | ||
|
||
@pytest.fixture(scope="module") | ||
def historical_klines_response(client): | ||
with requests_mock.mock() as mock: | ||
mock.get(url_1, json=response_1) | ||
mock.get(url_2, json=response_2) | ||
mock.get(url_3, json=response_3) | ||
|
||
klines = client.get_historical_klines( | ||
symbol="BNBBTC", | ||
interval=KLINE_INTERVAL_1MINUTE, | ||
start_str="1st March 2018" | ||
) | ||
#print(klines) | ||
return klines | ||
|
||
@pytest.fixture(scope="module") | ||
def historical_klines_response_df(data_client): | ||
with requests_mock.mock() as mock: | ||
mock.get(url_1, json=response_1) | ||
mock.get(url_2, json=response_2) | ||
mock.get(url_3, json=response_3) | ||
|
||
klines = data_client.get_historical_klines( | ||
symbol="BNBBTC", | ||
interval=KLINE_INTERVAL_1MINUTE, | ||
start_str="1st March 2018" | ||
) | ||
#print(klines) | ||
return klines | ||
|
||
@pytest.fixture(scope="module") | ||
def klines(historical_klines_response): | ||
"""Alias method""" | ||
return historical_klines_response | ||
|
||
@pytest.fixture(scope="module") | ||
def klines_df(historical_klines_response_df): | ||
"""Alias method""" | ||
return historical_klines_response_df |
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 @@ | ||
pandas==1.4.3 |
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
Empty file.
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,10 @@ | ||
|
||
from pandas import DataFrame | ||
|
||
from binance.enums import KLINES_RESPONSE_COLUMNS | ||
|
||
|
||
def test_historical_klines(klines_df): | ||
assert len(klines_df) == 500 | ||
assert isinstance(klines_df, DataFrame) | ||
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. Nice! |
||
assert klines_df.columns.tolist() == KLINES_RESPONSE_COLUMNS |
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
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.
Are you shure, that this file should be stored in project root?
And add module's description
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.
Confest in the root directory is a best practice to help us avoid adding init files to the test directory. It facilities local imports, especially as they pertain to testing.
https://stackoverflow.com/questions/34466027/in-pytest-what-is-the-use-of-conftest-py-files