def assert_okay(response): """Helper function to run next query and its assertion twice""" result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2 binance trades assert_binance_trades_result([t['entry'] for t in result if t['entry']['location'] == 'binance']) # noqa: E501 msg = 'binance trades should now be ignored for accounting' assert all(t['ignored_in_accounting'] is True for t in result if t['entry']['location'] == 'binance'), msg # noqa: E501
def assert_okay(response): """Helper function for DRY checking below assertions""" task_id = assert_ok_async_response(response) outcome = wait_for_async_task(rotkehlchen_api_server_with_exchanges, task_id) assert_binance_trades_result(outcome['result']['binance']) assert_poloniex_trades_result(outcome['result']['poloniex'], trades_to_check=(0, ))
def assert_okay(response): """Helper function to run next query and its assertion twice""" assert_proper_response(response) data = response.json() assert data['message'] == '' assert len(data['result']) == 2 # only 2 binance trades assert_binance_trades_result( [t for t in data['result'] if t['location'] == 'binance'])
def assert_okay(response): """Helper function for DRY checking below assertions""" assert_proper_response(response) json_data = response.json() assert json_data['message'] == '' assert len(json_data['result'] ) == 2, 'only two exchanges should be registered' assert_binance_trades_result(json_data['result']['binance']) assert_poloniex_trades_result(json_data['result']['poloniex'], trades_to_check=(0, ))
def test_exchange_query_trades(rotkehlchen_api_server_with_exchanges): """Test that using the exchange trades query endpoint works fine""" server = rotkehlchen_api_server_with_exchanges setup = mock_history_processing_and_exchanges(server.rest_api.rotkehlchen) # query trades of one specific exchange with setup.binance_patch: response = requests.get( api_url_for( server, "named_exchanges_trades_resource", name='binance', )) assert_proper_response(response) json_data = response.json() assert json_data['message'] == '' assert_binance_trades_result(json_data['result']) # query trades of all exchanges with setup.binance_patch, setup.polo_patch: response = requests.get(api_url_for(server, "exchangetradesresource")) assert_proper_response(response) json_data = response.json() assert json_data['message'] == '' assert len( json_data['result']) == 2, 'only two exchanges should be registered' assert_binance_trades_result(json_data['result']['binance']) assert_poloniex_trades_result(json_data['result']['poloniex']) def assert_okay(response): """Helper function for DRY checking below assertions""" assert_proper_response(response) json_data = response.json() assert json_data['message'] == '' assert len(json_data['result'] ) == 2, 'only two exchanges should be registered' assert_binance_trades_result(json_data['result']['binance']) assert_poloniex_trades_result(json_data['result']['poloniex'], trades_to_check=(0, )) # and now query them in a specific time range excluding two of poloniex's trades data = {'from_timestamp': 1499865548, 'to_timestamp': 1539713118} with setup.binance_patch, setup.polo_patch: response = requests.get(api_url_for(server, "exchangetradesresource"), json=data) assert_okay(response) # do the same but with query args. This serves as test of from/to timestamp with query args with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for(server, "exchangetradesresource") + '?' + urlencode(data)) assert_okay(response)
def assert_okay(response): """Helper function for DRY checking below assertions""" if async_query: task_id = assert_ok_async_response(response) outcome = wait_for_async_task(rotkehlchen_api_server_with_exchanges, task_id) result = outcome['result'] else: result = assert_proper_response_with_result(response) trades = result['entries'] assert_binance_trades_result([x['entry'] for x in trades if x['entry']['location'] == 'binance']) # noqa: E501 assert_poloniex_trades_result( trades=[x['entry'] for x in trades if x['entry']['location'] == 'poloniex'], trades_to_check=(2,), )
def test_exchange_query_trades_async(rotkehlchen_api_server_with_exchanges): """Test that using the exchange trades query endpoint works fine when called asynchronously""" server = rotkehlchen_api_server_with_exchanges setup = mock_history_processing_and_exchanges(server.rest_api.rotkehlchen) # async query trades of one specific exchange with setup.binance_patch: response = requests.get(api_url_for( server, "named_exchanges_trades_resource", name='binance', ), json={'async_query': True}) task_id = assert_ok_async_response(response) outcome = wait_for_async_task(rotkehlchen_api_server_with_exchanges, task_id) assert_binance_trades_result(outcome['result']) def assert_okay(response): """Helper function for DRY checking below assertions""" task_id = assert_ok_async_response(response) outcome = wait_for_async_task(rotkehlchen_api_server_with_exchanges, task_id) assert_binance_trades_result(outcome['result']['binance']) assert_poloniex_trades_result(outcome['result']['poloniex'], trades_to_check=(0, )) # query trades of all exchanges and in a specific range asynchronously data = { 'from_timestamp': 1499865548, 'to_timestamp': 1539713118, 'async_query': True } with setup.binance_patch, setup.polo_patch: response = requests.get(api_url_for(server, "exchangetradesresource"), json=data) assert_okay(response) # do the same but with query args. This serves as test of async query with query args with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for(server, "exchangetradesresource") + '?' + urlencode(data), ) assert_okay(response)
def test_query_trades(rotkehlchen_api_server_with_exchanges): """Test that querying the trades endpoint works as expected Many similarities with test_exchanges.py::test_exchange_query_trades since those two endpoints got merged. """ rotki = rotkehlchen_api_server_with_exchanges.rest_api.rotkehlchen setup = mock_history_processing_and_exchanges(rotki) # Simply get all trades without any filtering with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), ) result = assert_proper_response_with_result(response) result = result['entries'] assert len(result) == 5 # 3 polo and 2 binance trades binance_ids = [t['entry']['trade_id'] for t in result if t['entry']['location'] == 'binance'] assert_binance_trades_result([t['entry'] for t in result if t['entry']['location'] == 'binance']) # noqa: E501 assert_poloniex_trades_result([t['entry'] for t in result if t['entry']['location'] == 'poloniex']) # noqa: E501 msg = 'should have no ignored trades at start' assert all(t['ignored_in_accounting'] is False for t in result), msg # now also try to ignore all binance trades for accounting response = requests.put( api_url_for( rotkehlchen_api_server_with_exchanges, 'ignoredactionsresource', ), json={'action_type': 'trade', 'action_ids': binance_ids}, ) result = assert_proper_response_with_result(response) assert result == {'trade': binance_ids} def assert_okay(response): """Helper function to run next query and its assertion twice""" result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2 binance trades assert_binance_trades_result([t['entry'] for t in result if t['entry']['location'] == 'binance']) # noqa: E501 msg = 'binance trades should now be ignored for accounting' assert all(t['ignored_in_accounting'] is True for t in result if t['entry']['location'] == 'binance'), msg # noqa: E501 # Now filter by location with json body with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={'location': 'binance'}, ) assert_okay(response) # Now filter by location with query params with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ) + '?location=binance', ) assert_okay(response) # Now filter by time with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={'from_timestamp': 1512561942, 'to_timestamp': 1539713237}, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 3 # 1 binance trade and 2 poloniex trades assert_binance_trades_result( trades=[t['entry'] for t in result if t['entry']['location'] == 'binance'], trades_to_check=(0,), ) assert_poloniex_trades_result( trades=[t['entry'] for t in result if t['entry']['location'] == 'poloniex'], trades_to_check=(1, 2), ) # and now filter by both time and location with setup.binance_patch, setup.polo_patch: data = {'from_timestamp': 1512561942, 'to_timestamp': 1539713237, 'location': 'poloniex'} response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json=data, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2/3 poloniex trades assert_poloniex_trades_result( trades=[t['entry'] for t in result if t['entry']['location'] == 'poloniex'], trades_to_check=(1, 2), )
def assert_okay(response): """Helper function to run next query and its assertion twice""" result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2 binance trades assert_binance_trades_result( [t for t in result if t['location'] == 'binance'])
def test_query_trades(rotkehlchen_api_server_with_exchanges): """Test that querying the trades endpoint works as expected Many similarities with test_exchanges.py::test_exchange_query_trades since those two endpoints got merged. """ rotki = rotkehlchen_api_server_with_exchanges.rest_api.rotkehlchen setup = mock_history_processing_and_exchanges(rotki) # Simply get all trades without any filtering with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), ) result = assert_proper_response_with_result(response) result = result['entries'] assert len(result) == 5 # 3 polo and 2 binance trades assert_binance_trades_result( [t for t in result if t['location'] == 'binance']) assert_poloniex_trades_result( [t for t in result if t['location'] == 'poloniex']) def assert_okay(response): """Helper function to run next query and its assertion twice""" result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2 binance trades assert_binance_trades_result( [t for t in result if t['location'] == 'binance']) # Now filter by location with json body with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={'location': 'binance'}, ) assert_okay(response) # Now filter by location with query params with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ) + '?location=binance', ) assert_okay(response) # Now filter by time with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={ 'from_timestamp': 1512561942, 'to_timestamp': 1539713237 }, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 3 # 1 binance trade and 2 poloniex trades assert_binance_trades_result( trades=[t for t in result if t['location'] == 'binance'], trades_to_check=(0, ), ) assert_poloniex_trades_result( trades=[t for t in result if t['location'] == 'poloniex'], trades_to_check=(1, 2), ) # and now filter by both time and location with setup.binance_patch, setup.polo_patch: data = { 'from_timestamp': 1512561942, 'to_timestamp': 1539713237, 'location': 'poloniex' } response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json=data, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2/3 poloniex trades assert_poloniex_trades_result( trades=[t for t in result if t['location'] == 'poloniex'], trades_to_check=(1, 2), )
def test_exchange_query_trades(rotkehlchen_api_server_with_exchanges): """Test that using the exchange trades query endpoint works fine""" async_query = random.choice([False, True]) server = rotkehlchen_api_server_with_exchanges setup = mock_history_processing_and_exchanges(server.rest_api.rotkehlchen) # query trades of one specific exchange with setup.binance_patch: response = requests.get( api_url_for( server, "tradesresource", ), json={ 'location': 'binance', 'async_query': async_query }, ) if async_query: task_id = assert_ok_async_response(response) outcome = wait_for_async_task( rotkehlchen_api_server_with_exchanges, task_id) result = outcome['result'] else: result = assert_proper_response_with_result(response) assert result['entries_found'] > 0 assert result['entries_limit'] == FREE_TRADES_LIMIT assert_binance_trades_result([x['entry'] for x in result['entries']]) # query trades of all exchanges with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for(server, "tradesresource"), json={'async_query': async_query}, ) if async_query: task_id = assert_ok_async_response(response) outcome = wait_for_async_task( rotkehlchen_api_server_with_exchanges, task_id) result = outcome['result'] else: result = assert_proper_response_with_result(response) trades = result['entries'] assert_binance_trades_result([ x['entry'] for x in trades if x['entry']['location'] == 'binance' ]) # noqa: E501 assert_poloniex_trades_result([ x['entry'] for x in trades if x['entry']['location'] == 'poloniex' ]) # noqa: E501 def assert_okay(response): """Helper function for DRY checking below assertions""" if async_query: task_id = assert_ok_async_response(response) outcome = wait_for_async_task( rotkehlchen_api_server_with_exchanges, task_id) result = outcome['result'] else: result = assert_proper_response_with_result(response) trades = result['entries'] assert_binance_trades_result([ x['entry'] for x in trades if x['entry']['location'] == 'binance' ]) # noqa: E501 assert_poloniex_trades_result( trades=[ x['entry'] for x in trades if x['entry']['location'] == 'poloniex' ], trades_to_check=(2, ), ) # and now query them in a specific time range excluding two of poloniex's trades data = { 'from_timestamp': 1499865548, 'to_timestamp': 1539713118, 'async_query': async_query } with setup.binance_patch, setup.polo_patch: response = requests.get(api_url_for(server, "tradesresource"), json=data) assert_okay(response) # do the same but with query args. This serves as test of from/to timestamp with query args with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for(server, "tradesresource") + '?' + urlencode(data)) assert_okay(response)
def test_query_trades(rotkehlchen_api_server_with_exchanges, start_with_valid_premium): """Test that querying the trades endpoint works as expected Many similarities with test_exchanges.py::test_exchange_query_trades since those two endpoints got merged. """ rotki = rotkehlchen_api_server_with_exchanges.rest_api.rotkehlchen setup = mock_history_processing_and_exchanges(rotki) # Simply get all trades without any filtering with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), ) result = assert_proper_response_with_result(response) result = result['entries'] assert len(result) == 5 # 3 polo and 2 binance trades binance_ids = [ t['entry']['trade_id'] for t in result if t['entry']['location'] == 'binance' ] assert_binance_trades_result([ t['entry'] for t in result if t['entry']['location'] == 'binance' ]) # noqa: E501 assert_poloniex_trades_result([ t['entry'] for t in result if t['entry']['location'] == 'poloniex' ]) # noqa: E501 msg = 'should have no ignored trades at start' assert all(t['ignored_in_accounting'] is False for t in result), msg # now also try to ignore all binance trades for accounting response = requests.put( api_url_for( rotkehlchen_api_server_with_exchanges, 'ignoredactionsresource', ), json={ 'action_type': 'trade', 'action_ids': binance_ids }, ) result = assert_proper_response_with_result(response) assert set(result['trade']) == set(binance_ids) def assert_okay(response): """Helper function to run next query and its assertion twice""" result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2 binance trades assert_binance_trades_result([ t['entry'] for t in result if t['entry']['location'] == 'binance' ]) # noqa: E501 msg = 'binance trades should now be ignored for accounting' assert all(t['ignored_in_accounting'] is True for t in result if t['entry']['location'] == 'binance'), msg # noqa: E501 # Now filter by location with json body with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json={'location': 'binance'}, ) assert_okay(response) # Now filter by location with query params with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ) + '?location=binance', ) assert_okay(response) # Now filter by time with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json={ 'from_timestamp': 1512561942, 'to_timestamp': 1539713237 }, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 3 # 1 binance trade and 2 poloniex trades assert_binance_trades_result( trades=[ t['entry'] for t in result if t['entry']['location'] == 'binance' ], trades_to_check=(0, ), ) assert_poloniex_trades_result( trades=[ t['entry'] for t in result if t['entry']['location'] == 'poloniex' ], trades_to_check=(1, 2), ) # filter by both time and location with setup.binance_patch, setup.polo_patch: data = { 'from_timestamp': 1512561942, 'to_timestamp': 1539713237, 'location': 'poloniex' } response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json=data, ) result = assert_proper_response_with_result(response)['entries'] assert len(result) == 2 # only 2/3 poloniex trades assert_poloniex_trades_result( trades=[ t['entry'] for t in result if t['entry']['location'] == 'poloniex' ], trades_to_check=(1, 2), ) # test pagination data = { 'location': 'poloniex', 'offset': 1, 'limit': 1, 'only_cache': True } response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json=data, ) result = assert_proper_response_with_result(response) assert result[ 'entries_limit'] == -1 if start_with_valid_premium else FREE_TRADES_LIMIT assert result['entries_total'] == 5 assert result['entries_found'] == 3 # for this filter result = result['entries'] assert len(result) == 1 # this filter and pagination assert_poloniex_trades_result( trades=[ t['entry'] for t in result if t['entry']['location'] == 'poloniex' ], trades_to_check=(1, ), ) def assert_order_by(order_by: str): """A helper to keep things DRY in the test""" data = { 'order_by_attribute': order_by, 'ascending': False, 'only_cache': True } response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json=data, ) result = assert_proper_response_with_result(response) assert result[ 'entries_limit'] == -1 if start_with_valid_premium else FREE_TRADES_LIMIT assert result['entries_total'] == 5 assert result['entries_found'] == 5 desc_result = result['entries'] assert len(desc_result) == 5 data = { 'order_by_attribute': order_by, 'ascending': True, 'only_cache': True } response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, 'tradesresource', ), json=data, ) result = assert_proper_response_with_result(response) assert result[ 'entries_limit'] == -1 if start_with_valid_premium else FREE_TRADES_LIMIT assert result['entries_total'] == 5 assert result['entries_found'] == 5 asc_result = result['entries'] assert len(asc_result) == 5 return desc_result, asc_result # test order by location desc_result, asc_result = assert_order_by('location') assert all(x['entry']['location'] == 'binance' for x in desc_result[:2]) assert all(x['entry']['location'] == 'poloniex' for x in desc_result[2:]) assert all(x['entry']['location'] == 'poloniex' for x in asc_result[:3]) assert all(x['entry']['location'] == 'binance' for x in asc_result[3:]) # test order by type desc_result, asc_result = assert_order_by('type') assert all(x['entry']['trade_type'] == 'sell' for x in desc_result[:2]) assert all(x['entry']['trade_type'] == 'buy' for x in desc_result[2:]) assert all(x['entry']['trade_type'] == 'buy' for x in asc_result[:3]) assert all(x['entry']['trade_type'] == 'sell' for x in asc_result[3:]) # test order by amount desc_result, asc_result = assert_order_by('amount') for idx, x in enumerate(desc_result): if idx < len(desc_result) - 1: assert FVal(x['entry']['amount']) >= FVal( desc_result[idx + 1]['entry']['amount']) for idx, x in enumerate(asc_result): if idx < len(asc_result) - 1: assert FVal(x['entry']['amount']) <= FVal( asc_result[idx + 1]['entry']['amount']) # test order by rate desc_result, asc_result = assert_order_by('rate') for idx, x in enumerate(desc_result): if idx < len(desc_result) - 1: assert FVal(x['entry']['rate']) >= FVal( desc_result[idx + 1]['entry']['rate']) for idx, x in enumerate(asc_result): if idx < len(asc_result) - 1: assert FVal(x['entry']['rate']) <= FVal( asc_result[idx + 1]['entry']['rate']) # test order by fee desc_result, asc_result = assert_order_by('fee') for idx, x in enumerate(desc_result): if idx < len(desc_result) - 1: assert FVal(x['entry']['fee']) >= FVal( desc_result[idx + 1]['entry']['fee']) for idx, x in enumerate(asc_result): if idx < len(asc_result) - 1: assert FVal(x['entry']['fee']) <= FVal( asc_result[idx + 1]['entry']['fee'])
def test_query_trades(rotkehlchen_api_server_with_exchanges): """Test that querying the trades endpoint works as expected""" rotki = rotkehlchen_api_server_with_exchanges.rest_api.rotkehlchen setup = mock_history_processing_and_exchanges(rotki) # Query trades of all exchanges to get them saved in the DB with setup.binance_patch, setup.polo_patch: response = requests.get( api_url_for(rotkehlchen_api_server_with_exchanges, "exchangetradesresource")) assert_proper_response(response) # Simply get all trades without any filtering response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), ) assert_proper_response(response) data = response.json() assert data['message'] == '' assert len(data['result']) == 5 # 3 polo and 2 binance trades assert_binance_trades_result( [t for t in data['result'] if t['location'] == 'binance']) assert_poloniex_trades_result( [t for t in data['result'] if t['location'] == 'poloniex']) def assert_okay(response): """Helper function to run next query and its assertion twice""" assert_proper_response(response) data = response.json() assert data['message'] == '' assert len(data['result']) == 2 # only 2 binance trades assert_binance_trades_result( [t for t in data['result'] if t['location'] == 'binance']) # Now filter by location with json body response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={'location': 'binance'}, ) assert_okay(response) # Now filter by location with query params response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ) + '?location=binance', ) assert_okay(response) # Now filter by time response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={ 'from_timestamp': 1512561942, 'to_timestamp': 1539713237 }, ) assert_proper_response(response) data = response.json() assert data['message'] == '' assert len(data['result']) == 3 # 1 binance trade and 2 poloniex trades assert_binance_trades_result( trades=[t for t in data['result'] if t['location'] == 'binance'], trades_to_check=(1, ), ) assert_poloniex_trades_result( trades=[t for t in data['result'] if t['location'] == 'poloniex'], trades_to_check=(0, 1), ) # and now filter by both time and location response = requests.get( api_url_for( rotkehlchen_api_server_with_exchanges, "tradesresource", ), json={ 'from_timestamp': 1512561942, 'to_timestamp': 1539713237, 'location': 'poloniex' }, ) assert_proper_response(response) data = response.json() assert data['message'] == '' assert len(data['result']) == 2 # only 2/3 poloniex trades assert_poloniex_trades_result( trades=[t for t in data['result'] if t['location'] == 'poloniex'], trades_to_check=(0, 1), )