def test_get_latest_portfolio_positions(mocker): id_1 = 'MP1' date = dt.date(2019, 2, 18) mock_response = {'results': ( PositionSet.from_dict({ 'id': 'mock1', 'positionDate': '2019-02-18', 'lastUpdateTime': '2019-02-19T12:10:32.401Z', 'positions': [ {'assetId': 'MQA123', 'quantity': 0.3}, {'assetId': 'MQA456', 'quantity': 0.7} ] }), )} expected_response = ( PositionSet('mock1', date, dup.parse('2019-02-19T12:10:32.401Z'), ( Position(assetId='MQA123', quantity=0.3), Position(assetId='MQA456', quantity=0.7) )) ) # 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 = GsPortfolioApi.get_latest_positions(id_1) GsSession.current._get.assert_called_with('/portfolios/{id}/positions/last?type=close'.format(id=id_1), cls=PositionSet) assert response == expected_response
def test_get_portfolio_positions(mocker): id_1 = 'MP1' start_date = dt.date(2019, 2, 18) end_date = dt.date(2019, 2, 19) mock_response = {'positionSets': ( { 'id': 'mock1', 'positionDate': '2019-02-18', 'lastUpdateTime': '2019-02-19T12:10:32.401Z', 'positions': [ {'assetId': 'MQA123', 'quantity': 0.3}, {'assetId': 'MQA456', 'quantity': 0.7} ] }, { 'id': 'mock2', 'positionDate': '2019-02-19', 'lastUpdateTime': '2019-02-20T05:04:32.981Z', 'positions': [ {'assetId': 'MQA123', 'quantity': 0.4}, {'assetId': 'MQA456', 'quantity': 0.6} ] } )} expected_response = ( PositionSet('mock1', start_date, dup.parse('2019-02-19T12:10:32.401Z'), ( Position(assetId='MQA123', quantity=0.3), Position(assetId='MQA456', quantity=0.7) )), PositionSet('mock2', end_date, dup.parse('2019-02-20T05:04:32.981Z'), ( Position(assetId='MQA123', quantity=0.4), Position(assetId='MQA456', quantity=0.6) )) ) # 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 = GsPortfolioApi.get_positions(id_1, start_date, end_date) GsSession.current._get.assert_called_with( '/portfolios/{id}/positions?type=close&startDate={sd}&endDate={ed}'.format(id=id_1, sd=start_date, ed=end_date)) assert response == expected_response
def test_create(mocker): portfolio = Portfolio(position_sets=(PositionSet( positions=(Position(asset_id='MA4B66MW5E27UAHKG34', quantity=50), ), date=datetime.date(2020, 1, 1)), ), name='Test Portfolio', currency='EUR') mq_portfolio = MQPortfolio( name='Test Portfolio', currency='EUR', id='portfolio_id', entitlements=Entitlements(admin=('guid:12345', ))) mocker.patch.object(GsSession.current, '_post', return_value=mq_portfolio) mocker.patch.object(GsSession.current, '_put', return_value=()) mocker.patch.object(Portfolio, 'update_positions', return_value=()) mocker.patch.object(Portfolio, '_schedule_first_reports', return_value=()) mocker.patch.object(User, 'get_many', return_value=([ User(user_id='12345', name='Fake User', email='*****@*****.**', company='Goldman Sachs') ])) portfolio._create() assert portfolio.currency.value == 'EUR'
def test_get_positions(mocker): positions = { 'positionSets': [{ 'positionDate': '2020-01-01', 'positions': [{ 'asset_id': 'asset_id_1', 'quantity': 100 }, { 'asset_id': 'asset_id_2', 'quantity': 150 }] }] } position_set = PositionSet(positions=[ Position(asset_id='asset_id_1', quantity=100), Position(asset_id='asset_id_2', quantity=150) ], date=datetime.date(2020, 1, 1)) portfolio = test_pull_from_marquee(mocker) mocker.patch.object(GsSession.current, '_get', return_value=positions) mocker.patch.object(PositionSet, 'from_target', return_value=position_set) returned_positions = portfolio.get_position_sets() assert returned_positions[0].date == datetime.date(2020, 1, 1)
def test_get_asset(mocker): marquee_id = 'MA1234567890' mock_response = GsAsset(AssetClass.Equity, GsAssetType.Single_Stock, 'Test Asset') # mock GsSession mocker.patch.object(GsSession.__class__, 'default_value', return_value=GsSession.get(Environment.QA, 'client_id', 'secret')) mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.STOCK asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID, as_of=dt.date.today()) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.STOCK asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID, as_of=dt.datetime.utcnow()) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.STOCK mock_response = GsAsset(AssetClass.Equity, GsAssetType.Index, 'Test Asset') mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.INDEX mock_response = GsAsset(AssetClass.Equity, GsAssetType.Future, 'Test Asset') mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.FUTURE mock_response = GsAsset(AssetClass.Equity, GsAssetType.ETF, 'Test Asset') mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.ETF mock_response = GsAsset(AssetClass.Equity, GsAssetType.Custom_Basket, 'Test Asset', id_=marquee_id) mock_positions = PositionSet(positions=tuple( [Position(asset_id='asset_1', quantity=100)]), position_date=dt.date(2021, 1, 1), divisor=100) mock_price = {'price': 100} mock_position_data = { 'asset_1': [{ 'id': 'asset_1', 'name': 'Asset 1', 'bbid': 'A1 BBID' }] } mock_report = Report(marquee_id, 'Asset', 'Basket Create', ReportParameters()) mocker.patch.object(GsAssetApi, 'get_latest_positions', return_value=mock_positions) mocker.patch.object(GsReportApi, 'get_reports', return_value=mock_report) mocker.patch.object(GsIndexApi, 'initial_price', return_value=mock_price) mocker.patch.object(GsAssetApi, 'get_many_assets_data', return_value=mock_position_data) mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) assert asset.name == "Test Asset" assert asset.get_type() == AssetType.CUSTOM_BASKET mock_response = { 'results': (GsAsset(id=marquee_id, assetClass='Equity', type='Single Stock', name='Test 1'), ), } mocker.patch.object(GsSession.current, '_post', return_value=mock_response) asset = SecurityMaster.get_asset('GS.N', AssetIdentifier.REUTERS_ID) assert asset.name == "Test 1" assert asset.get_type() == AssetType.STOCK asset = SecurityMaster.get_asset('GS', AssetIdentifier.TICKER, exchange_code=ExchangeCode.NYSE) assert asset.name == "Test 1" assert asset.get_type() == AssetType.STOCK asset = SecurityMaster.get_asset('GS', AssetIdentifier.TICKER, asset_type=AssetType.STOCK) assert asset.name == "Test 1" assert asset.get_type() == AssetType.STOCK mocker.patch.object(GsSession.current, '_post', return_value={'results': ()}) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.REUTERS_ID) assert asset is None
def test_asset_identifiers(mocker): marquee_id = 'MA1234567890' mocker.patch.object(GsSession, 'default_value', return_value=GsSession.get(Environment.QA, 'client_id', 'secret')) mock_response = GsAsset(AssetClass.Equity, GsAssetType.Custom_Basket, 'Test Asset', id_=marquee_id) mock_positions = PositionSet(positions=tuple( [Position(asset_id='asset_1', quantity=100)]), position_date=dt.date(2021, 1, 1), divisor=100) mock_price = {'price': 100} mock_position_data = { 'asset_1': [{ 'id': 'asset_1', 'name': 'Asset 1', 'bbid': 'A1 BBID' }] } mock_report = Report(marquee_id, 'Asset', 'Basket Create', ReportParameters()) mocker.patch.object(GsAssetApi, 'get_latest_positions', return_value=mock_positions) mocker.patch.object(GsReportApi, 'get_reports', return_value=mock_report) mocker.patch.object(GsIndexApi, 'initial_price', return_value=mock_price) mocker.patch.object(GsAssetApi, 'get_many_assets_data', return_value=mock_position_data) mocker.patch.object(GsSession.current, '_get', return_value=mock_response) asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID) 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', } }) } mocker.patch.object(GsSession.current, '_get', return_value=mock_response) identifiers = asset.get_identifiers(dt.date.today()) assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHVIP' assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHVIP' assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FPE5' assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHVIP' assert asset.get_identifier(AssetIdentifier.REUTERS_ID, as_of=dt.date.today()) == '.GSTHHVIP' assert asset.get_identifier(AssetIdentifier.BLOOMBERG_ID, as_of=dt.date.today()) == 'GSTHHVIP' assert asset.get_identifier(AssetIdentifier.CUSIP, as_of=dt.date.today()) == '9EQ24FPE5' assert asset.get_identifier(AssetIdentifier.TICKER, as_of=dt.date.today()) == 'GSTHHVIP' market = PricingContext(dt.date(2018, 3, 1)) with market: identifiers = asset.get_identifiers() assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHOLD' assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHOLD' assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FOLD' assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHOLD' market = PricingContext(dt.date(2018, 3, 1)) with market: identifiers = asset.get_identifiers() assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHOLD' assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHOLD' assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FOLD' assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHOLD'