Exemplo n.º 1
0
def test_get_monitor(mocker):
    monitor_id = 'abc'
    mock_response = Monitor(id=monitor_id, name='test', type='assets')

    # 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 = GsMonitorsApi.get_monitor(monitor_id)
    GsSession.current._get.assert_called_with(
        '/monitors/{id}'.format(id=monitor_id), cls=Monitor)
    assert response == mock_response
Exemplo n.º 2
0
def test_delete_portfolio(mocker):
    monitor_id = 'abc'

    mock_response = True

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

    # run test
    response = GsMonitorsApi.delete_monitor(monitor_id)
    GsSession.current._delete.assert_called_with(
        '/monitors/{id}'.format(id=monitor_id))
    assert response == mock_response
Exemplo n.º 3
0
def test_create_monitor(mocker):
    monitor_id = 'abc'

    monitor = Monitor(id=monitor_id, name='test', type='assets')

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

    # run test
    response = GsMonitorsApi.create_monitor(monitor)
    GsSession.current._post.assert_called_with(
        '/monitors',
        monitor,
        request_headers={'Content-Type': 'application/json;charset=utf-8'},
        cls=Monitor)
    assert response == monitor
Exemplo n.º 4
0
def test_calculate_monitor(mocker):
    monitor_id = 'abc'

    calc_data = {
        'Name': {
            'value': "Multiline Retail 4",
            'metadata': {
                'tooltip': "GSS4MRET"
            }
        },
        'Last': {
            'timestamp': "2019-06-03T20:57:31.407999992Z",
            'value': 119.21,
            'formattedValue': "119.21"
        }
    }

    mock_response = MonitorResponseData(id=monitor_id,
                                        result={
                                            'groupName':
                                            'some name',
                                            'rows': [{
                                                'entityId': 'id',
                                                'data': calc_data
                                            }]
                                        })

    # 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 = GsMonitorsApi.calculate_monitor(monitor_id)
    GsSession.current._get.assert_called_with(
        '/monitors/{id}/data'.format(id=monitor_id), cls=MonitorResponseData)
    assert response == mock_response
Exemplo n.º 5
0
def get_my_baskets(user_id: str = None) -> Optional[pd.DataFrame]:
    """
    Retrieve a list of baskets a user is permissioned to

    :param user_id: Marquee user/app ID (default is current application's id)
    :return: dataframe of baskets user has access to

    **Usage**

    Retrieve a list of baskets a user is permissioned to

    **Examples**

    Retrieve a list of baskets the current user is permissioned to

    >>> from gs_quant.markets.indices_utils import *
    >>>
    >>> get_my_baskets()
    """
    user_id = user_id if user_id is not None else GsSession.current.client_id
    tag = f'Custom Basket:{user_id}'
    response = GsMonitorsApi.get_monitors(tags=tag)
    if len(response):
        row_groups = get(response, '0.parameters.row_groups')
        my_baskets = []
        for row_group in row_groups:
            entity_ids = [entity.id for entity in row_group.entity_ids]
            baskets = GsAssetApi.get_many_assets_data(
                id=entity_ids, fields=['id', 'ticker', 'name', 'liveDate'])
            my_baskets += [
                dict(monitor_name=row_group.name,
                     id=get(basket, 'id'),
                     ticker=get(basket, 'ticker'),
                     name=get(basket, 'name'),
                     live_date=get(basket, 'liveDate')) for basket in baskets
            ]
        return pd.DataFrame(my_baskets)
Exemplo n.º 6
0
def test_get_many_monitors(mocker):
    mock_response = {
        'results': (Monitor(id='abc', name='test', type='assets'),
                    Monitor(id='bde', name='test2', type='assets')),
        'totalResults':
        2
    }

    expected_response = (Monitor(id='abc', name='test', type='assets'),
                         Monitor(id='bde', name='test2', type='assets'))

    # 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
    GsMonitorsApi.get_monitors()
    GsSession.current._get.assert_called_with('/monitors?limit=100',
                                              cls=Monitor)
    GsMonitorsApi.get_monitors(monitor_id='abc')
    GsSession.current._get.assert_called_with('/monitors?id=abc&limit=100',
                                              cls=Monitor)
    GsMonitorsApi.get_monitors(owner_id='aedf')
    GsSession.current._get.assert_called_with(
        '/monitors?ownerId=aedf&limit=100', cls=Monitor)
    GsMonitorsApi.get_monitors(name='name')
    GsSession.current._get.assert_called_with('/monitors?name=name&limit=100',
                                              cls=Monitor)
    GsMonitorsApi.get_monitors(folder_name='folderName')
    GsSession.current._get.assert_called_with(
        '/monitors?folderName=folderName&limit=100', cls=Monitor)
    GsMonitorsApi.get_monitors(monitor_type='type')
    GsSession.current._get.assert_called_with('/monitors?type=type&limit=100',
                                              cls=Monitor)
    response = GsMonitorsApi.get_monitors(name='name', owner_id='ia', limit=10)
    GsSession.current._get.assert_called_with(
        '/monitors?ownerId=ia&name=name&limit=10', cls=Monitor)
    assert response == expected_response