tensortrade-org / tensortrade

An open source reinforcement learning framework for training, evaluating, and deploying robust trading agents.
https://discord.gg/ZZ7BGWh
Apache License 2.0
4.54k stars 1.02k forks source link

Wrote some testing for the exceptions.py class, hope this is helpful for future testings. #149

Open diazagasatya opened 4 years ago

diazagasatya commented 4 years ago
import unittest

class TestExceptions(unittest.TestCase):

    # =============================================================================
    # Quantity Exceptions
    # =============================================================================
    def test_invalid_negative_quantity(self):
        with self.assertRaises(InvalidNegativeQuantity):
            Quantity(instrument=BTC, size=10) * -1

    def test_invalid_non_numeric_quantity(self):
        with self.assertRaises(InvalidNonNumericQuantity):
            Quantity(instrument=BTC, size=10) + 'a'

    def test_quantity_operation_path_mismatch(self):
        with self.assertRaises(QuantityOpPathMismatch):
            wallet_one = Quantity(instrument=BTC, size=10, path_id='wallet1')
            wallet_two = Quantity(instrument=BTC, size=100, path_id='wallet2')
            Quantity.validate(wallet_one, wallet_two)

    # =============================================================================
    # Instrument Exceptions
    # =============================================================================
    def test_incompatible_instrument_operation(self):
        with self.assertRaises(IncompatibleInstrumentOperation):
            (1000 * BTC) + (1000 * ETH)

    def test_incompatible_trading_pair_operation(self):
        with self.assertRaises(IncompatibleTradingPairOperation):
            'eth/btc' * (BTC/ETH)

    # =============================================================================
    # Order Exceptions
    # =============================================================================
    def test_invalid_order_quantity(self):
        with self.assertRaises(InvalidOrderQuantity):
            Order(step=global_clock.step, 
                  side=TradeSide.SELL,
                  trade_type=TradeType.LIMIT,
                  pair=ETH/BTC,
                  quantity=0 * BTC,
                  portfolio=Portfolio(BTC),
                  price=3900)

    def test_incompatible_recipe_path(self):
        # no implementation
        with self.assertRaises(IncompatibleRecipePath):
            raise IncompatibleRecipePath(order='order', recipe='recipe')

    # =============================================================================
    # Wallet Exceptions
    # =============================================================================
    def test_insufficient_funds_for_allocation(self):
        with self.assertRaises(InsufficientFundsForAllocation):
            base_wallet = Wallet(exchange='Exchange', quantity=(100 * BTC))
            base_wallet -= (1000 * BTC)

    # =============================================================================
    # Trading Pair Exceptions
    # =============================================================================
    def test_invalid_trading_pair(self):
        with self.assertRaises(InvalidTradingPair):
            BTC/BTC

    # =============================================================================
    # Price Exceptions
    # =============================================================================
    def test_incompatible_price_quantity_operation(self):
        with self.assertRaises(IncompatiblePriceQuantityOperation):
            price = 1000 * (BTC/ETH)
            price * Quantity(instrument=BTC, size=10)
rhamnett commented 3 years ago

Thanks, will check these out and potentially add them to the existing tests.

rhamnett commented 3 years ago

@diazagasatya can you please attach the exceptions you added into tensortrade/core/exceptions.py