Skip to content

Commit

Permalink
Updated test cases and version number
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettre committed Dec 7, 2024
1 parent b986ca0 commit 819602f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 134 deletions.
2 changes: 1 addition & 1 deletion coinbase_advanced_trader/tests/test_account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_list_held_cryptocurrencies(self):
}
self.account_service._get_accounts = Mock(return_value=mock_accounts)

held_currencies = self.account_service.list_held_cryptocurrencies()
held_currencies = self.account_service.list_held_crypto_balances()
self.assertEqual(set(held_currencies), {'BTC', 'XRP'})

@patch('coinbase_advanced_trader.services.account_service.datetime')
Expand Down
36 changes: 6 additions & 30 deletions coinbase_advanced_trader/tests/test_enhanced_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,48 +68,24 @@ def test_fiat_limit_buy(self):
product_id = "BTC-USDC"
fiat_amount = "10"
price_multiplier = 0.9995
mock_order = Order(
id='fb67bb54-73ba-41ec-a038-9883664325b7',
product_id=product_id,
side=OrderSide.BUY,
type=OrderType.LIMIT,
size=Decimal('0.0002'),
price=Decimal('49975.00')
)
self.client._order_service.fiat_limit_buy.return_value = mock_order

result = self.client.fiat_limit_buy(
product_id, fiat_amount, price_multiplier
)

result = self.client.fiat_limit_buy(product_id, fiat_amount)

self.client._order_service.fiat_limit_buy.assert_called_once_with(
product_id, fiat_amount, price_multiplier
product_id, fiat_amount, None, price_multiplier
)
self.assertEqual(result, mock_order)

def test_fiat_limit_sell(self):
"""Test the fiat_limit_sell method."""
product_id = "BTC-USDC"
fiat_amount = "10"
price_multiplier = 1.005
mock_order = Order(
id='fb67bb54-73ba-41ec-a038-9883664325b7',
product_id=product_id,
side=OrderSide.SELL,
type=OrderType.LIMIT,
size=Decimal('0.0002'),
price=Decimal('50250.00')
)
self.client._order_service.fiat_limit_sell.return_value = mock_order

result = self.client.fiat_limit_sell(
product_id, fiat_amount, price_multiplier
)

result = self.client.fiat_limit_sell(product_id, fiat_amount)

self.client._order_service.fiat_limit_sell.assert_called_once_with(
product_id, fiat_amount, price_multiplier
product_id, fiat_amount, None, price_multiplier
)
self.assertEqual(result, mock_order)

def test_trade_based_on_fgi(self):
"""Test the trade_based_on_fgi method."""
Expand Down
160 changes: 62 additions & 98 deletions coinbase_advanced_trader/tests/test_order_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,133 +77,97 @@ def test_fiat_limit_buy(self):
"""Test the fiat_limit_buy method."""
product_id = "BTC-USDC"
fiat_amount = "10"
price_multiplier = Decimal('0.9995')

mock_spot_price = Decimal('50000')
mock_product_details = {
'base_increment': '0.00000001',
'quote_increment': '0.01'
}
self.price_service_mock.get_spot_price.return_value = mock_spot_price
self.price_service_mock.get_product_details.return_value = mock_product_details

mock_response = {

# Mock responses with correct format
mock_order_response = {
'success': True,
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'success_response': {
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'product_id': 'BTC-USDC',
'side': 'BUY',
'client_order_id': '12345678901'
},
'order_configuration': {
'limit_limit_gtc': {
'base_size': '0.00020010',
'limit_price': '49975.00',
'post_only': False
}
'order_id': 'test-order-id',
'product_id': product_id,
'side': 'BUY'
}
}
self.rest_client_mock.limit_order_gtc_buy.return_value = mock_response

order = self.order_service.fiat_limit_buy(product_id, fiat_amount, price_multiplier)
self.rest_client_mock.limit_order_gtc_buy.return_value = mock_order_response

# Mock price service to return specific values
spot_price = Decimal('50000')
base_increment = Decimal('0.00000001')
quote_increment = Decimal('0.01')
price_multiplier = Decimal('0.9995')

self.order_service.price_service.get_spot_price.return_value = spot_price
self.order_service.price_service.get_product_details.return_value = {
'base_increment': str(base_increment),
'quote_increment': str(quote_increment)
}

self.assertIsInstance(order, Order)
self.assertEqual(order.id, 'fb67bb54-73ba-41ec-a038-9883664325b7')
self.assertEqual(order.product_id, 'BTC-USDC')
self.assertEqual(order.side, OrderSide.BUY)
self.assertEqual(order.type, OrderType.LIMIT)
self.assertEqual(order.size, Decimal('0.00020010'))
self.assertEqual(order.price, Decimal('49975.00'))
order = self.order_service.fiat_limit_buy(product_id, fiat_amount)

# Calculate expected values
adjusted_price = (spot_price * price_multiplier).quantize(quote_increment)
expected_size = (Decimal(fiat_amount) / adjusted_price).quantize(base_increment)

self.assertEqual(order.size, expected_size)
self.assertEqual(order.price, adjusted_price)

def test_fiat_limit_sell(self):
"""Test the fiat_limit_sell method."""
product_id = "BTC-USDC"
fiat_amount = "10"
price_multiplier = Decimal('1.005')

mock_spot_price = Decimal('50000')
mock_product_details = {
'base_increment': '0.00000001',
'quote_increment': '0.01'
}
self.price_service_mock.get_spot_price.return_value = mock_spot_price
self.price_service_mock.get_product_details.return_value = mock_product_details

mock_response = {

# Mock responses with correct format
mock_order_response = {
'success': True,
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'success_response': {
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'product_id': 'BTC-USDC',
'side': 'SELL',
'client_order_id': '12345678901'
},
'order_configuration': {
'limit_limit_gtc': {
'base_size': '0.00019900',
'limit_price': '50250.00',
'post_only': False
}
'order_id': 'test-order-id',
'product_id': product_id,
'side': 'SELL'
}
}
self.rest_client_mock.limit_order_gtc_sell.return_value = mock_response
self.rest_client_mock.limit_order_gtc_sell.return_value = mock_order_response

# Mock price service responses
self.price_service_mock.get_spot_price.return_value = Decimal('50000')
self.price_service_mock.get_product_details.return_value = {
'base_increment': '0.00000001',
'quote_increment': '0.01'
}

order = self.order_service.fiat_limit_sell(product_id, fiat_amount, price_multiplier)

self.assertIsInstance(order, Order)
self.assertEqual(order.id, 'fb67bb54-73ba-41ec-a038-9883664325b7')
self.assertEqual(order.product_id, 'BTC-USDC')
self.assertEqual(order.side, OrderSide.SELL)
self.assertEqual(order.type, OrderType.LIMIT)
self.assertEqual(order.size, Decimal('0.00019900'))
self.assertEqual(order.price, Decimal('50250.00'))
order = self.order_service.fiat_limit_sell(product_id, fiat_amount)

expected_size = (Decimal(fiat_amount) / (Decimal('50000') * Decimal('1.005'))).quantize(Decimal('0.00000001'))
self.assertEqual(order.size, expected_size)

def test_place_limit_order(self):
"""Test the _place_limit_order method."""
product_id = "BTC-USDC"
fiat_amount = "10"
price_multiplier = Decimal('0.9995')
price_multiplier = 0.9995
side = OrderSide.BUY

mock_spot_price = Decimal('50000')
mock_product_details = {
'base_increment': '0.00000001',
'quote_increment': '0.01'
}
self.price_service_mock.get_spot_price.return_value = mock_spot_price
self.price_service_mock.get_product_details.return_value = mock_product_details

mock_response = {
# Mock responses with correct format
mock_order_response = {
'success': True,
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'success_response': {
'order_id': 'fb67bb54-73ba-41ec-a038-9883664325b7',
'product_id': 'BTC-USDC',
'side': 'BUY',
'client_order_id': '12345678901'
},
'order_configuration': {
'limit_limit_gtc': {
'base_size': '0.00020010',
'limit_price': '49975.00',
'post_only': False
}
'order_id': 'test-order-id',
'product_id': product_id,
'side': 'BUY'
}
}
self.rest_client_mock.limit_order_gtc_buy.return_value = mock_response
self.rest_client_mock.limit_order_gtc_buy.return_value = mock_order_response

# Mock price service responses
self.price_service_mock.get_spot_price.return_value = Decimal('50000')
self.price_service_mock.get_product_details.return_value = {
'base_increment': '0.00000001',
'quote_increment': '0.01'
}

order = self.order_service._place_limit_order(
product_id, fiat_amount, price_multiplier, side
product_id, fiat_amount, None, price_multiplier, side
)

self.assertIsInstance(order, Order)
self.assertEqual(order.id, 'fb67bb54-73ba-41ec-a038-9883664325b7')
self.assertEqual(order.product_id, 'BTC-USDC')
self.assertEqual(order.side, OrderSide.BUY)
self.assertEqual(order.type, OrderType.LIMIT)
self.assertEqual(order.size, Decimal('0.00020010'))
self.assertEqual(order.price, Decimal('49975.00'))

self.assertEqual(order.id, 'test-order-id')

@patch('coinbase_advanced_trader.services.order_service.logger')
def test_log_order_result(self, mock_logger):
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
coinbase-advanced-py
requests >=2.31.0
coinbase-advanced-py == 1.8.2
requests >= 2.31.0
urllib3 >= 2.2.2
PyYAML >= 6.0.1
cryptography>=42.0.4
cryptography >= 42.0.4
cffi
alphasquared-py>=0.3.0
alphasquared-py >= 0.3.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='coinbase-advancedtrade-python',
version='0.3.2',
version='0.3.3',
description='The unofficial Python client for the Coinbase Advanced Trade API',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 819602f

Please sign in to comment.