-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for storing nsrr_key in env variable or config, added r…
…elevant test cases
- Loading branch information
1 parent
e2799c0
commit 5c7c8d1
Showing
2 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
|
||
|