|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from py_alpaca_api import PyAlpacaAPI |
| 6 | +from py_alpaca_api.models.account_config_model import AccountConfigModel |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.skipif( |
| 10 | + not os.environ.get("ALPACA_API_KEY") or not os.environ.get("ALPACA_SECRET_KEY"), |
| 11 | + reason="API credentials not set", |
| 12 | +) |
| 13 | +class TestAccountConfigIntegration: |
| 14 | + @pytest.fixture |
| 15 | + def alpaca(self): |
| 16 | + return PyAlpacaAPI( |
| 17 | + api_key=os.environ.get("ALPACA_API_KEY"), |
| 18 | + api_secret=os.environ.get("ALPACA_SECRET_KEY"), |
| 19 | + api_paper=True, |
| 20 | + ) |
| 21 | + |
| 22 | + @pytest.fixture |
| 23 | + def original_config(self, alpaca): |
| 24 | + """Get the original configuration to restore after tests.""" |
| 25 | + return alpaca.trading.account.get_configuration() |
| 26 | + |
| 27 | + def test_get_configuration(self, alpaca): |
| 28 | + config = alpaca.trading.account.get_configuration() |
| 29 | + |
| 30 | + assert isinstance(config, AccountConfigModel) |
| 31 | + # These fields should always be present |
| 32 | + assert config.dtbp_check in ["entry", "exit", "both"] |
| 33 | + assert isinstance(config.fractional_trading, bool) |
| 34 | + assert config.max_margin_multiplier in ["1", "2", "4"] |
| 35 | + assert isinstance(config.no_shorting, bool) |
| 36 | + assert config.pdt_check in ["entry", "exit", "both"] |
| 37 | + assert isinstance(config.ptp_no_exception_entry, bool) |
| 38 | + assert isinstance(config.suspend_trade, bool) |
| 39 | + assert config.trade_confirm_email in ["all", "none"] |
| 40 | + |
| 41 | + def test_update_single_configuration_param(self, alpaca, original_config): |
| 42 | + # Toggle trade confirmation email |
| 43 | + new_setting = "none" if original_config.trade_confirm_email == "all" else "all" |
| 44 | + |
| 45 | + updated_config = alpaca.trading.account.update_configuration( |
| 46 | + trade_confirm_email=new_setting |
| 47 | + ) |
| 48 | + |
| 49 | + assert isinstance(updated_config, AccountConfigModel) |
| 50 | + assert updated_config.trade_confirm_email == new_setting |
| 51 | + |
| 52 | + # Verify other settings remain unchanged |
| 53 | + assert updated_config.dtbp_check == original_config.dtbp_check |
| 54 | + assert updated_config.fractional_trading == original_config.fractional_trading |
| 55 | + assert updated_config.no_shorting == original_config.no_shorting |
| 56 | + assert updated_config.pdt_check == original_config.pdt_check |
| 57 | + assert updated_config.suspend_trade == original_config.suspend_trade |
| 58 | + |
| 59 | + # Restore original setting |
| 60 | + alpaca.trading.account.update_configuration( |
| 61 | + trade_confirm_email=original_config.trade_confirm_email |
| 62 | + ) |
| 63 | + |
| 64 | + def test_update_multiple_configuration_params(self, alpaca, original_config): |
| 65 | + # Toggle no_shorting and change pdt_check |
| 66 | + new_no_shorting = not original_config.no_shorting |
| 67 | + new_pdt_check = "exit" if original_config.pdt_check == "entry" else "entry" |
| 68 | + |
| 69 | + updated_config = alpaca.trading.account.update_configuration( |
| 70 | + no_shorting=new_no_shorting, pdt_check=new_pdt_check |
| 71 | + ) |
| 72 | + |
| 73 | + assert isinstance(updated_config, AccountConfigModel) |
| 74 | + assert updated_config.no_shorting == new_no_shorting |
| 75 | + assert updated_config.pdt_check == new_pdt_check |
| 76 | + |
| 77 | + # Verify other settings remain unchanged |
| 78 | + assert updated_config.dtbp_check == original_config.dtbp_check |
| 79 | + assert updated_config.fractional_trading == original_config.fractional_trading |
| 80 | + assert ( |
| 81 | + updated_config.max_margin_multiplier |
| 82 | + == original_config.max_margin_multiplier |
| 83 | + ) |
| 84 | + assert updated_config.suspend_trade == original_config.suspend_trade |
| 85 | + assert updated_config.trade_confirm_email == original_config.trade_confirm_email |
| 86 | + |
| 87 | + # Restore original settings |
| 88 | + alpaca.trading.account.update_configuration( |
| 89 | + no_shorting=original_config.no_shorting, |
| 90 | + pdt_check=original_config.pdt_check, |
| 91 | + ) |
| 92 | + |
| 93 | + def test_update_margin_multiplier(self, alpaca, original_config): |
| 94 | + # Test changing margin multiplier |
| 95 | + current_multiplier = original_config.max_margin_multiplier |
| 96 | + new_multiplier = "2" if current_multiplier != "2" else "4" |
| 97 | + |
| 98 | + updated_config = alpaca.trading.account.update_configuration( |
| 99 | + max_margin_multiplier=new_multiplier |
| 100 | + ) |
| 101 | + |
| 102 | + assert updated_config.max_margin_multiplier == new_multiplier |
| 103 | + |
| 104 | + # Restore original |
| 105 | + alpaca.trading.account.update_configuration( |
| 106 | + max_margin_multiplier=original_config.max_margin_multiplier |
| 107 | + ) |
| 108 | + |
| 109 | + def test_update_dtbp_check(self, alpaca, original_config): |
| 110 | + # Cycle through dtbp_check options |
| 111 | + options = ["entry", "exit", "both"] |
| 112 | + current = original_config.dtbp_check |
| 113 | + new_value = options[(options.index(current) + 1) % 3] |
| 114 | + |
| 115 | + updated_config = alpaca.trading.account.update_configuration( |
| 116 | + dtbp_check=new_value |
| 117 | + ) |
| 118 | + |
| 119 | + assert updated_config.dtbp_check == new_value |
| 120 | + |
| 121 | + # Restore original |
| 122 | + alpaca.trading.account.update_configuration( |
| 123 | + dtbp_check=original_config.dtbp_check |
| 124 | + ) |
| 125 | + |
| 126 | + def test_toggle_fractional_trading(self, alpaca, original_config): |
| 127 | + # Toggle fractional trading |
| 128 | + new_value = not original_config.fractional_trading |
| 129 | + |
| 130 | + updated_config = alpaca.trading.account.update_configuration( |
| 131 | + fractional_trading=new_value |
| 132 | + ) |
| 133 | + |
| 134 | + assert updated_config.fractional_trading == new_value |
| 135 | + |
| 136 | + # Restore original |
| 137 | + alpaca.trading.account.update_configuration( |
| 138 | + fractional_trading=original_config.fractional_trading |
| 139 | + ) |
| 140 | + |
| 141 | + def test_configuration_persistence(self, alpaca, original_config): |
| 142 | + # Update a configuration |
| 143 | + new_email_setting = ( |
| 144 | + "none" if original_config.trade_confirm_email == "all" else "all" |
| 145 | + ) |
| 146 | + alpaca.trading.account.update_configuration( |
| 147 | + trade_confirm_email=new_email_setting |
| 148 | + ) |
| 149 | + |
| 150 | + # Get configuration again to verify persistence |
| 151 | + config = alpaca.trading.account.get_configuration() |
| 152 | + assert config.trade_confirm_email == new_email_setting |
| 153 | + |
| 154 | + # Restore original |
| 155 | + alpaca.trading.account.update_configuration( |
| 156 | + trade_confirm_email=original_config.trade_confirm_email |
| 157 | + ) |
| 158 | + |
| 159 | + def test_invalid_parameter_handling(self, alpaca): |
| 160 | + # Test that invalid parameters raise appropriate errors |
| 161 | + with pytest.raises(ValueError): |
| 162 | + alpaca.trading.account.update_configuration(dtbp_check="invalid") |
| 163 | + |
| 164 | + with pytest.raises(ValueError): |
| 165 | + alpaca.trading.account.update_configuration(pdt_check="invalid") |
| 166 | + |
| 167 | + with pytest.raises(ValueError): |
| 168 | + alpaca.trading.account.update_configuration(max_margin_multiplier="5") |
| 169 | + |
| 170 | + with pytest.raises(ValueError): |
| 171 | + alpaca.trading.account.update_configuration(trade_confirm_email="sometimes") |
| 172 | + |
| 173 | + @pytest.mark.skip(reason="Suspend trade affects account functionality") |
| 174 | + def test_suspend_trade_toggle(self, alpaca, original_config): |
| 175 | + # This test is skipped as it would actually suspend trading |
| 176 | + # Only run manually when testing this specific feature |
| 177 | + updated_config = alpaca.trading.account.update_configuration(suspend_trade=True) |
| 178 | + assert updated_config.suspend_trade is True |
| 179 | + |
| 180 | + # Immediately restore |
| 181 | + alpaca.trading.account.update_configuration( |
| 182 | + suspend_trade=original_config.suspend_trade |
| 183 | + ) |
0 commit comments