def test_locale(self): prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("1.5"), rate_open=Decimal("1.3"), low24h=Decimal("1.2"), high24h=Decimal("1.6"), last_trade_at=datetime.now(), ) fpr = FormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "*USD EUR* 1.5 ⬆️\n+0.2 (+15.38%)\n*Low*: 1.2 *High*: 1.6\n_17 March, 22:14 UTC_\ntest-exchanger 📡", ) fpr = FormatPriceRequestResult(prr, "de") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "*USD EUR* 1,5 ⬆️\n+0,2 (+15,38%)\n*Low*: 1,2 *High*: 1,6\n_17 March, 22:14 UTC_\ntest-exchanger 📡", ) fpr = FormatPriceRequestResult(prr, "zh-hans-sg") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "*USD EUR* 1.5 ⬆️\n+0.2 (+15.38%)\n*Low*: 1.2 *High*: 1.6\n_17 March, 22:14 UTC_\ntest-exchanger 📡", )
def test_price_mode_no_diff(self): prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("1.5"), last_trade_at=datetime.now(), ) fpr = NotifyFormatPriceRequestResult(prr, "en") self.assertFalse(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "*USD EUR* 1.5 🔔\n_17 March, 22:14 UTC_\ntest-exchanger 📡")
def test_price_mode_no_diff(self): prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("1.5"), rate_open=None, low24h=Decimal("1.2"), high24h=Decimal("1.6"), last_trade_at=datetime.now(), ) fpr = InlineFormatPriceRequestResult(prr, "en") self.assertFalse(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual(fpr.get(), "USD EUR 1.5")
def test_convert_r2l(self): prr = PriceRequestResult( price_request=PriceRequest( amount=Decimal("0.5"), currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.RIGHT2LEFT, ), exchanges=["test-exchanger"], rate=Decimal("3"), rate_open=Decimal("1"), low24h=Decimal("1"), high24h=Decimal("1"), last_trade_at=datetime.now(), ) fpr = InlineFormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertTrue(fpr.is_convert_mode()) self.assertEqual(fpr.get(), "1.5 EUR = 0.5 USD")
def test_convert_mode_1996(self): prr = PriceRequestResult( price_request=PriceRequest( amount=Decimal("0.5"), currency="USD", to_currency="USD", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("3"), rate_open=Decimal("1"), low24h=Decimal("1"), high24h=Decimal("1"), last_trade_at=datetime.now(), ) fpr = FormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertTrue(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "0.5 *USD* = 1.5 *USD*\n_17 March 1996_\ntest-exchanger 📡")
def convert(price_request: PriceRequest) -> PriceRequestResult: if price_request.currency == price_request.to_currency: return PriceRequestResult( price_request=price_request, exchanges=["Baba Vanga"], rate=Decimal("1"), last_trade_at=datetime(1996, 8, 11), ) if price_request.amount == 0: return PriceRequestResult( price_request=price_request, exchanges=["Baba Vanga"], rate=Decimal("0"), last_trade_at=datetime(1996, 8, 11), ) from_currency = Session.query(Currency).filter_by( code=price_request.currency).one() to_currency = (Session.query(Currency).filter_by( code=price_request.to_currency).one()) rate_obj = (Session.query(Rate).filter_by( from_currency=from_currency, to_currency=to_currency).join(Exchange).filter( Exchange.is_active == sa.true()).order_by(sa.desc( Exchange.weight)).first()) if rate_obj: price_request_result = PriceRequestResult( price_request=price_request, exchanges=[rate_obj.exchange.name], rate=rate_obj.rate, rate_open=rate_obj.rate_open, last_trade_at=rate_obj.last_trade_at, low24h=rate_obj.low24h, high24h=rate_obj.high24h, ) else: rate0_model = orm.aliased(Rate) rate1_model = orm.aliased(Rate) exchange0_model = orm.aliased(Exchange) exchange1_model = orm.aliased(Exchange) rate_obj = (Session.query( rate0_model, rate1_model, (exchange0_model.weight + exchange1_model.weight).label("w"), ).filter_by(from_currency=from_currency).join( rate1_model, sa.and_( rate1_model.from_currency_id == rate0_model.to_currency_id, rate1_model.to_currency == to_currency, ), ).join( exchange0_model, sa.and_( exchange0_model.id == rate0_model.exchange_id, exchange0_model.is_active == sa.true(), ), ).join( exchange1_model, sa.and_( exchange1_model.id == rate1_model.exchange_id, exchange1_model.is_active == sa.true(), ), ).order_by(sa.desc("w")).first()) if rate_obj: rate = combine_values(rate_obj[0].rate, rate_obj[1].rate) rate_open = combine_values(rate_obj[0].rate_open, rate_obj[1].rate_open) low24h = high24h = None price_request_result = PriceRequestResult( price_request=price_request, exchanges=[ rate_obj[0].exchange.name, rate_obj[1].exchange.name ], rate=rate, rate_open=rate_open, last_trade_at=min(rate_obj[0].last_trade_at, rate_obj[1].last_trade_at), low24h=low24h, high24h=high24h, ) else: raise NoRatesException check_overflow(price_request_result) return price_request_result
def test_rounding(self): prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("0.5"), rate_open=Decimal("1"), low24h=Decimal("0.05"), high24h=Decimal("0.005"), last_trade_at=datetime.now(), ) fpr = FormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), "*USD EUR* 0.5 🔻\n-0.5 (-50.0%)\n*Low*: 0.05 *High*: 0.0050\n_17 March, 22:14 UTC_\ntest-exchanger 📡", ) prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("123456789012.123456789012"), rate_open=Decimal("1"), low24h=Decimal("0.000000000012"), high24h=Decimal("1"), last_trade_at=datetime.now(), ) fpr = FormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), """*USD EUR* 123,456,789,012.1235 ⬆️ +123,456,789,011.1235 (+12,345,678,901,112.35%) *Low*: 0.000000000012 *High*: 1.0 _17 March, 22:14 UTC_ test-exchanger 📡""", ) prr = PriceRequestResult( price_request=PriceRequest( amount=None, currency="USD", to_currency="EUR", parser_name="test-parser", direction_writing=DirectionWriting.UNKNOWN, ), exchanges=["test-exchanger"], rate=Decimal("123456789012.123456789012"), rate_open=Decimal("123456789000.000012345"), low24h=Decimal("1234567890.000"), high24h=Decimal("198765432112.000000000012"), last_trade_at=datetime.now(), ) fpr = FormatPriceRequestResult(prr, "en") self.assertTrue(fpr.is_rate_diff_available()) self.assertFalse(fpr.is_convert_mode()) self.assertEqual( fpr.get(), """*USD EUR* 123,456,789,012.1235 ⬆️ +12.1234 (+0.0000000098%) *Low*: 1,234,567,890.0 *High*: 198,765,432,112.0 _17 March, 22:14 UTC_ test-exchanger 📡""", )