|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +import io |
| 4 | +import pandas as pd |
| 5 | +import subprocess |
| 6 | +from bitcoin_trading_simulation import Colors, simulate_trading, simulate_bitcoin_prices |
| 7 | + |
| 8 | +class TestBitcoinTradingSimulation(unittest.TestCase): |
| 9 | + |
| 10 | + def setUp(self): |
| 11 | + # Reset Colors to default before each test to prevent side effects |
| 12 | + Colors.HEADER = '\033[95m' |
| 13 | + Colors.BLUE = '\033[94m' |
| 14 | + Colors.GREEN = '\033[92m' |
| 15 | + Colors.RED = '\033[91m' |
| 16 | + Colors.ENDC = '\033[0m' |
| 17 | + Colors.BOLD = '\033[1m' |
| 18 | + |
| 19 | + def test_simulate_bitcoin_prices(self): |
| 20 | + prices = simulate_bitcoin_prices(days=10, initial_price=100) |
| 21 | + self.assertEqual(len(prices), 10) |
| 22 | + self.assertEqual(prices.iloc[0], 100) |
| 23 | + |
| 24 | + def test_colors_disable(self): |
| 25 | + Colors.disable() |
| 26 | + self.assertEqual(Colors.HEADER, '') |
| 27 | + self.assertEqual(Colors.GREEN, '') |
| 28 | + self.assertEqual(Colors.RED, '') |
| 29 | + |
| 30 | + def test_quiet_mode_function(self): |
| 31 | + # Create dummy signals |
| 32 | + prices = pd.Series([100, 101, 102], name='Price') |
| 33 | + signals = pd.DataFrame(index=prices.index) |
| 34 | + signals['price'] = prices |
| 35 | + signals['positions'] = 0.0 |
| 36 | + |
| 37 | + # Capture stdout |
| 38 | + captured_output = io.StringIO() |
| 39 | + sys.stdout = captured_output |
| 40 | + |
| 41 | + simulate_trading(signals, quiet=True) |
| 42 | + |
| 43 | + sys.stdout = sys.__stdout__ |
| 44 | + output = captured_output.getvalue() |
| 45 | + |
| 46 | + # Expect no output in quiet mode (since no trades and quiet=True) |
| 47 | + self.assertEqual(output, "") |
| 48 | + |
| 49 | + def test_verbose_mode_function(self): |
| 50 | + # Create dummy signals |
| 51 | + prices = pd.Series([100, 101, 102], name='Price') |
| 52 | + signals = pd.DataFrame(index=prices.index) |
| 53 | + signals['price'] = prices |
| 54 | + signals['positions'] = 0.0 |
| 55 | + |
| 56 | + # Capture stdout |
| 57 | + captured_output = io.StringIO() |
| 58 | + sys.stdout = captured_output |
| 59 | + |
| 60 | + simulate_trading(signals, quiet=False) |
| 61 | + |
| 62 | + sys.stdout = sys.__stdout__ |
| 63 | + output = captured_output.getvalue() |
| 64 | + |
| 65 | + self.assertIn("Daily Trading Ledger", output) |
| 66 | + |
| 67 | + def test_integration_no_color(self): |
| 68 | + # Run the script via subprocess to verify --no-color |
| 69 | + result = subprocess.run( |
| 70 | + [sys.executable, 'bitcoin_trading_simulation.py', '--days', '5', '--no-color'], |
| 71 | + capture_output=True, |
| 72 | + text=True |
| 73 | + ) |
| 74 | + # Check that ANSI codes are NOT present |
| 75 | + self.assertNotIn('\033[', result.stdout) |
| 76 | + # Check that the output is still meaningful |
| 77 | + self.assertIn('Final Portfolio Performance', result.stdout) |
| 78 | + |
| 79 | + def test_integration_quiet(self): |
| 80 | + # Run the script via subprocess to verify --quiet |
| 81 | + result = subprocess.run( |
| 82 | + [sys.executable, 'bitcoin_trading_simulation.py', '--days', '5', '--quiet'], |
| 83 | + capture_output=True, |
| 84 | + text=True |
| 85 | + ) |
| 86 | + # Check that Daily Ledger is NOT present |
| 87 | + self.assertNotIn('Daily Trading Ledger', result.stdout) |
| 88 | + # Check that Final Performance IS present |
| 89 | + self.assertIn('Final Portfolio Performance', result.stdout) |
| 90 | + |
| 91 | + def test_integration_args(self): |
| 92 | + # Verify custom arguments work |
| 93 | + result = subprocess.run( |
| 94 | + [sys.executable, 'bitcoin_trading_simulation.py', '--days', '5', '--initial-cash', '500'], |
| 95 | + capture_output=True, |
| 96 | + text=True |
| 97 | + ) |
| 98 | + self.assertIn('Initial Cash: $500.00', result.stdout) |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + unittest.main() |
0 commit comments