From 69e44b9fdb09ee7413125098e30f33a41f3694f1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 23 Apr 2022 15:02:29 +0100 Subject: [PATCH] Fix public ad related LBTC calls and tests --- handler/markets.py | 4 ++-- handler/sources/localbitcoins.py | 31 +++++++++++-------------------- handler/tests/common.py | 26 ++++++++++++++++++++++++++ handler/tests/test_lbtc.py | 17 ++++++++++------- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/handler/markets.py b/handler/markets.py index ff4fdc9..4eb21dc 100644 --- a/handler/markets.py +++ b/handler/markets.py @@ -51,6 +51,8 @@ class Markets(util.Base): assets = self.get_all_assets(platform) currencies = self.get_all_currencies(platform) providers = self.get_all_providers(platform) + if platform == "lbtc": + providers = [self.sources.lbtc.map_provider(x, reverse=True) for x in providers] sinks_currencies = self.sinks.currencies supported_currencies = [currency for currency in currencies if currency in sinks_currencies] currencies = supported_currencies @@ -69,10 +71,8 @@ class Markets(util.Base): # Filter asset public_ads_filtered = [ad for ad in public_ads_currency if ad[4] == asset] - # Filter provider public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == provider] - our_ads = [ad for ad in public_ads_filtered if ad[1] == username] if not our_ads: self.log.warning(f"No ads found in public listing for {asset} {currency} {provider}") diff --git a/handler/sources/localbitcoins.py b/handler/sources/localbitcoins.py index 429eb98..1051572 100644 --- a/handler/sources/localbitcoins.py +++ b/handler/sources/localbitcoins.py @@ -250,12 +250,18 @@ class LBTC(util.Base): ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]]) return ads_total - def map_provider(self, provider): + def map_provider(self, provider, reverse=False): provider_map = {"NATIONAL_BANK": "national-bank-transfer"} - try: - return provider_map[provider] - except KeyError: - return False + if reverse: + try: + return next(key for key, value in provider_map.items() if value == provider) + except StopIteration: + return False + else: + try: + return provider_map[provider] + except KeyError: + return False @util.handle_exceptions def enum_public_ads(self, asset, currency, providers=None, page=1): @@ -282,26 +288,19 @@ class LBTC(util.Base): # f.write(json.dumps([page, currency, asset, ads]) + "\n") # f.close() if ads is None: - print("ASDS IS NONE") return False if ads is False: - print("ADS IS FALSE") return False if ads["response"] is None: - print("NO RESPONSE") return False if "data" not in ads["response"]: - print("NO DATAS") return False for ad in ads["response"]["data"]["ad_list"]: if self.map_provider(ad["data"]["online_provider"]) not in providers: - print(ad["data"]["online_provider"], "is not in", providers) - print(ad["data"]["online_provider"] in providers) continue date_last_seen = ad["data"]["profile"]["last_online"] # Check if this person was seen recently if not util.last_online_recent(date_last_seen): - print("NOT SEEN RECENTLTY") continue ad_id = str(ad["data"]["ad_id"]) username = ad["data"]["profile"]["username"] @@ -348,16 +347,13 @@ class LBTC(util.Base): @util.handle_exceptions def update_prices(self, assets=None): - print("UPDATE_PRICES", assets) # Get all public ads for the given assets public_ads = self.get_all_public_ads(assets) if not public_ads: - print("NOT PUBLIC ADS") return False # Get the ads to update to_update = self.markets.get_new_ad_equations(self.platform, public_ads, assets) - print("TO UPdATE", to_update) self.slow_ad_update(to_update) # TODO: make generic and move to markets @@ -368,7 +364,6 @@ class LBTC(util.Base): :return: dict of public ads keyed by currency :rtype: dict """ - print("GET ALL PUBLIC ADS") public_ads = {} crypto_map = { "BTC": "bitcoin", @@ -394,14 +389,11 @@ class LBTC(util.Base): except KeyError: # self.log.error("Error getting public ads for currency {currency}", currency=currency) continue - print("ABOUT TO RUN ENUM PUBLIC ADS WITH ", asset, currency, providers) ads_list = self.enum_public_ads(asset, currency, providers) - print("ADS LIST", ads_list) if not ads_list: continue ads = self.money.lookup_rates(self.platform, ads_list, rates=rates) if not ads: - print("NOT ADS CONTINUE") continue self.write_to_es_ads("ads", ads) if currency in public_ads: @@ -410,7 +402,6 @@ class LBTC(util.Base): public_ads[currency].append(ad) else: public_ads[currency] = ads - print("RETURNING PUBLIC ADS", public_ads) return public_ads def write_to_es_ads(self, msgtype, ads): diff --git a/handler/tests/common.py b/handler/tests/common.py index 0f693eb..e35efab 100644 --- a/handler/tests/common.py +++ b/handler/tests/common.py @@ -1,3 +1,6 @@ +expected_to_update_lbtc = [["1452045", "btc_in_usd*1.1", "BTC", "GBP", False]] + + expected_to_update = [ [ "2caa4afa-a1c7-4683-aa4a-faa1c7a683dc", @@ -224,6 +227,29 @@ cg_prices = { "sek": 1620.26, }, } + +fake_public_ads_lbtc = { + "GBP": [ + ["1452045", "Harrey", "30800.00", "NATIONAL_BANK", "BTC", "GBP", 0.95], + ["1359267", "Angelchira", "32201.41", "NATIONAL_BANK", "BTC", "GBP", 1.0], + ["1434612", "Bittylicious.com-James", "32308.88", "NATIONAL_BANK", "BTC", "GBP", 1.0], + ["1451964", "tutsidelopez", "32308.88", "NATIONAL_BANK", "BTC", "GBP", 1.0], + ["1354377", "DeltaTip", "32328.19", "NATIONAL_BANK", "BTC", "GBP", 1.0], + ["1330431", "starclicks_co", "33603.21", "NATIONAL_BANK", "BTC", "GBP", 1.04], + ["1431277", "bitotal", "34712.75", "NATIONAL_BANK", "BTC", "GBP", 1.08], + ["1382819", "Bitcoinmarketprice", "34871.26", "NATIONAL_BANK", "BTC", "GBP", 1.08], + ["1068826", "JDRbitcoins", "36454.75", "NATIONAL_BANK", "BTC", "GBP", 1.13], + ["1170564", "mommabear007", "36456.32", "NATIONAL_BANK", "BTC", "GBP", 1.13], + ["1331232", "ParadoxHD", "36765.39", "NATIONAL_BANK", "BTC", "GBP", 1.14], + ["1445019", "demoric102", "37090.34", "NATIONAL_BANK", "BTC", "GBP", 1.15], + ["1379791", "eenitan161", "37850.00", "NATIONAL_BANK", "BTC", "GBP", 1.17], + ["1435762", "btcladybd", "41202.59", "NATIONAL_BANK", "BTC", "GBP", 1.28], + ["1445674", "CarloMazzillo", "41202.59", "NATIONAL_BANK", "BTC", "GBP", 1.28], + ["1428769", "miguelnicotra", "44807.39", "NATIONAL_BANK", "BTC", "GBP", 1.39], + ["326809", "Ceres", "50710.88", "NATIONAL_BANK", "BTC", "GBP", 1.57], + ] +} + fake_public_ads = { "AUD": [ [ diff --git a/handler/tests/test_lbtc.py b/handler/tests/test_lbtc.py index 3b413c9..0f092d1 100644 --- a/handler/tests/test_lbtc.py +++ b/handler/tests/test_lbtc.py @@ -3,12 +3,13 @@ from unittest.mock import MagicMock, patch from json import loads from copy import deepcopy -from tests.common import fake_public_ads, cg_prices, expected_to_update +from tests.common import fake_public_ads_lbtc, cg_prices, expected_to_update_lbtc from sources.localbitcoins import LBTC from markets import Markets from money import Money import util import settings +import sources class TestLBTC(TestCase): @@ -25,6 +26,8 @@ class TestLBTC(TestCase): self.markets = Markets() self.lbtc = LBTC() self.money = Money() + self.sources = sources.Sources() + setattr(self.markets, "sources", self.sources) setattr(self.lbtc, "markets", self.markets) setattr(self.money, "markets", self.markets) setattr(self.lbtc, "money", self.money) @@ -63,7 +66,9 @@ class TestLBTC(TestCase): self.lbtc.markets.get_all_providers.return_value = self.all_providers public_ads = self.lbtc.get_all_public_ads() - self.assertDictEqual(public_ads, fake_public_ads) + print("public_ads", public_ads) + # print("fake_public_ads", fake_public_ads) + self.assertDictEqual(public_ads, fake_public_ads_lbtc) for currency, ads in public_ads.items(): ad_ids = [ad[0] for ad in ads] @@ -91,7 +96,7 @@ class TestLBTC(TestCase): def test_update_prices(self): settings.settings.LocalBitcoins.Username = "Harrey" # Override the providers - settings.settings.LocalBitcoins.ProviderList = '["REVOLUT", "NATIONAL_BANK"]' + settings.settings.LocalBitcoins.ProviderList = '["national-bank-transfer"]' # Override enum_public_ads self.lbtc.lbtc._api_call = self.mock_enum_public_ads_api_call @@ -104,10 +109,8 @@ class TestLBTC(TestCase): self.lbtc.slow_ad_update = MagicMock() self.lbtc.update_prices() - print("CALL ARGS", self.lbtc.slow_ad_update.call_args_list) call_args = self.lbtc.slow_ad_update.call_args_list[0][0][0] - print("EXPECTED", expected_to_update) - self.assertCountEqual(call_args, expected_to_update) + self.assertCountEqual(call_args, expected_to_update_lbtc) def test_enum_public_ads(self): # Override enum_public_ads @@ -169,7 +172,7 @@ class TestLBTC(TestCase): expected_return = [] # Let's manually calculate what it's supposed to look like - price_btc = cg_prices["bitcoin"]["usd"] + price_btc = cg_prices["bitcoin"]["gbp"] for ad in deepcopy(enum_ads_return): price = float(ad[2]) margin = round(price / price_btc, 2)