class TestMarket(unittest.TestCase): def setUp(self): self.market = MockMarket() def test_getdepth_invaliddata(self): self.market.set_mock_depth(depth_bad) self.market.get_depth() assert self.market.depth['asks'][0]['price'] == 0 def test_getdepth_validdata(self): self.market.set_mock_depth(depth_ok) self.market.get_depth() assert self.market.depth['asks'][0]['price'] == 32.8 assert self.market.depth['bids'][0]['price'] == 31.8 def test_validation_functions(self): assert self.market.uses("USD") assert self.market.uses("BTC") assert not self.market.uses("LTC") def test_invalid_execution(self): try: self.market.execute_trade(2, "BTC") assert False except Exception: assert True def test_divide_by_zero_bug(self): self.market.set_mock_depth(depth_div_by_zero) self.market.get_depth() assert self.market.value_of("USD") == 0 assert self.market.r_value_of("BTC") == 0 def test_volume_functions(self): self.market.set_mock_depth(depth_profit) # Lock the order book. self.market.begin_transaction() # Should match the figures from the first order in the list. assert self.market.volume_to_next_price_as("BTC") == 2 assert self.market.volume_to_next_price_as("USD") == 131.2 # Make sure it returns the amount of USD that 0.1 BTC would obtain. assert self.market.value_of("BTC", volume = 0.1) == 3.18 # Make sure it returns the amount of BTC that 65.60 USD would obtain. assert self.market.value_of("USD", volume = 65.60) == 2 # Make sure it returns the amount of BTC obtained from 65.60 USD. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should have half the original figure left in USD... assert self.market.volume_to_next_price_as("USD") == 65.6 # ...and all the original figure left in BTC. assert self.market.volume_to_next_price_as("BTC") == 2 # Make sure it returns the amount of USD obtained from 2 BTC. assert self.market.execute_trade(2, "BTC").to_volume == 63.6 # Clear out the top ask order. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should match the figures from the second order in the list now. assert self.market.volume_to_next_price_as("BTC") == 4 assert self.market.volume_to_next_price_as("USD") == 263.2 self.market.end_transaction()
class TestMarket(unittest.TestCase): def setUp(self): self.market = MockMarket() # Invalidated following @weidenrinde's (temporary?) removal of the # market depth sanity check on the grounds that some markets have # valid states where bid price is greater than ask price. # Leaving this in here until the originator of the sanity check, # @mdespriee, can weigh in on the check's removal. # def test_getdepth_invaliddata(self): # self.market.set_mock_depth(depth_bad) # self.market.get_depth() # assert self.market.depth['asks'][0]['price'] == 0 def test_getdepth_validdata(self): self.market.set_mock_depth(depth_ok) self.market.get_depth() assert self.market.depth["asks"][0]["price"] == 32.8 assert self.market.depth["bids"][0]["price"] == 31.8 def test_validation_functions(self): assert self.market.uses("USD") assert self.market.uses("BTC") assert not self.market.uses("LTC") def test_invalid_execution(self): try: self.market.execute_trade(2, "BTC") assert False except Exception: assert True def test_divide_by_zero_bug(self): self.market.set_mock_depth(depth_div_by_zero) self.market.get_depth() assert self.market.value_of("USD") == 0 assert self.market.r_value_of("BTC") == 0 def test_volume_functions(self): self.market.set_mock_depth(depth_profit) # Lock the order book. self.market.begin_transaction() # Should match the figures from the first order in the list. assert self.market.volume_to_next_price_as("BTC") == 2 assert self.market.volume_to_next_price_as("USD") == 131.2 # Make sure it returns the amount of USD that 0.1 BTC would obtain. assert self.market.value_of("BTC", volume=0.1) == 3.18 # Make sure it returns the amount of BTC that 65.60 USD would obtain. assert self.market.value_of("USD", volume=65.60) == 2 # Make sure it returns the amount of BTC obtained from 65.60 USD. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should have half the original figure left in USD... assert self.market.volume_to_next_price_as("USD") == 65.6 # ...and all the original figure left in BTC. assert self.market.volume_to_next_price_as("BTC") == 2 # Make sure it returns the amount of USD obtained from 2 BTC. assert self.market.execute_trade(2, "BTC").to_volume == 63.6 # Clear out the top ask order. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should match the figures from the second order in the list now. assert self.market.volume_to_next_price_as("BTC") == 4 assert self.market.volume_to_next_price_as("USD") == 263.2 self.market.end_transaction() # Make sure the original market depth was restored # after the transaction ended. assert self.market.depth == depth_profit
class TestMarket(unittest.TestCase): def setUp(self): self.market = MockMarket() def test_getdepth_invaliddata(self): self.market.set_mock_depth(depth_bad) self.market.get_depth() assert self.market.depth['asks'][0]['price'] == 0 def test_getdepth_validdata(self): self.market.set_mock_depth(depth_ok) self.market.get_depth() assert self.market.depth['asks'][0]['price'] == 32.8 assert self.market.depth['bids'][0]['price'] == 31.8 def test_validation_functions(self): assert self.market.uses("USD") assert self.market.uses("BTC") assert not self.market.uses("LTC") def test_invalid_execution(self): try: self.market.execute_trade(2, "BTC") assert False except Exception: assert True def test_divide_by_zero_bug(self): self.market.set_mock_depth(depth_div_by_zero) self.market.get_depth() assert self.market.value_of("USD") == 0 assert self.market.r_value_of("BTC") == 0 def test_volume_functions(self): self.market.set_mock_depth(depth_profit) # Lock the order book. self.market.begin_transaction() # Should match the figures from the first order in the list. assert self.market.volume_to_next_price_as("BTC") == 2 assert self.market.volume_to_next_price_as("USD") == 131.2 # Make sure it returns the amount of USD that 0.1 BTC would obtain. assert self.market.value_of("BTC", volume=0.1) == 3.18 # Make sure it returns the amount of BTC that 65.60 USD would obtain. assert self.market.value_of("USD", volume=65.60) == 2 # Make sure it returns the amount of BTC obtained from 65.60 USD. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should have half the original figure left in USD... assert self.market.volume_to_next_price_as("USD") == 65.6 # ...and all the original figure left in BTC. assert self.market.volume_to_next_price_as("BTC") == 2 # Make sure it returns the amount of USD obtained from 2 BTC. assert self.market.execute_trade(2, "BTC").to_volume == 63.6 # Clear out the top ask order. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should match the figures from the second order in the list now. assert self.market.volume_to_next_price_as("BTC") == 4 assert self.market.volume_to_next_price_as("USD") == 263.2 self.market.end_transaction()
class TestMarket(unittest.TestCase): def setUp(self): self.market = MockMarket() # Invalidated following @weidenrinde's (temporary?) removal of the # market depth sanity check on the grounds that some markets have # valid states where bid price is greater than ask price. # Leaving this in here until the originator of the sanity check, # @mdespriee, can weigh in on the check's removal. #def test_getdepth_invaliddata(self): # self.market.set_mock_depth(depth_bad) # self.market.get_depth() # assert self.market.depth['asks'][0]['price'] == 0 def test_getdepth_validdata(self): self.market.set_mock_depth(depth_ok) self.market.get_depth() assert self.market.depth['asks'][0]['price'] == 32.8 assert self.market.depth['bids'][0]['price'] == 31.8 def test_validation_functions(self): assert self.market.uses("USD") assert self.market.uses("BTC") assert not self.market.uses("LTC") def test_invalid_execution(self): try: self.market.execute_trade(2, "BTC") assert False except Exception: assert True def test_divide_by_zero_bug(self): self.market.set_mock_depth(depth_div_by_zero) self.market.get_depth() assert self.market.value_of("USD") == 0 assert self.market.r_value_of("BTC") == 0 def test_volume_functions(self): self.market.set_mock_depth(depth_profit) # Lock the order book. self.market.begin_transaction() # Should match the figures from the first order in the list. assert self.market.volume_to_next_price_as("BTC") == 2 assert self.market.volume_to_next_price_as("USD") == 131.2 # Make sure it returns the amount of USD that 0.1 BTC would obtain. assert self.market.value_of("BTC", volume=0.1) == 3.18 # Make sure it returns the amount of BTC that 65.60 USD would obtain. assert self.market.value_of("USD", volume=65.60) == 2 # Make sure it returns the amount of BTC obtained from 65.60 USD. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should have half the original figure left in USD... assert self.market.volume_to_next_price_as("USD") == 65.6 # ...and all the original figure left in BTC. assert self.market.volume_to_next_price_as("BTC") == 2 # Make sure it returns the amount of USD obtained from 2 BTC. assert self.market.execute_trade(2, "BTC").to_volume == 63.6 # Clear out the top ask order. assert self.market.execute_trade(65.60, "USD").to_volume == 2 # Should match the figures from the second order in the list now. assert self.market.volume_to_next_price_as("BTC") == 4 assert self.market.volume_to_next_price_as("USD") == 263.2 self.market.end_transaction() # Make sure the original market depth was restored # after the transaction ended. assert self.market.depth == depth_profit