def test_equality(self): c1, c1_, c2, c3 = Currency("c1"), Currency("c1"), Currency( "c2"), Currency("c3") p1, p1_, p2, p3 = CurrencyPair(c1, c2), CurrencyPair( c1_, c2), CurrencyPair(c1, c3), CurrencyPair(c3, c2) self.assertEqual(p1, p1_) self.assertNotEqual(p1, p2) self.assertNotEqual(p1, p3)
def test_hash(self): c1, c1_, c2 = Currency("c1"), Currency("c1"), Currency("c2") p1, p1_, p2 = CurrencyPair(c1, c2), CurrencyPair(c1_, c2), CurrencyPair(c2, c1) self.assertEqual(set((p1, p1_)), set((p1, ))) self.assertEqual(len({p1: 1, p1_: 1}), 1) self.assertEqual(len({p1: 1, p2: 1}), 2)
def find(self, currency1=None, currency2=None, exchange=None): '''Returns a sublist of contained markets, filtered by the given criteria''' exchange_name = exchange.name if isinstance(exchange, Exchange) else exchange results = set(self._all) # make copy if currency1: currency1 = Currency(currency1) results &= self._by_currency[currency1] if currency2: currency2 = Currency(currency2) results &= self._by_currency[currency2] if exchange_name: exchange_name_lower = exchange_name.lower() if exchange_name else None results &= self._by_exchange[exchange_name_lower] return MarketList(results)
def test_find_double(self): c3, c4 = (Currency(c) for c in ('c3', 'c4')) market_currencies = (c1, c2, c3, c4) # each market has a successive pair of currencies m1, m2, m3 = ( SimpleMarket(cx1, cx2) for cx1, cx2 in zip(market_currencies[:-1], market_currencies[1:])) # ---- empty ml = MarketList([]) self.assertItemsEqual(ml.find(c1).find(c1), []) self.assertItemsEqual(ml.find(c1).find(c2), []) # ---- one element ml = MarketList([m1]) self.assertItemsEqual(ml.find(c1).find(c1), [m1]) self.assertItemsEqual(ml.find(c1).find(c2), [m1]) self.assertItemsEqual(ml.find(c2).find(c1), [m1]) self.assertItemsEqual(ml.find(c1, c2).find(c2, c1), [m1]) self.assertItemsEqual(ml.find(c1).find(c3), []) self.assertItemsEqual(ml.find(c3).find(c1), []) # ---- many elements ml = MarketList([m1, m2, m3]) self.assertItemsEqual(ml.find(c1).find(c1), [m1]) self.assertItemsEqual(ml.find(c1).find(c2), [m1]) self.assertItemsEqual(ml.find(c2).find(c2), [m1, m2]) self.assertItemsEqual(ml.find(c2).find(c1), [m1]) self.assertItemsEqual(ml.find(c2).find(c2), [m1, m2]) self.assertItemsEqual(ml.find(c2).find(c3), [m2]) self.assertItemsEqual(ml.find(c3).find(c3), [m2, m3]) self.assertItemsEqual(ml.find(c3).find(c2), [m2])
def test_convert(self): c1, c2, er = ExchangeRateTest.create_er() self.assertEqual(er.convert(Amount(1, c1)), 2 * c2) self.assertEqual(er.convert(Amount(1, c2)), 0.5 * c1) self.assertEqual(er.convert(Amount(1, c1), c1), 1 * c1) self.assertEqual(er.convert(Amount(1, c1), c2), 2 * c2) self.assertRaises(CurrencyPair.WrongCurrency, lambda: er.convert(Amount(1, c1), Currency('c')))
def test_find_by_exchange(self): e1, e2 = SimpleExchange("E1"), SimpleExchange("e2") c3 = Currency('c3') m1, m2 = SimpleMarket(c1, c2, e1), SimpleMarket(c1, c2, e2) ml = MarketList([m1, m2]) #simple find self.assertItemsEqual(ml.find(exchange="E1"), (m1, )) #simple 2 self.assertItemsEqual(ml.find(exchange="E2"), (m2, )) #lower case self.assertItemsEqual(ml.find(exchange="e1"), (m1, )) #with currency, 1 result self.assertItemsEqual(ml.find(c1, exchange="E1"), (m1, )) #with currency, 0 results self.assertItemsEqual(ml.find(c3, exchange="E1"), ())
exchange = exchange or test_exchange Market.__init__(self, exchange, counter_currency, base_currency) def authenticate(self, *args, **kwargs): raise NotImplementedError def get_ticker(self): raise NotImplementedError def get_orderbook(self): raise NotImplementedError #GLOBALS test_exchange = SimpleExchange() c1, c2 = Currency("c1"), Currency("c2") m = SimpleMarket(c1, c2) def craft_orderbook(bids_list, asks_list): '''create orderbook from two lists of tuples (amount, rate)''' bids_list = [Order(l[0] * c1, l[1] * c1 / c2, market=m) for l in bids_list] asks_list = [Order(l[0] * c2, l[1] * c1 / c2, market=m) for l in asks_list] return Orderbook(m, bids_list, asks_list) def craft_order(amount, rate, bid=True, market=m): from_c = market.counter_currency if bid else market.base_currency return Order(amount * from_c, rate * c1 / c2, market=market)
def test_create(self): c1 = Currency("c1") self.assertIsInstance(c1, Currency)
def create_amount(): c1, c2 = Currency("C1"), Currency("C2") amount = Amount('1.0', c1) return c1, c2, amount
def test_equality_reversed(self): c1, c2 = Currency("c1"), Currency("c2") self.assertNotEqual(CurrencyPair(c1, c2), CurrencyPair(c2, c1))
def create_pair(): c1, c2, c3 = Currency("c1"), Currency("c2"), Currency("c3") p1, p1_, p2, p3 = CurrencyPair(c1, c2), CurrencyPair(c1, c3)
def test_hash(self): c1, c1_, c2 = Currency("c1"), Currency("c1"), Currency("c2") self.assertEqual(set((c1, c1_)), set((c1, ))) self.assertEqual(len({c1: 1, c1_: 1}), 1) self.assertEqual(len({c1: 1, c2: 1}), 2) self.assertEqual(len({c1: 1, "c1": 1}), 2)
def test_equality(self): c1, c1_, c2 = Currency("c1"), Currency("c1"), Currency("c2") self.assertEqual(c1, c1_) self.assertNotEqual(c1, c2)
def create_er(): c1, c2 = Currency("C1"), Currency("C2") er = ExchangeRate(denominator_currency=c1, numerator_currency=c2, rate='2.0') return c1, c2, er