Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nwis_client iv context handler #257

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python/nwis_client/src/hydrotools/nwis_client/iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def __init__(self, *,
)
self._value_time_label = value_time_label

def __enter__(self):
return self

def __exit__(self, *args):
self._restclient.close()

@verify_case_insensitive_kwargs(handler=_verify_case_insensitive_kwargs_handler)
def get(
self,
Expand Down
15 changes: 13 additions & 2 deletions python/nwis_client/tests/test_nwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def wrap_iv_cache_location_to_temp(loop):

o = partial(iv.IVDataService, cache_filename=cache_file)
return o


@pytest.fixture
def setup_iv(IVDataServiceWithTempCache):
Expand Down Expand Up @@ -119,7 +119,7 @@ def mocked_iv(mock_iv, setup_iv):
`iv.IVDataService`'s `get_raw` method has been mocked to return an empty list.
"""
return setup_iv


simplify_variable_test_data = [
("test", ",", "test"),
Expand Down Expand Up @@ -556,6 +556,17 @@ def test_nwis_client_cache_path(loop):
# close resources
service._restclient.close()


@pytest.mark.slow
def test_nwis_client_context_manager(loop):
"""verify that context manager closes resources"""
with iv.IVDataService() as service:
service.get(sites=["01189000"], startDT="2022-01-01")
assert service._restclient._session.closed
# should this be closed? The assertion fails if uncommented
# assert service._restclient._loop.is_closed()


def test_fixes_209(loop, monkeypatch):
"""
verify that pandas FutureWarning is not raised by `IVDataService.get`. This FutureWarning was
Expand Down
Loading