예제 #1
0
    def get_identifiers(self, as_of: dt.date = None) -> dict:
        """
        Get asset identifiers

        :param as_of: As of date for query
        :return: dict of identifiers

        **Usage**

        Get asset identifiers as of a given date. Where the identifiers are temporal (and can change over time), this
        function will return the identifiers as of that point in time. If no date is provided as a parameter, will use
        the current PricingContext.

        **Examples**

        Get current asset identifiers:

        >>> gs = SecurityMaster.get_asset("GS", AssetIdentifier.TICKER)
        >>> gs.get_identifiers()

        Get identifiers as of 1Jan18:

        >>> gs.get_identifiers(dt.date(2018,1,1))

        Use PricingContext to determine as of date:

        >>> with PricingContext(dt.date(2018,1,1)) as ctx:
        >>>     gs.get_identifiers()

        **See also**

        :class:`AssetIdentifier`
        :func:`get_asset`

        """
        if not as_of:
            as_of = PricingContext.current.pricing_date

            if isinstance(as_of, dt.datetime):
                as_of = as_of.date()

        valid_ids = set(item.value for item in AssetIdentifier)
        xrefs = GsAssetApi.get_asset_xrefs(self.__id)
        identifiers = {}

        for xref in xrefs:
            start_date = xref.startDate
            end_date = xref.endDate

            if start_date <= as_of <= end_date:
                identifiers = {
                    k.upper(): v
                    for k, v in xref.identifiers.as_dict().items()
                    if k.upper() in valid_ids
                }

        return identifiers
예제 #2
0
def test_get_asset_xrefs(mocker):
    marquee_id = 'MQA1234567890'

    mock_response = {
        'xrefs': ({
            'startDate': '1952-01-01',
            'endDate': '2018-12-31',
            'identifiers': {
                'ric': '.GSTHHOLD',
                'bbid': 'GSTHHOLD',
                'cusip': '9EQ24FOLD',
                'ticker': 'GSTHHOLD'
            }
        }, {
            'startDate': '2019-01-01',
            'endDate': '2952-12-31',
            'identifiers': {
                'ric': '.GSTHHVIP',
                'bbid': 'GSTHHVIP',
                'cusip': '9EQ24FPE5',
                'ticker': 'GSTHHVIP',
            }
        })
    }

    expected_response = (GsTemporalXRef(
        dt.date(1952, 1, 1), dt.date(2018, 12, 31),
        XRef(
            ric='.GSTHHOLD',
            bbid='GSTHHOLD',
            cusip='9EQ24FOLD',
            ticker='GSTHHOLD',
        )),
                         GsTemporalXRef(
                             dt.date(2019, 1, 1), dt.date(2952, 12, 31),
                             XRef(
                                 ric='.GSTHHVIP',
                                 bbid='GSTHHVIP',
                                 cusip='9EQ24FPE5',
                                 ticker='GSTHHVIP',
                             )))

    # mock GsSession
    mocker.patch.object(GsSession.__class__,
                        'current',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    # run test
    response = GsAssetApi.get_asset_xrefs(marquee_id)

    GsSession.current._get.assert_called_with(
        '/assets/{id}/xrefs'.format(id=marquee_id))
    testfixtures.compare(response, expected_response)