Beispiel #1
0
    def __create_query(cls,
                       fields: Union[List, Tuple] = None,
                       as_of: dt.datetime = None,
                       limit: int = None,
                       scroll: str = None,
                       scroll_id: str = None,
                       order_by: List[str] = None,
                       **kwargs) -> EntityQuery:
        keys = set(kwargs.keys())
        valid = keys.intersection(
            i for i in dir(FieldFilterMap)
            if isinstance(getattr(FieldFilterMap, i), property))
        invalid = keys.difference(valid)

        if invalid:
            bad_args = ['{}={}'.format(k, kwargs[k]) for k in invalid]
            raise KeyError('Invalid asset query argument(s): {}'.format(
                ', '.join(bad_args)))

        return EntityQuery(where=FieldFilterMap(**kwargs),
                           fields=fields,
                           asOfTime=as_of or dt.datetime.utcnow(),
                           limit=limit,
                           scroll=scroll,
                           scroll_id=scroll_id,
                           order_by=order_by)
Beispiel #2
0
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