Compare commits

..

No commits in common. "110ddaf3a1ad141c210c0d1d23ab85b85a930af7" and "158fb1d08b1f1b5233a46fd9a2856d6ad74836d8" have entirely different histories.

6 changed files with 21 additions and 84 deletions

View file

@ -1,7 +1,6 @@
import os import os
from configparser import ConfigParser from configparser import ConfigParser
from typing import Any from typing import Any
from pathlib import Path
CONFIG_FILENAME = ".corrlib" CONFIG_FILENAME = ".corrlib"
cached: bool = True cached: bool = True
@ -74,7 +73,7 @@ def k2m(k: float) -> float:
return (1/(2*k))-4 return (1/(2*k))-4
def set_config(path: Path, section: str, option: str, value: Any) -> None: def set_config(path: str, section: str, option: str, value: Any) -> None:
""" """
Set configuration parameters for the library. Set configuration parameters for the library.
@ -89,7 +88,7 @@ def set_config(path: Path, section: str, option: str, value: Any) -> None:
value: Any value: Any
The value we set the option to. The value we set the option to.
""" """
config_path = os.path.join(path, CONFIG_FILENAME) config_path = os.path.join(path, '.corrlib')
config = ConfigParser() config = ConfigParser()
if os.path.exists(config_path): if os.path.exists(config_path):
config.read(config_path) config.read(config_path)
@ -101,7 +100,7 @@ def set_config(path: Path, section: str, option: str, value: Any) -> None:
return return
def get_db_file(path: Path) -> str: def get_db_file(path: str) -> str:
""" """
Get the database file associated with the library at the given path. Get the database file associated with the library at the given path.
@ -119,13 +118,11 @@ def get_db_file(path: Path) -> str:
config = ConfigParser() config = ConfigParser()
if os.path.exists(config_path): if os.path.exists(config_path):
config.read(config_path) config.read(config_path)
else:
raise FileNotFoundError("Configuration file not found.")
db_file = config.get('paths', 'db', fallback='backlogger.db') db_file = config.get('paths', 'db', fallback='backlogger.db')
return db_file return db_file
def cache_enabled(path: Path) -> bool: def cache_enabled(path: str) -> bool:
""" """
Check, whether the library is cached. Check, whether the library is cached.
Fallback is true. Fallback is true.
@ -144,10 +141,6 @@ def cache_enabled(path: Path) -> bool:
config = ConfigParser() config = ConfigParser()
if os.path.exists(config_path): if os.path.exists(config_path):
config.read(config_path) config.read(config_path)
else:
raise FileNotFoundError("Configuration file not found.")
cached_str = config.get('core', 'cached', fallback='True') cached_str = config.get('core', 'cached', fallback='True')
if cached_str not in ['True', 'False']:
raise ValueError(f"String {cached_str} is not a valid option, only True and False are allowed!")
cached_bool = cached_str == ('True') cached_bool = cached_str == ('True')
return cached_bool return cached_bool

View file

@ -2,19 +2,18 @@ from typer.testing import CliRunner
from corrlib.cli import app from corrlib.cli import app
import os import os
import sqlite3 as sql import sqlite3 as sql
from pathlib import Path
runner = CliRunner() runner = CliRunner()
def test_version() -> None: def test_version():
result = runner.invoke(app, ["--version"]) result = runner.invoke(app, ["--version"])
assert result.exit_code == 0 assert result.exit_code == 0
assert "corrlib" in result.output assert "corrlib" in result.output
def test_init_folders(tmp_path: Path) -> None: def test_init_folders(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
result = runner.invoke(app, ["init", "--dataset", str(dataset_path)]) result = runner.invoke(app, ["init", "--dataset", str(dataset_path)])
assert result.exit_code == 0 assert result.exit_code == 0
@ -22,7 +21,7 @@ def test_init_folders(tmp_path: Path) -> None:
assert os.path.exists(str(dataset_path / "backlogger.db")) assert os.path.exists(str(dataset_path / "backlogger.db"))
def test_init_db(tmp_path: Path) -> None: def test_init_db(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
result = runner.invoke(app, ["init", "--dataset", str(dataset_path)]) result = runner.invoke(app, ["init", "--dataset", str(dataset_path)])
assert result.exit_code == 0 assert result.exit_code == 0
@ -82,7 +81,7 @@ def test_init_db(tmp_path: Path) -> None:
assert expected_col in backlog_column_names assert expected_col in backlog_column_names
def test_list(tmp_path: Path) -> None: def test_list(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
result = runner.invoke(app, ["init", "--dataset", str(dataset_path)]) result = runner.invoke(app, ["init", "--dataset", str(dataset_path)])
assert result.exit_code == 0 assert result.exit_code == 0

View file

@ -1,7 +1,7 @@
import corrlib.toml as t import corrlib.toml as t
def test_toml_check_measurement_data() -> None: def test_toml_check_measurement_data():
measurements = { measurements = {
"a": "a":
{ {

View file

@ -1,7 +1,7 @@
import corrlib.input.sfcf as input import corrlib.input.sfcf as input
import json import json
def test_get_specs() -> None: def test_get_specs():
parameters = { parameters = {
'crr': [ 'crr': [
'f_P', 'f_A' 'f_P', 'f_A'

View file

@ -1,23 +1,22 @@
import corrlib.initialization as init import corrlib.initialization as init
import os import os
import sqlite3 as sql import sqlite3 as sql
from pathlib import Path
def test_init_folders(tmp_path: Path) -> None: def test_init_folders(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
init.create(str(dataset_path)) init.create(str(dataset_path))
assert os.path.exists(str(dataset_path)) assert os.path.exists(str(dataset_path))
assert os.path.exists(str(dataset_path / "backlogger.db")) assert os.path.exists(str(dataset_path / "backlogger.db"))
def test_init_folders_no_tracker(tmp_path: Path) -> None: def test_init_folders_no_tracker(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
init.create(str(dataset_path), tracker="None") init.create(str(dataset_path), tracker="None")
assert os.path.exists(str(dataset_path)) assert os.path.exists(str(dataset_path))
assert os.path.exists(str(dataset_path / "backlogger.db")) assert os.path.exists(str(dataset_path / "backlogger.db"))
def test_init_config(tmp_path: Path) -> None: def test_init_config(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
init.create(str(dataset_path), tracker="None") init.create(str(dataset_path), tracker="None")
config_path = dataset_path / ".corrlib" config_path = dataset_path / ".corrlib"
@ -35,7 +34,7 @@ def test_init_config(tmp_path: Path) -> None:
assert config.get("paths", "import_scripts_path") == "import_scripts" assert config.get("paths", "import_scripts_path") == "import_scripts"
def test_init_db(tmp_path: Path) -> None: def test_init_db(tmp_path):
dataset_path = tmp_path / "test_dataset" dataset_path = tmp_path / "test_dataset"
init.create(str(dataset_path)) init.create(str(dataset_path))
assert os.path.exists(str(dataset_path / "backlogger.db")) assert os.path.exists(str(dataset_path / "backlogger.db"))

View file

@ -1,85 +1,31 @@
from corrlib import tools as tl from corrlib import tools as tl
from configparser import ConfigParser
import os
from pathlib import Path
import pytest
def test_m2k() -> None: def test_m2k():
for m in [0.1, 0.5, 1.0]: for m in [0.1, 0.5, 1.0]:
expected_k = 1 / (2 * m + 8) expected_k = 1 / (2 * m + 8)
assert tl.m2k(m) == expected_k assert tl.m2k(m) == expected_k
def test_k2m() -> None: def test_k2m():
for m in [0.1, 0.5, 1.0]: for m in [0.1, 0.5, 1.0]:
assert tl.k2m(m) == (1/(2*m))-4 assert tl.k2m(m) == (1/(2*m))-4
def test_k2m_m2k() -> None: def test_k2m_m2k():
for m in [0.1, 0.5, 1.0]: for m in [0.1, 0.5, 1.0]:
k = tl.m2k(m) k = tl.m2k(m)
m_converted = tl.k2m(k) m_converted = tl.k2m(k)
assert abs(m - m_converted) < 1e-9 assert abs(m - m_converted) < 1e-9
def test_str2list() -> None: def test_str2list():
assert tl.str2list("a,b,c") == ["a", "b", "c"] assert tl.str2list("a,b,c") == ["a", "b", "c"]
assert tl.str2list("1,2,3") == ["1", "2", "3"] assert tl.str2list("1,2,3") == ["1", "2", "3"]
def test_list2str() -> None: def test_list2str():
assert tl.list2str(["a", "b", "c"]) == "a,b,c" assert tl.list2str(["a", "b", "c"]) == "a,b,c"
assert tl.list2str(["1", "2", "3"]) == "1,2,3" assert tl.list2str(["1", "2", "3"]) == "1,2,3"
def test_set_config(tmp_path: Path) -> None:
section = "core"
option = "test_option"
value = "test_value"
# config is not yet available
tl.set_config(tmp_path, section, option, value)
config_path = os.path.join(tmp_path, '.corrlib')
config = ConfigParser()
config.read(config_path)
assert config.get('core', 'test_option', fallback="not the value") == "test_value"
# now, a config file is already present
section = "core"
option = "test_option2"
value = "test_value2"
tl.set_config(tmp_path, section, option, value)
config_path = os.path.join(tmp_path, '.corrlib')
config = ConfigParser()
config.read(config_path)
assert config.get('core', 'test_option2', fallback="not the value") == "test_value2"
# update option 2
section = "core"
option = "test_option2"
value = "test_value3"
tl.set_config(tmp_path, section, option, value)
config_path = os.path.join(tmp_path, '.corrlib')
config = ConfigParser()
config.read(config_path)
assert config.get('core', 'test_option2', fallback="not the value") == "test_value3"
def test_get_db_file(tmp_path: Path) -> None:
section = "paths"
option = "db"
value = "test_value"
# config is not yet available
tl.set_config(tmp_path, section, option, value)
assert tl.get_db_file(tmp_path) == "test_value"
def test_cache_enabled(tmp_path: Path) -> None:
section = "core"
option = "cached"
# config is not yet available
tl.set_config(tmp_path, section, option, "True")
assert tl.cache_enabled(tmp_path)
tl.set_config(tmp_path, section, option, "False")
assert not tl.cache_enabled(tmp_path)
tl.set_config(tmp_path, section, option, "lalala")
with pytest.raises(ValueError) as e_info:
tl.cache_enabled(tmp_path)