Skip to content

Commit

Permalink
added support for storing nsrr_key in env variable or config, added r…
Browse files Browse the repository at this point in the history
…elevant test cases
  • Loading branch information
simon-p-2000 committed Nov 2, 2024
1 parent e2799c0 commit 5c7c8d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/sleepecg/io/nsrr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path

import requests
import os
from tqdm import tqdm

from sleepecg.io.utils import _download_file
Expand Down Expand Up @@ -61,8 +62,16 @@ def _get_nsrr_url(db_slug: str) -> str:
str
The download URL.
"""
if _nsrr_token is None:
raise RuntimeError("NSRR token not set, use `sleepecg.set_nsrr_token(<token>)`!")
global _nsrr_token
if not _nsrr_token:
_nsrr_token = os.environ.get("nsrr_token")
if not _nsrr_token:
try:
from sleepecg import get_config_value
_nsrr_token = get_config_value("nsrr_token")
except ValueError:
raise RuntimeError("NSRR token not set, use `sleepecg.set_nsrr_token(<token>)`, set the token in the "
"'config.yml' file or set an environment variable 'nsrr_token'!")
return f"https://sleepdata.org/datasets/{db_slug}/files/a/{_nsrr_token}/m/sleepecg/"


Expand Down
56 changes: 56 additions & 0 deletions tests/test_nsrr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# © SleepECG developers
#
# License: BSD (3-clause)

import pytest
from unittest.mock import patch

import sleepecg.io.nsrr
import sleepecg.config
from sleepecg.io.nsrr import _get_nsrr_url

@pytest.fixture(autouse=True)
def temp_test_config(tmp_path):
"""Create, use and delete a temporary user config file for testing."""
# setup
user_config_path_backup = sleepecg.config._USER_CONFIG_PATH
sleepecg.config._USER_CONFIG_PATH = tmp_path / "testconfig.yml"

# execute test
yield

# cleanup
sleepecg.config._USER_CONFIG_PATH = user_config_path_backup

def test_get_nsrr_url_no_nsrr_token_set(monkeypatch):
"""Tests the get_nsrr_url method with no nsrr_token set."""
# simulate blank environment variable
monkeypatch.delenv("nsrr_url", raising=False)

with pytest.raises(RuntimeError, match="NSRR token not set"):
_get_nsrr_url("mesa")

def test_get_nsrr_url_env_nsrr_token_set(monkeypatch):
"""Tests the get_nsrr_url method with nsrr_token set as environment variable."""
# simulate set environment variable
monkeypatch.setenv("nsrr_token", "token")
nsrr_url = _get_nsrr_url("mesa")
assert nsrr_url == "https://sleepdata.org/datasets/mesa/files/a/token/m/sleepecg/"

def patch_get_config_value_return(key):
return "token"

def test_get_nsrr_url_config_nsrr_token_set(monkeypatch):
# simulate blank environment variable
monkeypatch.delenv("nsrr_token", raising=False)
monkeypatch.setattr("sleepecg.get_config_value", patch_get_config_value_return)
nsrr_url = _get_nsrr_url("mesa")
assert nsrr_url == "https://sleepdata.org/datasets/mesa/files/a/token/m/sleepecg/"

@patch("sleepecg.io.nsrr._nsrr_token", "token")
def test_get_nsrr_url_function_nsrr_token_set():
"""Tests the get_nsrr_url method with nsrr_token set via the set_nsrr_token function."""
nsrr_url = _get_nsrr_url("mesa")
assert nsrr_url == "https://sleepdata.org/datasets/mesa/files/a/token/m/sleepecg/"


0 comments on commit 5c7c8d1

Please sign in to comment.