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 cache_filename parameter to the NWIS Client constructor #176

Merged
merged 11 commits into from
Feb 9, 2022
2 changes: 1 addition & 1 deletion python/nwis_client/src/hydrotools/nwis_client/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.0.7"
__version__ = "3.1.0"
18 changes: 13 additions & 5 deletions python/nwis_client/src/hydrotools/nwis_client/iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import datetime
from collections.abc import Iterable
from functools import partial
from typing import Dict, List, Set, T, Union, Iterable
import re
import aiohttp
import six
Expand All @@ -25,11 +24,18 @@
from hydrotools._restclient import RestClient, Url
from collections.abc import Sequence

# typing imports
from pathlib import Path
from typing import Dict, List, Set, TypeVar, Union, Iterable

T = TypeVar("T")


# local imports
from ._utilities import verify_case_insensitive_kwargs

def _verify_case_insensitive_kwargs_handler(m: str) -> None:
warnings.warn("`hydrotools.nwis_client` >= 3.1 will raise RuntimeError exception instead of RuntimeWarning.", DeprecationWarning)
warnings.warn("`hydrotools.nwis_client` > 3.1 will raise RuntimeError exception instead of RuntimeWarning.", DeprecationWarning)
warnings.warn(m, RuntimeWarning)

class IVDataService:
Expand All @@ -47,6 +53,8 @@ class IVDataService:
Cached item life length in seconds
value_time_label: str, default 'value_date'
Label to use for datetime column returned by IVDataService.get
cache_filename: str or Path default 'nwisiv_cache'
Sqlite cache filename or filepath. Suffix '.sqlite' will be added to file if not included.

Examples
--------
Expand Down Expand Up @@ -92,21 +100,21 @@ class IVDataService:
safe="/:",
quote_overide_map={"+": "%2B"},
)
_requests_cache_filename = "nwisiv_cache"
_headers = {"Accept-Encoding": "gzip, compress"}
_value_time_label = None

def __init__(self, *,
enable_cache: bool = True,
cache_expire_after: int = 43200,
value_time_label: str = None
value_time_label: str = None,
cache_filename: Union[str, Path] = "nwisiv_cache"
):
self._cache_enabled = enable_cache
self._restclient = RestClient(
base_url=self._base_url,
headers=self._headers,
enable_cache=self._cache_enabled,
cache_filename=self._requests_cache_filename,
cache_filename=str(cache_filename),
cache_expire_after=cache_expire_after,
)
if value_time_label == None:
Expand Down
55 changes: 47 additions & 8 deletions python/nwis_client/tests/test_nwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,50 @@ def text(self):

##### FIXTURES #####

@pytest.fixture(name="IVDataServiceWithTempCache")
def wrap_iv_cache_location_to_temp(loop):
from tempfile import TemporaryDirectory
from pathlib import Path
from functools import partial

with TemporaryDirectory() as temp:
cache_file = Path(temp) / "cache.sqlite"

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


@pytest.fixture
def setup_iv(loop):
o = iv.IVDataService()
def setup_iv(IVDataServiceWithTempCache):
"""Setup IVDataService client. Cache file is """
o = IVDataServiceWithTempCache()
yield o
o._restclient.close()

@pytest.fixture
def setup_iv_value_time(loop):
o = iv.IVDataService(value_time_label="value_time")
def setup_iv_value_time(IVDataServiceWithTempCache):
o = IVDataServiceWithTempCache(value_time_label="value_time")
yield o
o._restclient.close()

@pytest.fixture
def mocked_iv(setup_iv, monkeypatch):
def mock_iv(setup_iv, monkeypatch):
"""mock `iv.IVDataService`. `iv.IVDataService`'s `get_raw` method has been mocked to return an
empty list.
"""
def wrapper(*args, **kwargs):
return []

# Monkey patch get_raw method to return []
monkeypatch.setattr(iv.IVDataService, "get_raw", wrapper)

@pytest.fixture
def mocked_iv(mock_iv, setup_iv):
"""return mocked and setup `iv.IVDataService`.
`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 @@ -456,9 +478,26 @@ def test_nwis_client_get_throws_warning_for_kwargs(mocked_iv):
version = version.parse(nwis_client.__version__)
version = (version.major, version.minor)

# versions <= than 3.1 should throw an exception instead of a warning
assert version < (3, 1)
# versions > 3.1 should throw an exception instead of a warning
assert version <= (3, 1)

with pytest.warns(RuntimeWarning, match="function parameter, 'startDT', provided as 'startDt'"):
# startdt should be startDT
mocked_iv.get(sites=["01189000"], startDt="2022-01-01")

@pytest.mark.slow
def test_nwis_client_cache_path(loop):
"""verify that cache directory has configurable location"""
from tempfile import TemporaryDirectory
from pathlib import Path

with TemporaryDirectory() as temp:
cache_file = Path(temp) / "cache.sqlite"

service = iv.IVDataService(cache_filename=cache_file)
service.get(sites=["01189000"], startDT="2022-01-01")

assert cache_file.exists()

# close resources
service._restclient.close()