def test_context_within_context(): with TradingContext(config) as tc1: assert get_context() == tc1 with TradingContext(config) as tc2: assert get_context() == tc2 assert get_context() == tc1
def test_context_creation(): with TradingContext(config) as tc1: assert tc1.data == config with TradingContext(config) as tc2: assert TradingContext.get_context() == tc2 assert TradingContext.get_context() == tc1
def test_init(): c = TradingContext({ "base_instrument": config['base_instrument'], "instruments": config['instruments'] }) assert c.shared.get('base_instrument') == 'EURO' assert c.shared.get('instruments') == ['BTC', 'ETH']
def test_only_name_registered_component_space(): c = { 'actions': { 'n_actions': 20 }, 'messages': { 'msg_var': 0, 'valid': ['Hello', 'World'] }, 'plans': { 'future': 'looking good' }, **config } with TradingContext(c) as c: name = 'TensorTrade' value = 'the time and effort.' instance = WorthMessageComponent(name=name, value=value) assert 'msg_var' in instance.context.keys() assert 'valid' in instance.context.keys() assert instance.context['msg_var'] == 0 assert instance.context == { 'msg_var': 0, 'valid': ['Hello', 'World'], **config }
def test_injects_reward_scheme_with_context(self): with TradingContext(self.config): reward_scheme = self.concrete_class() assert hasattr(reward_scheme.context, 'size') assert reward_scheme.context.size == 0 assert reward_scheme.context['size'] == 0
def test_injects_concrete_tensor_trade_component_with_context(): with TradingContext(config): name = 'TensorTrade' value = 'the time and effort.' instance = WorthMessageComponent(name=name, value=value) assert instance.context == config assert instance.message( ) == 'TensorTrade is worth the time and effort.'
def test_injects_string_initialized_reward_scheme(self): with TradingContext(self.config): reward_scheme = rewards.get('simple') assert reward_scheme.registered_name == "rewards" assert hasattr(reward_scheme.context, 'size') assert reward_scheme.context.size == 0 assert reward_scheme.context['size'] == 0
def test_inject_multiple_components_with_context(): with TradingContext(config): name = 'TensorTrade' value = 'the time and effort.' instance = WorthMessageComponent(name=name, value=value) win = WinPlanComponent() lose = LosePlanComponent() assert instance.context == win.context == lose.context assert instance.context == win.context == lose.context
def test_inject_contexts_at_different_levels(): c1 = {'messages': {'msg_var': 0}, 'plans': {'plans_var': 24}, **config} c2 = {'messages': {'level': 1}, **config} with TradingContext(c1): name = 'TensorTrade' value = 'the time and effort.' instance1 = WorthMessageComponent(name=name, value=value) win1 = WinPlanComponent() lose1 = LosePlanComponent() assert hasattr(instance1.context, 'msg_var') assert hasattr(win1.context, 'plans_var') assert hasattr(lose1.context, 'plans_var') with TradingContext(c2): name = 'TensorTrade' value = 'the time and effort.' instance2 = WorthMessageComponent(name=name, value=value) assert hasattr(instance2.context, 'level') assert instance2.context == {'level': 1, **config}
def test_injects_component_space(): c = {'messages': {'msg_var': 0, 'valid': ['Hello', 'World']}, **config} with TradingContext(c) as c: name = 'TensorTrade' value = 'the time and effort.' instance = WorthMessageComponent(name=name, value=value) assert hasattr(instance.context, 'msg_var') assert hasattr(instance.context, 'valid') assert instance.context.msg_var == 0 assert instance.context.valid == ['Hello', 'World']
def test_has_config_attribute(): c = TradingContext({ "test": True, "exchanges": { "test": True }, "actions": { "test": True }, "rewards": { "test": True }, }) assert hasattr(c, 'shared')
def test_create_trading_context_from_json(): path = "tests/data/config/configuration.json" actions = {"n_actions": 24, "action_type": "discrete"} exchanges = { "credentials": { "api_key": "487r63835t4323", "api_secret_key": "do8u43hgiurwfnlveio" }, "name": "bitfinex" } with TradingContext.from_json(path) as tc: assert tc.shared['base_instrument'] == "EURO" assert tc.shared['instruments'] == ["BTC", "ETH"] assert tc._config['actions'] == actions assert tc._config['exchanges'] == exchanges
def test_context_retains_data_outside_with(): with TradingContext(config) as tc: assert tc.data == config assert tc.data == config
def test_get_context_from_tensor_trade_level(): with TradingContext(config) as tc: assert get_context() == tc
def test_init_with_kwargs(): c = TradingContext(config) assert c.shared.get('base_instrument') == 'EURO' assert c.shared.get('instruments') == ['BTC', 'ETH']
def get_context(): return TradingContext.get_context()