def search(self, request): serializer = AssetSearchSerializer( data=request.query_params, context=self.get_serializer_context() ) serializer.is_valid(raise_exception=True) identifier = serializer.validated_data["identifier"] # Look for new querysets. stock_exchanges.search_and_create_assets(identifier) # Alternative form because e.g. brk.b is stored as brk-b in eod. alternative_identifier = identifier.replace(".", "-") stock_exchanges.search_and_create_assets(alternative_identifier) queryset = self.get_queryset().filter( Q(isin__iexact=identifier) | Q(name__icontains=identifier) | Q(symbol__iexact=identifier) | Q(symbol__iexact=alternative_identifier) ) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data)
def test_searching_unsupported_exchange_and_currency(self, mock): mock.return_value = UNSUPPORTED_EXCHANGE_AND_CURRENCY_RESULTS assets = stock_exchanges.search_and_create_assets("IL0011016669") self.assertEqual(len(assets), 0) for asset in assets: self.assertEqual(asset.isin, "IL0011016669") self.assertEqual(asset.asset_type, models.AssetType.FUND)
def test_searching_unsupported_exchange(self, mock): mock.return_value = UNSUPPORTED_EXCHANGE_RESULTS assets = stock_exchanges.search_and_create_assets("HK0002007356") self.assertEqual(len(assets), 0) for asset in assets: self.assertEqual(asset.isin, "HK0002007356") self.assertEqual(asset.asset_type, models.AssetType.FUND)
def test_searching_fund_assets_by_isin(self, mock): mock.return_value = VWCE_SEARCH_BY_ISIN_RESULTS assets = stock_exchanges.search_and_create_assets("IE00BK5BQT80") self.assertEqual(len(assets), 5) for asset in assets: self.assertEqual(asset.isin, "IE00BK5BQT80") self.assertEqual(asset.asset_type, models.AssetType.FUND)
def test_searching_crypto_assets(self, mock): mock.return_value = BTC_ASSET_SEARCH_RESULT assets = stock_exchanges.search_and_create_assets("btc") self.assertTrue( models.Asset.objects.filter( symbol="BTC", asset_type=models.AssetType.CRYPTO, currency=models.Currency.USD, ).exists()) self.assertEqual(len(assets), 2)
def test_searching_fund_assets(self, mock): mock.return_value = VTSAX_SEARCH_RESULTS assets = stock_exchanges.search_and_create_assets("Vtsax") self.assertEqual(len(assets), 1)
def test_searching_stock_assets(self, mock): mock.return_value = KO_ASSET_SEARCH_RESULT assets = stock_exchanges.search_and_create_assets("ko") self.assertEqual(len(assets), 1)