def test_fetch_historical_prices_by_epic_and_date_range(self, ig_service: IGService):
     response = ig_service.fetch_historical_prices_by_epic_and_date_range(
         "CS.D.EURUSD.MINI.IP", "D", "2020-09-01 00:00:00", "2020-09-04 23:59:59"
     )
     assert isinstance(response["allowance"], dict)
     assert isinstance(response["prices"], pd.DataFrame)
     assert len(response["prices"]) == 4
    def test_historical_prices_by_epic_and_date_range_happy(self):

        # fetch_historical_prices_by_epic_and_date_range, daily resolution

        with open('tests/data/historic_prices_v1.json', 'r') as file:
            response_body = json.loads(file.read())

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.GC.Month2.IP/DAY',
            match_querystring=False,
            headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'},
            json=response_body,
            status=200)

        ig_service = IGService('username', 'password', 'api_key', 'DEMO')
        ig_service.crud_session.HEADERS["LOGGED_IN"] = {}
        result = ig_service.fetch_historical_prices_by_epic_and_date_range(
            epic='MT.D.GC.Month2.IP',
            resolution='D',
            start_date='2020:09:01-00:00:00',
            end_date='2020:09:04-23:59:59')

        prices = result['prices']
        assert isinstance(result, dict)
        assert isinstance(prices, pd.DataFrame)

        # assert DataFrame shape
        assert prices.shape[0] == 4
        assert prices.shape[1] == 13

        # assert time series rows are 1 day apart
        prices['tvalue'] = prices.index
        prices['delta'] = (prices['tvalue'] - prices['tvalue'].shift())
        assert any(prices["delta"].dropna() == datetime.timedelta(days=1))
    def test_historical_prices_by_epic_and_date_range_bad_date_order(self):

        # fetch_historical_prices_by_epic_and_date_range - bad date order

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.XX.Month1.IP/DAY',
            headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'},
            json={"errorCode": "error.invalid.daterange"},
            status=400)

        with pytest.raises(Exception):
            ig_service = IGService('username', 'password', 'api_key', 'DEMO')
            ig_service.crud_session.HEADERS["LOGGED_IN"] = {}
            result = ig_service.fetch_historical_prices_by_epic_and_date_range(
                epic='MT.D.GC.Month2.IP',
                resolution='D',
                start_date='2020:09:04-23:59:59',
                end_date='2020:09:01-00:00:00')
            assert result['errorCode'] == 'error.invalid.daterange'
    def test_historical_prices_by_epic_and_date_range_bad_date_format(self):

        # fetch_historical_prices_by_epic_and_date_range - bad date format

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.XX.Month1.IP/DAY',
            headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'},
            json={'errorCode': 'Unable to parse datetime=2020/09/01T00:00:00'},
            status=400)

        with pytest.raises(Exception):
            ig_service = IGService('username', 'password', 'api_key', 'DEMO')
            ig_service.crud_session.HEADERS["LOGGED_IN"] = {}
            result = ig_service.fetch_historical_prices_by_epic_and_date_range(
                epic='MT.D.GC.Month2.IP',
                resolution='D',
                start_date='2020/09/01T00:00:00',
                end_date='2020-09-04T23:59:59')
            assert result['errorCode'].startswith('Unable to parse datetime')
Пример #5
0
    def test_historical_prices_by_epic_and_date_range_bad_epic(self):

        # fetch_historical_prices_by_epic_and_date_range - bad epic

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.X.Month1.IP/DAY',
            headers={
                'CST': 'abc123',
                'X-SECURITY-TOKEN': 'xyz987'
            },
            json={'errorCode': 'error.error.price-history.io-error'},
            status=404)

        with pytest.raises(Exception):
            ig_service = IGService('username', 'password', 'api_key', 'DEMO')
            result = ig_service.fetch_historical_prices_by_epic_and_date_range(
                epic='MT.D.X.Month1.IP',
                resolution='D',
                start_date='2020-09-01T00:00:00',
                end_date='2020-09-04T23:59:59')
            assert result['errorCode'] == 'error.error.price-history.io-error'