Skip to content

Commit

Permalink
Merge a8cdc11 into b084337
Browse files Browse the repository at this point in the history
  • Loading branch information
CDJellen committed Jan 8, 2023
2 parents b084337 + a8cdc11 commit 74fc200
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: [3.6, 3.7]
python-version: [3.6.15, 3.7]
steps:
- uses: actions/checkout@v2
with:
Expand Down
23 changes: 14 additions & 9 deletions ndbc_api/api/requests/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def _build_request_historical(
def req_hist_helper_year(req_year: int) -> str:
return f'{cls.BASE_URL}{cls.HISTORICAL_URL_PREFIX}{station_id}{cls.HISTORICAL_IDENTIFIER}{req_year}{cls.HISTORICAL_FILE_EXTENSION_SUFFIX}{cls.HISTORICAL_DATA_PREFIX}{cls.HISTORICAL_SUFFIX}{cls.FORMAT}/'

def req_hist_helper_month(req_month: int) -> str:
def req_hist_helper_month(req_year: int, req_month: int) -> str:
month = month_abbr[req_month]
month = month.capitalize()
return f'{cls.BASE_URL}{cls.HISTORICAL_URL_PREFIX}{station_id}{req_month}{current_year}{cls.HISTORICAL_FILE_EXTENSION_SUFFIX}{cls.HISTORICAL_DATA_PREFIX}{cls.FORMAT}/{month}/'
return f'{cls.BASE_URL}{cls.HISTORICAL_URL_PREFIX}{station_id}{req_month}{req_year}{cls.HISTORICAL_FILE_EXTENSION_SUFFIX}{cls.HISTORICAL_DATA_PREFIX}{cls.FORMAT}/{month}/'

def req_hist_helper_month_current(current_month: int) -> str:
month = month_abbr[current_month]
Expand All @@ -61,25 +61,30 @@ def req_hist_helper_month_current(current_month: int) -> str:
raise ValueError(
'Please provide a format for this historical data request, or call a formatted child class\'s method.'
)

# store request urls
reqs = []

current_year = now.year
last_available_month = (now - timedelta(days=31)).month
has_realtime = (now - end_time) < timedelta(days=44)
months_req_year = (now - timedelta(days=44)).year
last_avail_month = (now - timedelta(days=44)).month

# handle year requests
for hist_year in range(int(start_time.year),
min(int(current_year),
int(end_time.year) + 1)):
reqs.append(req_hist_helper_year(hist_year))
if end_time.year == current_year:

# handle month requests
if end_time.year == months_req_year:
for hist_month in range(
int(start_time.month),
min(int(end_time.month) + 1, int(last_available_month)),
min(int(end_time.month), int(last_avail_month))+1
):
reqs.append(req_hist_helper_month(hist_month))
if int(last_available_month) <= (end_time.month):
reqs.append(req_hist_helper_month(months_req_year, hist_month))
if int(last_avail_month) <= (end_time.month):
reqs.append(
req_hist_helper_month_current(int(last_available_month)))
req_hist_helper_month_current(int(last_avail_month)))

if has_realtime:
reqs.append(
Expand Down
3 changes: 2 additions & 1 deletion tests/api/handlers/test_stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ def test_station_realtime(stations_handler, request_handler, read_parsed_yml,

@pytest.mark.private
@pytest.mark.usefixtures('mock_socket', 'read_responses', 'read_parsed_yml')
def test_station_historical(stations_handler, request_handler, read_parsed_yml,
def test_station_historical(stations_handler, monkeypatch, request_handler, read_parsed_yml,
read_responses, mock_socket):
_ = mock_socket
monkeypatch.setenv('MOCKDATE', '2022-08-13')
reqs = HistoricalRequest.build_request(station_id=TEST_STN,)
assert len([reqs]) == len(read_responses['historical'].values())
mock_register_uri([reqs], list(read_responses['historical'].values()))
Expand Down
3 changes: 2 additions & 1 deletion tests/api/parsers/test_station_historical.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def stations_historical():


@pytest.mark.private
def test_available_measurements(stations_historical, historical_response,
def test_available_measurements(stations_historical, monkeypatch, historical_response,
parsed_stations_historical):
monkeypatch.setenv('MOCKDATE', '2022-08-13')
resp = historical_response.get(list(historical_response.keys())[0])
want = parsed_stations_historical
got = stations_historical.available_measurements(resp)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_ndbc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ def test_get_modes(ndbc_api):


@pytest.mark.usefixtures('mock_socket', 'read_responses', 'read_parsed_yml')
def test_station_realtime(ndbc_api, mock_socket, read_responses,
def test_station_realtime(ndbc_api, monkeypatch, mock_socket, read_responses,
read_parsed_yml):
_ = mock_socket
monkeypatch.setenv('MOCKDATE', '2022-08-13')
reqs = RealtimeRequest.build_request(station_id=TEST_STN_REALTIME,)
assert len([reqs]) == len(read_responses['realtime'].values())
mock_register_uri([reqs], list(read_responses['realtime'].values()))
Expand All @@ -269,9 +270,10 @@ def test_station_realtime(ndbc_api, mock_socket, read_responses,


@pytest.mark.usefixtures('mock_socket', 'read_responses', 'read_parsed_yml')
def test_station_historical(ndbc_api, mock_socket, read_responses,
def test_station_historical(ndbc_api, monkeypatch, mock_socket, read_responses,
read_parsed_yml):
_ = mock_socket
monkeypatch.setenv('MOCKDATE', '2022-08-13')
reqs = HistoricalRequest.build_request(station_id=TEST_STN_STDMET,)
assert len([reqs]) == len(read_responses['historical'].values())
mock_register_uri([reqs], list(read_responses['historical'].values()))
Expand Down

0 comments on commit 74fc200

Please sign in to comment.