def test_get_many_assets(mocker, monkeypatch): marquee_id_1 = 'MQA1234567890' marquee_id_2 = 'MQA4567890123' query = {'id': [marquee_id_1, marquee_id_2]} as_of = dt.datetime.utcnow() inputs = EntityQuery(where=FieldFilterMap(**query), fields=None, asOfTime=as_of, limit=100) mock_response = { 'results': (GsAsset.from_dict({ 'id': marquee_id_1, 'assetClass': 'Equity', 'type': 'Single Stock', 'name': 'Test 1' }), GsAsset.from_dict({ 'id': marquee_id_2, 'assetClass': 'Equity', 'type': 'Single Stock', 'name': 'Test 2' })) } expected_response = (GsAsset(id=marquee_id_1, assetClass='Equity', type='Single Stock', name='Test 1'), GsAsset(id=marquee_id_2, assetClass='Equity', type='Single Stock', name='Test 2')) # mock GsSession mocker.patch.object(GsSession.__class__, 'default_value', return_value=GsSession.get(Environment.QA, 'client_id', 'secret')) mocker.patch.object(GsSession.current, '_post', return_value=mock_response) # run test monkeypatch.delenv(ENABLE_ASSET_CACHING, raising=False) response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2], as_of=as_of) GsSession.current._post.assert_called_with('/assets/query', cls=GsAsset, payload=inputs) assert response == expected_response monkeypatch.setenv(ENABLE_ASSET_CACHING, 1) # run 2x with cache on response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2], as_of=as_of) assert response == expected_response response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2], as_of=as_of) assert response == expected_response
def __get_gs_asset(self, identifier: str) -> GsAsset: response = GsAssetApi.resolve_assets(identifier=[identifier], limit=1)[identifier] gs_asset = None if len(response) == 0 else GsAsset.from_dict( response[0]) if gs_asset is None: raise MqValueError( f'Basket could not be found using identifier {identifier}') return gs_asset
def get_asset(cls, id_value: str, id_type: AssetIdentifier, as_of: Union[dt.date, dt.datetime] = None, exchange_code: ExchangeCode = None, asset_type: AssetType = None, sort_by_rank: bool = False) -> Asset: """ Get an asset by identifier and identifier type :param id_value: identifier value :param id_type: identifier type :param exchange_code: exchange code :param asset_type: asset type :param as_of: As of date for query :param sort_by_rank: whether to sort assets by rank. :return: Asset object or None **Usage** Get asset object using a specified identifier and identifier type. Where the identifiers are temporal (and can change over time), will use the current MarketContext to evaluate based on the specified date. **Examples** Get asset by bloomberg id: >>> gs = SecurityMaster.get_asset("GS UN", AssetIdentifier.BLOOMBERG_ID) Get asset by ticker and exchange code: >>> gs = SecurityMaster.get_asset("GS", AssetIdentifier.TICKER, exchange_code=ExchangeCode.NYSE) Get asset by ticker and asset type: >>> spx = SecurityMaster.get_asset("SPX", AssetIdentifier.TICKER, asset_type=AssetType.INDEX) **See also** :class:`AssetIdentifier` :func:`get_many_assets` """ if not as_of: as_of = PricingContext.current.pricing_date if isinstance(as_of, dt.date): as_of = dt.datetime.combine(as_of, dt.time(0, 0), pytz.utc) if id_type is AssetIdentifier.MARQUEE_ID: gs_asset = GsAssetApi.get_asset(id_value) return cls.__gs_asset_to_asset(gs_asset) query = {id_type.value.lower(): id_value} if exchange_code is not None: query['exchange'] = exchange_code.value if asset_type is not None: query['type'] = [ t.value for t in cls.__asset_type_to_gs_types(asset_type) ] if sort_by_rank: results = GsAssetApi.get_many_assets(as_of=as_of, return_type=dict, order_by=['>rank'], **query) result = get(results, '0') if result: result = GsAsset.from_dict(result) else: results = GsAssetApi.get_many_assets(as_of=as_of, **query) result = next(iter(results), None) if result: return cls.__gs_asset_to_asset(result)