Example #1
0
def test_lists():

    vi = ValetInterpreter(logger=logger, timeout=timeout)
    # Check that the json/xml formats return correct error
    with pytest.raises(NotImplementedError):
        vi.list_series(response_format='xml')
    with pytest.raises(NotImplementedError):
        vi.list_groups(response_format='xml')

    logger.info("Passed check for unsupported formats")

    # Series Lists
    df = vi.list_series(response_format='csv')
    assert isinstance(df, pd.DataFrame)
    js = vi.list_series(response_format='json')

    assert isinstance(js, dict)

    # Group Lists
    df = vi.list_groups(response_format='csv')
    assert isinstance(df, pd.DataFrame)
    js = vi.list_groups(response_format='json')
    assert isinstance(js, dict)

    logger.info("Checked that the lists are accessible, are cached correctly.")

    logger.info("Completed tests for lists")
Example #2
0
def test_et():
    vi = ValetInterpreter(logger=logger, timeout=0.1)
    js_series = vi.get_series_observations("STATIC_ATABLE_V41690973",
                                           response_format='json',
                                           recent=1)
    assert isinstance(js_series, list)
    logger.info(f"Inflation is now {js_series[0]}")
    logger.info("Tested retries on timeout")

    js_series = vi.get_series_observations("V80691311",
                                           response_format='json',
                                           recent=1)
    assert isinstance(js_series, list)
    logger.info(f"Prime rate is now {js_series[0]}")

    vi = ValetInterpreter(logger=logger, check=False)
    with pytest.raises(BOCException):
        # One incorrect
        df_series = vi.get_series_observations(['FXUSDCAD', 'INCORRECT'],
                                               response_format='csv',
                                               end_date='2018-12-01')
    with pytest.raises(BOCException):
        # One incorrect
        js_series = vi.get_series_observations(['FXUSDCAD', 'INCORRECT'],
                                               response_format='json',
                                               end_date='2018-12-01')
Example #3
0
def test_fx_rss():
    vi = ValetInterpreter(logger=logger, timeout=timeout)
    rss = vi.get_fx_rss('FXUSDCAD')
    assert isinstance(rss, str)
    rss = vi.get_fx_rss('FXUSDCAD')  # Call again to check that caching works.

    # Check multiple series
    rss = vi.get_fx_rss(['FXUSDCAD', 'FXEURCAD'])
    assert isinstance(rss, str)

    with pytest.raises(SeriesException):
        rss = vi.get_fx_rss('INCORRECT')
    with pytest.raises(SeriesException):
        rss = vi.get_fx_rss(['FXUSDCAD', 'INCORRECT'])
Example #4
0
                        '2016/03/Bank_of_Canada_logo.png')
boc_logo = BytesIO(boc_logo.content)
st.sidebar.image(boc_logo, use_column_width=True)

# Define the web application title
st.title(':bank: Bank of Canada :maple_leaf: [Open Data]'
         '(https://github.com/tylercroberts/pyvalet) Explorer :mag:')
# -------------------------------------------------------------------------------
# Introductory expander section covering overall application details
with st.beta_expander(label='Application details', expanded=True):
    st.header(
        '**Developer:** [Yoseph Zuskin](https://linkedin.com/in/Yoseph-Zuskin)'
        ', **Source Code:** [GitHub](https://github.com/Yoseph-Zuskin/webapp)')
    st.markdown(app_details)
# -------------------------------------------------------------------------------
api_client = ValetInterpreter(
)  # API Client for the Bank of Canada's open data
groups = api_client.list_groups()  # Retrieve list of available data groups
group_options = groups.label[groups.label !=
                             'delete']  # Filter data groups list

# Enable selection of a specific data group using a selectbox in the sidebar
chosen_group = st.sidebar.selectbox(
    label='Pick a group from which to select a time series:',
    options=['Click here to select...'] + group_options.tolist(),
    key='chosen_group')
if chosen_group == 'Click here to select...':
    st.warning('A Bank of Canada data group must be selected to proceed')
    st.stop()

# Retrieve metadata and data on the chosen data group
group_details, group_series = api_client.get_group_detail(
from pyvalet import ValetInterpreter
import requests
import pandas as pd
from datetime import datetime as dt
import re
vi = ValetInterpreter()

# vi = ValetInterpreter()
# df_series, df = vi.get_series_observations("BD.CDN.2YR.DQ.YLD", response_format='csv')
# print(df)


# BOC/V39055    Government of Canada benchmark bond yields - 10 year    daily https://www.bankofcanada.ca/valet/series/BD.CDN.10YR.DQ.YLD
# BOC/V39051    Government of Canada benchmark bond yields - 2 year    daily https://www.bankofcanada.ca/valet/series/BD.CDN.2YR.DQ.YLD
# BOC/V39052    Government of Canada benchmark bond yields - 3 year    daily https://www.bankofcanada.ca/valet/series/BD.CDN.3YR.DQ.YLD
# BOC/V39053    Government of Canada benchmark bond yields - 5 year    daily https://www.bankofcanada.ca/valet/series/BD.CDN.5YR.DQ.YLD
# BOC/V39054    Government of Canada benchmark bond yields - 7 year    daily https://www.bankofcanada.ca/valet/series/BD.CDN.7YR.DQ.YLD
# BOC/V39056    Government of Canada benchmark bond yields - long-term    daily https://www.bankofcanada.ca/valet/series/BD.CDN.LONG.DQ.YLD

links = [
         'BD.CDN.2YR.DQ.YLD',
         'BD.CDN.3YR.DQ.YLD',
         'BD.CDN.5YR.DQ.YLD',
         'BD.CDN.7YR.DQ.YLD',
         'BD.CDN.10YR.DQ.YLD',
         'BD.CDN.LONG.DQ.YLD']
bond_names = {
    'V39051': 'two year bond',
    'V39052': 'three year bond',
    'V39053': 'five year bond',
    'V39054': 'seven year bond',
Example #6
0
def test_details():
    # TODO: Here we just test a sample of handcoded endpoints, may want to look at a broader selection.

    vi = ValetInterpreter(logger=logger, timeout=timeout)
    # Check that the json/xml formats return correct error
    with pytest.raises(NotImplementedError):
        vi.get_series_detail('FXUSDCAD', response_format='xml')
    with pytest.raises(NotImplementedError):
        vi.get_group_detail("FX_RATES_DAILY", response_format='xml')

    logger.info("Passed check for unsupported formats")

    # Series Detail
    df = vi.get_series_detail("FXUSDCAD", response_format='csv')
    assert isinstance(df, pd.DataFrame)
    js = vi.get_series_detail("FXUSDCAD", response_format='json')
    assert isinstance(js, dict)
    # Group Detail
    df_group, df_series = vi.get_group_detail("FX_RATES_DAILY",
                                              response_format='csv')
    assert isinstance(df_group, pd.Series)
    assert isinstance(df_series, pd.DataFrame)
    js_group, js_series = vi.get_group_detail("FX_RATES_DAILY",
                                              response_format='json')
    assert isinstance(js_group, dict)
    assert isinstance(js_series, dict)

    logger.info(
        "Checked that the details are accessible, are cached correctly.")

    with pytest.raises(SeriesException):
        # Test with a non-correct series or group name:
        df = vi.get_series_detail("NOTCORRECT", response_format='csv')
    with pytest.raises(SeriesException):
        # Test with a non-correct series or group name:
        js = vi.get_series_detail("NOTCORRECT", response_format='json')

    with pytest.raises(GroupException):
        df = vi.get_group_detail("NOTCORRECT", response_format='csv')
    with pytest.raises(GroupException):
        df = vi.get_group_detail("NOTCORRECT", response_format='json')

    logger.info(
        "Checked that the correct exceptions are raised when a series or group is not recognized."
    )
    logger.info("Completed tests for details")
Example #7
0
def test_endpoints():
    vi = ValetInterpreter(logger=logger, timeout=timeout)
    # Test that the list endpoints are still valid. This may make debugging easier if they change, or become outdated.
    response = vi._get_lists('series', response_format='csv')
    assert response.status_code == 200
    vi._reset_url()
    response = vi._get_lists('groups', response_format='csv')
    assert response.status_code == 200
    vi._reset_url()
    logger.info(
        "Confirmed that endpoints are still valid for series list and groups list."
    )
    # Test that the list endpoints are still valid. This may make debugging easier if they change, or become outdated.
    response = vi._get_lists('series', response_format='json')
    assert response.status_code == 200
    vi._reset_url()
    response = vi._get_lists('groups', response_format='json')
    assert response.status_code == 200
    vi._reset_url()
    logger.info(
        "Confirmed that endpoints are still valid for series list and groups list."
    )

    with pytest.raises(ValueError):
        vi._prepare_requests('a', 2, 'c')
    with pytest.raises(ValueError):
        vi._prepare_requests('a', ['hi'], 'c')
    with pytest.raises(ValueError):
        vi._prepare_requests('a', {'a': 1}, 'c')
    with pytest.raises(ValueError):
        vi._prepare_requests('a', (1, 2), 'c')

    logger.info(
        "Confirmed that only strings will work for _prepare_requests()")
    logger.info("Completed tests for endpoints")
Example #8
0
def test_observations():
    vi = ValetInterpreter(logger=logger, timeout=timeout)

    # Check that the json/xml formats return correct error
    with pytest.raises(NotImplementedError):
        vi.get_series_observations('FXUSDCAD',
                                   response_format='xml',
                                   end_date='2018-12-01')
    with pytest.raises(NotImplementedError):
        vi.get_group_observations("FX_RATES_DAILY",
                                  response_format='xml',
                                  end_date='2018-12-01')
    logger.info("Passed check for unsupported formats")

    # Series Lists
    df_series = vi.get_series_observations("FXUSDCAD",
                                           response_format='csv',
                                           end_date='2018-12-01')
    assert isinstance(df_series, pd.DataFrame)
    #assert isinstance(df, pd.DataFrame)
    js_series = vi.get_series_observations("FXUSDCAD",
                                           response_format='json',
                                           end_date='2018-12-01')
    assert isinstance(js_series, list)
    #assert isinstance(js, dict)

    # Groups Lists
    df_series, df = vi.get_group_observations("FX_RATES_DAILY",
                                              response_format='csv',
                                              end_date='2018-12-01')
    assert isinstance(df_series, pd.DataFrame)
    assert isinstance(df, pd.DataFrame)
    js_series, js = vi.get_group_observations("FX_RATES_DAILY",
                                              response_format='json',
                                              end_date='2018-12-01')
    assert isinstance(js_series, dict)
    assert isinstance(js, list)

    logger.info(
        "Checked that the observations are accessible, lists are being cached")

    # Try an incorrect series name
    with pytest.raises(SeriesException):
        # Test with a non-correct series or group name:
        df_series = vi.get_series_observations("NOTCORRECT",
                                               response_format='csv')
    with pytest.raises(GroupException):
        # Test with a non-correct series or group name:
        df_series, df = vi.get_group_observations("NOTCORRECT",
                                                  response_format='csv')
    # Try an incorrect series name
    with pytest.raises(SeriesException):
        # Test with a non-correct series or group name:
        js_series = vi.get_series_observations("NOTCORRECT",
                                               response_format='json')
    with pytest.raises(GroupException):
        # Test with a non-correct series or group name:
        js_series, js = vi.get_group_observations("NOTCORRECT",
                                                  response_format='json')

    logger.info(
        "Passed check that the correct exceptions are raised when a series or group is not recognized."
    )

    # Try without any kwargs:
    df_series = vi.get_series_observations("FXUSDCAD", response_format='csv')
    # Try without any kwargs:
    js_series = vi.get_series_observations("FXUSDCAD", response_format='json')

    # Try without any kwargs:
    df_series, df = vi.get_group_observations("FX_RATES_DAILY",
                                              response_format='csv')
    # Try without any kwargs:
    js_series, js = vi.get_group_observations("FX_RATES_DAILY",
                                              response_format='json')

    logger.info("Passed run of observations without kwargs")

    # Test multiple series:
    df_series = vi.get_series_observations(['FXUSDCAD', 'A.AGRI'],
                                           response_format='csv',
                                           end_date='2018-12-01')

    assert isinstance(df_series, pd.DataFrame)
    #assert isinstance(df, pd.DataFrame)
    js_series = vi.get_series_observations(['FXUSDCAD', 'A.AGRI'],
                                           response_format='json',
                                           end_date='2018-12-01')
    assert isinstance(js_series, list)
    #assert isinstance(js, dict)

    with pytest.raises(SeriesException):
        # One incorrect
        df_series = vi.get_series_observations(['FXUSDCAD', 'INCORRECT'],
                                               response_format='csv',
                                               end_date='2018-12-01')
    with pytest.raises(SeriesException):
        # One incorrect
        js_series = vi.get_series_observations(['FXUSDCAD', 'INCORRECT'],
                                               response_format='json',
                                               end_date='2018-12-01')

    logger.info("Completed tests for observations")
Example #9
0
def test_interpreter():
    vi = ValetInterpreter()
    vi._enable_logging(logger=logger)