You can write a unit test using a testing framework like pytest to validate the buy functionality of the DecisionMaker class.
import unittest
from unittest.mock import MagicMock
from main import DecisionMaker, DecisionContext, Asset, Enviorment, RESTClient, MongoClient
from decisions import DecisionType
class TestDecisionMaker(unittest.TestCase):
def test_buy_asset(self):
# Mocking necessary dependencies
cb_client = MagicMock(spec=RESTClient)
mongo_client = MagicMock(spec=MongoClient)
# Creating test data
asset_config = Asset(name="TestAsset", symbol="TEST", account_id="123", amount_to_buy=100.0, buy_price_percentage_change_threshold=0.5, sell_price_percentage_change_threshold=1.0, max_open_buys=2)
context = DecisionContext(enviorment=Enviorment.TEST, price=100.0, symbol="TEST", asset_balance=10.0, total_asset_value=1000.0, usdc_balance=1000.0, volume_24h=1000.0, volume_percentage_change_24h=0.5, price_percentage_change_24h=1.0, total_asset_holdings_value=1000.0, price_change_check=True, buy_buffer_check=True, open_buy_check=True, open_buy_count=0, open_buy_decisions=[])
# Creating an instance of DecisionMaker
decision_maker = DecisionMaker(Enviorment.TEST, cb_client, mongo_client, "test_db", "test_collection", asset_config, "usd_account_id")
# Mocking the necessary methods for buying
decision_maker.get_decision_context = MagicMock(return_value=context)
decision_maker.get_buying_power = MagicMock(return_value=1000.0)
decision_maker.place_buy_order = MagicMock(return_value={"mock": "buy_order_result"})
# Running the buy decision
decision_maker.compute_decisions()
# Asserting that the buy order was placed
decision_maker.place_buy_order.assert_called()
if __name__ == '__main__':
unittest.main()
You can write a unit test using a testing framework like pytest to validate the buy functionality of the DecisionMaker class.